robocup-zunda’s blog

高崎高校物理部の部活です。

Raspberry PiにTensorflowが入らない時の入れ方

こんにちは、参禅町です。今回は、Raspberry Piにpipコマンドでtensorflowが入らなかった時に行った入れ方について話したいと思います。

原因と解決法

PyPIにラズパイ用のtensorflowが登録されていないことが原因であるため、対応するensorflowをビルドするか、既にビルドされたものを使用すればインストールできます。今回の場合は、以下のリンクにある既にビルドされたもの(whlファイル)をダウンロードしてインストールします。

github.com

環境

機種: Raspberry Pi 3 model B+

OS: Debian GNU/Linux 11 (bullseye)

Python: 3.9.2

pip: 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)

手順

Raspberry Piコマンドライン上で完結する手順を紹介します。

まず今回の環境の場合は、download_tensorflow-2.9.0-cp39-none-linux_aarch64.shに書かれている

curl -L https://github.com/PINTO0309/Tensorflow-bin/releases/download/v2.9.0/tensorflow-2.9.0-cp39-none-linux_aarch64.whl -o tensorflow-2.9.0-cp39-none-linux_aarch64.whl

を実行します。ファイル名のcp**の部分がPythonのバージョンを表しているので使用している環境のバージョンにあったものを使用してください。(バージョンは python -V もしくは python3 -V で確認可能)今回の場合は3.9.2だったので、cp39となっているものを使用しました。
そして、ダウンロードされたwhlファイルを使用して以下のようにインストールを行なってください。

sudo pip install tensorflow-2.9.0-cp39-none-linux_aarch64.whl

最終的に、

Successfully installed astunparse-1.6.3 flatbuffers-1.12 gast-0.4.0 google-pasta-0.2.0 h5py-3.7.0 keras-2.9.0 keras-preprocessing-1.1.2 libclang-14.0.1 opt-einsum-3.3.0 tensorflow-2.9.0 tensorflow-estimator-2.9.0 tensorflow-io-gcs-filesystem-0.26.0 termcolor-1.1.0 wrapt-1.14.1

といった表示が出ればインストール完了です。

動作確認

Pythonで次のように実行し、2.9.0のようにバージョンが出力されれば正常にインストールできています。

#TensorFlow と tf.keras のインボート
import tensorflow as tf from tensorflow import keras
print(tf.__version__)

実際のログ

pi@raspi:~ $ python -V
Python 3.9.2
pi@raspi:~ $ curl -L https://github.com/PINTO0309/Tensorflow-bin/releases/download/v2.9.0/tensorflow-2.9.0-cp39-none-linux_aarch64.whl -o tensorflow-2.9.0-cp39-none-linux_aarch64.whl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  287M  100  287M    0     0  2618k      0  0:01:52  0:01:52 --:--:-- 2996k
pi@raspi:~ $ ls
cap_folder   main.py           momo-2022.2.0_raspberry-pi-os_armv8         runs                 tensorflow-2.9.0-cp39-none-linux_aarch64.whl  yolov5s.pt
cap_test.py  main.py.old       momo-2022.2.0_raspberry-pi-os_armv8.tar.gz  raspi-request.txt  yolov5                                        yolov7
git_folder   main__test.ipynb  now.jpg                                     target.jpg           yolov5s6.pt
pi@raspi:~ $ sudo pip install tensorflow-2.9.0-cp39-none-linux_aarch64.whl
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Processing ./tensorflow-2.9.0-cp39-none-linux_aarch64.whl
Collecting opt-einsum>=2.3.2
  Downloading https://www.piwheels.org/simple/opt-einsum/opt_einsum-3.3.0-py3-none-any.whl (65 kB)
     |████████████████████████████████| 65 kB 62 kB/s 

Collecting gast<=0.4.0,>=0.2.1
  Downloading https://www.piwheels.org/simple/gast/gast-0.4.0-py3-none-any.whl (9.9 kB)
Collecting flatbuffers<2,>=1.12
  Downloading https://www.piwheels.org/simple/flatbuffers/flatbuffers-1.12-py2.py3-none-any.whl (15 kB)
Requirement already satisfied: six>=1.12.0 in /usr/lib/python3/dist-packages (from tensorflow==2.9.0) (1.16.0)
Collecting keras<2.10.0,>=2.9.0rc0
  Downloading keras-2.9.0-py2.py3-none-any.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 14 kB/s 
Requirement already satisfied: packaging in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (21.3)
Collecting google-pasta>=0.1.1
  Downloading https://www.piwheels.org/simple/google-pasta/google_pasta-0.2.0-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 78 kB/s 
Requirement already satisfied: numpy>=1.20 in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (1.23.0)
Requirement already satisfied: protobuf>=3.9.2 in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (3.19.4)
Collecting tensorflow-estimator<2.10.0,>=2.9.0rc0
  Downloading tensorflow_estimator-2.9.0-py2.py3-none-any.whl (438 kB)
     |████████████████████████████████| 438 kB 1.6 MB/s 
Collecting tensorflow-io-gcs-filesystem>=0.23.1
  Downloading tensorflow_io_gcs_filesystem-0.26.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.8 MB)
     |████████████████████████████████| 2.8 MB 55 kB/s 
Collecting wrapt>=1.11.0
  Downloading wrapt-1.14.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (77 kB)
     |████████████████████████████████| 77 kB 379 kB/s 
Collecting termcolor>=1.1.0
  Downloading https://www.piwheels.org/simple/termcolor/termcolor-1.1.0-py3-none-any.whl (4.8 kB)
Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (4.3.0)
Requirement already satisfied: tensorboard<2.10,>=2.9 in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (2.9.1)
Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (1.1.0)
Collecting keras-preprocessing>=1.1.1
  Downloading https://www.piwheels.org/simple/keras-preprocessing/Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 7.3 kB/s 
Collecting astunparse>=1.6.0
  Downloading https://www.piwheels.org/simple/astunparse/astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.9/dist-packages (from tensorflow==2.9.0) (1.47.0)
Collecting h5py>=2.9.0
  Downloading h5py-3.7.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.5 MB)
     |████████████████████████████████| 8.5 MB 137 kB/s 
Collecting libclang>=13.0.0
  Downloading libclang-14.0.1-py2.py3-none-manylinux2014_aarch64.whl (12.6 MB)
     |████████████████████████████████| 12.6 MB 120 kB/s 
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from tensorflow==2.9.0) (52.0.0)
Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/lib/python3/dist-packages (from astunparse>=1.6.0->tensorflow==2.9.0) (0.34.2)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (1.8.1)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (0.4.6)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (3.3.7)
Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (2.9.0)
Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (2.1.2)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/lib/python3/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (2.25.1)
Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.9/dist-packages (from tensorboard<2.10,>=2.9->tensorflow==2.9.0) (0.6.1)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.9/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (5.2.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.9/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (0.2.8)
Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.9/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (4.8)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.9/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (1.3.1)
Requirement already satisfied: importlib-metadata>=4.4 in /usr/local/lib/python3.9/dist-packages (from markdown>=2.6.8->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (4.12.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.9/dist-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (3.8.0)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.9/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.9/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow==2.9.0) (3.2.0)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.9/dist-packages (from packaging->tensorflow==2.9.0) (3.0.9)
Installing collected packages: wrapt, termcolor, tensorflow-io-gcs-filesystem, tensorflow-estimator, opt-einsum, libclang, keras-preprocessing, keras, h5py, google-pasta, gast, flatbuffers, astunparse, tensorflow
  Attempting uninstall: flatbuffers
    Found existing installation: flatbuffers 20181003210633
    Uninstalling flatbuffers-20181003210633:
      Successfully uninstalled flatbuffers-20181003210633
Successfully installed astunparse-1.6.3 flatbuffers-1.12 gast-0.4.0 google-pasta-0.2.0 h5py-3.7.0 keras-2.9.0 keras-preprocessing-1.1.2 libclang-14.0.1 opt-einsum-3.3.0 tensorflow-2.9.0 tensorflow-estimator-2.9.0 tensorflow-io-gcs-filesystem-0.26.0 termcolor-1.1.0 wrapt-1.14.1