Warning
- This documentation is obsolete.
- Please visit the STMicroelectronics Wiki page.
NanoEdge AI Library (Cl)¶
Documentation for Classification (Cl)
I. What is NanoEdge AI Library for classification?¶
NanoEdge AI Library is an artificial intelligence static library developed by Cartesiam, for embedded C software running on ARM Cortex microcontroller.
When embedded on microcontrollers, it gives them the ability to easily “classify” sensor patterns, by themselves, without the need for the user to have additional skills in Mathematics, Machine Learning, or Data science.
The NanoEdge AI static library for classification is the code that contains an AI model (for example, as a bundle of signal treatment, machine learning model, optimally tuned hyperparameters, etc.) designed to identify a sensor pattern in a class. All classes are defined by the user in the Studio and are used during the training process of the AI model.
Note
The library is not available in the free version of the Studio.
II. Install / Getting started¶
The main functions available via the library are:
knowledge_init() |
run first before the classification process to load the knowledge |
classifier() |
run a classification iteration (inference) |
1. Static library¶
The static NanoEdge AI Library is only available via NanoEdge AI Studio in its full version.
- Clicking Compile in Step 6: “Deploy” of NanoEdge AI Studio;
- Open the .zip file;
- Select and copy the static library
libneai.a
;- Link this static library to your project’s code.
2. NanoEdge AI Library functions¶
i. Initialization:¶
uint8_t NanoEdgeAI_knowledge_init(const float knowledge_buffer[]);
Initialization must be called at the beginning to load the knowledge.
Input:
const float knowledge_buffer[]
, this buffer is defined in the header file knowledge.h provided in the .zip file containing the static NanoEdge AI Library.Output:
0
means success (the knowledge has been loaded), other values mean that loading has failed.123
: the library is being used on a board that is not authorized (only for libraries that are limited to partner/authorized development boards).
ii. Classification:¶
uint16_t NanoEdgeAI_classifier(float input_buffer[], float output_buffer[], uint8_t class_threshold);
This function returns the class identifier.
Input:
float input_buffer[]
, the length of the buffer isDATA_INPUT_USER * AXIS_NUMBER
.float output_buffer[]
, the length of the buffer isCLASS_NUMBER
.uint8_t class_thresold
,0
or1
.Output:
The class identifier (the input signal has been classified).
Note
- The class identifier returned by the function is a number between
0
andCLASS_NUMBER
. - The user can easily get the name of the class associated with the identifier returned by the function using the array
id2class[]
, defined in the header file NanoEdgeAI.h. The name of classes is defined by the user in the Studio. output_buffer[]
contains the probabilities of each class.- The sum of the probabilities of each class is equal to 1.
- If
class_threshold = 0
the function returns the identifier of the class which has the maximum probability. - If
class_threshold = 1
the function returns the identifier of the class which has a probability strictly greater than 0.5. If there is no probability strictly greater than 0.5 it returns 0 which means that the class is unknown.
Warning
NanoEdge AI Library uses float
data types instead of int
. If you’re using ints, please convert (cast) them into floats.
iii. Getting status¶
uint8_t NanoEdgeAI_get_status_classifier(void)
This function returns the current status of the library.
Input: None
Output: The status of the library.
0
: the library is successfully deployed and is ready to be used.123
: the library is being used on a board that is not authorized (only for libraries that are limited to partner/authorized development boards).
3. Example “Hello World!”¶
Header files:
NanoEdgeAI.h and knowledge.h (provided in the .zip file that you download by clicking Compile in Step 6: “Deploy” of NanoEdge AI Studio)
Example of NanoEdge AI Library header file:
/* * NanoEdge AI header file */ /* Includes */ #include <stdint.h> /* Define */ #define NEAI_ID "5d5fd41ec8e0327c934" #define AXIS_NUMBER 3 #define DATA_INPUT_USER 256 #define CLASS_NUMBER /* Input and output buffers */ float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; float output_class_buffer[CLASS_NUMBER]; /* Array for mapping class id to class name */ const char *id2class[CLASS_NUMBER + 1] = { "unknown", "stop", "low", "medium", "high" }; /* Function prototypes */ #ifdef __cplusplus extern "C" { #endif uint8_t NanoEdgeAI_knowledge_init(const float knowledge_buffer[]); uint16_t NanoEdgeAI_classifier(float input_buffer[], float output_buffer[], uint8_t class_threshold); #ifdef __cplusplus } #endif
Important
- The input buffer collected from your sensor is already declared in the header file NanoEdgeAI.h,
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER];
, feel free to comment this line and rename and declare your input buffer in the main program. - The knowledge buffer
knowledge
is declared in the header file knowledge.h.
Main program: main.c
This program must be completed by the user (depending on the applications, the desired features…).
/* Includes -------------------------------------------------------------------*/ #include "NanoEdgeAI.h" #include "knowledge.h" /* Private define -------------------------------------------------------------*/ /* Private variables defined by user ------------------------------------------*/ /* Private function prototype defined by user ---------------------------------*/ /* * @brief Collect data process * * This function is defined by user, depends on applications and sensors * * @param sample_buffer: [in, out] buffer of sample values * * @retval None */ void get_buffer(float sample_buffer[]) { /* USER BEGIN */ /* USER END */ } /* ----------------------------------------------------------------------------*/ int main() { /* Initialization ----------------------------------------------------------*/ uint8_t error_code = NanoEdgeAI_knowledge_init(knowledge); if (error_code != 0) { /* This happens if the knowledge is not loaded. */ } /* Classification ----------------------------------------------------------*/ uint16_t id_class; while (true) { get_buffer(input_user_buffer); id_class = NanoEdgeAI_classifier(input_user_buffer, output_class_buffer, 0); /* USER BEGIN */ /* * e.g.: Trigger functions depending on id_class * (print output class probabilities using output_class_buffer[], * print the name of the identified class using id2class[id_class], * blink LED, ring alarm, etc.). */ /* USER END */ } }
Resources¶
Anomaly Detection:
Classification:
Useful links:
- Cartesiam's Website
- Download NanoEdge AI Studio
- For technical issues, please check ST Support.
- For general enquiries, please contact Edge AI Services.