Skip to main content

A Python library to export scikit-learn models to microcontrollers

Project description

PyPI version

micro-learn

micro-learn is a Python module for converting trained machine learning models into inference code that can run on microcontrollers in real time.

Machine learning algorithms typically require heavy computing and memory resources in the training phase, far greater than what a typical constrained microcontroller can offer. However, post training, many of these algorithms boil down to simple parameters that require simple arithmetic and logical operations for inference. These can easily run on microcontrollers in real time. The purpose of this library is to convert ML models (trained using scikit-learn) directly into Arduino inference code.

All the inference algorithms are optimized for microcontrollers and require least possible arithmetic computations. Division operations have been converted to multiplications since the latter is much more computationally efficient. Note that all the algorithms are exact and not approximate. Please refer to my ACM paper for theoretical and practical foundations regarding this procedure.

Installation

Dependencies

  • Python (>= 3.6)
  • NumPy (>= 1.13.3)

User Installation

Use Python3 pip to install the latest micro-learn package.

pip install micro-learn

Supported Algorithms

The following scikit-learn models are supported for now:

  • KMeans
  • Perceptron
  • Linear Regression (LR)
  • Logistic Regression (Logit)
  • Gaussian Naive Bayes (GNB)
  • Principal Component Analysis (PCA)
  • Passive-Aggressive Classifier (PA)
  • Linear Discriminant Analysis (LDA)
  • Quadratic Discriminant Analysis (QDA)
  • Support Vector Machine (SVM) (Linear Kernel)

Note that only binary-class classification models are supported as of now. Support for multi-class and other scikit-learn models coming soon!

Usage

Train a binary classifier using any of the supported scikit-learn models and simply pass this trained model to Offload(). Example for LDA is shown below. For SVM (LinearSVM or SVC), Offload() expects a fitted StandardScaler model as a second argument since SVM is a scale-variant algorithm.

>>> from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
>>> lda = LinearDiscriminantAnalysis(store_covariance=True)
>>> lda.fit(X, Y)
>>> from microlearn.offloader import Offload
>>> off = Offload(lda) #Simply pass your trained model!
>>> off.export_to_arduino('/home/adarsh1001/lda.ino')

And that's it! The output Arduino template will have the corresponding ML inference code along with all the trained parameters. After exporting, open the .ino file and edit the data section as per your need. And of course, since the Arduino programming language is a derivative of C/C++, you can directly edit the template and convert it into a generic .c or .cpp code. For a more detailed guide, you can check out my medium post on this.

Project History

I started working on embedded machine learning, both from a theoretical as well as practical perspective, in August 2019 as part of my MS. In November 2019, my paper on a related topic was accepted in ACM SAC. The work on coding a unified library for offloading trained ML models to microcontroller code (micro-learn) was started in May 2020.

Citation

If you use micro-learn in a scientific publication, please do cite my paper.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micro-learn-0.0.2.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

micro_learn-0.0.2-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page