NanoEdge AI Library for 1-class classification (1CC)

Revision as of 21:33, 19 November 2021 by Registered User

XXXXX RENAME THIS PAGE : AI:NanoEdge AI Library for 1-class classification (1CC) (with a SPACE between "NanoEdge" and "AI") XXXXX

1. What is NanoEdge AI Library for 1-class classification (outlier detection)?

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 "identify" outliers from 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 1-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 identify if a sensor pattern is inside the class or outside the class (outlier/anomaly). The class under consideration is 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
The library is not available in the free version of the Studio.

2. Install / Getting started

The main functions available via the library are:

oneclass_init() run first before the classification process to load the knowledge
oneclass() run a classification iteration to identify if the pattern is inside or outside the class (inference)
status() returns the current status of the library

2.1. Static library

The static NanoEdge AI Library is only available via NanoEdge AI Studio in its full version.

  • In NanoEdge AI Studio, after obtaining a library, click Compile (on the "Deploy" screen, which follows the "Optimize and 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

2.2.1. Initialization

enum neai_state neai_oneclass_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).

For more details on the output of the initialization function, refer to the header file NanoEdgeAI.h provided in the .zip file containing the static NanoEdge AI Library.

2.2.2. 1-class classification

uint8_t neai_oneclass(float input_buffer[]);

This function returns the class identifier.

  • Input:
float input_buffer[], the length of the buffer is DATA_INPUT_USER * AXIS_NUMBER.
  • Output:
0 means the input pattern is inside the class under consideration.
1 means the input pattern is outside the class under consideration (outlier/anomaly).
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.2.3. Getting status

enum neai_state neai_oneclass_status(void);

This function returns the current status of the library.

  • Input: None
  • Output: The status of the library. For more details refer to the header file NanoEdgeAI.h provided in the .zip file containing the static NanoEdge AI Library.

2.3. 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:

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.*
*/

#ifndef __NANOEDGEAI_H__
#define __NANOEDGEAI_H__

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

/* Define */

#define AXIS_NUMBER 3
#define DATA_INPUT_USER 512

enum neai_state { 
    OK = 0,
    INIT_FCT_NOT_CALLED = 123,
    BOARD_ERROR,
    KNOWLEDGE_BUFFER_ERROR,
    NOT_ENOUGH_CALL_TO_LEARNING_FCT, //This is a fail-safe to prevent users from learning one or even no signals.
    UNKNOWN_ERROR};

/* Input buffer */
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER];

/* Function prototypes */
#ifdef __cplusplus
extern "C" {
#endif
	enum neai_state neai_oneclass_init(const float knowledge_buffer[]);
	uint8_t neai_oneclass(float input_buffer[]);
	enum neai_state neai_oneclass_status(void);
#ifdef __cplusplus
}
#endif

#endif
Warning DB.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.*
*/
/* 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 -----------------------------------------------------------*/
	enum NanoEdgeAI_state error_code = neai_oneclass_init(knowledge);
	if (error_code != OK) {
		/* This happen if the knowledge do not correspond to the library or if the library work into a not supported board. */
	}
	/* oneclass -----------------------------------------------------------*/
	uint8_t oneclass_result;
	while (true) {
		get_buffer(input_user_buffer);
		outlier_result = neai_oneclass(input_user_buffer);
		/* USER BEGIN */
		/*
		* e.g.: Trigger functions depending on outlier_result
		* (blink LED, ring alarm, etc.).
		*/
		/* USER END */
	}
}

3. Resources

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.