Install TensorFlow 2.2.0 on Raspberry Pi 4

From CMEE4K
Jump to navigation Jump to search

We following this article Install TensorFlow 2.2.0 on Raspberry Pi 4 to build TensorFlow 2.2.0 for Python 3.7 on Raspberry Pi 4 32-bit OS.
Note: Some programs were created in ~/.local/bin directory. Use ". ./.profile" command to update $PATH in Terminal.

Install TensorFlow 2.2.0

Date: 2021-Apr-14
$ uname -a
Linux raspberrypi 5.10.17-v7l+ #1403 SMP Mon Feb 22 11:33:35 GMT 2021 armv7l GNU/Linux

# Get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade

# Remove old versions, if not placed in a virtual environment (let pip search for them)
$ sudo pip uninstall tensorflow
$ sudo pip3 uninstall tensorflow

# Install the dependencies (if not already onboard)
$ sudo apt-get install gfortran
$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install openmpi-bin libopenmpi-dev
$ sudo apt-get install liblapack-dev cython
$ sudo pip3 install keras_applications==1.0.8 --no-deps
$ sudo pip3 install keras_preprocessing==1.1.0 --no-deps
$ sudo pip3 install -U --user six wheel mock
$ sudo -H pip3 install pybind11
$ sudo -H pip3 install h5py==2.10.0

# Upgrade setuptools 40.8.0 -> 52.0.0
$ sudo -H pip3 install --upgrade setuptools

# Install gdown to download from Google drive
$ pip install gdown

# Copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=11mujzVaFqa7R1_lB7q0kVPW22Ol51MPg
# install TensorFlow
$ sudo -H pip3 install tensorflow-2.2.0-cp37-cp37m-linux_armv7l.whl

# And complete the installation by rebooting
$ sudo reboot

When we test with the simple program: "import tensorflow as tf"
Python3 complains the following:

tensorflow 2.2.0 has requirement __following__ which is incompatible.
    numpy .... 2.xx 
    gast==0.3.3, but you'll have gast 0.2.2
    h5py<2.11.0,>=2.10.0, but you'll have h5py 3.2.1 
    tensorboard<2.3.0,>=2.2.0, but you'll have tensorboard 2.0.2
    tensorflow-estimator<2.3.0,>=2.2.0, but you'll have tensorflow-estimator 1.14.0

We manually changing / reinstall the compatible versions

pip3 install numpy==1.16.5
pip3 install gast==0.3.3
pip3 install h5py==2.10.0
pip3 install tensorboard==2.2.0
pip3 install tensorflow-estimator==2.2.0

Then, the sample testing is passed.
Note: we should use virtual environment to prepare this.

Testing

Still have error, but this is the latest.

import tensorflow as tf
from tensorflow import Graph

grp = Graph()
with grp.as_default():
    variable = tf.Variable(42, name='foo')
    # initialize = tf.global_variables_initializer()
    assign = variable.assign(13)
sess = tf.compat.v1.Session(graph=grp)

hello = tf.constant('Hello, TensorFlow!')
print(sess.run(hello))

Install Bazel release 2.0.0

$ sudo apt-get install build-essential zip unzip curl
$ sudo apt-get install openjdk-11-jdk
$ wget https://github.com/bazelbuild/bazel/releases/download/2.0.0/bazel-2.0.0-dist.zip
$ unzip -d bazel bazel-2.0.0-dist.zip
$ cd bazel

$ vim scripts/bootstrap/compile.sh
# See the web page: Append "-J-Xmx800M" to "-encoding UTF-8 ..." line
# Still in bazel directory, issue this command.  It takes 30+ min to build.
$ env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh

# The output is My_working_dir/bazel/output/bazel
$ sudo cp output/bazel /usr/local/bin/bazel

Install Keras

$ pip3 install keras
$ pip3 list | grep -i -e pyyaml -e keras 
Keras                  2.4.3         
Keras-Applications     1.0.8         
Keras-Preprocessing    1.1.2         
PyYAML                 5.4.1         

__END__