Difference between revisions of "AI:NanoEdge AI Library for n-class classification (nCC)"

[unchecked revision] [quality revision]
m (Example "Hello World!")
m (Example "Hello World!")
 
Warning white.png Warning

Major update for NanoEdge AI v3.
Pay close attention to the new function names, new arguments, and new return types.


1 What is NanoEdge AI Library for n-class classification?[edit]

NanoEdge™ AI Library is an Artificial Intelligence (AI) static library originally developed by Cartesiam, for embedded C software running on Arm® Cortex® microcontrollers.

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 n-class classification is the code that contains an AI model (for example, as a bundle of signal treatment, Machine Learning model, and optimally tuned hyperparameters) designed to determine which class a given sensor pattern belongs to. All classes are defined by the user in NanoEdge AI Studio (NanoEdgeAIStudio), also called the Studio, and are used during the training process of the AI model.

Info white.png Information

In the free version of NanoEdgeAIStudio the NanoEdge AI library is only compatible with a selection of STM32 evaluation boards. See the full list here in the Studio documentation.

2 Install / Getting started[edit]

The main functions available via the library are:

init() run first before the classification process to load the knowledge
classification() run a classification iteration (inference)

2.1 Static library[edit]

  • In NanoEdge AI Studio, after obtaining a library, click Compile (on the "Deployment" screen, which follows the "Benchmark" and "Emulator" screens)
  • Open the .zip file obtained
  • Select and copy the static library libneai.a
  • Link this static library to your project code

2.2 NanoEdge AI Library functions[edit]

Most NanoEdge AI function return the status of the library in the following enum, neai_state:

enum neai_state { 
    NEAI_OK = 0,
    NEAI_INIT_FCT_NOT_CALLED = 123,
    NEAI_BOARD_ERROR,
    NEAI_KNOWLEDGE_BUFFER_ERROR,
    NEAI_NOT_ENOUGH_CALL_TO_LEARNING,
    NEAI_UNKNOWN_ERROR};

Here are the possible statuses:

NEAI_OK: the library is working as expected
NEAI_INIT_FCT_NOT_CALLED: the learn or detect function has been called without running the init function before. Initialize your library.
NEAI_BOARD_ERROR: the board detected is not authorized. For instance, it may happen if you are trying to use a limited library (for instance obtained from the free version of NanoEdge AI Studio) with a non-supported STM32 board.
NEAI_KNOWLEDGE_BUFFER_ERROR: the knowledge loaded is not compatible with this library. Make sure that the knowledge being used is the one obtained with this exact library.
NEAI_NOT_ENOUGH_CALL_TO_LEARNING: this is a fail-safe to prevent users from running an insufficient (only one or a few) number of iterations of the learning function. Run more learning iterations.
NEAI_UNKNOWN_ERROR: there is an unknown error with the library.
Info white.png Information

Although it is present in all NanoEdge AI header files, the NEAI_NOT_ENOUGH_CALL_TO_LEARNING status is only relevant for anomaly detection libraries.

2.2.1 Initialization[edit]

enum neai_state neai_classification_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:
the neai_state enum (NEAI_OK == 0, in case of success).

2.2.2 Classification[edit]

enum neai_state neai_classification(float input_buffer[], float output_buffer[], uint16_t *id_class);

This function returns the class identifier.

  • Input:
float input_buffer[], the length of the buffer is DATA_INPUT_USER * AXIS_NUMBER.
float output_buffer[], the length of the buffer is CLASS_NUMBER .
uint16_t *id_class, the variable that contains the class ID returned by the function, as a result of inference.
  • Output:
The class identifier, to which the input has been attributed with the highest probability.
The neai_state enum (NEAI_OK == 0, in case of success).
Info white.png Information
  • The uint16_t *id_class variable must be defined prior to calling the detection function, and pointed to using &id_class when passed as an argument (see code example below).
  • The class identifier returned by the function is a number between 0 and CLASS_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 all classes.
  • The sum of the all class probabilities is equal to 1.
Warning white.png Warning

NanoEdge AI Library uses float data types instead of int. If you are using intdata types, convert (cast) them into float.

2.3 Example "Hello World!"[edit]

Header files:

NanoEdgeAI.h and knowledge.h (provided in the .zip file that you download by clicking Compile in NanoEdge AI Studio (on the "Deploy" screen after obtaining a library)

Example of NanoEdge AI Library header file:

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Application.
/* =============
Copyright (c) 2021, STMicroelectronics

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
  products derived from this software without specific prior written permission.

*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER / OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*
*/

#ifndef __NANOEDGEAI_H__
#define __NANOEDGEAI_H__

/* Includes */
#include <stdint.h>

/* Define */

#define AXIS_NUMBER 3
#define DATA_INPUT_USER 512
#define CLASS_NUMBER 45

#ifndef __NEAI_STATE__
#define __NEAI_STATE__
enum neai_state { 
    NEAI_OK = 0,
    NEAI_INIT_FCT_NOT_CALLED = 123,
    NEAI_BOARD_ERROR,
    NEAI_KNOWLEDGE_BUFFER_ERROR,
    NEAI_NOT_ENOUGH_CALL_TO_LEARNING, //This is a fail-safe to prevent users from learning one or even no signals.
    NEAI_UNKNOWN_ERROR};
#endif
/* Function prototypes */
#ifdef __cplusplus
extern "C" {
#endif
	enum neai_state neai_classification_init(const float knowledge_buffer[]);
	enum neai_state neai_classification(float input_buffer[], float output_buffer[], uint16_t *id_class);
#ifdef __cplusplus
}
#endif

#endif

/* =============
Here some sample declarationdeclarations to be added in your main program for the use of the NanoEdge AI library.
You can directly copy these declarations or modify the names.
* WARNING: respect the sizes of the different buffers.

uint16_t id_class = 0; // Point to class id class (see argument of neai_classification fct)
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
	"Class 1",
	"Class 2",
	"Class 3",
	"Class 4",
	"Class 5",
};
*/

Warning white.png 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 then 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 or the desired features).

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Application.
/* =============
Copyright (c) 2020, STMicroelectronics

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
  products derived from this software without specific prior written permission.

*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER / OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*
*/
/**
  **************************************************************************
  * Demo: NanoEdge AI process to include in main program body
  *
  * @note  This program must be completed and customized by the user
  **************************************************************************
  */

/* Includes --------------------------------------------------------------------*/
#include "NanoEdgeAI.h"
#include "knowledge.h"
/* Private define --------------------------------------------------------------*/
/* Private variables defined by user -------------------------------------------*/
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
	"Class 1",
	"Class 2",
	"Class 3",
	"Class 4",
	"Class 5",
};
/* Private function prototypes 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
 * @note   If AXIS_NUMBER = 3 (cf NanoEdgeAI.h), the buffer must be
 *         ordered as follow:
 *         [x0 y0 z0 x1 y1 z1 ... xn yn zn], where xi, yi and zi
 *         are the values for x, y and z axes, n is equal to
 *         DATA_INPUT_USER (cf NanoEdgeAI.h)
 */
void fill_buffer(float sample_buffer[])
{
	/* USER BEGIN */
	/* USER END */
}
/* -----------------------------------------------------------------------------*/
int main(void)
{
	/* Initialization ------------------------------------------------------------*/
	enum neai_state error_code = neai_classification_init(knowledge);
	if (error_code != NEAI_OK) {
		/* This happens if the knowledge does not correspond to the library or if the library works into a not supported board. */
	}

	/* Classification ------------------------------------------------------------*/
	uint16_t id_class = 0;
	while (1) {
		fill_buffer(input_user_buffer);
		neai_classification(input_user_buffer, output_class_buffer, &id_class);
		/* 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 */
	}
}

/* =============
Here some sample declarations to be added in your main program for the use of the NanoEdge AI library.
You can directly copy these declarations or modify the names.
* WARNING: respect the sizes of the different buffers.

uint16_t id_class = 0; // Point to class id (see argument of neai_classification fct)
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
	"Class 1",
	"Class 2",
	"Class 3",
	"Class 4",
	"Class 5",
};
*/

3 Resources[edit]

Documentation
All NanoEdge AI Studio documentation is available here.

Tutorials
Step-by-step tutorials, to use NanoEdge AI Studio to build a smart device from A to Z.



{{Warning|
'''Major update''' for NanoEdge AI v3.<br />

Pay close attention to the '''new function names''', '''new arguments''', and '''new return types.'''
}}

==What is NanoEdge AI Library for n-class classification? ==
NanoEdge™ AI Library is an Artificial Intelligence (AI) static library originally developed by Cartesiam, for embedded C software running on Arm<sup>®</sup> Cortex<sup>®</sup> microcontrollers.

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 n-class classification is the code that contains an AI model (for example, as a bundle of signal treatment, Machine Learning model, and optimally tuned hyperparameters) designed to determine which class a given sensor pattern belongs to. All classes are defined by the user in NanoEdge AI Studio ([https://www.st.com/en/product/nanoedgeaistudio NanoEdgeAIStudio]), also called the Studio, and are used during the training process of the AI model.

{{Info|
In the free version of [https://www.st.com/en/product/nanoedgeaistudio NanoEdgeAIStudio] the NanoEdge AI library is only compatible with a selection of STM32 evaluation boards. See the [[AI:NanoEdge_AI_Studio#Project_settings | full list here]] in the Studio documentation.
}}

==Install / Getting started ==
The main functions available via the library are:

{|class="st-table"
| <code>init()</code>

| run first before the classification process to load the knowledge
|-
| <code>classification()</code>

| run a classification iteration (inference)
|}

===Static library ===

* In NanoEdge AI Studio, after obtaining a library, click Compile (on the "Deployment" screen, which follows the "Benchmark" and "Emulator" screens)
* Open the ''.zip'' file obtained
* Select and copy the static library <code>libneai.a</code>

* Link this static library to your project code

===NanoEdge AI Library functions ===
Most NanoEdge AI function return the status of the library in the following enum, <code>neai_state</code>:
<syntaxhighlight lang="c">

enum neai_state { 
    NEAI_OK = 0,
    NEAI_INIT_FCT_NOT_CALLED = 123,
    NEAI_BOARD_ERROR,
    NEAI_KNOWLEDGE_BUFFER_ERROR,
    NEAI_NOT_ENOUGH_CALL_TO_LEARNING,
    NEAI_UNKNOWN_ERROR};</syntaxhighlight>


Here are the '''possible statuses''':
::<code>NEAI_OK</code>: the library is working as expected
::<code>NEAI_INIT_FCT_NOT_CALLED</code>: the learn or detect function has been called without running the init function before. Initialize your library.
::<code>NEAI_BOARD_ERROR</code>: the board detected is not authorized. For instance, it may happen if you are trying to use a limited library (for instance obtained from the free version of NanoEdge AI Studio) with a non-supported STM32 board.
::<code>NEAI_KNOWLEDGE_BUFFER_ERROR</code>: the knowledge loaded is not compatible with this library. Make sure that the knowledge being used is the one obtained with this exact library.
::<code>NEAI_NOT_ENOUGH_CALL_TO_LEARNING</code>: this is a fail-safe to prevent users from running an insufficient (only one or a few) number of iterations of the learning function. Run more learning iterations.
::<code>NEAI_UNKNOWN_ERROR</code>: there is an unknown error with the library.

{{Info|
Although it is present in all NanoEdge AI header files, the  ''NEAI_NOT_ENOUGH_CALL_TO_LEARNING'' status is only relevant for anomaly detection libraries.
}}

====Initialization ====
<syntaxhighlight lang="c">

enum neai_state neai_classification_init(const float knowledge_buffer[]);</syntaxhighlight>


Initialization must be called at the beginning to load the knowledge.

*'''Input:'''
::<code>const float knowledge_buffer[]</code>, this buffer is defined in the header file ''knowledge.h'' provided in the ''.zip'' file containing the static NanoEdge AI Library.
*'''Output:'''
:: the <code>neai_state</code> enum (NEAI_OK == 0, in case of success).

====Classification ====<syntaxhighlight lang="c">

enum neai_state neai_classification(float input_buffer[], float output_buffer[], uint16_t *id_class);</syntaxhighlight>

This function returns the class identifier.

*'''Input:'''
::<code>float input_buffer[]</code>, the length of the buffer is <code>DATA_INPUT_USER * AXIS_NUMBER</code>.<br />

::<code>float output_buffer[]</code>, the length of the buffer is <code>CLASS_NUMBER</code> .<br />

::<code>uint16_t *id_class</code>, the variable that contains the class ID returned by the function, as a result of inference.
*'''Output:'''
::The class identifier, to which the input has been attributed with the highest probability.
::The <code>neai_state</code> enum (NEAI_OK == 0, in case of success).

{{Info|
* The <code>uint16_t *id_class</code> variable must be defined prior to calling the detection function, and pointed to using <code>&id_class</code> when passed as an argument (see [[#Example_-22Hello_World-21-22 | code example]] below).
* The class identifier returned by the function is a number between <code>0</code> and <code>CLASS_NUMBER</code>.
* The user can easily get the name of the class associated with the identifier returned by the function using the array <code>id2class[]</code>, defined in the header file ''NanoEdgeAI.h''. The name of classes is defined by the user in the Studio.
* <code>output_buffer[]</code> contains the probabilities of all classes.
* The sum of the all class probabilities is equal to 1.
}}

{{Warning|
NanoEdge AI Library uses <code>float</code> data types instead of <code>int</code>. If you are using <code>int</code>data types, convert (cast) them into <code>float</code>.
}}

===Example "Hello World!" ===
'''Header files:'''

''NanoEdgeAI.h'' and ''knowledge.h'' (provided in the ''.zip'' file that you download by clicking ''Compile'' in NanoEdge AI Studio (on the "Deploy" screen after obtaining a library)

'''Example of NanoEdge AI Library header file:'''

{{Snippet | category=AI | component=Application | snippet=<syntaxhighlight lang="c">

/* =============
Copyright (c) 2021, STMicroelectronics

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
  products derived from this software without specific prior written permission.

*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER / OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*
*/

#ifndef __NANOEDGEAI_H__
#define __NANOEDGEAI_H__

/* Includes */
#include <stdint.h>


/* Define */

#define AXIS_NUMBER 3
#define DATA_INPUT_USER 512
#define CLASS_NUMBER 45


#ifndef __NEAI_STATE__
#define __NEAI_STATE__
enum neai_state { 
    NEAI_OK = 0,
    NEAI_INIT_FCT_NOT_CALLED = 123,
    NEAI_BOARD_ERROR,
    NEAI_KNOWLEDGE_BUFFER_ERROR,
    NEAI_NOT_ENOUGH_CALL_TO_LEARNING, //This is a fail-safe to prevent users from learning one or even no signals.
    NEAI_UNKNOWN_ERROR};
#endif
/* Function prototypes */
#ifdef __cplusplus
extern "C" {
#endif
	enum neai_state neai_classification_init(const float knowledge_buffer[]);
	enum neai_state neai_classification(float input_buffer[], float output_buffer[], uint16_t *id_class);
#ifdef __cplusplus
}
#endif

#endif

/* =============
Here some sample declaration declarations to be added in your main program for the use of the NanoEdge AI library.
You can directly copy these declarations or modify the names.
* WARNING: respect the sizes of the different buffers.

uint16_t id_class = 0; // Point to class id class (see argument of neai_classification fct)
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities*/const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
	"Class 1",
	"Class 2",
	"Class 3",
	"Class 4",
	"Class 5",
};
*/</syntaxhighlight>

}}

{{Important|
* The input buffer collected from your sensor is already declared in the header file ''NanoEdgeAI.h'', <code>float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER];</code>, feel free to comment this line, and then rename and declare your input buffer in the main program.
* The knowledge buffer <code>knowledge</code> is declared in the header file ''knowledge.h''.}}

'''Main program:''' ''main.c''

This program must be completed by the user (depending on the applications or the desired features).
{{Snippet | category=AI | component=Application | snippet=<syntaxhighlight lang="c">

/* =============
Copyright (c) 2020, STMicroelectronics

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
  products derived from this software without specific prior written permission.

*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER / OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*
*/
/**
  **************************************************************************
  * Demo: NanoEdge AI process to include in main program body
  *
  * @note  This program must be completed and customized by the user
  **************************************************************************
  */

/* Includes --------------------------------------------------------------------*/
#include "NanoEdgeAI.h"
#include "knowledge.h"
/* Private define --------------------------------------------------------------*/
/* Private variables defined by user -------------------------------------------*/
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities/const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
	"Class 1",
	"Class 2",
	"Class 3",
	"Class 4",
	"Class 5",
};
/* Private function prototypes 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
 * @note   If AXIS_NUMBER = 3 (cf NanoEdgeAI.h), the buffer must be
 *         ordered as follow:
 *         [x0 y0 z0 x1 y1 z1 ... xn yn zn], where xi, yi and zi
 *         are the values for x, y and z axes, n is equal to
 *         DATA_INPUT_USER (cf NanoEdgeAI.h)
 */
void fill_buffer(float sample_buffer[])
{
	/* USER BEGIN */
	/* USER END */
}
/* -----------------------------------------------------------------------------*/
int main(void)
{
	/* Initialization ------------------------------------------------------------*/
	enum neai_state error_code = neai_classification_init(knowledge);
	if (error_code != NEAI_OK) {
		/* This happens if the knowledge does not correspond to the library or if the library works into a not supported board. */
	}

	/* Classification ------------------------------------------------------------*/
	uint16_t id_class = 0;
	while (1) {
		fill_buffer(input_user_buffer);
		neai_classification(input_user_buffer, output_class_buffer, &id_class);
		/* 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 */
	}
}

/* =============
Here some sample declarations to be added in your main program for the use of the NanoEdge AI library.
You can directly copy these declarations or modify the names.
* WARNING: respect the sizes of the different buffers.

uint16_t id_class = 0; // Point to class id (see argument of neai_classification fct)
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
	"Class 1",
	"Class 2",
	"Class 3",
	"Class 4",
	"Class 5",
};
*/

</syntaxhighlight>

}}

== Resources ==
'''Documentation'''<br />

All NanoEdge AI Studio documentation is [[Artificial Intelligence overview | available here]].

'''Tutorials'''<br />

[[AI:NanoEdge_AI_examples | Step-by-step tutorials]], to use NanoEdge AI Studio to build a smart device from A to Z.
<noinclude>

{{PublicationRequestId | 21757 | 2021-11-22 |
Old TLMS request: <br>

* version1: 21523 (2021-11-03)}}
[[Category:NanoEdgeAI|30]]</noinclude>
Line 143: Line 143:
 
#define AXIS_NUMBER 3
 
#define AXIS_NUMBER 3
 
#define DATA_INPUT_USER 512
 
#define DATA_INPUT_USER 512
#define CLASS_NUMBER 4
+
#define CLASS_NUMBER 5
   
 
#ifndef __NEAI_STATE__
 
#ifndef __NEAI_STATE__
Line 168: Line 168:
   
 
/* =============
 
/* =============
Here some sample declaration added in your main program for the use of the NanoEdge AI library.
+
Here some sample declarations to be added in your main program for the use of the NanoEdge AI library.
 
You can directly copy these declarations or modify the names.
 
You can directly copy these declarations or modify the names.
 
* WARNING: respect the sizes of the different buffers.
 
* WARNING: respect the sizes of the different buffers.
   
uint16_t id_class = 0; // Point to id class (see argument of neai_classification fct)
+
uint16_t id_class = 0; // Point to class id (see argument of neai_classification fct)
 
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
 
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
 
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
 
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
  +
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
  +
"unknown",
  +
"Class 1",
  +
"Class 2",
  +
"Class 3",
  +
"Class 4",
  +
"Class 5",
  +
};
 
*/
 
*/
 
</syntaxhighlight >
 
</syntaxhighlight >
Line 229: Line 237:
 
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
 
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
 
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
 
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
  +
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
  +
"unknown",
  +
"Class 1",
  +
"Class 2",
  +
"Class 3",
  +
"Class 4",
  +
"Class 5",
  +
};
 
/* Private function prototypes defined by user ---------------------------------*/
 
/* Private function prototypes defined by user ---------------------------------*/
 
/*
 
/*
Line 272: Line 288:
 
}
 
}
 
}
 
}
 
/* =============
 
Here some sample declarations to be added in your main program for the use of the NanoEdge AI library.
 
You can directly copy these declarations or modify the names.
 
* WARNING: respect the sizes of the different buffers.
 
 
uint16_t id_class = 0; // Point to class id (see argument of neai_classification fct)
 
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
 
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
 
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
 
"unknown",
 
"Class 1",
 
"Class 2",
 
"Class 3",
 
"Class 4",
 
"Class 5",
 
};
 
*/
 
 
 
 
 
</syntaxhighlight >
 
</syntaxhighlight >
 
}}
 
}}