LoginSignup
6
10

More than 5 years have passed since last update.

TensorFlowで画像認識

Last updated at Posted at 2017-09-03

MacbookでTensorFlowでAIを使ってみよう、という企画です
TensorFlowをインストールするでTensorFlowを稼働させる環境を準備し、TensorFlowを動かしてみるでTensorFlowを動かしてみました。
Macbookなど稼働環境の能力に余裕が無い場合には、他のアプリケーションを停止してから実行します

1. TensorFlowで画像認識してみる

スクリーンショット 2017-09-03 23.22.13.png

スクリーンショット 2017-09-03 17.49.20.png

  • 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. 別の写真で画像認識させてみる

3. モデルを再学習させる

スクリーンショット 2017-09-03 23.22.30.png

  • 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_imagelabel_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)

参考 画像を集めるツール

  • 画像を集める際には以下のようなツールが便利なようです

スクレイピング初心者がpythonでかわいい猫ちゃん画像をコマンド一発でネットから収集してみた

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10