Skip to main content

Machine learning for microcontrollers and embedded systems

Project description

Travis CI Build Status Appveyor Build status DOI

emlearn

Machine learning for microcontroller and embedded systems. Train in Python, then do inference on any device with a C99 compiler.

Key features

Embedded-friendly Inference

  • Portable C99 code
  • No libc required
  • No dynamic allocations
  • Support integer/fixed-point math
  • Single header file include

Convenient Training

  • Using Python with scikit-learn or Keras
  • The generated C classifier is also accessible in Python

MIT licensed

Can be used as an open source alternative to MATLAB Classification Trees, Decision Trees using MATLAB Coder for C/C++ code generation. fitctree, fitcensemble, TreeBagger, ClassificationEnsemble, CompactTreeBagger

Status

Minimally useful

Classifiers:

  • eml_trees: sklearn.RandomForestClassifier, sklearn.ExtraTreesClassifier, sklearn.DecisionTreeClassifier
  • eml_net: sklearn.MultiLayerPerceptron, Keras.Sequential with fully-connected layers
  • eml_bayes: sklearn.GaussianNaiveBayes

Feature extraction:

  • eml_audio: Melspectrogram

Tested running on AVR Atmega, ESP8266, Linux and Windows. Mac OS should also work fine, please file a bug report if it does not.

Installing

Install from PyPI

pip install --user emlearn

Usage

  1. Train your model in Python
from sklearn.ensemble import RandomForestClassifier
estimator = RandomForestClassifier(n_estimators=10, max_depth=10)
estimator.fit(X_train, Y_train)
...
  1. Convert it to C code
import emlearn
cmodel = emlearn.convert(estimator, method='inline')
cmodel.save(file='sonar.h')
  1. Use the C code
#include "sonar.h"

const int32_t length = 60;
int32_t values[length] = { ... };
const int32_t predicted_class = sonar_predict(values, length):

For full example code, see examples/digits.py

Contributors

Jon Nordby
Mark Cooke

Citations

If you use emlearn in an academic work, please reference it using:

@misc{emlearn,
  author       = {Jon Nordby},
  title        = {{emlearn: Machine Learning inference engine for 
                   Microcontrollers and Embedded Devices}},
  month        = mar,
  year         = 2019,
  doi          = {10.5281/zenodo.2589394},
  url          = {https://doi.org/10.5281/zenodo.2589394}
}

Made with emlearn

emlearn has been used in the following works.

  • TinyML-Enabled Frugal Smart Objects: Challenges and Opportunities by Ramon Sanchez-Iborra and Antonio F. Skarmeta. Created a model for automatically selecting which radio transmission method to use in an IoT device. Running on Arduino Uno (AVR8) device. Tested Multi-layer Perceptron, Decision Tree and Random Forest from emlearn. Compared performance with sklearn-porter, and found that Random Forest to be faster in emlearn, while Decision Tree faster in sklearn-porter. Compared emlearn MLP to MicroMLGen’s SVM, and found the emlearn MLP to be more accurate and lower inference time.
  • A Machine Learning Approach for Real Time Android Malware Detection by Ngoc C. Lê et al. Created a C++ model for detecting malware. Used a set of hand-engineered features and a Random Forest from emlearn as the classifier. Running on Android devices.
  • RIOT OS has a package for emlearn. RIOT OS emlearn package example. Their build system automatically runs this test on tens of different hardware boards.

If you are using emlearn, let us know! You can for example submit a pull request for inclusion in this README, or create an issue on Github.

Project details


Download files

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

Files for emlearn, version 0.12.2
Filename, size File type Python version Upload date Hashes
Filename, size emlearn-0.12.2.tar.gz (41.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page