Tensorflow 2.2 install on Raspberry Pi — build from source( 安裝於 樹莓派 )

Seachaos
tree.rocks
Published in
3 min readMay 24, 2020

--

below content provide Mandarin / English ( 以下為中英文 )

Recently I am trying to use Tensorflow 2.2 on Raspberry Pi.
But seem it’s not available on pip3 install yet. ( internal built-in version )
最近想在樹莓派上使用 Tensorflow 2.2 , 但是發現內建 pip3 無法安裝

So I have some note here:
所以做了些筆記

1. Build Tensorflow 2.2 / 編譯 Tensorflow 2.2

You can build Tensorflow 2.2 from source code via TensorFlow website. after that you need you need Python version 3.5.x to run on Raspberry Pi. ( default is build for Python 3.5.x )
可以透過 TensorFlow 官方編譯出 Tensorflow 2.2, 預設是 Python 3.5 環境,所以還需要安裝在樹莓派上
https://www.tensorflow.org/install/source_rpi

Here is how to build Tensorflow 2.2 from source code for Raspberry Pi:
以下是編譯 Tensorflow 2.2 給樹莓派 :

You need x86/amd64 machine and already installed Docker environment.
需要一台 x86/amd64 且可以執行 Docker 的電腦

git clone -b v2.2.0 https://github.com/tensorflow/tensorflow.git --depth 1
cd tensorflow
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

Waiting a couple of hours. You will get whl file and copy it to Raspberry Pi.
等待幾個小時 ( 看電腦效能 ). 然後得到產出的 whl 檔案,將檔案複製到 Raspberry Pi 上

2. Install Python 3.5.X

You need install python 3.5.X if your raspberry pi not running with this version. in this example I choose 3.5.7
如果你樹莓派內建的 Python 版本不是 3.5.x 的話,需要額外安裝,以下範例是 3.5.7

use pyenv to install it :

sudo apt-get install libsqlite3-dev zlib1g-devgit clone -b v1.2.18 https://github.com/pyenv/pyenv.git ~/.pyenv --depth 1echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
# enable shims and autocompletion
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
source ~/.bashrcpyenv install 3.5.7
pyvenv global 3.5.7

Make sure you are using 3.5.7 / 確認是使用 3.5.7

If not, you can use this command / 如果不是,可以用以下指令
( you can add to your bashrc / 可以自行加入 bashrc )

export PATH="$HOME/.pyenv/versions/3.5.7/bin/:$PATH"

Install dependency library / 安裝其他需要的套件

pip3 install -U pip setuptools wheel
pip3 install six google-api-python-client

3. Install Numpy and Scipy from Source

In case if you encounter an error when install numpy, scipy, you need to build/install it from the source code.
如果你安裝 numpy 與 scipy 遇到錯誤,需要從 soruce code 安裝

# for build scipy
pip3 install pybind Cython
# prepare for build
sudo apt-get install -y python3-scipy libgfortran3
sudo apt-get install -y libopenblas-base libopenblas-dev python-dev gcc gfortran

Install numpy :

git clone -b v1.18.4 https://github.com/numpy/numpy.git --depth 1
cd numpy
python3 setup.py build_ext --inplace
pip3 install .

Install Scipy :

git clone -b v1.4.1 https://github.com/scipy/scipy.git --depth 1
cd scipy
git clean -xdf
python3 setup.py install --user

Install Tensorflow-2.2 on Raspberry

should be good to install it now.

pip3 install ./tensorflow-2.2.0-cp35-none-linux_armv7l.whl

after that you can try this command and get 2.2.0

python3 -c "import tensorflow as tf; print(tf.__version__)"

--

--