plant-raspberrypi3のブログ

ラズベリーパイ3とPythonに挑戦して、植物を愛でたり画像を加工したりします。最近はscikit-imageの勉強してます。

ラズパイで学習済みモデルを使った画像認識をしてみた

こんにちは、らずべりーです。

無事、環境構築ができたので、早速おためし。 Kerasは学習済みのモデルがたくさんついていていいですね!

環境構築については過去記事を参照のこと。

plant-raspberrypi3.hatenablog.com

とりあえずVGG16で試してみた

早速、VGG16でお試し!

$ python
Python 3.6.5 (default, Apr 22 2018, 13:08:36) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
from keras.applications.vgg16 import VGG16
model = VGG16(weights="imagenet")

、、、20分以上経ってもラズパイさんは帰ってきません(T T)

やむなくキーボードで中断。

学習済みモデルには色々なデータサイズのものがある

どうもVGG16は重すぎるようだったので、それぞれのモデルの特徴について真面目に調べてみました。

引用元: Applications - Keras Documentation

f:id:plant-raspberrypi3:20180423113941p:plain

VGG16は重い方から2番目ですね。一番軽いMobileNetと比べると30倍ほども違いがあります。

一番軽いMobileNetで試してみた

from keras.applications.mobilenet import MobileNet
model = MobileNet(weights="imagenet")

今度は比較的すぐ帰ってきました(^ ^)

とりあえず、何か試したい!ということで、以下のページを参考に象さんの写真をダウンロードしてお試し。

ken5owata.hatenablog.com

from keras.preprocessing.image import load_img, img_to_array
import numpy as np
from keras.applications.mobilenet import preprocess_input, decode_predictions

img = load_img("elephant.jpg", target_size=(224,224))
img_array = img_to_array(img)
img_array1 = np.expand_dims(img_array,axis=0)
img_array1 = preprocess_input(img_array1)
pred = model.predict(img_array1)
results = decode_predictions(pred,top=5)[0]
for result in results:
     print(result)

('n01871265', 'tusker', 0.8331707)
('n02504458', 'African_elephant', 0.13779889)
('n02504013', 'Indian_elephant', 0.02774448)
('n02437312', 'Arabian_camel', 0.00064588425)
('n02113799', 'Standard_poodle', 0.00016444316)

tuskerというのは大きい牙の生えた動物のことで、象や猪のことを指すそうです。

ejje.weblio.jp

3位まで象関係で占められていますので、合っていると言えそうです。

少し重いものの正答率の高いXceptionで試してみた

次に、VGG16より6倍軽いXceptionで試してみました。このモデル、軽量ながらTop-5 accuracyは0.945と、Kerasに付属されている学習済みモデルの中ではトップクラスの成績を誇ります。

del model #modelの中身の消去

from keras.applications.xception import Xception, preprocess_input, decode_predictions

model = Xception(weights="imagenet")

MobileNetほどは速くはないですが、こちらもそこそこの時間で帰ってきました。実用上は問題なさそうです。

img = load_img("elephant.jpg", target_size=(299,299))
img_array = img_to_array(img)
img_array1 = np.expand_dims(img_array,axis=0)
img_array1 = preprocess_input(img_array1)
pred = model.predict(img_array1)
results = decode_predictions(pred,top=5)[0]
for result in results:
     print(result)
 
('n01871265', 'tusker', 0.35963103)
('n02504458', 'African_elephant', 0.27961522)
('n02504013', 'Indian_elephant', 0.023076782)
('n02422106', 'hartebeest', 0.0021587133)
('n03967562', 'plow', 0.0018908447)

3位まで象関係なので、こちらもオッケーですね。

象さんは、MobileNetとXceptionのどちらでもちゃんと判定できることがわかりました。

(2018/4/25 23:12追記) モデルの読み込みと予測にかかった時間を測って別記事にアップしました。こちらもご参照ください。

plant-raspberrypi3.hatenablog.com