LoginSignup
1
1

More than 3 years have passed since last update.

Google Colaboratory上でCaffeモデルを動かして世界のスーパーモデルの年齢と性別を予測する

Last updated at Posted at 2019-12-24

オープンソースのディープラーニングライブラリ Caffe を Google Colaboratory 上で動かしてみました。

Caffe のインストール

Google Colab 上に Caffe はないようなので、次のようにしてインストールします。

!apt install caffe-cpu

Caffe モデルの git clone

計算済みの Caffe モデルを選ぶにあたって、説明の詳しかった AgeGenderDeepLearning を選びました。 Google Colaboratory 上に git clone します。

!git clone https://github.com/GilLevi/AgeGenderDeepLearning

これは写真に映った人物の年齢と性別を予測するモデルです。このモデルを使って、世界のスーパーモデルの年齢と性別を予測したいと思います。

Caffe まわりの初期設定

# https://github.com/GilLevi/AgeGenderDeepLearning のコードそのまま
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' # 変更箇所
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' # 変更箇所
age_net_model_file='./AgeGenderDeepLearning/age_net_definitions/deploy.prototxt' # 変更箇所
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' # 変更箇所
gender_net_model_file='./AgeGenderDeepLearning/gender_net_definitions/deploy.prototxt' # 変更箇所
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 のコードそのまま
age_list=['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
gender_list=['Male','Female']

世界のスーパーモデルの写真

スーパーモデルの写真はこちらから取得しました。

# URL によるリソースへのアクセスを提供するライブラリをインポートする。
import urllib.request 
# ウェブ上のリソースを指定する
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Gisele_B_edit.jpg/300px-Gisele_B_edit.jpg'
# 指定したURLからリソースをダウンロードし、名前をつける。
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

(著作権的に問題になる気がするので、この写真は見せません。)

年齢と性別の予測

# https://github.com/GilLevi/AgeGenderDeepLearning のコードそのまま
prediction = age_net.predict([input_image]) 

print ('predicted age:', age_list[prediction[0].argmax()]) 
predicted age: (15, 20)

なるほど。

# https://github.com/GilLevi/AgeGenderDeepLearning のコードそのまま
prediction = gender_net.predict([input_image]) 

print ('predicted gender:', gender_list[prediction[0].argmax()])
predicted gender: Female

なるほど。

再度トライ

# URL によるリソースへのアクセスを提供するライブラリをインポートする。
import urllib.request 
# ウェブ上のリソースを指定する
url = 'https://upload.wikimedia.org/wikipedia/commons/f/f3/LindaEvangelista.jpg'
# 指定したURLからリソースをダウンロードし、名前をつける。
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

(著作権的に問題になる気がするので、この写真は見せません。)

# https://github.com/GilLevi/AgeGenderDeepLearning のコードそのまま
prediction = age_net.predict([input_image]) 

print ('predicted age:', age_list[prediction[0].argmax()]) 
predicted age: (60, 100)

なるほど?

# https://github.com/GilLevi/AgeGenderDeepLearning のコードそのまま
prediction = gender_net.predict([input_image]) 

print ('predicted gender:', gender_list[prediction[0].argmax()])
predicted gender: Male

ふーむ?

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1