LoginSignup
5
4

More than 3 years have passed since last update.

Raspberry PiにTensorFlow 1.15.0をインストールする

Last updated at Posted at 2020-02-12

こんにちは。
Raspberry PiへのTensorFlowのバージョン1.15.0以降をインストールしたかったのですが、
pipではうまくいかず、日本語でまとまっているものが見つからなかったので、備忘録がてらまとめておきます。

環境

ラズパイの環境は下記の通りです。

  • Raspberry Pi 3 Model B+
  • Raspbian 9.11(stretch)
  • Kernel 4.19.66-v7+
$ uname -a
#Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux
$ lsb_release -a                 
#No LSB modules are available.
#Distributor ID: Raspbian
#Description:    Raspbian GNU/Linux 9.11 (stretch)
#Release:        9.11
#Codename:       stretch

Pythonのバージョンは下記です。

$ python3 --version
#Python 3.5.3
$ python3 -m pip --version
#pip 20.0.2 from /home/pi/.local/lib/python3.5/site-packages/pip (python 3.5)

TensorFlowのインストール

pipでのインストール

通常通りpipでインストールしようとしました。

$ pip3 install tensorflow==xxx
#Could not find a version that satisfies the requirement tensorflow==xxx (from versions: 0.11.0,  1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.13.1, 1.14.0)
#No matching distribution found for tensorflow==xxx

しかし、ラズパイからは1.14.0までしかインストールできないようです。
そのため、別の方法でインストールすることにしました。

ソースからのビルド

公式ページの情報からビルド

TensorFlowの公式ページによると、Raspberry Piのソースからビルドすることで、インストールできそうです。
dockerを用いてビルドしていきます。
ラズパイ上でビルドすると時間がかかるのため、クロスコンパイルをした方が良いとのことなので、mac上でビルドしていきます。
先に結論からいうと、この方法ではビルドできませんでした。

mac
$ docker --version
#Docker version 19.03.5, build 633a0ea

githubからクローンしてきます。
今回は1.15.0をインストールしたいので、ブランチをチェックアウトしておきます。

mac
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ git checkout r1.15

公式ページに従ってクロスコンパイルを行います。

mac
$ CI_DOCKER_EXTRA_PARAMS="-e CI_BUILD_PYTHON=python3 -e CROSSTOOL_PYTHON_INCLUDE_PATH=/usr/include/python3.4" \
        tensorflow/tools/ci_build/ci_build.sh PI-PYTHON3 \
        tensorflow/tools/ci_build/pi/build_raspberry_pi.sh

ビルドが完了すると(約 30 分ほど)、.whl パッケージ ファイルがホストのソースツリーの出力アーティファクト用ディレクトリに作成されます。

とありますが、下記のようなエラーが出てビルドが完了しませんでした。

#addgroup: Please enter a username matching the regular expression configured
#via the NAME_REGEX[_SYSTEM] configuration variable.  Use the `--force-badname'
#option to relax this check or reconfigure NAME_REGEX.

調べてみると、公式の手順ではビルドできないというisuueがたっていました。

なんで公式のままできないんだよ。。。という感じですが、
issue内に、ネイティブビルドしたファイルをあげている人がいたので、今回はそれを使用します。

ビルドファイルからインストール

Tensorflow-binにあげられているので、READMEを参考にインストールしていきます。

ラズパイの環境がRaspbian 9.11(stretch)なので、tensorflow-1.15.0-cp35-cp35m-linux_armv7l.whlを使用します。

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran python-dev \
         libatlas3-base libatlas-base-dev libopenblas-dev libopenblas-base libblas-dev \
         liblapack-dev cython openmpi-bin libopenmpi-dev libatlas-base-dev python3-dev 
#libgfortran5がインストールできなかったので抜きました
$ sudo pip3 install keras_applications==1.0.8 --no-deps
$ sudo pip3 install keras_preprocessing==1.1.0 --no-deps
$ sudo pip3 install h5py==2.9.0
$ sudo pip3 install pybind11
$ pip3 install -U --user six wheel mock
$ sudo pip3 uninstall tensorflow
$ wget https://github.com/PINTO0309/Tensorflow-bin/raw/master/tensorflow-1.15.0-cp35-cp35m-linux_armv7l.whl
$ sudo pip3 install tensorflow-1.15.0-cp35-cp35m-linux_armv7l.whl

最後のインストールコマンドで何度かTypeError: unsupported operand type(s) for -=: 'Retry' and 'int'で失敗してしまいましたが、めげずにsudo pip3 install tensorflow-1.15.0-cp35-cp35m-linux_armv7l.whlを打っていたら5回ほどでインストール成功しました。。。

$ python3
>>> import tensorflow as tf
>>> tf.__version__
1.15.0
>>> exit()

無事に1.15.0がインストールできました!
今日はここまでです。

5
4
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
5
4