2 weeks ago

Use TensorFlow AI on Raspberry Pi

Discover how to install and use Google’s TensorFlow framework to learn AI techniques and add AI to your future projects

Google TensorFlow is a powerful open-source software framework used to power AI projects around the globe.

TensorFlow is used for machine learning and the creation of neural networks. These make it possible for computers to perform increasingly complex tasks, such as image recognition and text analysis.

When it comes to AI, most people think of powerful supercomputers crunching billions of numbers in giant databanks. But there are two parts to machine learning. There is a train/test part, where you use a lot of data to build a model. And there’s deployment, where you take a model and use it as part of a project. And that’s where the Raspberry Pi fits in.

Although Raspberry Pi isn’t officially supported by Google, there are example models included for the Raspberry Pi and it can be fun (if a bit hacky) to get TensorFlow up and running on a Pi. And there are lots of interesting community projects around that put TensorFlow to good use.

Using TensorFlow can give you a good understanding of how AI works, and how to put AI to practical use in your projects.

STEP-01 Install TensorFlow with pip

TensorFlow can be incredibly easy to install on a Raspberry Pi, or something of a nightmare. It depends on the current build and which version of Raspbian OS you are running. Installation is often troublesome, but we’ve had recent success with building it directly using pip. Open a Terminal window and enter:

STEP-02 Build from wheel

If pip doesn’t work, you can try to build TensorFlow using the wheel file. In a Terminal, enter:

Alternatively, you can use a nightly wheel built for Raspberry Pi, which is available from magpi.cc/xKLBzu. Download the wheel file and run it, like this:

Take a look at TensorFlow’s Install Sources page or Common Installation Problems page.

STEP-03 Build TensorFlow from source

If pip fails, you could always build TensorFlow from source; Sam Abrahams has written detailed instructions on building TensorFlow from source. You will need a spare USB stick (1GB or higher) to extend the amount of swap space on your Raspberry Pi and be sure to follow the instructions carefully. It takes around six hours to build, but we have gone through the steps and they do work.

STEP-04 Hello TensorFlow

Hopefully, you now have TensorFlow up and running. So let’s start it up. Open Python 3 (IDLE) using Menu > Programming > Python 3 (IDLE). Choose File > New File and enter the hello_tensorflow.py code:

Save the code file as hello_tensorflow.py and Choose Run > Run Module. You will get a warning because TensorFlow is compiled for Python 3.4 and we’re running Python 3.5. Don’t worry, the code works. The Python shell will display:

‘Hello TensorFlow’

STEP-05 Pi examples

Google has a bunch of models developed for Raspberry Pi that you can test out. Start by cloning the TensorFlow repository:

Follow the instructions here  to build the example models.

Now head to the part of the TensorFlow repository  to find Google example models and instructions.

The default example is a picture of Grace Hopper. Run it and you will see that it identifies a ‘military uniform’, ‘suit’, and ‘academic gown’ (and then other items in order of decreasing probability). From here you can see how this model could be used to identify objects in your own images, and use that in your own code. There is also a link to an example that uses the Pi Camera Module directly.

STEP-06 Community TensorFlow

Now you have everything you need to start using TensorFlow. It’s a big subject and there’s far more to it than we could outline in this tutorial (or even this entire magazine). Learn by doing and follow some TensorFlow projects. Start with Sarthak Jain’s ‘How to easily detect objects with deep learning on Raspberry Pi’ or Alasdair Allan’s ‘Magic mirror with TensorFlow’.