[PYTHON] Run the Caffe model on Google Colaboratory to predict the age and gender of the world's supermodels

I tried running the open source deep learning library Caffe on Google Colaboratory.

Install Caffe

There seems to be no Caffe on Google Colab, so install it as follows:

!apt install caffe-cpu

Caffe model git clone

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.

Initial settings around Caffe

# 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']

Pictures of world supermodels

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`')

png

(I don't show this photo because it seems to be a copyright issue.)

Age and gender prediction

# 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.

Try again

#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`')

png

(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

Run the Caffe model on Google Colaboratory to predict the age and gender of the world's supermodels
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
Caffe Model Zoo for beginners [Age and gender classification]
How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"
Run Keras on Google Colaboratory TPU
I tried to visualize the age group and rate distribution of Atcoder
Steps to run Google Chrome headless on EC2 Ubuntu and take screenshots
I tried to predict the behavior of the new coronavirus with the SEIR model.
Use The Metabolic Disassembler on Google Colaboratory
Setting to make the scale and label of the figure easy to see even in the dark theme with google Colaboratory
Make sure to align the pre-processing at the time of forecast model creation and forecast
Retry to turn on/off the power of the USB port on RaspberryPi 4 Model B
Until you install Caffe and run the sample
I tried to predict the price of ETF
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)
[Python] Save the result of web scraping the Mercari product page on Google Colab to Google Sheets and display the product image as well.