Reader small image

You're reading from  TinyML Cookbook - Second Edition

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781837637362
Edition2nd Edition
Right arrow
Author (1)
Gian Marco Iodice
Gian Marco Iodice
author image
Gian Marco Iodice

Gian Marco Iodice is team and tech lead in the Machine Learning Group at Arm, who co-created the Arm Compute Library in 2017. The Arm Compute Library is currently the most performant library for ML on Arm, and it's deployed on billions of devices worldwide – from servers to smartphones. Gian Marco holds an MSc degree, with honors, in electronic engineering from the University of Pisa (Italy) and has several years of experience developing ML and computer vision algorithms on edge devices. Now, he's leading the ML performance optimization on Arm Mali GPUs. In 2020, Gian Marco cofounded the TinyML UK meetup group to encourage knowledge-sharing, educate, and inspire the next generation of ML developers on tiny and power-efficient devices.
Read more about Gian Marco Iodice

Right arrow

Enabling Compelling tinyML Solutions with On-Device Learning and scikit-learn on the Arduino Nano and Raspberry Pi Pico

We are now ready for our final chapter of this practical learning journey into tinyML.

If you have made it this far, I bet you have a myriad of questions in mind to help you start or continue building compelling applications with machine learning (ML) on microcontrollers. Therefore, this chapter has a different format, seeking to answer three questions you might be pondering.

The first question will delve into the feasibility of training models directly on microcontrollers. How can we have on-device learning on microcontrollers? And what groundbreaking applications can this unlock? In this part, we will discuss the backpropagation algorithm to train a shallow neural network. We will also show how to use the CMSIS-DSP library to accelerate its implementation on any microcontroller with an Arm Cortex-M CPU.

After discussing on-device learning, we will tackle...

Technical requirements

To complete all the practical recipes of this chapter, we will need the following:

  • An Arduino Nano 33 BLE Sense
  • A Raspberry Pi Pico
  • A SparkFun RedBoard Artemis Nano (optional)
  • A micro-USB data cable
  • A USB-C data cable (optional)
  • 1 x half-size solderless breadboard
  • 2 x jumper wires
  • 1 x 3 AA battery holder (Raspberry Pi Pico only)
  • 1 x 4 AA battery holder (Arduino Nano only)
  • 4 x AA batteries
  • Laptop/PC with either Linux, macOS, or Windows
  • Google Drive account

    The source code and additional material are available in the Chapter12 folder in the GitHub repository: https://github.com/PacktPublishing/TinyML-Cookbook_2E/tree/main/Chapter12

How can we train a model on microcontrollers?

In every project presented in this book, we have discussed how to run model inference on microcontrollers and demonstrated that even a model like MobileNet v2 can be deployed on these devices. However, is it possible to train a neural network on microcontrollers?

In this recipe, we will answer this question and provide an example of training a simple neural network using backpropagation on the Arduino Nano and Raspberry Pi Pico with the CMSIS-DSP library.

The network will be trained to return the result of the following logical (exclusive OR) XOR and NOT-AND (NAND) operators:

Figure 12.1: The logical XOR and NAND operators

As you can see from the preceding image, the result of the XOR operator is 1 when the binary inputs a and b are different. On the other hand, the output of the NAND operator is 1 when at least one of the binary inputs, a or b, is 0.

Getting ready

Training an ML model using the microcontroller...

How can we deploy scikit-learn models on microcontrollers?

Artificial neural networks are incredibly accurate and versatile to solve a wide range of data analysis problems. However, this model is not the only player in the ML arena. Indeed, many other ML models are available that can be just as effective for specific tasks and be less compute- and memory-demanding.

In this recipe, we will learn how to deploy a random forest model trained with the scikit-learn framework on the Arduino Nano and Raspberry Pi Pico with the Python emlearn project.

Getting ready

Whether you are just starting in ML, an enthusiast, or a researcher, you will have probably come across the scikit-learn (https://scikit-learn.org/) framework, a pillar, like TensorFlow, of the ML community.

As we have seen through all the projects developed in this book, TensorFlow is a low-level library providing the building blocks for ML algorithms, particularly for creating deep learning models. Scikit-learn...

How can we power microcontrollers with batteries?

For many tinyML solutions, batteries could be the only power source for our microcontrollers and this final recipe will teach us how to power them with AA batteries.

Getting ready

Batteries are sources of electric power and have a limited charge capacity. The charge capacity (or battery capacity) quantifies the stored charge and is measured in milli-ampere-hour (mAh). Therefore, a higher mAh implies a longer battery life.

The following table reports some commercial batteries that find applicability with microcontrollers:

Figure 12.4: Some commercial batteries for microcontrollers

The battery selection depends on the required microcontroller voltage and other factors such as charge capacity, form factor, and operating temperature.

As we can observe from the preceding table, the AA battery provides a higher capacity, but it supplies 1.5V, which is typically insufficient for microcontrollers.

Therefore...

Summary

In this concluding chapter, we have aimed to address three questions that may have crossed your mind to bring your existing and future tinyML projects to the next level.

The first question of this chapter centered on the practicality of training a model on microcontrollers. Here, we have ascertained that training is possible, albeit with certain constraints. Nonetheless, despite these limitations, the potential offered by on-device learning is vast, as it enables the creation of intelligent devices capable of learning how to interact with the environment autonomously.

Following that question, we explored the feasibility of deploying generic ML algorithms on microcontrollers, such as random forest, to build even more compact tinyML solutions. In this context, we deployed a trained scikit-learn model on microcontrollers using the emlearn project.

The last question was about powering microcontrollers with batteries. Here, we discussed how to connect batteries in series...

References

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/tiny

lock icon
The rest of the chapter is locked
You have been reading a chapter from
TinyML Cookbook - Second Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781837637362
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gian Marco Iodice

Gian Marco Iodice is team and tech lead in the Machine Learning Group at Arm, who co-created the Arm Compute Library in 2017. The Arm Compute Library is currently the most performant library for ML on Arm, and it's deployed on billions of devices worldwide – from servers to smartphones. Gian Marco holds an MSc degree, with honors, in electronic engineering from the University of Pisa (Italy) and has several years of experience developing ML and computer vision algorithms on edge devices. Now, he's leading the ML performance optimization on Arm Mali GPUs. In 2020, Gian Marco cofounded the TinyML UK meetup group to encourage knowledge-sharing, educate, and inspire the next generation of ML developers on tiny and power-efficient devices.
Read more about Gian Marco Iodice