Machine Learning for Arduino and Other Microprocessors

There are hundreds of topics about Arduino and Machine Learning on many foreign forums and discussion platforms of developers. A certain segment, Arduino argues, are too limited to manage machine learning. He argues that another definitive Multi Layer Perceptron application can be easily used on Arduino development cards. Finally, there are opinions that tensorflow lite,specially developed for microprocessors, can be used.

First of all, it is worth noting something very important to resolve a general confusion:

AI ≠ Machine learning ≠ Neural networks

Users and developers who think these three concepts are the same thing have false and incorrect ideas about microprocessors and machine learning. On arduino cards, you can run classification andregression,even in less powerful ones (e.g. ATmega128, ATtiny Series, ATmega8): you should not use only neural networks.

What is MicroML Builder?

Are you running machine learning that's outdated, powerless, and limited as hardware? That's what MicroML is coming to your rescue.

MicroMLis a project to bring machine learning algorithms to microcontrollers.It was developed as an alternative to Tensorflow for Microcontrollers dedicated only to Artificial Neural Networks: here you will find leaner alternatives to neural networks to machine learning even on 8-bit microcontrollers.

Currently, support vector machinescan be converted into optimized C code that you can use in any MCU of your choice: Arduino (Uno, Nano, Micro…), ESP8266, ESP32 and any MCU that really supported C.

Why Support Vector Machines?Because they are really good at classifying multidimensional features, and they are quite easy to optimize for RAM-restricted environments. Hence With Arduino, you can see our Motion Identification article with Machine Learning.

Creating a Classifier

First of all, you need to train a classifier.You should use Python's scikit-learn library – which, considering that it is widely adopted, is probably already in use.Then you need to install the MicroML package.

pip install micromlgen

Finally, we have added a trained classifier to the optimized C code.

from micromlgen import port
from sklearn.svm import SVC
from sklearn.datasets import load_iris

if __name__ == '__main__':
    iris = load_iris()
    X = iris.data
    y = iris.target
    clf = SVC(kernel='linear', gamma=0.001).fit(X, y)
    print(port(clf))

You must set the gamma value to a specific value. Otherwise, the incoming auto by default will cause an error.

That's our preparation, you have everything you need to classify your Arduino projects.

MicroML Alternatives

  1. sklearn-porter (among others) can give c code, but it is not optimized for microcontrollers.You will hit a wall in RAM as it must report all support vectors in memory (to have an idea, the breast cancer dataset produces a pair of matrices of 57×30, a total of 6840 bytes for support vectors only).
  2. Emlearn is optimized for microcontrollers, Decision Tree, Random Forest, Naive Gaussian Bayes, Full Connected Neural Networks.Still, there is no SVM.

Use in arduino project

There are two methods you must call to run estimates in your project:

  1. predict(double features[]): runs the actual estimate and returns a number representing the predicted class
  2. classIdxToName(uint8_t classIdx): converts the class index to a readable string based on the class map created from your files
#include "model.h"

void classify() {
    Serial.print("Predicted class: ");
    Serial.println(classIdxToName(predict(features)));
}

We are starting a series about applied projects to introduce machine learning in microprocessors, continuity and writing tracking are very important in this regard, do not forget to read the next articles and projects.