Layman installation guide for Keras and Tensorflow on RPi 3
Introduction
I have tried to put Keras and Tensorflow inside my Raspberry Pi 3 over the weekend. After a frustrating process of installing both software, a pre-trained model for MNIST is put inside and use the pi-camera for photo taking, Then the image is evaluated by the model and result is predicted.
In this article I will share the easiest method I found to get everything running, I am using as much pre-build binary as possible. Therefore the software stack may not be the latest, but it takes least time to install.
Hardware
- Raspberry Pi 3 Model B
- Raspberry Pi Camera Module v2
Installation
Getting Python 3.4
An pre-build binary of tensorflow for python3.4 is available online, therefore, the first thing to get is Python 3.4. However, the lastest Raspbian is shipped with Python 3.5. Now of course you can download and build python 3.4 on raspberry pi, however the building process takes a long time.
An alternative to get is to use an order version of the OS, which ship with the desired python version. Raspbian Jessie is a good choices : it comes with python3.4 and it is not too outdated. Here is the link to the OS version I used :
To install the OS, download the zip folder,unzip it and copy every folders and file onto a formatted micro SD card, insert the micro SD card to the RPi, turn on the machine and just wait for installation.
Tensorflow
Tensorflow is a machine learning library developed by Google. An unofficial binaries for raspberry pi made available by samjabrahams , all you have to do is to get the .whl file and install it by pip3 , type these commands in the terminal:
wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/releases/download/v1.1.0/tensorflow-1.1.0-cp34-cp34m-linux_armv7l.whlsudo pip3 install tensorflow-1.1.0-cp34-cp34m-linux_armv7l.whlsudo pip3 uninstall mock
sudo pip3 install mock
its takes around 15 mins for my raspberry pi to finish the installation, so go on… make some tea!
Keras
I installed Keras according from this blog post by nikhilraghava, in the terminal :
sudo apt-get install libblas-dev
sudo apt-get install liblapack-dev
sudo apt-get install python3-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install gfortran
sudo apt-get install python3-setuptools
sudo apt-get install python3-scipy
sudo apt-get update
sudo apt-get install python3-h5pysudo pip3 install keras
sudo apt-get install python3-skimage
Test Run
Training
In order to test the software stack, I have trained a model with the MNIST data( basically a series of image of hand written digits with label ) with a more powerful machine(my Desktop ), as it takes a lot more computer power to train the model than to use it. You may refer the training code from the post :
https://elitedatascience.com/keras-tutorial-deep-learning-in-python
Export the model
To save a trained model, use the model.save() function in keras, this is the python code to save the trained model :
from keras.models import load_modelmodel.save('mnist_trained_model.h5') # creates a HDF5 file
To use the model in your RPi, copy your HDF5 file saved to a thumb drive and transfer it to the Raspberry Pi.
Raspberry Pi digit recognizer
We will make use of the picamera to take a picture of a hand written digit, and then use the pre-trained model to decide what it is :
from picamera import PiCamera
from time import sleepcamera = PiCamera(resolution=(1792,1792)) photocamera.start_preview()
sleep(10)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()from skimage.io import imread
import numpy as np
im = imread("image.jpg")from skimage.transform import resize# resize to 28 x 28
im_resize = resize(im,(28,28), mode='constant')from skimage.color import rgb2gray# turn the image from color to gray
im_gray = rgb2gray(imresize)# the color of the original set are inverted,so we invert it hereim_gray_invert = 255 - im_gray*255#treat color under threshold as black
im_gray_invert[im8_gray_invert<=90] = 0from keras.models import load_model
model=load_model('mnist_trained_model.h5')
im_final = im_gray_invert.reshape(1,28,28,1)# the below output is a array of possibility of respective digit
ans = model.predict(im_final)
print(ans)# choose the digit with greatest possibility as predicted dight
ans = ans[0].tolist().index(max(ans[0].tolist()))
print(the predicted digit is,ans)
For my raspberry pi, ignoring the time used to take photo, the above code used around 19 seconds to import those library as well as loading the pre-trained model, it takes shorter than 1 seconds to process the image and identify what digit it is.
Conclusion
As we can see the recognizer successfully identify the hand written digit as 8. Although it is a rather simple and dull recognizer, what we did here is the first step to many great application such as an automatically controlled RPi Car, or facial recognition done by RPi.