EmbedIA is a compact and lightweight framework capable of providing the necessary functionalities for the execution of inferences from Convolutional Neural Network models, created and trained using the Python Tensorflow/Keras library, on microcontrollers with limited hardware resources. It is designed to be compatible with the C and C++ languages for the Arduino IDE, both with support for a wide range of MCUs.
For the conversion and use of Neural Network models in microcontrollers using EmbedIA, the following workflow must be followed:
- Generation of the model: Architecture selection, network hyperparameters and training data.
- Training: Neural Network Training Using Tensorflow/Keras in Python.
- EmbedIA Export: Export of C/C++ application with model and necessary libraries using the EmbedIA converter.
- Solution Deployment: Project Compilation on the Microcontroller Platform.
- Running Inferences:Running Inferences on the device.
Currently it is possible to incorporate certain layers to the neural network model for execution on microcontrollers. The layers supported by EmbedIA, implemented in the C library, are the following:
Layers based from Keras:
Activation functions from Keras:
Layers from Larq:
Layers from Scikit-Learn (integrated for preprocessing):
In order to use the EmbedIA Python converter, the first step is to clone the repository
git clone https://github.com/Embed-ML/EmbedIA.git
cd EmbedIA
Open the create_embedia_project.py script and configure the converter parameters:
-
OUTPUT_FOLDER: output folder path
-
PROJECT_NAME: generated project name
-
MODEL_FILE: model path in .h5 format to use
-
options.embedia_folder: folder of EmbedIA files:
options.embedia_folder = ...
-
options.project_type: type of project among those available:
ProjectType.ARDUINO
ProjectType.C
ProjectType.CODEBLOCK
ProjectType.CPP
-
options.data_type: selection of data type among those available:
ModelDataType.FLOAT
ModelDataType.FIXED32
ModelDataType.FIXED16
ModelDataType.FIXED8
ModelDataType.BINARY
ModelDataType.BINARY-FIXED32
ModelDataType.BINARY-FLOAT16
-
options.binary_block: options for block size of binary layers:
ModelDataType.BinaryBlockSize.Bits8
ModelDataType.BinaryBlockSize.Bits16
ModelDataType.BinaryBlockSize.Bits32
ModelDataType.BinaryBlockSize.Bits64
-
options.debug_mode: options for inclusion and use of debug functions:
DebugMode.DISCARD
DebugMode.DISABLED
DebugMode.HEADERS
DebugMode.DATA
-
options.files: Selection of files to be executed:
ProjectFiles.ALL
{ProjectFiles.MAIN}
{ProjectFiles.MODEL}
{ProjectFiles.LIBRARY}
-
options.example_data: array of data to include as examples:
options.example_data = samples
-
options.example_ids: array of id of data in example_data property:
options.example_ids = ids
-
options.clean_output: if True, remove output folder and start a clean export:
options.clean_output = True
Run the script as follows:
python create_embedia_project.py
If the process was successful, a message will be displayed indicating where the project has been generated
Example: In the following Colab there is an example of the use of the EmbedIA converter to create a project in C language for the classification of the images of the digits dataset:
Or an example of simulation in the Wokwi online environment: https://wokwi.com/projects/359745013247499265To use the EmbedIA features in the microcontroller, you need to include model initialization and inference execution in your code, using the provided functions:
void model_init(void)
: function in charge of executing the initialization of the model in C language from the load of the weights obtained in Python of the model trained through Tensorflow/Kerasint model_predict(input, * results)
: method that will finally execute the inference using the input data passed by parameter (input). It builds the architecture of the network, that is, it is responsible for concatenating the outputs of the layers in the correct order. In this way, a vector of probabilities is obtained for each class (received by parameter, * results) and the value of the class with greater confidence (integer value returned), given a certain input passed by parameter to the function.
Example:
// model initialization
model_init();
// model inference
int prediction = model_predict(input, &results);