自前の保存したモデルを用いて画像を分類してみる。
はじめに
以前、CIFAR-10チュートリアルという記事を投稿しました。この記事ではテスト画像がどの程度の精度で分類されるかを可視化するものでしたが、いささか大げさだったので、余計なパッケージを使わなくても調べられる方法をメモしておきます.
まず、テスト画像の分類についてまとめました。これはテスト画像をそれぞれ分類し、その精度を表示させました。次に任意の画像を使って分類させました。カテゴリーわけすることはできたのですが、その精度についてはどう出力するかわかりませんでした。課題ですね。分かり次第修正させていただきます。
動作環境
Tensorflow 1.2.1, Python3
テスト画像を分類する
from keras.datasets import cifar10
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.contrib.keras.python.keras.utils.np_utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
def plot_image(X, label=None):
print('☆テスト画像: %s' % label)
plt.imshow(X)
plt.show()
plt.clf()
if __name__=='__main__':
## import data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.reshape([-1, 32, 32, 3])
X_test = X_test.reshape([-1, 32, 32, 3])
print('%i training samples' % X_train.shape[0])
print('%i test samples' % X_test.shape[0])
print(X_train.shape)
# convert integer RGB values (0-255) to float values (0-1)
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
## CIFAR-10公式ラベル
cifar10_labels = np.array([
'airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck'])
# create model
model = load_model('my_model.h5') # 保存したモデルから読み込む
# output
preds = model.predict(X_test) # modelは学習させたもの
category = np.argmax(preds, axis=1)
for i in range(10):
classed = category[i]
labels = flower_vgg_label[classed]
plot_image(X_test[i], labels)
print('%i - 分類されたものは%sです。精度は%f%です。\n' % (i, labels, preds[i][classed]*100))
print("################################################")
from keras.datasets import cifar10
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.contrib.keras.python.keras.utils.np_utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
def plot_image(X, label=None):
print('☆テスト画像: %s' % label)
plt.imshow(X)
plt.show()
plt.clf()
if __name__=='__main__':
## import data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.reshape([-1, 32, 32, 3])
X_test = X_test.reshape([-1, 32, 32, 3])
print('%i training samples' % X_train.shape[0])
print('%i test samples' % X_test.shape[0])
print(X_train.shape)
# convert integer RGB values (0-255) to float values (0-1)
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
## CIFAR-10公式ラベル
cifar10_labels = np.array([
'airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck'])
# create model
model = load_model('my_model.h5') # 保存したモデルから読み込む
# output
preds = model.predict(X_test) # modelは学習させたもの
category = np.argmax(preds, axis=1)
for i in range(10):
classed = category[i]
labels = flower_vgg_label[classed]
plot_image(X_test[i], labels)
print('%i - 分類されたものは%sです。精度は%f%です。\n' % (i, labels, preds[i][classed]*100))
print("################################################")
参考:
出力例
50000 training samples
10000 test samples
(50000, 32, 32, 3)
50000 training samples
10000 test samples
(50000, 32, 32, 3)
50000 training samples
10000 test samples
(50000, 32, 32, 3)
50000 training samples
10000 test samples
(50000, 32, 32, 3)
0 - 分類されたものはcatです。精度は66.098875%です。
1 - 分類されたものはshipです。精度は99.913126%です。
2 - 分類されたものはshipです。精度は51.876527%です。
(to be continued...)
任意の画像を分類する
ネットから拾ってきた画像等は以下のようにすれば分類できます。
from keras.models import load_model
from keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input
import numpy as np
# CIFAR-10公式ラベル
cifar10_labels = np.array([
'airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck'])
model = load_model('my_model.h5') # 保存したモデルから読み込む
img_path = 'car.png' # 任意の画像
img = image.load_img(img_path, target_size=(32, 32)) #入力画像のサイズ
x = image.img_to_array(img) # 画像データをnumpy.arrayへ変換
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x) # modelは学習させたもの
category = np.argmax(preds, axis=1)
classed = category[0]
print('分類されたものは{0}です。' .format(cifar10_labels[classed]))
分類されたものはautomobileです。
しかしながら画像の精度等を出力することができませんでした。おそらくmodel.predict
の引数が一つだけなのが良くないのかもしれません(未確認)。もしご存知の方がいらっしゃいましたら教えていただけるとうれしいです。
コメント
予め学習されたモデル(pretrained model)を使えば、各画像がどのカテゴリーに分けられるかを詳細に知ることができます。詳しくは以下参考文献を参照ください。
参考文献
Author And Source
この問題について(自前の保存したモデルを用いて画像を分類してみる。), 我々は、より多くの情報をここで見つけました https://qiita.com/hiroyuki827/items/e6914b9b85f7bfe55078著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .