RaspberryPiに比較的新しいTensorFlowをインストールしてみる(Python2系、Python3系)

RaspberryPiに比較的新しいTensorFlowをインストールしてみる(Python2系、Python3系)

そろそろ、TensorFlowを入れなきゃと思いインストールしてみたのですが、ネットを調べただけで、なにもしていなかったので年末だということもあり設定をしてみました。

github.com

比較的新しいバージョンのインストールエントリーがあったので以下2つを参考にさせてもらいました。基本的に1からビルドするとかなり時間がかかるのですが、これにより時間を大幅に短縮できたのでとても助かりました。ただ、この情報もそのままだとうまくいかない(インストールが失敗する)ので、失敗の状況も含めていろいろ調べて修正してみました。参考になればと思います。

qiita.com

petewarden.com

個人的にはpython3にそろそろ移行していきたいところ(個人的な来年の目標はpython3系に移行することにしています) 手順のエントリーには、python2系、python3系の両方のやり方が書いてあったので、折角なので両方セットアップしてみます。

python2系(version2.7)の手順

TensorFlowのインストール

python開発系のパッケージをインストールしていきます。インストールのログが長いのでコマンドとログを分けました。

コマンド:失敗例

$ sudo apt-get install libblas-dev liblapack-dev python-dev libatlas-base-dev gfortran python-setuptools
$ sudo pip2 install http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/bazel-out/pi/tensorflow-1.3.0-cp27-none-any.whl

【ログ】

$ sudo apt-get install libblas-dev liblapack-dev python-dev libatlas-base-dev gfortran python-setuptools
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-dev is already the newest version (2.7.13-2).
python-dev set to manually installed.
python-setuptools is already the newest version (33.1.1-1).
python-setuptools set to manually installed.
The following additional packages will be installed:
  gfortran-6 libatlas-dev libatlas3-base libgfortran-6-dev
Suggested packages:
  gfortran-doc gfortran-6-doc libgfortran3-dbg libcoarrays-dev libblas-doc
  liblapack-doc liblapack-doc-man
The following NEW packages will be installed:
  gfortran gfortran-6 libatlas-base-dev libatlas-dev libatlas3-base
  libblas-dev libgfortran-6-dev liblapack-dev
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.7 MB of archives.
After this operation, 56.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libgfortran-6-dev armhf 6.3.0-18+rpi1 [199 kB]
Get:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf gfortran-6 armhf 6.3.0-18+rpi1 [5,422 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian stretch/main armhf gfortran armhf 4:6.3.0-4 [1,352 B]
Get:4 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libatlas3-base armhf 3.10.3-1+rpi1 [1,920 kB]
Get:5 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libblas-dev armhf 3.7.0-2 [114 kB]
Get:6 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libatlas-dev armhf 3.10.3-1+rpi1 [65.9 kB]
Get:7 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libatlas-base-dev armhf 3.10.3-1+rpi1 [2,528 kB]
Get:8 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf liblapack-dev armhf 3.7.0-2 [1,483 kB]
Fetched 11.7 MB in 8s (1,304 kB/s)
Selecting previously unselected package libgfortran-6-dev:armhf.
(Reading database ... 122979 files and directories currently installed.)
Preparing to unpack .../0-libgfortran-6-dev_6.3.0-18+rpi1_armhf.deb ...
Unpacking libgfortran-6-dev:armhf (6.3.0-18+rpi1) ...
Selecting previously unselected package gfortran-6.
Preparing to unpack .../1-gfortran-6_6.3.0-18+rpi1_armhf.deb ...
Unpacking gfortran-6 (6.3.0-18+rpi1) ...
Selecting previously unselected package gfortran.
Preparing to unpack .../2-gfortran_4%3a6.3.0-4_armhf.deb ...
Unpacking gfortran (4:6.3.0-4) ...
Selecting previously unselected package libatlas3-base.
Preparing to unpack .../3-libatlas3-base_3.10.3-1+rpi1_armhf.deb ...
Unpacking libatlas3-base (3.10.3-1+rpi1) ...
Selecting previously unselected package libblas-dev.
Preparing to unpack .../4-libblas-dev_3.7.0-2_armhf.deb ...
Unpacking libblas-dev (3.7.0-2) ...
Selecting previously unselected package libatlas-dev.
Preparing to unpack .../5-libatlas-dev_3.10.3-1+rpi1_armhf.deb ...
Unpacking libatlas-dev (3.10.3-1+rpi1) ...
Selecting previously unselected package libatlas-base-dev.
Preparing to unpack .../6-libatlas-base-dev_3.10.3-1+rpi1_armhf.deb ...
Unpacking libatlas-base-dev (3.10.3-1+rpi1) ...
Selecting previously unselected package liblapack-dev.
Preparing to unpack .../7-liblapack-dev_3.7.0-2_armhf.deb ...
Unpacking liblapack-dev (3.7.0-2) ...
Setting up libatlas3-base (3.10.3-1+rpi1) ...
update-alternatives: using /usr/lib/atlas-base/atlas/libblas.so.3 to provide /usr/lib/libblas.so.3 (libblas.so.3) in auto mode
update-alternatives: using /usr/lib/atlas-base/atlas/liblapack.so.3 to provide /usr/lib/liblapack.so.3 (liblapack.so.3) in auto mode
Setting up libgfortran-6-dev:armhf (6.3.0-18+rpi1) ...
Processing triggers for libc-bin (2.24-11+deb9u1) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up gfortran-6 (6.3.0-18+rpi1) ...
Setting up gfortran (4:6.3.0-4) ...
update-alternatives: using /usr/bin/gfortran to provide /usr/bin/f95 (f95) in auto mode
update-alternatives: using /usr/bin/gfortran to provide /usr/bin/f77 (f77) in auto mode
Setting up libblas-dev (3.7.0-2) ...
update-alternatives: using /usr/lib/libblas/libblas.so to provide /usr/lib/libblas.so (libblas.so) in auto mode
Setting up libatlas-dev (3.10.3-1+rpi1) ...
Setting up liblapack-dev (3.7.0-2) ...
update-alternatives: using /usr/lib/lapack/liblapack.so to provide /usr/lib/liblapack.so (liblapack.so) in auto mode
Setting up libatlas-base-dev (3.10.3-1+rpi1) ...
update-alternatives: using /usr/lib/atlas-base/atlas/libblas.so to provide /usr/lib/libblas.so (libblas.so) in auto mode
update-alternatives: using /usr/lib/atlas-base/atlas/liblapack.so to provide /usr/lib/liblapack.so (liblapack.so) in auto mode

コマンドの1行目の必須パッケージのインストールはうまく行ったのですが、tensorflowの本体のインストールでは失敗してしまいます。 ブラウザでwhlファイルがあるURLを確認するとファイルのアクセスができない状態のようです。nightlyビルドってこんなものなの?かな。

$ sudo pip2 install http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/bazel-out/pi/tensorflow-1.3.0-cp27-none-any.whl
Collecting tensorflow==1.3.0 from http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/bazel-out/pi/tensorflow-1.3.0-cp27-none-any.whl
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 855, in _download_http_url
    resp.raise_for_status()
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/models.py", line 893, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 404 Client Error: Not Found for url: http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/bazel-out/pi/tensorflow-1.3.0-cp27-none-any.whl

いろいろと考えてnightlyビルドなのでもしかしたら新しいURLに格納されているのかなと思い、サイトを調べてみました。

nightly-pi [Jenkins]

このページから最新のRaspberryPi用のnightlyビルドが取得できそうです。現在はtensorflowのバージョンは1.4になっていますので参考にしたサイトより更に最新のものになります。(わーい)2017.12.30現在のビルドは#140なのでそれを使用してインストールを行っています。URLを変更して以下の様にします。

【インストールコマンド変更】

$ sudo pip2 install http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp27-none-any.whl

【ログ】

$ sudo pip2 install http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp27-none-any.whl
Collecting tensorflow==1.4.0 from http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp27-none-any.whl
  Downloading http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp27-none-any.whl (56.3MB)
    100% |????????????????????????????????| 56.3MB 10.9MB/s
Collecting protobuf>=3.4.0 (from tensorflow==1.4.0)
  Downloading protobuf-3.5.1-py2.py3-none-any.whl (388kB)
    100% |????????????????????????????????| 389kB 550kB/s
Collecting absl-py>=0.1.6 (from tensorflow==1.4.0)
  Downloading absl-py-0.1.7.tar.gz (78kB)
    100% |????????????????????????????????| 81kB 1.6MB/s
Collecting tensorflow-tensorboard (from tensorflow==1.4.0)
  Downloading tensorflow_tensorboard-0.1.8-py2-none-any.whl (1.6MB)
    100% |????????????????????????????????| 1.6MB 154kB/s
Requirement already satisfied: six>=1.10.0 in /usr/lib/python2.7/dist-packages (from tensorflow==1.4.0)
Requirement already satisfied: numpy>=1.12.1 in /usr/lib/python2.7/dist-packages (from tensorflow==1.4.0)
Requirement already satisfied: enum34>=1.1.6 in /usr/lib/python2.7/dist-packages (from tensorflow==1.4.0)
Collecting backports.weakref>=1.0rc1 (from tensorflow==1.4.0)
  Downloading backports.weakref-1.0.post1-py2.py3-none-any.whl
Requirement already satisfied: wheel in /usr/lib/python2.7/dist-packages (from tensorflow==1.4.0)
Collecting mock>=2.0.0 (from tensorflow==1.4.0)
  Downloading mock-2.0.0-py2.py3-none-any.whl (56kB)
    100% |????????????????????????????????| 61kB 1.8MB/s
Requirement already satisfied: setuptools in /usr/lib/python2.7/dist-packages (from protobuf>=3.4.0->tensorflow==1.4.0)
Collecting markdown>=2.6.8 (from tensorflow-tensorboard->tensorflow==1.4.0)
  Downloading Markdown-2.6.10.zip (414kB)
    100% |????????????????????????????????| 419kB 646kB/s
Requirement already satisfied: werkzeug>=0.11.10 in /usr/lib/python2.7/dist-packages (from tensorflow-tensorboard->tensorflow==1.4.0)
Collecting bleach==1.5.0 (from tensorflow-tensorboard->tensorflow==1.4.0)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorflow-tensorboard->tensorflow==1.4.0)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |????????????????????????????????| 890kB 261kB/s
Collecting funcsigs>=1; python_version < "3.3" (from mock>=2.0.0->tensorflow==1.4.0)
  Downloading funcsigs-1.0.2-py2.py3-none-any.whl
Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow==1.4.0)
  Downloading pbr-3.1.1-py2.py3-none-any.whl (99kB)
    100% |????????????????????????????????| 102kB 954kB/s
Building wheels for collected packages: absl-py, markdown, html5lib
  Running setup.py bdist_wheel for absl-py ... done
  Stored in directory: /root/.cache/pip/wheels/c4/71/71/7bd70f858f062d3f99990dcbc3f45b795bde057a42f971537f
  Running setup.py bdist_wheel for markdown ... done
  Stored in directory: /root/.cache/pip/wheels/1e/5a/55/a80b200d12e234d575ad68c1528593d1ce488720b65b24e48c
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: /root/.cache/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built absl-py markdown html5lib
Installing collected packages: protobuf, absl-py, markdown, html5lib, bleach, tensorflow-tensorboard, backports.weakref, funcsigs, pbr, mock, tensorflow
Successfully installed absl-py-0.1.7 backports.weakref-1.0.post1 bleach-1.5.0 funcsigs-1.0.2 html5lib-0.9999999 markdown-2.6.10 mock-2.0.0 pbr-3.1.1 protobuf-3.5.1 tensorflow-1.4.0 tensorflow-tensorboard-0.1.8

コマンド:成功例

$ sudo apt-get install libblas-dev liblapack-dev python-dev libatlas-base-dev gfortran python-setuptools
$ sudo pip2 install http://ci.tensorflow.org/view/Nightly/job/nightly-pi/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp27-none-any.whl

一般のパッケージに比べれば少し時間はかかりますが、そんな大した時間ではありません。これでインストールが完了しましたので対話型インターフェースで実行を行ってみます。

【実行テスト】

$ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] 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
>>>

TensorFlowが無事にインストールされたようです。やりました!

Kerasもインストールする

Kerasは,Pythonで書かれた,TensorFlowまたはCNTK,Theano上で実行可能な高水準のニューラルネットワークライブラリです. Kerasは,迅速な実験を可能にすることに重点を置いて開発されました. アイデアから結果に到達するまでのリードタイムをできるだけ小さくすることが,良い研究をするための鍵になります.

Kerasのサイトは以下になります。

Keras Documentation

https://keras.io/ja/keras.io

Keras Documentation

Kerasのインストールに関しては比較的簡単です。あとは依存系のライブラリモジュールもインストールしておきます。

【コマンド】

$ sudo apt-get install python-h5py
$ sudo pip install keras

【ログ】

$ sudo apt-get install python-h5py
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libaec0 libhdf5-100 libsz2
Suggested packages:
  python-h5py-doc
The following NEW packages will be installed:
  libaec0 libhdf5-100 libsz2 python-h5py
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,790 kB of archives.
After this operation, 7,210 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libaec0 armhf 0.3.2-1 [19.4 kB]
Get:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libsz2 armhf 0.3.2-1 [5,836 B]
Get:3 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libhdf5-100 armhf 1.10.0-patch1+docs-3 [1,203 kB]
Get:4 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf python-h5py armhf 2.7.0-1 [561 kB]
Fetched 1,790 kB in 3s (480 kB/s)
Selecting previously unselected package libaec0:armhf.
(Reading database ... 123241 files and directories currently installed.)
Preparing to unpack .../libaec0_0.3.2-1_armhf.deb ...
Unpacking libaec0:armhf (0.3.2-1) ...
Selecting previously unselected package libsz2:armhf.
Preparing to unpack .../libsz2_0.3.2-1_armhf.deb ...
Unpacking libsz2:armhf (0.3.2-1) ...
Selecting previously unselected package libhdf5-100:armhf.
Preparing to unpack .../libhdf5-100_1.10.0-patch1+docs-3_armhf.deb ...
Unpacking libhdf5-100:armhf (1.10.0-patch1+docs-3) ...
Selecting previously unselected package python-h5py.
Preparing to unpack .../python-h5py_2.7.0-1_armhf.deb ...
Unpacking python-h5py (2.7.0-1) ...
Setting up libaec0:armhf (0.3.2-1) ...
Processing triggers for libc-bin (2.24-11+deb9u1) ...
Setting up libsz2:armhf (0.3.2-1) ...
Setting up libhdf5-100:armhf (1.10.0-patch1+docs-3) ...
Setting up python-h5py (2.7.0-1) ...
Processing triggers for libc-bin (2.24-11+deb9u1) ...

$ sudo pip install keras
Collecting keras
  Using cached Keras-2.1.2-py2.py3-none-any.whl
Collecting pyyaml (from keras)
Requirement already satisfied: six>=1.9.0 in /usr/lib/python2.7/dist-packages (from keras)
Collecting scipy>=0.14 (from keras)
  Using cached scipy-1.0.0.tar.gz
Requirement already satisfied: numpy>=1.9.1 in /usr/lib/python2.7/dist-packages (from keras)
Building wheels for collected packages: scipy
  Running setup.py bdist_wheel for scipy ... done
  Stored in directory: /root/.cache/pip/wheels/b9/3e/bd/e9f0e1707c4be2bb1f671fd4246028d5d2e10cd70740ffd801
Successfully built scipy
Installing collected packages: pyyaml, scipy, keras
Successfully installed keras-2.1.2 pyyaml-3.12 scipy-1.0.0

これでインストールできました。(2時間弱かかりました)

python3系(version3.4またはversion3.5)の手順

pippip3と明示する必要があります。

TensorFlowのインストール

pythonの動作するバージョンに合わせる必要があります。 念のためRaspberryPiで動作するpython3系のバージョンを調べましょう。

比較的新しいRaspbianではPythonのバージョンは3.5.3のようです。

動作するpythonのバージョンがversion3.4の場合

すみませんが、こちらは動かせていませんが、whlファイルも存在するので多分うまく行きそうです。

【version3.4のコマンド】

$ sudo apt-get install libblas-dev liblapack-dev python-dev libatlas-base-dev gfortran python-setuptools
$ sudo pip3 install http://ci.tensorflow.org/view/Nightly/job/nightly-pi-python3/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp34-none-any.whl

動作するpythonのバージョンがversion3.5の場合

作業自体は大きくは変化しないのですが、3.4用に作成されたwhlファイルを3.5用に名前だけ変更してインストールしています。そのため一度名前変更するためにcurlでローカルにダウンロードしてからpip3でインストールしています。また、python3用にパッケージ名も変更しています。(既にpython3-devpython3-setuptoolsインストールはされているので気にしなくてもいいかもしれません。)

【version3.5のコマンド】

$ sudo apt-get install libblas-dev liblapack-dev python3-dev libatlas-base-dev gfortran python3-setuptools
$ curl -O http://ci.tensorflow.org/view/Nightly/job/nightly-pi-python3/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp34-none-any.whl
$ mv tensorflow-1.4.0-cp34-none-any.whl tensorflow-1.4.0-cp35-none-any.whl
$ sudo pip3 install tensorflow-1.4.0-cp35-none-any.whl

【ログ】

$ sudo apt-get install libblas-dev liblapack-dev python3-dev libatlas-base-dev gfortran python3-setuptools
Reading package lists... Done
Building dependency tree
Reading state information... Done
gfortran is already the newest version (4:6.3.0-4).
libatlas-base-dev is already the newest version (3.10.3-1+rpi1).
libblas-dev is already the newest version (3.7.0-2).
liblapack-dev is already the newest version (3.7.0-2).
python3-dev is already the newest version (3.5.3-1).
python3-dev set to manually installed.
python3-setuptools is already the newest version (33.1.1-1).
python3-setuptools set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

$ curl -O http://ci.tensorflow.org/view/Nightly/job/nightly-pi-python3/lastSuccessfulBuild/artifact/output-artifacts/tensorflow-1.4.0-cp34-none-any.whl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 53.6M  100 53.6M    0     0  2912k      0  0:00:18  0:00:18 --:--:-- 2966k
pi@raspberrypi:~/tensorflow $ mv tensorflow-1.4.0-cp34-none-any.whl tensorflow-1.4.0-cp35-none-any.whl
pi@raspberrypi:~/tensorflow $ ls
tensorflow-1.4.0-cp35-none-any.whl

$ sudo pip3 install tensorflow-1.4.0-cp35-none-any.whl
Processing ./tensorflow-1.4.0-cp35-none-any.whl
Requirement already satisfied: numpy>=1.12.1 in /usr/lib/python3/dist-packages (from tensorflow==1.4.0)
Collecting absl-py>=0.1.6 (from tensorflow==1.4.0)
  Downloading https://www.piwheels.hostedpi.com/simple/absl-py/absl_py-0.1.7-py3-none-any.whl (93kB)
    100% |????????????????????????????????| 102kB 91kB/s
Collecting protobuf>=3.4.0 (from tensorflow==1.4.0)
  Using cached protobuf-3.5.1-py2.py3-none-any.whl
Requirement already satisfied: wheel>=0.26 in /usr/lib/python3/dist-packages (from tensorflow==1.4.0)
Collecting tensorflow-tensorboard (from tensorflow==1.4.0)
  Downloading tensorflow_tensorboard-0.1.8-py3-none-any.whl (1.6MB)
    100% |????????????????????????????????| 1.6MB 176kB/s
Requirement already satisfied: six>=1.10.0 in /usr/lib/python3/dist-packages (from tensorflow==1.4.0)
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from protobuf>=3.4.0->tensorflow==1.4.0)
Collecting bleach==1.5.0 (from tensorflow-tensorboard->tensorflow==1.4.0)
  Using cached bleach-1.5.0-py2.py3-none-any.whl
Requirement already satisfied: werkzeug>=0.11.10 in /usr/lib/python3/dist-packages (from tensorflow-tensorboard->tensorflow==1.4.0)
Collecting markdown>=2.6.8 (from tensorflow-tensorboard->tensorflow==1.4.0)
  Downloading https://www.piwheels.hostedpi.com/simple/markdown/Markdown-2.6.10-py3-none-any.whl (77kB)
    100% |????????????????????????????????| 81kB 58kB/s
Collecting html5lib==0.9999999 (from tensorflow-tensorboard->tensorflow==1.4.0)
  Downloading https://www.piwheels.hostedpi.com/simple/html5lib/html5lib-0.9999999-py3-none-any.whl (111kB)
    100% |????????????????????????????????| 112kB 165kB/s
Installing collected packages: absl-py, protobuf, html5lib, bleach, markdown, tensorflow-tensorboard, tensorflow
Successfully installed absl-py-0.1.7 bleach-1.5.0 html5lib-0.9999999 markdown-2.6.10 protobuf-3.5.1 tensorflow-1.4.0 tensorflow-tensorboard-0.1.8

無事にインストールが終了した様です。では、こちらも対話型インターフェースでテストしてみます。 importしてみると…

$ 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 as tf
/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)
>>>

げーッまたしてもエラーが発生しました。

ぐぐってみたら以下のエントリーが。

qiita.com

どうもこの問題は既知の問題のようです。インストールするときにもファイル名をpython3.5に合わせるために修正したことも関係しているのでしょう。一番最初リンクしたサイトにも以下の記述がありました。

If you’re running Python 3.5, you can use the same wheel but with a slight change to the file name, since that encodes the version. You will see a couple of warnings every time you import tensorflow, but it should work correctly.

but it should work correctly.の記載があるので今後のバージョンアップに期待したいところです。では、テストを続けてみます。

$ 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 as tf
/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)
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

いろいろメッセージは出ますが、動作には問題ないようです。ビルドさせられるのは厳しかったのでかなり助かった気分です。

Kerasもインストールする

【コマンド】

$ sudo apt-get install python3-h5py
$ sudo pip3 install keras

【ログ】

$ sudo apt-get install python3-h5py
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  python-h5py-doc
The following NEW packages will be installed:
  python3-h5py
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 558 kB of archives.
After this operation, 2,647 kB of additional disk space will be used.
Get:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf python3-h5py armhf 2.7.0-1 [558 kB]
Fetched 558 kB in 1s (396 kB/s)
Selecting previously unselected package python3-h5py.
(Reading database ... 123353 files and directories currently installed.)
Preparing to unpack .../python3-h5py_2.7.0-1_armhf.deb ...
Unpacking python3-h5py (2.7.0-1) ...
Setting up python3-h5py (2.7.0-1) ...

$ sudo pip3 install keras
Collecting keras
  Using cached Keras-2.1.2-py2.py3-none-any.whl
Collecting pyyaml (from keras)
  Downloading https://www.piwheels.hostedpi.com/simple/pyyaml/PyYAML-3.12-cp35-cp35m-linux_armv7l.whl (43kB)
    100% |????????????????????????????????| 51kB 47kB/s
Requirement already satisfied: six>=1.9.0 in /usr/lib/python3/dist-packages (from keras)
Collecting scipy>=0.14 (from keras)
  Downloading https://www.piwheels.hostedpi.com/simple/scipy/scipy-1.0.0-cp35-cp35m-linux_armv7l.whl (35.1MB)
    100% |????????????????????????????????| 35.1MB 8.4kB/s
Requirement already satisfied: numpy>=1.9.1 in /usr/lib/python3/dist-packages (from keras)
Installing collected packages: pyyaml, scipy, keras
Successfully installed keras-2.1.2 pyyaml-3.12 scipy-1.0.0

こちらも無事にインストールができました。2系のインストールが終わっていたからかもしれませんが、すぐに完了しました。

おわりに

一応これでpython2系でもpython3系でもTensorFlow1.4のインストールができました。 あとは使うだけなんですよ。使うだけ…

/* -----codeの行番号----- */