Raspberry pi - machine learning

Intro

Let’s explore Machine Learning using a Raspberry Pi, the topics are a bit advanced and will require you to have some extra hardware and setup, but you can simply browse the code and try on a laptop/computer with Python as well.

  • Raspberry Pi
  • Raspberry Pi/compatible camera
  • Laptop/computer with Python installed

Books

There is a really nice book that I would recommend: “Beginning Artificial intelligence with the Raspberry Pi” (Donald J Norris).
https://link.springer.com/book/10.1007/978-1-4842-2743-5

The code from that book is included and available in GIT
In this article I will cover a basic setup using a Raspberry Pi, a camera, Python and Neural Networks to identify handwritten numbers – is not that complicated and it would make a great activity for a rainy day.
I will completely skip all the Machine Learning theory meanwhile so you may enjoy the fun part of it.

Raspberry Pi Setup

As I started from zero, I used a Raspberry Pi that apparently had Windows 10/Core, so I had to install Raspbian Stretch Lite following the setup instructions from here:

https://www.raspberrypi.com/software/

Later on I realized I was going to need some extra libraries so I installed a basic GUI:

1
2
3
4
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install xinit
sudo apt-get install lxde-core lxterminal lxappearance
sudo apt-get install lightdm

Local Setup

There are several things to install in your laptop/computer, as a side note I actually tried to setup a similar environment in Mac OS X but the versions of Python/NumPy/etc are old or incompatible.
It doesn’t work coding on a Mac then deploying/running on the device so I ended up using my Windows 10 laptop.

The basic things to install on the Raspberry Pi:

1
2
3
4
sudo apt-get install git (in case is not there)
sudo apt-get install python-numpy
sudo apt-get install python-matplotlib
sudo apt-get install python-camera

On the Windows side I needed to connect to the Raspberry Pi so I used MobaXTerm, is a great piece of software, supports SSH and X server, so instead of using the user interface connected to a monitor, USB keyboards etc.
We can SSH and run everything using command line, output/charts are displayed by Windows/X server – they provide a free version and paid version is not that expensive.
https://mobaxterm.mobatek.net/

Data and Code Setup

For Machine Learning everyone starts using the MNIST data set, recommended start is to use a reduced version of the data set for testing/debugging
https://github.com/makeyourownneuralnetwork/makeyourownneuralnetwork/tree/master/mnist_dataset

Grab a copy of the data-set from Make Your Own Neural Network GIT
https://pjreddie.com/media/files/mnist_test.csv
https://pjreddie.com/media/files/mnist_train.csv

Grab the code from chapters 8 and 9 of “Beginning Artificial intelligence with the Raspberry Pi”
The code from chapter 9 requires the code/library from chapter 8, but everything is there to start playing
https://github.com/Apress/beg-artificial-intelligence-w-raspberry-pi

Mad Scientist Setup

Fun part is to connect everything, then try to run the scripts and see if things are working.
My setup looks like this, using an Infra-red camera is not a great idea to start but is not a big deal, it works fine … but only if the objects are at different temperature :)

setup

Once everything is wired up and installed a basic check is start getting images, below a sample script:

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import picamera
camera = picamera.PiCamera()
camera.color_effects = (128, 128)
camera.capture('sample.jpg')
img = mpimg.imread('sample.jpg')
imgplot = plt.imshow(img)
plt.show()

This is one of the first images I got, I realized the camera was upside down?

infrared photo

We can also see what the machine is doing, internally:

machine learning view

You may spend time running the trainANN scripts, I ran couple iterations, checked some results and skipped to the automatedImager.

So far things are starting to work so time to review and to enjoy the rest of the evening.

Recap

This setup can capture an image, convert to a small sample, use an Artificial Neural Network and depending on training, identify the actual number – to be fair that is quite amazing for such a simple setup.
Of course there are specialized software that can do it quicker and better but the main purpose of this setup is to make sure things are more or less working fine and that is easy to add Machine Learning to a small device and experiment with it

I guess a major blocker for people to start experimenting with Machine Learning is all the theory and setup required.
In terms of theory there are many good books out there, like “Python Machine Learning” (Sebastian Raschka) and also great online resources like “Machine Learning” (Andrew Ng/Stanford University/Coursera).
https://www.packtpub.com/big-data-and-business-intelligence/python-machine-learning
https://www.coursera.org/learn/machine-learning

I would recommend to enroll in Coursera and go watching the videos and completing the course if you want to know more, the content of the books is more or less the same and in my humble opinion books sometimes add unnecessary chapters just to explain the same old theory. In a broad sense everything is about training logic components so they later can recognize items using patterns and maths (excuse me if I oversimplified it)

Regarding setup, we can se that a simple Raspberry Pi can do the trick, if you don’t have one available still you can use a laptop/computer, take photos and process the images with the Python scripts given.

By now I hope you may have a better appreciation of the things that can be done with simple hardware and software tools and how Machine Learning/Artificial Intelligence works.