Once you successfully trained or imported a model, you can use Edge Impulse to download a C++ library that bundles both your signal processing and your machine learning model. Until recently, we could only run one impulse on MCUs.
Feature under development
Please note that this method is still under integration in the studio and has not yet been fully tested on all targets. This tutorial is for advanced users only. Thus, we will provide limited support on the forum until the integration is completed. If you are interested in using it for an enterprise project, please check our pricing page and contact us directly, our solution engineers can work with you on the integration.
In this tutorial, we will see how to run multiple impulses using the downloaded C++ libraries of two different projects.
As an example, we will build an intrusion detection system. We will use a first model to detect glass-breaking sounds, if we detected this sound, we will then classify an image to see if there is a person or not in the image.
You can have a look at this Github repository to make sure your directory structures, files and variables are correct.
Multi-impulse vs multi-model vs sensor fusion
Running multi-impulse refers to running two separate projects (different data, different DSP blocks and different models) on the same target. It will require modifying some files in the EI-generated SDKs.
Running multi-model refers to running two different models (same data, same DSP block but different tflite models) on the same target. See how to run a motion classifier model and an anomaly detection model on the same device in this tutorial.
Sensor fusion refers to the process of combining data from different types of sensors to give more information to the neural network. See how to use sensor fusion in this tutorial.
Prerequisites
Make sure you have two impulses fully trained. In this tutorial, we will use the following public projects:
Rename the variables in the tflite-model directory
Rename the variables (EON model functions, such as trained_model_input etc or tflite model array names) by post-fixing them with the name of the project.
e.g: Change the trained_model_compiled_audio.h from:
#ifndef trained_model_GEN_H
#define trained_model_GEN_H
#include "edge-impulse-sdk/tensorflow/lite/c/common.h"
// Sets up the model with init and prepare steps.
TfLiteStatus trained_model_init( void*(*alloc_fnc)(size_t,size_t) );
// Returns the input tensor with the given index.
TfLiteStatus trained_model_input(int index, TfLiteTensor* tensor);
// Returns the output tensor with the given index.
TfLiteStatus trained_model_output(int index, TfLiteTensor* tensor);
// Runs inference for the model.
TfLiteStatus trained_model_invoke();
//Frees memory allocated
TfLiteStatus trained_model_reset( void (*free)(void* ptr) );
// Returns the number of input tensors.
inline size_t trained_model_inputs() {
return 1;
}
// Returns the number of output tensors.
inline size_t trained_model_outputs() {
return 1;
}
#endif
to:
#include "edge-impulse-sdk/tensorflow/lite/c/common.h"
// Sets up the model with init and prepare steps.
TfLiteStatus trained_model_audio_init( void*(*alloc_fnc)(size_t,size_t) );
// Returns the input tensor with the given index.
TfLiteStatus trained_model_audio_input(int index, TfLiteTensor* tensor);
// Returns the output tensor with the given index.
TfLiteStatus trained_model_audio_output(int index, TfLiteTensor* tensor);
// Runs inference for the model.
TfLiteStatus trained_model_audio_invoke();
//Frees memory allocated
TfLiteStatus trained_model_audio_reset( void (*free)(void* ptr) );
// Returns the number of input tensors.
inline size_t trained_model_audio_inputs() {
return 1;
}
// Returns the number of output tensors.
inline size_t trained_model_audio_outputs() {
return 1;
}
#endif
Tip: Use an IDE to use the "Find and replace feature.
Here is a list of the files that need to be modified (the names may change if not compiled with the EON compiler):
Create a new directory (merged-impulse for example). Copy the content of one project into this new directory (audio for example). Copy the content of the tflite-model directory from the other project (image) inside the newly created merged-impulse/tflite-model.
The structure of this new directory should look like the following:
The section that should be copied is from const char* ei_classifier_inferencing_categories... to the line before const ei_impulse_t ei_default_impulse = impulse_<ProjectID>_<version>.
Make sure to leave only one const ei_impulse_t ei_default_impulse = impulse_233502_3; this will define which of your impulse is the default one.
Subtract and merge the trained_model_ops_define.h or tflite_resolver.h
Make sure the macros EI_TFLITE_DISABLE_... are a COMBINATION of the ones present in two deployments.
For EON-compiled projects:
E.g. if #define EI_TFLITE_DISABLE_SOFTMAX_IN_U8 1 is present in one deployment and absent in the other, it should be ABSENT in the combined trained_model_ops_define.h.
For non-EON-Compiled projects:
E.g. if resolver.AddFullyConnected(); is present in one deployment and absent in the other, it should be PRESENT in the combined tflite-resolver.h. Remember to change the length of the resolver array if necessary.
In this example, here are the lines to deleted:
Run the multi-impulse (stand-alone)
Clone this repository: https://github.com/edgeimpulse/example-standalone-inferencing-multi-impulse
Copy the content of the merged-impulse directory to example-standalone-inferencing-multi-impulse (replace the files and directory sharing the same).
Rename the variables in source/main.cpp
Edit the source/main.cpp file and replace the callback function names, the features buffers.
Note: The run_classifier takes the impulse pointer as a first argument
Copy the raw features from the studio Live Classification page.
Compile and run
Enter make -j in this directory to compile the project Enter ./build/app to run the application Compare the output predictions to the predictions of the test sample in the Edge Impulse Studio.
Enter rm -f build/app && make clean to clean the project.
Congrats, you can now run multiple Impulse!!
Limitations
The custom ML accelerator deployments are unlikely to work (TDA4VM, DRPAI, MemoryX, Brainchip).
The custom tflite kernels (ESP NN, Silabs MVP, Arc MLI) should work - but may require some additional work. I.e: for ESP32 you may need to statically allocate arena for the image model.
In general, running multiple impulses on an MCU can be challenging due to limited processing power, memory, and other hardware constraints. Make sure to thoroughly evaluate the capabilities and limitations of your specific MCU and consider the resource requirements of the impulses before attempting to run them concurrently.