Technology

Raspberry Pi – ML setup

Let’s go from zero to machine learning using the Raspberry Pi

There is a really nice book that I would recommend: “Beginning Artificial intelligence with the Raspberry Pi” (Donald J Norris). 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 also totally skip all the Machine Learning theory leaving as a black box component meanwhile so you may enjoy the making and fun part of it

1. Raspberry Pi Setup

I really started from zero, had a spare Raspberry Pi that apparently had Windows 10/Core, so I started with Raspbian Stretch Lite and followed the setup instructions

Later on I realized I was going to need some extra libraries so I installed a basic GUI, detailed info in this article from Therry van Neerven

  • 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

2. Software Setup

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

The basic things to install on the Raspberry Pi:

  • 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 I just 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

4. 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

The code from chapter 9 requires the code/library from chapter 8, but everything is there to start playing

3. 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

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

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()

And this is one of the first images! I realized the camera was upside down ?

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

4. Conclusion

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). I would recommend to enroll in Coursera and go watching the videos and completing the course, the content of the books is more or less the same and in my opinion books sometimes add unnecessary chapters just to explain the same old theory

Regarding setup, well that’s this article about! So, by now I’m just hoping you may have a better appreciation of the things that can be done with simple hardware and software tools