Today’s society is full of “smart” technologies that makes our lives easier. Without thinking, we encounter them almost every day, from face recognition phone locks to selective social media feeds. These human-machine interactions provide a constant stream of data to the software behind these technologies, which is analyzed in real-time to improve the software. This feedback loop between gathered data and software improvement is the basic idea behind machine learning.

Resources for understanding machine learning have become more available in the past decade thanks to accessible and open-source platforms such as the Raspberry Pi. In this article, we’ll go over the basics of machine learning and how it can be applied to Pi projects!

Back to Contents

Raspberry Pi & Machine Learning: The Basics

What Is Machine Learning?

IBM Watson competed and won against Jeopardy! champions Ken Jennings and Brad Rutter
IBM Watson competed and won against Jeopardy! champions Ken Jennings and Brad Rutter (Source: Jeopardy! via YouTube)

A common misconception is that machine learning is another term for AI. In fact, the terms shouldn’t be used interchangeably. AI refers to a computer program’s ability to mimic human behaviors, whereas machine learning refers to an algorithm that learns from the data it processes. In other words, machine learning is concerned with more effectively carrying out a task over time, whereas AI builds on this principle with the added goal of creating a human-like response.

According the University of California, Berkeley, a machine learning algorithm consists of three parts:

  1. A decision process: A set of calculations that gathers data and attempts to draw a conclusion or predict the type of pattern in the data the algorithm should be searching for.
  2. An error function: A method of measuring the strength of a guess by comparing it to a known ideal example. This function determines the model’s accuracy.
  3. An optimization process: Repeatedly evaluating inaccurate predictions and updating the decision process until a pattern is successfully recognized.

Machine learning is extremely helpful for automated tasks that involve gathering data. For instance, email spam filters are algorithms trained to detect messages that could potentially be scams. Further along the spectrum of complexity, self-driving cars utilize a similar model but to detect objects such as people, other cars, and stop signals. Due to the versatility and scalability of machine learning, it can be used in almost any industry to help make data-driven predictions or solutions that would normally take a long time to manually analyze.

To better understand the above three parts, let’s consider the example of a decision-making program that plays chess. Naturally, the program uses its decision process to make moves based on current and previous states of the board. Then, based on the results – the other player’s reactions to particular moves or the game’s outcome – the program’s error function measures how well its strategy worked and adapts it. Allowing the algorithm to practice over time should then improve its performance.

In principle, machine learning can be applied to many different kinds of applications and systems, big or small. Enter the Pi.

Back to Contents

Advertisement
Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Power to the Pi

The AdventurePi is a DIY portable arcade that runs on a RetroPie
The AdventurePi is a DIY portable arcade that runs on a RetroPie (Source: Zach via Howchoo)

The Raspberry Pi is one of the most popular single-board controllers (SBCs) thanks to its affordability and the abundance of resources available. For just $4 and up, you can have an all-around versatile computer that runs on 5 V. At minimum, all Pi models come with general-purpose input/output (GPIO) ports to expand the hardware capabilities of your project. Here are a few popular beginner projects to give you an idea of what’s possible:

  • Kodi: A free, downloadable media center.
  • RetroPie: A retro video game emulator. 
  • OctoPi: A remote 3D printing management app. 
  • MotionEyeOS: A high quality surveillance camera feed.

Setting up such projects is typically straightforward, as they primarily involve installing a Python package using the Raspberry Pi OS and wiring basic hardware outputs, such as an LCD screen or speaker. As a pocket-sized computer, there are a variety of fantastic Pi projects for all skill levels.

Back to Contents

Advertisement
Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Teaching a Pi to Learn

A Pi running an object recognition algorithm
A Pi running an object recognition algorithm (Source: Adafruit)

While most Raspberry Pi projects collect and output data, the level of complexity increases when applying the data to make decisions. Running a machine learning program on a Pi requires a significant amount of memory (or RAM) to process calculations. Additionally, the speed of the on-board processor plays a role in how effectively the software can be run. Therefore, the latest and preferred model for machine learning applications is the Raspberry Pi 4 Model B.

Typical machine learning projects for the Pi involve classifying items, including different visual, vocal, or statistical patterns. For example, one could train a Pi to recognize coffee mugs.

The installation of a basic machine learning program is no different from installing any other Python package using the Pi OS. However, you’ll need to prepare it for training. To use the same example, this might involve uploading images of coffee mugs to a folder within the software’s directory. This set of images would then be used by the algorithm’s error function and optimization process to help evaluate and improve its decisions.

Back to Contents

Advertisement
Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Open-Source Software

TensorFlow provides an
TensorFlow provides an "ecosystem of tools for developers, enterprises, and researchers" (Source: TensorFlow via YouTube)

The backbone of all machine learning models is a software library and its dependencies. There are currently a variety of free machine learning frameworks, which are continuously improved by an active online community, thanks to the Open Source Initiative (OSI). Some of most well-known platforms include the following:

  • TensorFlow: A flexible platform for building general machine learning models.
  • OpenCV: A library dedicated to computer vision and related object detection tasks.
  • Google Assistant: A library dedicated to voice recognition tasks.
  • Edge Impulse: A cloud-based platform that simplifies machine learning app development

Depending on a project’s use case, one library may be more beneficial than another. Because TensorFlow and Edge Impulse are general machine learning platforms, they’re versatile enough to be used for any project. However, in the realm of object detection, a speciality library like OpenCV may be more useful. And if you wanted to make use of voice-enabling features, Google Assistant is worth looking into.

It’s also important to note which accessories are necessary for a project to work. For computer vision, a Pi will need a camera for real-time detection, and a microphone is necessary for voice recognition.

Back to Contents

Advertisement
Advertisement

Example Machine Learning Projects for the Pi

Using the aforementioned open-source software, the possibilities for a machine learning project are endless! You can even leverage multiple libraries at once to automate different tasks. The projects we’ll discuss have unique real-world applications and provide insight into the future of human-machine interactions.

Back to Contents

Advertisement
Raspberry Pi & Machine Learning: The Basics

Beginner Object Classification: Sock Sorter

Now here's something we can all benefit from
Now here's something we can all benefit from (Source: Balena Blog)

Edge Impulse is a newer platform in the machine learning scene. It was founded in 2019 with the vision of making machine learning accessible to all. Because it’s a cloud-based service, you can develop your app from their website and export to the Raspberry Pi when finished. Without having in-depth knowledge of machine learning, you can start with Edge Impulse to create a basic image classification project, like sorting your socks!

From the Edge Impulse website, you can easily upload images of paired socks as part of your dataset. After this, you can add processing and learning “blocks” that automatically develop the training model and its results. You can then test the final model to see if it recognizes which images of the pairs are matching. Upon completion, you can either export the app to an SD card for the Pi or upload more images and further train the model. 

  • Who designed it: Marc Pous
  • Machine learning library: Edge Impulse
  • Core components: Raspberry Pi 4, Raspberry Pi Camera Module V2, multiple pair of socks
  • Where to find it: Balena Blog

Back to Contents

Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Advanced Object Classification: Cucumber Sorter

Who's hungry?
Who's hungry? (Source: Kazunori Sato via YouTube)

Japanese engineer Makoto Koike came up with a cucumber sorter out of a need to help his parents’ cucumber farming business. To increase sales, only the best cucumbers are taken to the market. The process of manually sorting a batch (done by his mother) normally took eight hours.

Seeing that sorting was a specialized task and that existing automated sorters weren’t viable for a small farm, he decided to build his own. His Cucumber Sorter runs on a Raspberry Pi 3 with a camera that takes photos of cucumbers on a conveyor belt.

The images are first analyzed by a preliminary TensorFlow neural network to verify that the objects are indeed cucumbers. Then, those same images are sent to a more complex neural network to determine whether the vegetables are acceptable. The cucumbers are subsequently sorted into different boxes based on their grades.

  • Who designed it: Makoto Koike
  • Machine learning library: TensorFlow
  • Core components: Raspberry Pi 3, Arduino Micro, motorized conveyor belt, servo motors, webcams
  • Where to find it: Google Cloud Blog

Back to Contents

Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Autonomous Action: Chess Robot

An open-source, chess-playing robot
An open-source, chess-playing robot (Source: Joey Meyer via Tom's Hardware)

The Mechanical Turk was an 18th century chess-playing machine that could beat any human player that challenged it. The inventor, Wolfgang von Kempelen, claimed the Turk was able to play chess entirely on its own. Over 80 years later, it was revealed that human chess masters hid inside the machine to control it during games.

Although the Turk was a fake, its concept paved the way for machine learning. Out of interest in both the story and his field of study, software engineer Joey Meyer built a real chess-playing robot.

Because the robot has to both distinguish its own chess pieces and move them, OpenCV and TensorFlow are used. Overhead images of the board are taken each turn by a camera and processed through OpenCV to create a dataset for assessing the state of the game. The dataset is then forwarded to a TensorFlow model that classifies the different chess pieces and decides the best course of action.

  • Who designed it: Joey Meyer
  • Machine learning libraries: OpenCV and TensorFlow
  • Core components: Raspberry Pi, Raspberry Pi Camera Module, ArbotiX-M Robocontroller, servo motors, electromagnet
  • Where to find it: Raspberry Turk

Back to Contents

Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Facial Recognition: Door Lock

The front and back of the prototype system
The front and back of the prototype system (Source: Maker.io via Digi-Key)

Inspired by the face recognition unlocking feature in smartphones, this project applies a similar methodology to lock and unlock a door, where your face is the “password”. When it comes to detecting a human face, it’s most convenient for an algorithm to look for distinct, localized features such as a pair of eyes, a nose, a forehead, and a mouth.

Thankfully, OpenCV does all the work by implementing the Viola-Jones algorithm, which uses Haar Feature-based Cascade Classifiers. This detection method can recognize a face by contrasting dark and light pixels. For example, your eye region is normally darker than the upper cheeks due to the natural shape of the skull.

The face recognition portion of the code operates like a standard machine learning algorithm, where you need to store an image of your face in the repository. This will then be used as the dataset to “learn” to recognize your face. Although the steps of this project are few and simple, the theory behind the software is a great introduction to computer vision.

  • Who designed it: Maker.io
  • Machine learning library: OpenCV
  • Core components: Raspberry Pi 4, Raspberry Pi Camera Module V2, LED touchscreen, servo motor
  • Where to find it: Digi-Key

Back to Contents

Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Voice Recognition: Desktop Smart Mirror

Why thanks, Mirror!
Why thanks, Mirror! (Source: Zach via Howchoo)

The Smart Mirror or Magic Mirror is a favorite project among makers for its dual function as a mirror and a visual information hub. It also has a sleek, futuristic look that compliments the aesthetic of most rooms. The “magic” behind the mirror is an LCD screen seated behind a two-way mirror. When the screen’s on, it displays graphics. When off, the mirror is fully reflective.

In addition to showing you the weather forecast or the latest news, the Magic Mirror can become voice-activated thanks to Google Assistant. This version of the project uses pre-built hardware, so the build is fairly quick. Once you download the software to your Pi, it will not only learn to recognize your voice, but also become an extension of your home automation ecosystem.

  • Who designed it: Zach
  • Machine learning library: Google Assistant API
  • Core components: Raspberry Pi 3, Google AIY Voice Kit V2, Pi-topCEED, acrylic two-way mirror, Neodium magnets
  • Where to find it: Howchoo

Back to Contents

Advertisement
Advertisement
Raspberry Pi & Machine Learning: The Basics

Further Reading

Take advantage of free courses, which stand to benefit you and your Pi!
Take advantage of free courses, which stand to benefit you and your Pi! (Source: FutureLearn)

There’s an abundance of online resources to learn more about applying machine learning to the Raspberry Pi, it’s just a matter of where to start. If starting from scratch, reading official guides from developers is a good way to keep up with the latest information.

For the fundamentals, the Raspberry Pi Foundation offers a free introduction to machine learning and AI course. After you’ve learned the basic theory, we suggest taking a free, two-part course called “Teach Your Raspberry Pi“, which shows you how to run a TensorFlow algorithm on the Pi. This course is provided by Arm, the manufacturer of the Pi’s processor chips.

For most technology-based projects, the learning curve tends to be steep for beginners. However, the beauty of learning with open-source data is that all documentation is free and actively maintained by the community. This mutual trust between developers and users creates an extensive network of helpers ready to support you. With enough time, motivation, and resources, your future project has the potential to innovate the field!

Back to Contents

Advertisement
Advertisement

License: The text of "Raspberry Pi & Machine Learning: All You Need to Know" by All3DP is licensed under a Creative Commons Attribution 4.0 International License.

Stay informed with notifications from All3DP.

You get a notification when a new article is published.

You can’t subscribe to updates from All3DP. Learn more… Subscribe to updates

You can’t subscribe to updates from All3DP. Learn more…

Advertisement