The MagPi - July 2018

(Steven Felgate) #1

Tutorial


raspberrypi.org/magpi July 2018 59


USE TENSORFLOW ON RASPBERRY PI


SIGN UP FOR
THIS UDEMY
COURSE

If you want
to learn
TensorFlow in
depth, or are
finding the
‘TensorFlow
without a
PhD’ course
limiting, then
sign up for
a course.
There are lots
of different
courses out
there, and
we’ve tried a
lot of them.
Jose Portilla
is the Head of
Data Science
at Pierian
Data and his
Udemy course
‘Complete
Guide to
TensorFlow’
(magpi.cc/
VJUtKJ) is
the best
TensorFlow
course we
have found.

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

hello_tensorflow.py


Language
>PYTHON

DOWNLOAD:
magpi.cc/HuGfDm


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:



sudo apt-get update && sudo apt-get upgrade
sudo apt-get install python3-pip python3-dev
pip3 install tensorflow


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



sudo pip3 install --upgrade https://
storage.googleapis.com/tensorflow/linux/cpu/
tensorflow-1.8.0-cp34-cp34m-linux_x86_64.whl


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:


sudo pip3 install --upgrade tensorflow-
1.9.0rc0-cp34-none-linux_armv7l.whl


Take a look at TensorFlow’s Install Sources page
(magpi.cc/yIpbCX) or Common Installation Problems
page (magpi.cc/EHocYB).



STEP-03
Build from source
If pip fails, you could always build TensorFlow from
source; Sam Abrahams has written detailed instructions
on doing so (magpi.cc/oCYtme). 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:

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

Follow the instructions from on the page at
magpi.cc/BrsbKi to build the example models.
Now head to the part of the TensorFlow repository
at magpi.cc/sGOzbr 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: magpi.cc/xGurWt.

>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’ (magpi.cc/DFFAYt) or Alasdair Allan’s
‘Magic mirror with TensorFlow’ (magpi.cc/YGtrOB).

Simple variables, like strings, are stored inside objects known as
‘Tensors’. These are only accessed via a session, which makes
even a simple Hello World program more complex than usual
Free download pdf