寝台急行はまなす

札幌駅 22:00発 増結12両編成 青森行き

raspberry pi 3にTensorFlowを入れて画像認識させる

qiita.com

 

こいつの続き、ラズパイ3にTensorFlowを入れるところから。

これでわしもきゅうり判別機を作れるだろうかw 。

 

・TensorFlowを入れる 

GitHub - samjabrahams/tensorflow-on-raspberry-pi: TensorFlow for Raspberry Pi

pi@raspberrypi:~ $ wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/releases/download/v1.1.0/tensorflow-1.1.0-cp27-none-linux_armv7l.whl

pi@raspberrypi:~ $ sudo pip install tensorflow-1.1.0-cp27-none-linux_armv7l.whl

pi@raspberrypi:~ $ sudo pip uninstall mock

pi@raspberrypi:~ $ sudo pip install mock

 

・確認

pi@raspberrypi:~ $ python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a + b)
42
>>>

 

・kerasを入れる(かなり時間かかった。。)

pi@raspberrypi:~ $ sudo pip install keras

 

・学習済みのモデルを持ってくる

pi@raspberrypi:~ $ git clone https://github.com/fchollet/deep-learning-models

 

・実行 → 怒られた

pi@raspberrypi:~/deep-learning-models $ python inception_v3.py
Using TensorFlow backend.
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5
Traceback (most recent call last):
  File "inception_v3.py", line 399, in <module>
    model = InceptionV3(include_top=True, weights='imagenet')
  File "inception_v3.py", line 385, in InceptionV3
    model.load_weights(weights_path)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2531, in load_weights
    raise ImportError('`load_weights` requires h5py.')
ImportError: `load_weights` requires h5py.
pi@raspberrypi:~/deep-learning-models $

 

・h5pyっていうのがいるの? → pipで入れる → 怒られた

pi@raspberrypi:~ $ sudo pip install h5py

※中略

In file included from /tmp/pip-build-1n231R/h5py/h5py/defs.c:470:0:
    /tmp/pip-build-1n231R/h5py/h5py/api_compat.h:27:18: fatal error: hdf5.h: No such file or directory
     #include "hdf5.h"
                      ^
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

 

・必要なものをいれる

pi@raspberrypi:~ $ sudo apt-get install -y libhdf5-dev

 

・とりあえずh5pyっていうのが入った

pi@raspberrypi:~ $ sudo pip install h5py
Collecting h5py
  Using cached h5py-2.7.0.tar.gz
Requirement already satisfied: numpy>=1.7 in /usr/local/lib/python2.7/dist-packages (from h5py)
Requirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from h5py)
Building wheels for collected packages: h5py
  Running setup.py bdist_wheel for h5py ... done
  Stored in directory: /root/.cache/pip/wheels/3e/ff/67/d04f183c668c251c1bc737224584dec995715d486178f77ede
Successfully built h5py
Installing collected packages: h5py
Successfully installed h5py-2.7.0
pi@raspberrypi:~ $

 

・これで行けるか? → 失敗。。。

pi@raspberrypi:~/deep-learning-models $ python inception_v3.py
Using TensorFlow backend.
Traceback (most recent call last):
  File "inception_v3.py", line 402, in <module>
    img = image.load_img(img_path, target_size=(299, 299))
  File "/usr/local/lib/python2.7/dist-packages/keras/preprocessing/image.py", line 318, in load_img
    raise ImportError('Could not import PIL.Image. '
ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL.
pi@raspberrypi:~/deep-learning-models $

 

・これがいる模様 → pillow

pi@raspberrypi:~/deep-learning-models $ sudo pip install pillow

 

・必要なもの入ったんじゃね?

pi@raspberrypi:~/deep-learning-models $ python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>>

 

・来たーー!!???

pi@raspberrypi:~/deep-learning-models $ python inception_v3.py
Using TensorFlow backend.
Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
Predicted: (u'n02504458', u'African_elephant', 0.58458394), (u'n01871265', u'tusker', 0.35385633), (u'n02504013', u'Indian_elephant', 0.01978549), (u'n02134084', u'ice_bear', 0.0005529598), (u'n02437312', u'Arabian_camel', 0.00053362496)
pi@raspberrypi:~/deep-learning-models $

 

※この画像

f:id:ken5owata:20170612223613j:plain

 

アフリカゾウ、あってそう!

 

pi@raspberrypi:~/deep-learning-models $ time python inception_v3.py apple.jpg
Using TensorFlow backend.
Predicted: (u'n07742313', u'Granny_Smith', 0.41690961), (u'n07720875', u'bell_pepper', 0.056339104), (u'n07745940', u'strawberry', 0.037945356), (u'n07747607', u'orange', 0.031627435), (u'n07768694', u'pomegranate', 0.027530219)

pi@raspberrypi:~/deep-learning-models $

 

なるほどりんごですか、色はちがうけど合ってるね。

※この画像

 

pi@raspberrypi:~/deep-learning-models $ time python inception_v3.py fi004.jpg
Using TensorFlow backend.
Predicted: (u'n07684084', u'French_loaf', 0.318775), (u'n07716358', u'zucchini', 0.17547932), (u'n07579787', u'plate', 0.1427407), (u'n07860988', u'dough', 0.07184507), (u'n07871810', u'meat_loaf', 0.067287661)

pi@raspberrypi:~/deep-learning-models $

 

んーフランスパンですかー??

※この画像

 

pi@raspberrypi:~/deep-learning-models $ python inception_v3.py
Using TensorFlow backend.
Predicted: (u'n04350905', u'suit', 0.14072829), (u'n03763968', u'military_uniform', 0.1166545), (u'n02916936', u'bulletproof_vest', 0.068024598), (u'n02883205', u'bow_tie', 0.037428539), (u'n04591157', u'Windsor_tie', 0.02336538)
pi@raspberrypi:~/deep-learning-models $

 

スーツ、軍服、防弾チョッキ、蝶ネクタイ!??!!!

 

※この画像

f:id:ken5owata:20170612223841j:plain

 

あっ(察し)。