TensorFlow 2 on Raspberry Pi. TensorFlow Lite on Raspberry Pi 4 can... | by Leigh Johnson | Towards Data Science

Originally published at bitsy.ai/3-ways-to-install-tensorflow-on-raspberry-pi.
With the new Raspberry Pi 400 shipping worldwide, you might be wondering: can this little powerhouse board be used for Machine Learning?
The answer is, yes! TensorFlow Lite on Raspberry Pi 4 can achieve performance comparable to NVIDIA’s Jetson Nano at a fraction of the dollar and power cost. You can achieve real-time performance with state-of-the-art neural network architectures like MobileNetV2 by adding a Coral Edge TPU USB Accelerator.
This performance boost unlocks interesting offline TensorFlow applications, like detecting and tracking a moving object.
Cool! So you’ve decided to build a TensorFlow application for your Raspberry Pi. The first thing you might try is...
$ pip install --no-cache-dir tensorflowLooking in indexes: https://pypi.org/simple, https://www.piwheels.org/simpleCollecting tensorflow  Downloading https://www.piwheels.org/simple/tensorflow/tensorflow-1.14.0-cp37-none-linux_armv7l.whl
Oh no: the version of TensorFlow installed is 1.14. If you try to specify a higher version, you’ll see an error like:
$ pip install tensorflow==2.3.1Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simpleCollecting tensorflow==2.3.1  Could not find a version that satisfies the requirement tensorflow==2.3.1 (from versions: 0.11.0, 1.12.0, 1.13.1, 1.14.0)No matching distribution found for tensorflow==2.3.1
Yes! You’ll have to do a bit of extra work, but here are 3 different ways to use TensorFlow 2.x in your next Raspberry Pi project.
The TensorFlow team builds and tests binaries for a variety of platform and Python interpreter combination, listed at tensorflow.org/install/pip#package-location. Unfortunately, the Raspberry Pi wheels drift a bit behind the latest releases (2.3.1 and 2.4.0-rc2 at the time of this writing). Binaries for aarch64 are not officially supported yet.
Install tensorflow==2.3.0 with pip (requires Python 3.5, Raspberry Pi 3)
$ pip install https://storage.googleapis.com/tensorflow/raspberrypi/tensorflow-2.3.0-cp35-none-linux_armv7l.whl
I maintain TensorFlow binaries that I build from source at github.com/bitsy-ai/tensorflow-arm-bin. Where possible, I recommend using the official wheels because they’re more thoroughly tested.
Install tensorflow==2.4.0-rc2 with pip (requires Python 3.7, Raspberry Pi 4)
$ pip install https://github.com/bitsy-ai/tensorflow-arm-bin/releases/download/v2.4.0-rc2/tensorflow-2.4.0rc2-cp37-none-linux_armv7l.whl
Install tensorflow==2.4.0-rc2 with pip (requires Python 3.7, Raspberry Pi 4, 64-bit OS)
$ pip install https://github.com/bitsy-ai/tensorflow-arm-bin/releases/download/v2.4.0-rc2/tensorflow-2.4.0rc2-cp37-none-linux_aarch64.whl
Packaging a code-base is a great way to learn more about it (especially when things do not go as planned). I highly recommend this option! Building TensorFlow has taught me more about the framework’s complex internals than any other ML exercise.
The TensorFlow team recommends cross-compiling a Python wheel (a type of binary Python package) for Raspberry Pi [1]. For example, you can build a TensorFlow wheel for a 32-bit or 64-bit ARM processor on a computer running an x86 CPU instruction set.
Before you get started, install the following prerequisites on your build machine:
Dockerbazelisk (bazel version manager, like nvm for Node.js or rvm for Ruby)
Docker
bazelisk (bazel version manager, like nvm for Node.js or rvm for Ruby)
Next, pull down TensorFlow’s source code from git.
$ git clone https://github.com/tensorflow/tensorflow.git$ cd tensorflow
Check out the branch you want to build using git, then run the following to build a wheel for a Raspberry Pi 4 running a 32-bit OS and Python3.7:
$ git checkout v2.4.0-rc2$ tensorflow/tools/ci_build/ci_build.sh PI-PYTHON37 \    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh
For 64-bit support, add AARCH64 as an argument to the build_raspberry_pi.sh script.
$ tensorflow/tools/ci_build/ci_build.sh PI-PYTHON37 \    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh AARCH64
The official documentation can be found at tensorflow.org/install/source_rpi.
Grab a snack and water while you wait for the build to finish! On my Threadripper 3990X (64 cores, 128 threads), compilation takes roughly 20 minutes.
What if you want to distribute your TensorFlow application for other Raspberry Pi fans?
Here’s just one way specify which TensorFlow binary your package needs in setup.py.
I’m excited to hear about your next TensorFlow applications for Raspberry Pi! Drop your project link or tell us about your idea for offline Machine Learning in the comments below.
If you’re looking to start a computer vision project, check my example applications in Real-time Object Tracking with TensorFlow. A few folks have used these examples to jump-start their own research and applied ML projects.
@explorerkas submission to @Conservationx detects and tracks leopards in the field.
@niteshverma21i1 measures fish length and biomass for aquaculture automation.
Looking for more hands-on examples of Machine Learning for Raspberry Pi and other small device? Sign up for my newsletter!
I publish new examples of real-world ML applications (with full source code) and other nifty tricks like automating away the pains of bounding-box annotation.