TensorFlow is Google’s neural network library. Considering that machine learning is the hottest thing right now, it’s no surprise that Google is one of the leaders in this new technology.

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


To get started with image recognition, you need a Raspberry Pi (any model will do) and an SD card running Raspbian Stretch (9.0+) (if you’re new to Raspberry Pi, check out our setup 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 

For this tutorial, you can use both Python 2.7 and Python 3.4+. This example is for Python 3. For Python 2.7, replace Python3 on the Python a pip3 on the pip in this tutorial.

Pip is a package manager for Python, usually installed as standard on Linux distributions.

If you find that you don’t have it, follow the installation instructions for Linux in this article to install it.

Installing TensorFlow

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

Install the library before installing TensorFlow Atlas .

 sudo apt install libatlas-base-dev 

After that, install TensorFlow via pip3.

 pip3 install --user tensorflow 

This will install TensorFlow for the logged in user. If you prefer to use a virtual environment use a virtual environment change your code here to reflect this.

Testing TensorFlow

Once installed, you can check if it works with the TensorFlow equivalent Hello world!

From the command line, create a new Python script using nano or vim (if you’re not sure which one to use, they both have benefits) and name it easy to remember.

 sudo nano tftest.py 

Enter this code provided by Google for testing TensorFlow:

 import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello)) 

If you are using nano, exit by pressing ctrl+X, and save the file by typing Y in response to a request.

Run the code from terminal:

 python3 tftest.py 

You should see «Hello, TensorFlow» printed.

If you are using Python 3.5, you will receive several runtime warnings. The official TensorFlow guides acknowledge that this is happening and recommend ignoring it.

TensorFlow and Python3.5 - error

It is working! Now to do something fun with TensorFlow.

Installing an image classifier

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

 mkdir tf1 cd tf1 

TensorFlow has a git repository with sample models that you can try out. Clone the repository to a new directory:

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

You want to use the image classification example found in models/tutorials/image/imagenet . Navigate to this folder now:

 cd models/tutorials/image/imagenet 

The standard image classification script works with the provided panda image:

Tiny TensorFlow Panda

To run the standard image classifier with the provided panda image, type:

 python3 classify_image.py 

This feeds the panda image to the neural network, which returns guesses as to what the image matters for its confidence level.

TensorFlow Panda Classifying Inference

As the output image shows, the neural network was guessed correctly, with almost 90 percent certainty. He also thought that the image might contain a custard apple, but he is not very sure about this answer.

Using a Custom Image

The panda image proves that TensorFlow works, but that’s perhaps not surprising given that it’s an example the project provides. For a better test, you can provide your neural network image for classification.

In this case, you will see if the TensorFlow neural network can identify George.

George Dinosaur

Meet George. George is a dinosaur. To insert this image (available cropped here) into the neural network, add arguments when running the script.

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

Image_file= after the script name allows you to add any image along the path. Let’s see how this neural network did.

TensorFlow Dinosaur Classification Results

Not bad! Although George is not a Triceratops, the neural network classified the image as a dinosaur with a high degree of confidence compared to other options.

TensorFlow and Raspberry Pi ready to go

This basic implementation of TensorFlow already has potential. This object recognition happens on the Pi and does not require an internet connection to work. This means that with the addition of a Raspberry Pi camera module and a Raspberry Pi compatible battery pack, the whole project can become portable.

Most tutorials only scratch the surface of the subject, but that was never truer than in this case. Machine learning is an incredibly dense subject.

One way to deepen your knowledge is to take a special course. In the meantime, get machine learning and Raspberry Pi experience with these TensorFlow projects you can try for yourself.

Похожие записи