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
    

With your Raspberry Pi up to date, install Google TensorFlow by typing the following commands in the terminal:

        sudo apt install libatlas-base-dev
pip3 install tensorflow
    

Testing TensorFlow

Let’s double-check the installation. To check whether or not TensorFlow installed, try importing TensorFlow by typing

        Python3
    

And then

        import tensorflow
    

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:

        Tensorflow.__version__
    
Typing the Command Tensorflow.__version__

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()
print(sess.run(hello))
    

You should see “Hello, TensorFlow” printed.

Testing TensorFlow to print Hello World

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.

        mkdir tensorflow
cd tensorflow
    

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:

        cd models/tutorials/image/imagenet
    

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.

        python3 classify_image.py
    
Python3 Classify_image.py

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.

Image of a dog
        python3 classify_image.py –image_file=dog.jpg
    

It comes up with the following guesses:

How_to_Setup_Tensorflow_RPi_MP_image5.png

As you can see, it recognized that the highest probability of this image was that of a pug. 

Reginald Watson
I love challenging myself by creating new projects using different microcontrollers to see what I can come up with.