|
|
Log in / Subscribe / Register

Milliwatt machine learning with emlearn

February 12, 2025

This article was contributed by Koen Vervloesem


FOSDEM

While large language models and the expensive hardware they require are all the rage now, other areas of artificial intelligence work within much more constrained hardware environments. At FOSDEM 2025, Jon Nordby presented his open-source machine-learning inference engine for microcontrollers, named emlearn. The project also boasts bindings for MicroPython, thus making machine-learning applications even more accessible.

Nordby's talk, "Milliwatt sized Machine Learning on microcontrollers with emlearn", was part of the Low-level AI Engineering and Hacking developer room (devroom) at FOSDEM. Nordby is the CTO and co-founder of Soundsensing, a Norwegian company that monitors ventilation systems and other technical infrastructure for its clients. The company uses machine learning to detect anomalies in data from sound and vibration sensors, thereby providing early warning of potential failures.

Tiny chips, massive scale

The niche field of running machine learning on microcontrollers is also known as TinyML or, more recently, Edge AI. It's primarily used for analyzing sensor data. The idea involves a microcontroller processing data from connected sensors, running a machine-learning model to analyze the data, and then extracting useful information from it. The results of this analysis can then be sent over the network or used to control a locally connected motor if the microcontroller is part of a robotic system.

Although machine-learning models could be run on much more powerful hardware in the cloud, Nordby explained that the TinyML approach has some key benefits. It allows for the creation of standalone systems, without the need of a network connection. Another benefit is low latency: sending data over a network to a server and then retrieving the results introduces latency that should be avoided in many industrial applications. Furthermore, transmitting raw sensor data to a server instead of just the useful information requires more power. With TinyML, "sensors can run for multiple years on a single battery". Also, by not transferring all of the potentially sensitive data, a TinyML application can be "privacy-compatible". And finally, AI on microcontrollers is cost-effective, which allows using it on a massive scale.

TinyML is already used in many areas, Nordby noted, citing examples such as keyword spotting in smart speakers to recognize phrases like "Hey Google" or "Hey Siri", and sleep trackers that monitor sleep quality. The technology is also used for tracking animal health and, in the case of Soundsensing, the health of machines.

To give an idea about the constraints we're talking about, a microcontroller often has just a thousandth or even less of the CPU power and memory of a smartphone. RAM could be up to 1MB and program space up to 10MB. Over 20 billion microcontrollers are shipped each year, and they're becoming increasingly available for hobbyists. Nordby highlighted the Arduino Uno, ESP32, RP2040, and RP2350 as available hardware platforms, as well as the Arduino IDE and MicroPython as software platforms for hobbyists.

Real-world use cases

Nordby started the emlearn project in 2018, as part of his Master's degree in Data Science at the Norwegian University of Life Sciences, and published it under an MIT license. Emlearn fits in the regular workflow of machine-learning researchers. They can just create a model and train it on a data set using standard Python libraries such as scikit-learn or Keras on their PC. Emlearn comes into play only after they have trained a model.

Emlearn is used to convert the trained model from Python objects to a C data structure and save it to a C header file that can then be included in the microcontroller's code. This header file has portable C99 code, without dynamic allocations and using only integer or fixed-point arithmetic, making it a fit for many embedded devices and software frameworks. This includes Arduino, Zephyr, and FreeRTOS. The C code generated by emlearn can also be used from other programming languages, as long as they support C bindings.

Not all models from scikit-learn or Keras are supported, but emlearn is able to convert models for the most common tasks relevant for analyzing sensor data: classification, regression, and anomaly detection. So emlearn can't be used to run large language models on microcontrollers, but Nordby summarized that "you have access to decision trees, random forests, k-nearest neighbors, and some smaller neural networks". Of course, the trained model must fit into the microcontroller's RAM and storage constraints.

Nordby emphasized that emlearn is not just a toy project: it has many real-world uses around the globe and it's referenced in at least 40 scientific publications. For example, Abhishek Damle used the project for health monitoring of cattle, as described in his Master's thesis. He trained a decision tree classifier on accelerometer data from a sensor on the cow's head. The device uses this model for sending information to the farmer about the animal's current activity: lying, walking, standing, grazing, or ruminating. This allows the farmer to detect abnormal behavior that could indicate health issues or other problems, for example when a cow is not eating for a long time. Running the classifier model on-device with emlearn consumed less than 1mW, and the TinyML approach used 50 times less power than sending the raw data to the cloud for analysis, significantly improving battery life.

Samsung Research even trained a model with emlearn for monitoring people with respiratory health problems using the motion and acoustic sensors in earbuds. Because the TinyML approach has a low power consumption, this "can be used as a practical solution worn every day, not just in a clinical context".

Last year, Nordby undertook the challenge of building a sensor board capable of running a useful machine-learning model at a total component cost of less than one US dollar. His "1 dollar TinyML" project is based on a Puya PY32F003 microcontroller featuring a 32-bit Arm Cortex-M0 core running at 32MHz, with 8KB RAM and 64KB flash storage. While emlearn is often applied on slightly more powerful microcontrollers, Nordby succeeded in creating something useful even within those constraints, analyzing audio from a microphone or movements from an accelerometer.

Python all the way down

To make emlearn more accessible, Nordby ported it to MicroPython. Since many who are involved in machine learning primarily know Python, they can now develop TinyML solutions without needing to learn C. MicroPython implements a subset of Python for microcontrollers with 16KB RAM and more. We looked at in 2023 and Nordby gave another talk at FOSDEM 2025 on it, as well.

One of MicroPython's appealing features is that it can be extended with modules written in C, which offer greater performance than plain MicroPython. The emlearn-micropython project offers various models from emlearn as a prebuilt .mpy module with native machine code for multiple microcontrollers. Users don't need to set up a C toolchain or SDK, but can easily install the module onto their microcontroller's flash storage using MicroPython's "mpremote mip install" command and copy the model weights from their trained model to the device as a CSV file.

The MicroPython code on the device then creates the emlearn model from the .mpy file and loads the model weights from the CSV file. With the model ready, it can then read sensor data, preprocess the data, and run the model to return a prediction or classification.

As an example, Nordby developed an automatic toothbrush timer. He mounted M5Stack's M5StickC PLUS 2, which has an ESP32 microcontroller and an accelerometer, onto a 3D-printed case together with a toothbrush. By collecting and labeling one hour of accelerometer data while brushing his teeth and engaging in other activities, he trained a model to recognize these activities. In 500 lines of MicroPython code, his solution counts the number of seconds during which the device detects brushing activity. Once the user completes two minutes of brushing, the device's buzzer plays a song.

Conclusion

Emlearn shows how, even under the severe hardware limitations of embedded devices, one can develop practical AI applications. A couple of kilobytes of RAM, a couple of milliwatts of power, and a couple of dollars are enough for applications such as human or animal activity recognition. Even without knowing C, the necessary performance is possible thanks to emlearn-micropython's native module. It will be interesting to see what applications people will build with emlearn for sensor devices.

[While I was unable to attend FOSDEM in person, I watched Nordby's talks as they live-streamed on Saturday and Sunday.]
Index entries for this article
GuestArticlesVervloesem, Koen
ConferenceFOSDEM/2025


to post comments

Showing where ML is useful

Posted Feb 15, 2025 22:54 UTC (Sat) by fraetor (subscriber, #161147) [Link]

Microcontroller development often seems to do a really good job of bringing out the most useful elements of software developments. I would guess it comes from where they are so constrained that every bit of code has to provide some real value.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds