| [サイトマップへ] |
先人に感謝.
入力画像の例
出力としては「種類」を表すラベル(下の図では「Person」や「Bicycle」)と、バウンディングボックス
キーワード: SnowMasaya ssd_keras, Keras 2, Python, Windows インストール
Windows で、Anaconda を用いて隔離されたPython環境を作り、 そこに TensorFlow, Keras をインストールする手順について説明します
前準備として,Python 開発環境のAnaconda のインストールが終わっていること. Windows では Chocholatey のインストールが終わっていること
Windows での 手順は、 別の Web ページに記載しています
以下,Windows での Anaconda をインストール済み, 隔離された Python 環境(名前は keras)に、Tensorflow, Keras をインストール済みであるものとして説明を続けます.
https://github.com/SnowMasaya/ssd_keras
※ Windows での展開(解凍)のためのソフトは「7-Zip」をおすすめ.
この .zip ファイルは,E:\ssd_keras-master に展開(解凍)したものとして,説明を続けるので,適切に読み替えてください.
https://mega.nz/#F!7RowVLCL!q3cEVRK9jyOSB9el3SssIA
下の図のように
https://github.com/SnowMasaya/ssd_keras/blob/master/SSD.ipynb
そのために, IPython シェルのコンソールで、Python 環境(名前は keras)を使う
Windows では、Anacondaに入っている開発環境 spyder を実行し,右下の ipython コンソールを使うのが簡単.
※ Windows で、あるPython 環境(名前は aiとする)の spyder を使いたいとき:
ssd.py というファイルがあるディレクトリに移動.
cd E:\ssd_keras-master
次の Python プログラムを実行
※ https://github.com/SnowMasaya/ssd_keras/blob/master/SSD.ipynb の「in [1]」に記載の通り.
from keras.applications.imagenet_utils import preprocess_input from keras.backend.tensorflow_backend import set_session from keras.preprocessing import image import matplotlib.pyplot as plt import numpy as np from scipy.misc import imread import tensorflow as tf from ssd_v2 import SSD300v2 from ssd_utils import BBoxUtility from tensorflow.contrib.tensorboard.plugins import projector import os %matplotlib inline plt.rcParams['figure.figsize'] = (8, 8) plt.rcParams['image.interpolation'] = 'nearest' np.set_printoptions(suppress=True) config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.45 set_session(tf.Session(config=config))
次のようなエラーが出たときは,1つ前の「cd ...」に戻ってやり直す
「pandas パッケージ」のエラーが出た場合には、Anaconda のバージョンが古い可能性がある. Anaconda バージョン 4.2.0 ではエラーが出る. Windows では Anaconda バージョン 4.4.0 をインストールする.
次の Python プログラムを実行
※ https://github.com/SnowMasaya/ssd_keras/blob/master/SSD.ipynb の「in [2]」に記載の通り.
voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant',
'Sheep', 'Sofa', 'Train', 'Tvmonitor']
NUM_CLASSES = len(voc_classes) + 1
次の Python プログラムを実行
※ https://github.com/SnowMasaya/ssd_keras/blob/master/SSD.ipynb の「in [3]」に記載の通り.
input_shape=(300, 300, 3)
model = SSD300v2(input_shape, num_classes=NUM_CLASSES)
model.load_weights('weights_SSD300.hdf5', by_name=True)
bbox_util = BBoxUtility(NUM_CLASSES)
次の Python プログラムを実行
※ https://github.com/rykov8/ssd_keras/blob/master/SSD.ipynb の「in [4]」に記載の通り.
inputs = [] images = [] img_path = './pics/fish-bike.jpg' img = image.load_img(img_path, target_size=(300, 300)) img = image.img_to_array(img) images.append(imread(img_path)) inputs.append(img.copy()) img_path = './pics/cat.jpg' img = image.load_img(img_path, target_size=(300, 300)) img = image.img_to_array(img) images.append(imread(img_path)) inputs.append(img.copy()) img_path = './pics/boys.jpg' img = image.load_img(img_path, target_size=(300, 300)) img = image.img_to_array(img) images.append(imread(img_path)) inputs.append(img.copy()) img_path = './pics/car_cat.jpg' img = image.load_img(img_path, target_size=(300, 300)) img = image.img_to_array(img) images.append(imread(img_path)) inputs.append(img.copy()) img_path = './pics/car_cat2.jpg' img = image.load_img(img_path, target_size=(300, 300)) img = image.img_to_array(img) images.append(imread(img_path)) inputs.append(img.copy()) inputs = preprocess_input(np.array(inputs))
次の Python プログラムを実行
※ https://github.com/rykov8/ssd_keras/blob/master/SSD.ipynb の「in [5]」に記載の通り.
preds = model.predict(inputs, batch_size=1, verbose=1)
次の Python プログラムを実行
※ https://github.com/rykov8/ssd_keras/blob/master/SSD.ipynb の「in [6]」に記載の通り.
results = bbox_util.detection_out(preds)
次の Python プログラムを実行
※ https://github.com/rykov8/ssd_keras/blob/master/SSD.ipynb の「in [8]」に記載の通り.
for i, img in enumerate(images):
# Parse the outputs.
det_label = results[i][:, 0]
det_conf = results[i][:, 1]
det_xmin = results[i][:, 2]
det_ymin = results[i][:, 3]
det_xmax = results[i][:, 4]
det_ymax = results[i][:, 5]
# Get detections with confidence higher than 0.6.
top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]
top_conf = det_conf[top_indices]
top_label_indices = det_label[top_indices].tolist()
top_xmin = det_xmin[top_indices]
top_ymin = det_ymin[top_indices]
top_xmax = det_xmax[top_indices]
top_ymax = det_ymax[top_indices]
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
plt.imshow(img / 255.)
currentAxis = plt.gca()
for i in range(top_conf.shape[0]):
xmin = int(round(top_xmin[i] * img.shape[1]))
ymin = int(round(top_ymin[i] * img.shape[0]))
xmax = int(round(top_xmax[i] * img.shape[1]))
ymax = int(round(top_ymax[i] * img.shape[0]))
score = top_conf[i]
label = int(top_label_indices[i])
label_name = voc_classes[label - 1]
display_txt = '{:0.2f}, {}'.format(score, label_name)
coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
color = colors[label]
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
plt.show()
実行結果