vgg16とtensorrtを使った画像認識に前回失敗したので、別のチュートリアルをやってみた。今回はtensorrtエンジンは使用しない。
スポンサーリンク
Vgg16で画像認識¶
import tensorflow as tf
import numpy as np
import random, json, string, pickle
import keras
import keras.layers
import keras.models
import keras.optimizers
import keras.callbacks
from keras.preprocessing import image
import keras.applications.vgg16 as vgg16
import keras.applications.resnet50 as resnet50
import matplotlib.pyplot as plt
from nltk import word_tokenize
%matplotlib inline
model = vgg16.VGG16(weights='imagenet')
model.summary()
最後のdense layer “predictions”が、Softmax activationを使用しているので出力はImagenet ILSVRC taskの1000 classesの確率に合致している。
img_path = 'test10.png' # This is an image I took in my kitchen.
img = image.load_img(img_path, target_size=(224, 224))
img_arr = image.img_to_array(img)
x = np.expand_dims(img_arr, axis=0) # The model only accepts batches so we add a dummy dimension.
x = vgg16.preprocess_input(x) # The preprocessing should be the same that was used during training.
predictions = model.predict(x)
label_predictions = vgg16.decode_predictions(predictions, top = 10)
print('Input image size:', x.shape)
print('Prediction scores: ', predictions.shape)
print('Predictions:')
for (i, (category_id, name, probability)) in enumerate(label_predictions[0]):
print('%d. %s(%.3f)' % (i, name, probability))
plt.imshow(np.asarray(img));
前回の苦労は何だったのかと思わせる程簡単にホットドッグを推論した。
img_path = '12885395143_4569259f48.jpg' # This is an image I took in my kitchen.
img = image.load_img(img_path, target_size=(224, 224))
img_arr = image.img_to_array(img)
x = np.expand_dims(img_arr, axis=0) # The model only accepts batches so we add a dummy dimension.
x = vgg16.preprocess_input(x) # The preprocessing should be the same that was used during training.
predictions = model.predict(x)
label_predictions = vgg16.decode_predictions(predictions, top = 10)
print('Input image size:', x.shape)
print('Prediction scores: ', predictions.shape)
print('Predictions:')
for (i, (category_id, name, probability)) in enumerate(label_predictions[0]):
print('%d. %s(%.3f)' % (i, name, probability))
plt.imshow(np.asarray(img));
自分でもこれの犬種は分からなかったが、当たってるっぽい。
%download https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/1UA2000.695.jpg/108px-1UA2000.695.jpg
img_path = '108px-1UA2000.695.jpg' # This is an image I took in my kitchen.
img = image.load_img(img_path, target_size=(224, 224))
img_arr = image.img_to_array(img)
x = np.expand_dims(img_arr, axis=0) # The model only accepts batches so we add a dummy dimension.
x = vgg16.preprocess_input(x) # The preprocessing should be the same that was used during training.
predictions = model.predict(x)
label_predictions = vgg16.decode_predictions(predictions, top = 10)
print('Input image size:', x.shape)
print('Prediction scores: ', predictions.shape)
print('Predictions:')
for (i, (category_id, name, probability)) in enumerate(label_predictions[0]):
print('%d. %s(%.3f)' % (i, name, probability))
plt.imshow(np.asarray(img));
さすがにこれはあかんかった。
スポンサーリンク
スポンサーリンク