2025-04-23 12:06 AM - last edited on 2025-04-23 6:50 AM by Julian E.
I would like to ask how NanoEdge AI embeds multiple library models in one code, for example: I compiled a project called "test" in the Keil compiler and I want to create a project similar to an official demo video. Under the same project, there is both an anomaly detection (AD) library for learning and a multi classification (N-Classfial) library for classifying abnormal situations.
Now, I have trained two models for these two libraries, but the problem is that I don't know how to embed these two models into the same project for use. I am using the v5.0.2 version of NanoEdge AI STUDIO.
Can you help me solve this problem? Thank you.
Solved! Go to Solution.
2025-04-23 3:01 AM - edited 2025-04-23 3:03 AM
Hello @szxx,
In the last step of NanoEdge AI Studio, the compilation, you can select the Multi-library option.
You need to enter a name to your library that will be used as a suffix for the files to include in your project and in the function names, for you to freely use each library.
For example, here is a classification project, where I call my library lib1:
Here is the zip that you get:
You can see that my knowledge, libneai and NanoEdgeAI files all have the suffix lib1.
We can also look at the function in the header file NanoedgeAI_lib1.h:
/**
* @brief Initialization must be called at the beginning to load the knowledge.
* This buffer is defined in the header file knowledge.h provided in the .zip file
* @retval NEAI_OK in case of success.
*/
enum neai_state neai_classification_init_lib1(const float knowledge_buffer[]);
/**
* @brief This function returns the class identified
* @param data_input[] [IN]: Signal to be classified AXIS_NUMBER * DATA_INPUT_USER
* @param output_buffer[] [OUT]: Contains the probabilities for all classes
* @param *id_class [OUT]: Variable that contains the class ID with the highest probabilities
* @retval NEAI_OK in case of success.
*/
enum neai_state neai_classification_lib1(float data_input[], float output_buffer[], uint16_t *id_class);
As you can see my functions also have the suffix lib1.
Now if you want to add another library, you will have to select multilibrary and set a name, like lib2.
Let's say, you have a classification library called lib1 and an anomaly detection called lib2. In your project, you need to:
For example, something like this:
#include "NanoEdgeAI_lib1.h"
#include "NanoEdgeAI_lib2.h"
#include "knowledge_lib1.h"
#include "knowledge_lib2.h"
int main(void) {
// Initialize models
neai_state1 = neai_classification_init_lib1(knowledge_buffer);
neai_state2 = neai_anomalydetection_knowledge_lib2(knowledge_buffer);
if (neai_state1 == NEAI_OK and neai_state2 == NEAI_OK ) {
while (1) {
// Fill data_inputwith sensor data here
// Run Anomaly Detection
neai_anomalydetection_detect_lib2(float data_input[], uint8_t *similarity);
// If similarity is low, most likely an anomaly
if (similarity <= 90) {
neai_classification_lib1(float input_buffer[], float output_buffer[], uint16_t *id_class);
}
// Do something with the results
}
}
//else error in init
}
Documentation about libraries function:
Anomaly detection: https://wiki.st.com/stm32mcu/wiki/AI:NanoEdge_AI_Library_for_anomaly_detection_(AD)
Classification: https://wiki.st.com/stm32mcu/wiki/AI:NanoEdge_AI_Library_for_n-class_classification_(nCC)
You can also find code example here:
stm32ai-nanoedge-datalogger/Drivers at main · stm32-hotspot/stm32ai-nanoedge-datalogger
These code are the datalogger generator source code. These code both include datalogging and using a library so you can take a look.
(be careful to use the command in the readme to clone the repository as it needs external ressources)
have a good day,
Julian
2025-04-23 3:01 AM - edited 2025-04-23 3:03 AM
Hello @szxx,
In the last step of NanoEdge AI Studio, the compilation, you can select the Multi-library option.
You need to enter a name to your library that will be used as a suffix for the files to include in your project and in the function names, for you to freely use each library.
For example, here is a classification project, where I call my library lib1:
Here is the zip that you get:
You can see that my knowledge, libneai and NanoEdgeAI files all have the suffix lib1.
We can also look at the function in the header file NanoedgeAI_lib1.h:
/**
* @brief Initialization must be called at the beginning to load the knowledge.
* This buffer is defined in the header file knowledge.h provided in the .zip file
* @retval NEAI_OK in case of success.
*/
enum neai_state neai_classification_init_lib1(const float knowledge_buffer[]);
/**
* @brief This function returns the class identified
* @param data_input[] [IN]: Signal to be classified AXIS_NUMBER * DATA_INPUT_USER
* @param output_buffer[] [OUT]: Contains the probabilities for all classes
* @param *id_class [OUT]: Variable that contains the class ID with the highest probabilities
* @retval NEAI_OK in case of success.
*/
enum neai_state neai_classification_lib1(float data_input[], float output_buffer[], uint16_t *id_class);
As you can see my functions also have the suffix lib1.
Now if you want to add another library, you will have to select multilibrary and set a name, like lib2.
Let's say, you have a classification library called lib1 and an anomaly detection called lib2. In your project, you need to:
For example, something like this:
#include "NanoEdgeAI_lib1.h"
#include "NanoEdgeAI_lib2.h"
#include "knowledge_lib1.h"
#include "knowledge_lib2.h"
int main(void) {
// Initialize models
neai_state1 = neai_classification_init_lib1(knowledge_buffer);
neai_state2 = neai_anomalydetection_knowledge_lib2(knowledge_buffer);
if (neai_state1 == NEAI_OK and neai_state2 == NEAI_OK ) {
while (1) {
// Fill data_inputwith sensor data here
// Run Anomaly Detection
neai_anomalydetection_detect_lib2(float data_input[], uint8_t *similarity);
// If similarity is low, most likely an anomaly
if (similarity <= 90) {
neai_classification_lib1(float input_buffer[], float output_buffer[], uint16_t *id_class);
}
// Do something with the results
}
}
//else error in init
}
Documentation about libraries function:
Anomaly detection: https://wiki.st.com/stm32mcu/wiki/AI:NanoEdge_AI_Library_for_anomaly_detection_(AD)
Classification: https://wiki.st.com/stm32mcu/wiki/AI:NanoEdge_AI_Library_for_n-class_classification_(nCC)
You can also find code example here:
stm32ai-nanoedge-datalogger/Drivers at main · stm32-hotspot/stm32ai-nanoedge-datalogger
These code are the datalogger generator source code. These code both include datalogging and using a library so you can take a look.
(be careful to use the command in the readme to clone the repository as it needs external ressources)
have a good day,
Julian
2025-04-23 3:12 AM
To @Julian E. :
Thank you very much for helping me solve the problem of how to embed two libraries in the same project. However, after embedding the two libraries, I encountered this problem:
RTOS\RTOS.axf: Error: L6200E: Symbol _fminf multiply defined (by NanoEdgeAI_yuan.o and NanoEdgeAI_san.o).
I don't know how it was caused. Could you help me solve it again? Thank you very much!
Wish you a good mood every day.
szxx
2025-04-23 6:03 AM - edited 2025-04-23 6:04 AM
Hello @szxx,
Indeed, it seems that there is a bug with keil.
Because of another bug, from Arm this time, we have to make a simple test _fminf to warn the user if he is using keil.
We will solve it in the next version, but until then, can you test the following:
if you check the symbols in each:
nm NanoEdgeAI_san.o | grep fminf
nm NanoEdgeAI_yuan.o | grep fminf
You’ll see _fminf is defined in both.
Then to avoid clashing, rename symbols in just one object file (say the classification one):
objcopy --redefine-sym _fminf=_fminf_san NanoEdgeAI_san.o
Rebuild a new .a file with the fixed object
ar rcs libneai_san_fixed.a NanoEdgeAI_san.o
In Keil:
Sorry for the inconvenience.
Because of this, you will have to do it each time you want to deploy a new library.
Have a good day,
Julian
2025-04-23 8:26 AM
To @Julian E. :
I tried the method you mentioned to check the symbols in each project (nm NanoEdgeAI_san.o | grep fminf), and also attempted to enter objcopy --redefine-sym _fminf=_fminf_san NanoEdgeAI_san.o in the command line. However, I found that I couldn't find the corresponding.o file of the library. I searched in my project directory but still couldn't find it. May I ask if there are any other solutions to this problem? Or is there any way for me to find the corresponding.o file of the library for the generated NanoEdge AI? Please help me solve this problem again. Thank you very much!
Wish you a happy mood every day.
szxx