Level up your TinyML skills

Micromlgen

A Python package to convert Sklearn models to C++

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);
}
            

Monthly Newsletter

Get post previews and exclusive tutorials in your inbox

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.