LoginSignup
8
9

More than 5 years have passed since last update.

Raspberry PiにバイナリでTensorFlowをインストールする方法(2018-08-10)

Last updated at Posted at 2018-08-10

はじめに

TensorFlowがRaspberry Piに対応したというニュースを見たので試してみた。
グーグルの機械学習フレームワーク「TensorFlow」が「Raspberry Pi」を正式にサポート - ZDNet Japan

バイナリなので簡単のはずが、
思いの外、エラーとワーニングが沢山でた。

インストール

ここを見ながら、試してみた。
Installing TensorFlow on Raspbian  |  TensorFlow

環境は、こんな感じ。

cat /etc/debian_version 
9.1
python3 --version
Python 3.5.3
pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)

コマンドは実質、これだけで良さそう。

sudo apt install libatlas-base-dev
pip3 install tensorflow

ただ、

sudo apt install libatlas-base-dev

してるときに、エラーが出た。抜粋すると、

Err:9 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian stretch/main armhf libstdc++-6-dev armhf 6.3.0-18+rpi1+deb9u1
  Connection failed [IP: 203.178.132.80 80]

E: Failed to fetch http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/pool/main/g/gcc-6/libstdc++-6-dev_6.3.0-18+rpi1+deb9u1_armhf.deb  Connection failed [IP: 203.178.132.80 80]
E: Failed to fetch http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/pool/main/a/atlas/libatlas3-base_3.10.3-1+rpi1_armhf.deb  Connection failed [IP: 203.178.132.80 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

こんなん。
言われるがままに、

sudo apt-get update
sudo apt install libatlas-base-dev

したら、うまくいった。
一応、検索してみると、こんなんが見つかった。

Ubuntu日本語フォーラム / ソフトウェアの更新・インストール等ができません。

古いバージョン?はミラーサイトに置かれてないんかも。

次に、

pip3 install tensorflow

したけど、メモリエラーとかいうのが出る。
Chromium開いてたので、落としてみる。

もう一度、試してみるとメモリエラーはでなくなったけど、こんなん出た。

TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

他にも

  File "/usr/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

って書かれてた。
通信環境がいけてないんかな。
なぜか、50kB/sくらいしか出てないしなぁ。

もう一度試してみる。

Successfully installed absl-py-0.3.0 astor-0.7.1 gast-0.2.0 grpcio-1.14.1 markdown-2.6.11 numpy-1.15.0 protobuf-3.6.0 setuptools-39.1.0 six-1.11.0 tensorboard-1.9.0 tensorflow-1.9.0 termcolor-1.1.0 werkzeug-0.14.1 wheel-0.31.1

やっと入ったっぽい。よかった。

確認

import tensorflow

pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
  return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 432, got 412
  return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 56, got 52
  return f(*args, **kwds)

なんかワーニングでるけど、そこそこ動いてそう。

この人も同じワーニング出てそう。
Raspberry PiでTensorFlowを走らせた!やはり正式サポートは嬉しい - THE長文日記

もうちょい調べて下記サイトがひっかかった。よくわからんけど問題なさそう。
気持ち悪いけど、無視する。
そのうち、python3.6とかpython3.7にしてもいいのかも。
RaspberryPiに比較的新しいTensorFlowをインストールしてみる(Python2系、Python3系) - uepon日々の備忘録
機械学習とRaspberry Piを用いてレーザー加工機の動作状態を判定する(第3回) - Qiita
Cross-compiling TensorFlow for the Raspberry Pi « Pete Warden's blog

Hello World

最後に、ここのサイトのハローワールド的なことしてみる。
Installing TensorFlow on Raspbian  |  TensorFlow
Run a short TensorFlow program

hello_tensorflow.py
import tensorflow as tf
hello = tf.constant("Hello, TensorFlow!")
sess = tf.Session()
print(sess.run(hello))
pi@raspberrypi:~/prj/test_tensorflow $ python3 hello_tensorflow.py 
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
  return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 432, got 412
  return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 56, got 52
  return f(*args, **kwds)
b'Hello, TensorFlow!'

おわりに

むっちゃ時間かかった。ほとんど待ち時間やけど。
通信環境、安定したところでやったほうがよかった。
家の安いWiFi環境があかんかったんかな?
イーサネット使ったほうがいいか。
ワーニングが気持ち悪いけど我慢しよう。

追記

公式のチュートリアルが、Jupyter前提なので、Jupyterも入れてみた。
こっち参照。
必要最小限でRaspberry PiにJupyter (Python3)をInstallする方法(2018-08-10) - Qiita

8
9
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
8
9