Learn how to setup and use the open-source machine learning software, TensorFlow, on Raspberry Pi.
In this article, you are going to learn about how to install TensorFlow on Raspberry Pi.
Originally developed by the Google Brain team to conduct machine learning and deep neural networks research, TensorFlow is general enough to be applicable in a wide variety of other domains.
TensorFlow is an open-source machine learning software library for numerical computation using data flow graphs. The graph nodes represent mathematical operations, while the graph edges represent the multi-dimensional data arrays (tensors) that flow between them. This flexible architecture gives you the ability to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device without rewriting code.
Installing TensorFlow on Your Raspberry Pi
Installing TensorFlow on Raspberry Pi used to be a frustrating task. However, with the newer versions of Google TensorFlow officially supported on Raspberry Pi, you just need a couple of commands to get it installed.
First, make sure that your Raspberry Pi is up to date by typing the following commands. These commands update the installed packages on your Raspberry Pi to the latest versions.
sudo apt-get update
sudo apt-get upgrade
After that, we are going to install and create a virtual environment in which we will install Tensorflow.
python3 -m pip install virtualenv
python3 -m venv tf2.0
Then we are going to install dependencies required for installing TensorFlow on Raspberry Pi.
sudo apt install -y libatlas-base-dev libhdf5-dev
Now we are going to get the latest release of TensorFlow that is compatible with Raspberry Pi and install it.
python3 -m pip install tensorflow-2.0.0-cp37-none-linux_armv7l.whl
Let’s double-check the installation. To check whether or not TensorFlow installed, try importing TensorFlow by typing
It might raise an error if you are using a Python version greater than 3.4. Just ignore this error — everything will work fine.
To check what version of TensorFlow you have, type following command:
Hello World Example
Let’s write a simple code provided by Google for testing TensorFlow that will print hello world.
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
You should see “Hello, TensorFlow” printed.
If you are running Python 3.5, you will get several runtime warnings. The official TensorFlow tutorials acknowledge that this happens, and recommend you ignore it.
Installing the Image Classifier
First, create a new directory which saves the TensorFlow models.
Now, clone the TensorFlow models repository in this new directory.
git clone https://github.com/tensorflow/models.git
We are going to use the image classification example that comes with the models, so navigate to that folder:
Now run the script. It will feed the standard image of “panda” to the neural network which in return guesses what this image contains with a score.
Let’s give our own image to the neural network and see whether it can identify objects in the image or not.
I placed an image of a dog into the same folder we are already working in. Now I’ll run the script to see what it comes up with as a guess.
python3 classify_image.py –image_file=dog.jpg
It comes up with the following guesses:
As you can see, it recognized that the highest probability of this image was that of a pug.