I tried running the open source deep learning library Caffe on Google Colaboratory.
There seems to be no Caffe on Google Colab, so install it as follows:
!apt install caffe-cpu
In choosing the pre-computed Caffe model, I chose AgeGenderDeepLearning, which was well-explained. Git clone on Google Colaboratory.
!git clone https://github.com/GilLevi/AgeGenderDeepLearning
This is a model that predicts the age and gender of the person in the picture. I would like to use this model to predict the age and gender of the world's supermodels.
# https://github.com/GilLevi/AgeGenderDeepLearning code as is
import os
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
caffe_root = './caffe/'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
mean_filename='./AgeGenderDeepLearning/models/mean.binaryproto' #changes
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
age_net_pretrained='./AgeGenderDeepLearning/models/age_net.caffemodel' #changes
age_net_model_file='./AgeGenderDeepLearning/age_net_definitions/deploy.prototxt' #changes
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
mean=mean,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
gender_net_pretrained='./AgeGenderDeepLearning/models/gender_net.caffemodel' #changes
gender_net_model_file='./AgeGenderDeepLearning/gender_net_definitions/deploy.prototxt' #changes
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained,
mean=mean,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
# https://github.com/GilLevi/AgeGenderDeepLearning code as is
age_list=['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
gender_list=['Male','Female']
I got the photo of the super model from here.
#Import a library that provides access to resources by URL.
import urllib.request
#Specify resources on the web
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Gisele_B_edit.jpg/300px-Gisele_B_edit.jpg'
#Download the resource from the specified URL and give it a name.
urllib.request.urlretrieve(url, 'model1.jpg')
('model1.jpg', <http.client.HTTPMessage at 0x7f7d928eea58>)
example_image = 'model1.jpg'
input_image = caffe.io.load_image(example_image)
_ = plt.imshow(input_image)
/usr/local/lib/python3.6/dist-packages/skimage/io/_io.py:48: UserWarning: `as_grey` has been deprecated in favor of `as_gray`
warn('`as_grey` has been deprecated in favor of `as_gray`')
(I don't show this photo because it seems to be a copyright issue.)
# https://github.com/GilLevi/AgeGenderDeepLearning code as is
prediction = age_net.predict([input_image])
print ('predicted age:', age_list[prediction[0].argmax()])
predicted age: (15, 20)
I see.
# https://github.com/GilLevi/AgeGenderDeepLearning code as is
prediction = gender_net.predict([input_image])
print ('predicted gender:', gender_list[prediction[0].argmax()])
predicted gender: Female
I see.
#Import a library that provides access to resources by URL.
import urllib.request
#Specify resources on the web
url = 'https://upload.wikimedia.org/wikipedia/commons/f/f3/LindaEvangelista.jpg'
#Download the resource from the specified URL and give it a name.
urllib.request.urlretrieve(url, 'model2.jpg')
('model2.jpg', <http.client.HTTPMessage at 0x7f7d925944a8>)
example_image = 'model2.jpg'
input_image = caffe.io.load_image(example_image)
_ = plt.imshow(input_image)
/usr/local/lib/python3.6/dist-packages/skimage/io/_io.py:48: UserWarning: `as_grey` has been deprecated in favor of `as_gray`
warn('`as_grey` has been deprecated in favor of `as_gray`')
(I don't show this photo because it seems to be a copyright issue.)
# https://github.com/GilLevi/AgeGenderDeepLearning code as is
prediction = age_net.predict([input_image])
print ('predicted age:', age_list[prediction[0].argmax()])
predicted age: (60, 100)
I see?
# https://github.com/GilLevi/AgeGenderDeepLearning code as is
prediction = gender_net.predict([input_image])
print ('predicted gender:', gender_list[prediction[0].argmax()])
predicted gender: Male
Hmm?
Recommended Posts