Level up your TinyML skills

MicroMLGen library for Python

A Python library to export scikit-learn models into C format with a single line of code

MicroMLGen library for Python

Micromlgen is a Python library to export scikit-learn models into C format with a single line of code.

As an Arduino maker, you can then run that model on your microcontroller to perform Machine Learning in your own project.

Installation

Micromlgen is available on PyPi, so you can use pip to install.

pip install -U micromlgen

Or you can clone the Github repo

git clone https://github.com/eloquentarduino/micromlgen

Supported models

Micromlgen can port to plain C many types of models:

How to use in Python

Micromlgen package has a single method called port(): it accepts one of the above classifiers as input and returns the C code you need to import in your Arduino sketch.


from micromlgen import port
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris


if __name__ == '__main__':
    X, y = load_iris(return_X_y=True)
    clf = RandomForestClassifier(n_estimators=10).fit(X, y)
    c_code = port(clf)

    with open('classifier.h', 'w') as file:
        file.write(c_code)
        

How to use in Arduino sketch

classifier.h contains all the code you need to perform classification in your sketch. You have to include the file and instantiate the classifier.


#include "classifier.h"

Eloquent::ML::Port::RandomForest clf;

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

void loop() {
    float irisSample[4] = {6.2, 2.8, 4.8, 1.8};

    Serial.print("Predicted class (you should see '2'): ");
    Serial.println(clf.predict(irisSample));
    delay(1000);
}
        

Configuration

The port() method accepts 2 optional configurations:

  • classname (str): the name of the exported class. If not provided, the classifier's class name will be used
  • classmap (dict): if supplied, the exported class will also have a predictLabel() method

What is a classmap?

A classmap is a mapping from class numbers to class names. It is used to give a meaningful description to the classifier's predictions.


from micromlgen import port
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris


if __name__ == '__main__':
    X, y = load_iris(return_X_y=True)
    clf = RandomForestClassifier(n_estimators=10).fit(X, y)
    classmap = {
        0: 'Setosa',
        1: 'Versicolor',
        2: 'Virginica'
    }
    c_code = port(clf, classmap=classmap)

    with open('classifier.h', 'w') as file:
        file.write(c_code)
        

Now, instead of printing the class number in your Arduino sketch, you can print the class label.


#include "classifier.h"

Eloquent::ML::Port::RandomForest clf;

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

void loop() {
    float irisSample[4] = {6.2, 2.8, 4.8, 1.8};

    Serial.print("Predicted label (you should see 'Virginica'): ");
    Serial.println(clf.predictLabel(irisSample));
    delay(1000);
}
        

Having troubles? Ask a question

Related posts

tinyml

Attiny Machine Learning

Can an Attiny85 really run Machine Learning? Let's find out in this article!

libraries

TinyMLgen for Python

A Python package to export TensorFlow Lite neural networks to C++ for microcontrollers

tinyml

Separable Conv2D kernels: a benchmark

Real world numbers on 2D convolution optimization strategies for embedded microcontrollers

tinyml

TensorFlow Lite on Esp32

Convert an existing TensorFlow Lite model to be deployed on your ESP32 board using the Arduino IDE

tinyml

Arduino WiFi Indoor Positioning

Localize people and objects as they move around your building with the power of Machine Learning

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

© Copyright 2023 Eloquent Arduino. All Rights Reserved.