Are you starting machine learning on Arduino development cards and similar microprocessors?Would you like to run the python-trained model in any C++ project, such as Arduino, STM32, ESP32?
In this content we will show you how easy it is.
We need some data to train a classifier.If you're starting from scratch and don't already have your preferred folder structure, we recommend creating a folder to hold the data you collect.
Inside this folder, create a custom file with a .csv extension by placing an example on each line for each class you want to classify.If you have done so, you can use the next function to load this data.
Training the Classifier
When we have the data, we can train the classifier.
micromlgenpackage (which can move machine learning classifiers to plain C code) supports the following classes:
- Decision Tree
- Random Forest
- Gauss NB
- Support Vector Machines(SVM)
- Relevance Vector Machines(RVM)
In this article we will use the Random Forest class, but you can replace it with other classes without changing the rest of the code.
Exporting Plain C Code
Now we can convert the trained classifier to plain C code using the
This is the code you need to transfer to your Arduino project.To ensure integrity with the tutorials in this content, save this code as a file named model.h.
Using in a Project
We now have the code we need to run Machine Learning directly on our microcontroller.
If everything went smoothly, your microprocessor will be running machine learning smoothly.