TensorFlow is Google’s Neural Network library. Given that machine learning is the hottest thing around currently, it is no surprise that Google are among the leaders in this new technology.

In this article, you will learn how to install TensorFlow on the Raspberry Pi, and run simple image classification on a pre-trained neural network.

Getting Started

To get started with image recognition, you will need a Raspberry Pi (any model will work) and an SD card with the Raspbian Stretch (9.0+) operating system (if you’re new to the Raspberry Pi, use our installation guide). Boot up the Pi and open a terminal window. Make sure your Pi is up to date, and check your Python version.

sudo apt-get update
python --version
python3 --version

You can use both Python 2.7 or Python 3.4+ for this tutorial. This example is for Python 3. For Python 2.7, replace Python3 with Python, and pip3 with pip throughout this tutorial.

Installing TensorFlow

Installing TensorFlow used to be quite a frustrating process, but a recent update makes it incredibly simple. While you can follow this tutorial without any prior knowledge, it might be worth understanding the basics of machine learning before trying it out.

If you are using nano, exit by pressing Ctrl + X and save your file by typing Y when prompted.

Run the code from the terminal:

python3 tftest.py

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 to ignore it.

It works! Now to do something interesting with TensorFlow.

Installing the Image Classifier

In the terminal, create a directory for the project in your home directory and navigate into it.

mkdir tf1
cd tf1

TensorFlow has a git repository with example models to try out. Clone the repository into the new directory:

git clone https://github.com/tensorflow/models.git

You want to use the image classification example, which can be found at models/tutorials/image/imagenet. Navigate to that folder now:

cd models/tutorials/image/imagenet

The standard image classifying script runs with a provided image of a panda:

To run the standard image classifier with the panda image provided enter:

python3 classify_image.py

This feeds an image of a panda to the neural network, which returns guesses as to what the image is with a value for its level of certainty.

As the output image shows, the neural net guessed correctly, with an almost 90 percent certainty. It also thought the image might contain an custard apple, but it was not very confident with that answer.

Using a Custom Image

The panda image proves that TensorFlow works, but that is perhaps unsurprising given it is the example the project provides. For a better test, you can give your own image to the neural net for classification. In this instance, you’ll be seeing if the TensorFlow neural net can identify George.

Meet George. George is a dinosaur. To feed this image (available in cropped form here) into the neural net, add arguments when running the script.

python3 classify_image.py --image_file=/home/pi/george.jpg

The image_file= following the script name allows the addition of any image by path. Lets see how this neural net did.

Not bad! While George is not a triceratops, the neural net classified the image as a dinosaur with a high degree of certainty when compared to the other options.

TensorFlow and Raspberry Pi, Ready to Go

This basic implementation of TensorFlow already has potential. This object recognition is happening on the Pi, and needs no internet connection to function. This means that with the addition of a Raspberry Pi camera module and a Raspberry Pi-suitable battery unit, the whole project could go portable.

Ian Buckley started out with a degree in Music composition, before devoting his time to DIY tech and coding. He now works as a freelance journalist, performer and video producer living in Berlin, Germany. When he's not writing or on stage, he's tinkering with DIY electronics or code in the hope of becoming a mad scientist.