MacbookでTensorFlowでAIを使ってみよう、という企画です
TensorFlowをインストールするでTensorFlowを稼働させる環境を準備し、TensorFlowを動かしてみるでTensorFlowを動かしてみました。
Macbookなど稼働環境の能力に余裕が無い場合には、他のアプリケーションを停止してから実行します
1. TensorFlowで画像認識してみる
- TensorFlow models repoからcloneまたはダウンロードします
-
models-master/tutorials/image/imagenet
にあるclassify_image.py
を実行します
(tf) MacBook-Pro:tftest tohru$ cd models-master/tutorials/image/imagenet/
(tf) MacBook-Pro:imagenet tohru$ ls
BUILD classify_image.py
(tf) MacBook-Pro:imagenet tohru$ python classify_image.py
>> Downloading inception-2015-12-05.tgz 100.0%
Successfully downloaded inception-2015-12-05.tgz 88931400 bytes.
2017-09-03 17:45:15.573066: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-09-03 17:45:15.573106: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-09-03 17:45:15.573111: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-09-03 17:45:15.573114: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-09-03 17:45:16.291128: W tensorflow/core/framework/op_def_util.cc:333] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89107)
indri, indris, Indri indri, Indri brevicaudatus (score = 0.00779)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00296)
custard apple (score = 0.00147)
earthstar (score = 0.00117)
(tf) MacBook-Pro:imagenet tohru$
- パンダと認識されました
2. 別の写真で画像認識させてみる
-
TensorFlow Tutorial - Image Recognitionの説明に従って、
--image_file
パラメータで別の写真を指定して画像認識させてみます
3. モデルを再学習させる
- How to Retrain Inception's Final Layer for New Categoriesに従い、モデルを再学習させてみます
-
github - tensorflow - retrainから、
tensorflow-master.zip
をダウンロードします - 再学習には
tensorflow-master/tensorflow/examples/image_retraining
の retrain.py を使用します - TensorFlowの再学習のページで使用されている花のサンプルは
curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
でダウンロード可能です。 - tensorflow-master.zipファイルをホームディレクトリ(~)直下の/tftestディレクトリで展開し、同ディレクトリに再学習用の画像ファイルのフォルダー(flower_photosなど)も置いたとして、再学習は
python ~/tftest/tensorflow-master/tensorflow/examples/image_retraining/retrain.py --image_dir ~/tftest/flower_photos
となります。再学習用の画像ファイルが多いと再学習に長い時間を要するので注意します。再学習の画像ファイルは各フォルダーに最低30枚以上必要です。 - 再学習は下記のようなコマンドで実行可能です。実行するには
sudo
コマンドを使用します。
再学習
python retrain.py \
--bottleneck_dir=bottlenecks \
--how_many_training_steps=500 \
--model_dir=inception \
--summaries_dir=training_summaries/basic \
--output_graph=retrained_graph.pb \
--output_labels=retrained_labels.txt \
--image_dir=flower_photos
- 上記で再学習したモデルを用いて、
tensorflow-master/tensorflow/examples/label_image
のlabel_image.py
を用いると、新たに学習した情報で画像を認識することがわかります。
label_image.pyの実行
python label_image.py --image flower_test/test.jpg --graph retrained_graph.pb --labels retrained_labels.txt
再学習したモデルでの判定
(tf_py3) MacBook-Pro:tf_retrain tohru$ python label_image.py --image flower_test/test.jpg --graph retrained_graph.pb --labels retrained_labels.txt
:
dandelion (score = 0.99935)
roses (score = 0.00065)
参考 画像を集めるツール
- 画像を集める際には以下のようなツールが便利なようです