Level up your TinyML skills

How to get started fast with Arduino Machine Learning

A ready to use layout project to get up and running quickly with your next TinyML project

Arduino Machine Learning

Running Machine Learning on your Arduino board (a.k.a TinyML) can seem like an advanced topic meant for experienced programmers.

It is actually much easier than you think!

You don't have to either master Machine Learning nor C++ to successfully train, convert and deploy a TinyML model to your Arduino board starting from scratch.

In this post, I'll show you how to do it step by step.

What is TinyML?

TinyML is Machine Learning aimed at embedded devices.

It actually covers a wide range of devices (from microcontrollers to Raspberry Pis and smartphones), but in the context of this post we'll limit to microcontrollers with limited resources.

TinyML job is to learn a classification / prediction pattern from training data and use the generated model to classify / predict unseen data.

Due to the resource constraints of our target devices, not all Machine Learning algorithms are suitable for TinyML. But there exists a few of them that you can leverage.

The aim of this post (and my whole effort) is to show you what I call Truly TinyML™ : algorithms that can run even on 10 Kb of RAM and 30 kB of flash.

Not powerful, 32 bit ARM chips only, but 8 bit microcontrollers too.

In case you're asking, Truly TinyML™ can even run on the Attiny85!

What is not (only) TinyML?

Neural Networks are one of the most widely known Machine Learning tool in today TinyML industry, but I'll make it crystal clear: Machine Learning is not only Neural Networks!.

There are tens of alternative algorithms to implement TinyML that are not releated at all with Neural Networks.

In fact, many of them are much smaller and faster than Neural Networks, while preserving top notch accuracy on many tasks.

When approaching a new TinyML project, I suggest you always consider "traditional" algorithms first, then move on Neural Networks only if your results are not satisfying.

Create a Truly TinyML™ model for Arduino

To train a TinyML model, we will make use of Python and the everywhereml package, which is a wrapper around the well-known scikit-learn package.

In a real project, you will have a dataset you want to learn from. In this short example, I will fake a dataset. All the training and exporting code, though, remains valid for your very own project with real data.

I'll go with RandomForestClassifier in the example below because it's fast and accurate. It's my personal default for new TinyML projects.

First of all, install the everywhereml package.

pip install everywhereml

Then create a new Python project to train your classifier.

from sklearn.datasets import make_blobs
from everywhereml.sklearn.ensemble import RandomForestClassifier

X, y = make_blobs(n_samples=100, centers=3, n_features=2, random_state=0)
clf = RandomForestClassifier(n_estimators=10)

clf.fit(X, y)

'''
  Now we convert the classifier to C++ with a single line of code
  - instance_name will create an instance of the classifier in the produced code
    (you will use this name later)
'''
print(clf.to_arduino(instance_name='blobClassifier'))

Deploy and run Machine Learning on Arduino

Copy the produced code into your sketch, preferably into its own file (eg. BlobClassifier.h), then call the predict method.

#include "BlobClassifier.h"

void setup() {
    Serial.begin(115200);
}

void loop() {
    // replace with your actual feature vector
    float input[2] = {0, 1};

    Serial.print("Prediction: ");
    Serial.println(blobClassifier.predict(input));
}

How tiny is Truly TinyML™?

Depending on the algorithm you choose, Truly TinyML™ can be extremely tiny!

And fast.

To give you some real figures, a RandomForestClassifier can execute in ~10 microseconds on an input of 1000 features and it requires less then 1 Kb of RAM.

Conclusions

If you were able to follow the entire tutorial, you should now have the basic knowledge required to implement your very own Machine Learning on Arduino.

If looking for inspiration and more advanced use cases, you'll find many projects to implement on this blog.

If you're stuck, use the form below to ask for help, I'll help you fix your problem.

Get monthly updates

Do not miss the next posts on TinyML and Esp32 camera. No spam, I promise

We use Mailchimp as our marketing platform. By submitting this form, you acknowledge that the information you provided will be transferred to Mailchimp for processing in accordance with their terms of use. We will use your email to send you updates relevant to this website.

Having troubles? Ask a question

Need a one to one call? Book now!

© Copyright 2022 Eloquent Arduino. All Rights Reserved.