3 Ways to Install TensorFlow 2 on Raspberry Pi

Leigh Johnson
Leigh Johnson
3 Ways to Install TensorFlow 2 on Raspberry Pi
Table of Contents
Table of Contents

TensorFlow Lite on Raspberry Pi 4 can achieve performance comparable to NVIDIA's Jetson Nano at a fraction of the cost.

With the new Raspberry Pi 400 (image credit: raspberrypi.org) shipping worldwide, you might be wondering: can this little powerhouse board be used for Machine Learning?

The answer is, yes! TensorFlow Lite models running on Raspberry Pi 4 boards can achieve performance comparable to NVIDIA's Jetson Nano board. If you add a The Coral Edge TPU USB Accelerator, you can achieve real-time performance with state-of-the-art neural network architectures like MobileNetV2.

This performance boost unlocks interesting offline TensorFlow applications, like detecting and tracking a moving object. Offline inference is done entirely on the Raspberry Pi.

Benchmarking results in ms
Image Credit: Benchmarking TensorFlow Lite on the New Raspberry Pi 4, Model B by Alasdair Allan

Installation is Half the Battle 😠

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 tensorflow
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting 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.1
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting 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

Can I use TensorFlow 2? 😱

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.

Official TensorFlow Wheels

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.

Official TensorFlow wheels for Raspberry Pi

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

Community-built Wheels

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

Build from Source

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.

Image Credit: MLIR: A new intermediate representation and compiler framework

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:

  1. Docker
  2. 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.

Depend on TensorFlow 2.x in a Python Package🐍

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.

# setup.py
import os
from setuptools import setup,

arch = os.uname().machine

if arch == 'armv7l':
	tensorflow = 'tensorflow @ https://github.com/bitsy-ai/tensorflow-arm-bin/releases/download/v2.4.0-rc2/tensorflow-2.4.0rc2-cp37-none-linux_armv7l.whl'
	
elif arch == 'aarch64':
	tensorflow = 'https://github.com/bitsy-ai/tensorflow-arm-bin/releases/download/v2.4.0-rc2/tensorflow-2.4.0rc2-cp37-none-linux_aarch64.whl'	

elif arch == 'x86_64':
	tensorflow = "tensorflow==2.4.0rc2"
else:
	raise Exception(f'Could not find TensorFlow binary for target {arch}. Please open a Github issue.')
    
 requirements = [
 	tensorflow,
    # specify additional package requirements here
 ]
 
 setup(
 	install_requires=requirements,
    # specify additional setup parameters here
 )
 
# setup.py
import os
from setuptools import setup
arch = os.uname().machine
if arch == 'armv7l':
tensorflow = 'tensorflow @ https://github.com/bitsy-ai/tensorflow-arm-bin/releases/download/v2.4.0-rc2/tensorflow-2.4.0rc2-cp37-none-linux_armv7l.whl'
elif arch == 'aarch64':
tensorflow = 'https://github.com/bitsy-ai/tensorflow-arm-bin/releases/download/v2.4.0-rc2/tensorflow-2.4.0rc2-cp37-none-linux_aarch64.whl'
elif arch == 'x86_64':
tensorflow = "tensorflow==2.4.0rc2"
else:
raise Exception(f'Could not find TensorFlow binary for target {arch}. Please open a Github issue.')
requirements = [
tensorflow,
# specify additional package requirements here
]
setup(
install_requires=requirements,
# specify additional setup parameters here
)

Your adventure is just beginning ✨

I'm excited to hear about your new TensorFlow applications for Raspberry Pi! Drop your project link or tell us about your idea for offline Machine Learning at edge 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.

@martin2kid prototype detects face masks and tracks temperature with a thermal array.
@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.



Join the conversation.

Great! Check your inbox and click the link
Great! Next, complete checkout for full access to Bitsy.ai
Welcome back! You've successfully signed in
You've successfully subscribed to Bitsy.ai
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated