cancel
Showing results for 
Search instead for 
Did you mean: 

NanoEdge AI 训练后出现异常数据相似度为零,但工程运行错误。

dw1
Associate III

你好,近期我在使用NanoEdge AI Studio实现异常检测功能,基于三轴加速度计的数据实现。我出现了以下问题:

1.我使用的最新版本5.0.2,在benchmark界面训练模型后,表格Best Library Result 中异常数据的结果为零,就是在表格的底部。但我生成代码需要的文件后运用到工程中其相似度一直输出为91%,即使我大幅度的改变加速度计的值也仍然时91%。

2.在有一次训练时,表格Best Library Result 中异常数据的结果不为零了,大概为68%。我生成需要的额NanoEdge AI.h和library.a以及knowledge.h生成后运用到keil工程中发现能够正常进行学习,并且也可不学习使用knowledge.h的这个数据集进行检测相似度,结果较为明显。只是原本的异常数据其相似度对应的也为68%。

3.我的疑惑在于,对应的模型训练不应该异常数据接近为零,正常数据接近一百吗,可我似乎于认为的不一样。并且同样的数据进行第一次训练的结果可能异常数据不为零,但后面几次训练后一直为零。导致的结果就是,训练结果异常数据为零的模型,在使用官方给的先进行学习一段时间,在进行异常检测运行效果是错误的,相似度一直输出为一个固定值,比如91%。并且运行不事先学习,利用knowledge这个数组作为学习数据,运行结果也是不正确的,但是训练结果异常数据不为零的就可正常运行。但这个结果也不是一直会出现的,偶尔出现。我的数据上传使用的是USB串口上传(USB转TTL、或者蓝牙串口)

 

以下为代码(省略了部分)

#include “NanoEdgeAI.h”
#include “knowledge.h”
#define LEARNING_ITERATIONS 30
浮点数 input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER];输入值的缓冲区


无效 SystemClock_Config(void);
静态 void MPU_Config(void);

void fill_buffer(float input_buffer[])
{
/* 用户开始 */
float acc_mg[3]; // 临时存储单个样本的X/Y/Z数据
uint16_t samples_collected = 0;
uint32_t last_tick = HAL_GetTick();

持续采集直到存满256个样本
while (samples_collected < DATA_INPUT_USER) {
// 1. 精确控制采样间隔(1000Hz = 1ms)
HAL_Delay(1);

//阿拉伯数字。读取加速度数据
if (LSM6DSV16X_Read6AxisData(&lsm6dsv16x_dev_ctx, acc_mg, NULL) & 0x01) {

// 3. 填充到NanoEdge缓冲区(按X,Y,Z顺序交替存储)
input_buffer[samples_collected * AXIS_NUMBER + 0] = acc_mg[0]; // X
input_buffer[samples_collected * AXIS_NUMBER + 1] = acc_mg[1]; // Y
input_buffer[samples_collected * AXIS_NUMBER + 2] = acc_mg[2]; // Z
samples_collected++;
}
}
/* 用户结束 */

}

 


int main(void) (无效)
{

MPU_Config();

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
MX_USART1_UART_Init();
MX_CRC_Init();
/* 用户代码开始 2 */
GPS_Init(&huart1, &huart2);UART1接GPS,UART2接PC
MagIMU_Fusion_Init();
MX_MEMS_Init();

/* 初始化 ------------------------------------------------------------*/
enum neai_state error_code = neai_anomalydetection_init();

if (error_code != NEAI_OK) {
printf(“错误!\r\n”);
/* 如果库在不支持的板子中工作,则会发生这种情况。*/
}
printf(“学习开始!\r\n”);
/* 学习过程 ----------------------------------------------------------*/
for (uint16_t iteration = 0 ; iteration < LEARNING_ITERATIONS ; iteration++) {
fill_buffer(input_user_buffer);
printf(“正在学习 %d/%d!\r\n”,迭代,LEARNING_ITERATIONS);
HAL_Delay(50);
neai_anomalydetection_learn(input_user_buffer);
}
neai_anomalydetection_knowledge(知识);
printf(“学习结束!\r\n”);
uint32_t last_print_time = HAL_GetTick();
uint8_t相似度 = 0;
while (1)
{
1. 填充数据并检测
fill_buffer(input_user_buffer);
neai_anomalydetection_detect(input_user_buffer, &similarity);

//阿拉伯数字。定期打印相似度(避免刷屏)
if (HAL_GetTick() - last_print_time >= 1000) {
last_print_time = HAL_GetTick();


            // 打印带时间戳的相似度信息
            printf("[%lu ms] similarity: %d%%\r\n", 
                 (unsigned long) HAL_GetTick(), 
                  similarity);
        }
        HAL_Delay(100); // 控制检测频

  }
}

13 REPLIES 13

Hello @dw1,

 

The shape of data you get from the TOF VL53L5CX  is indeed one frame of size 64.

 

Concerning your other issue. I think you may not be correctly using this function.

 

In Anomaly detection, you get a untrained library as output of NanoEdge AI Studio.

The steps to use it then in your embedded project are:

  1. To init the library with the init() function
  2. To give the model knowledge (to train it). To do so you have 2 choices:
    1. You can get the "knowledge" from NanoEdge AI Studio, if you load this, you will get the exact same library that you got in the benchmark. To do so, you need to check the option to get the knowledge and re compile the library:
      JulianE_1-1751615181127.png
      In the .zip, you will get a new file to add to your embedded project. Finally, you can call the function:
      neai_anomalydetection_knowledge(knowledge)​
      (So to be clear, here you don't use TOF data, you just link the knowledge file in your project). 

    2. You can also choose to learn new data instead of using the knowledge. It allows you to get data for the training that are coming from your real final environment. A good example is, let's say, you train a model for a motor, then you want to deploy it on 10 other motors of the same kinds. If you retrain the model on each motor specifically, each model will better fit their final environments.
      To do so, you need to use the function:
      enum neai_state neai_anomalydetection_learn(float data_input[]);​

      For the learning/training, you need to use normal data only. You can create a loop to use this function multiple times (in NanoEdge, you should find the minimum recommended number of learning.)

    3. You can do both, you could load the knowledge of the benchmark and also learn new signals if you want.

 

I would suggest looking at this tutorial:

AI:How to Build an Anomaly Detection Project for Predictive Maintenance with NanoEdge AI Studio - stm32mcu

Here we use the learn function, not the knowledge.

 

You can also look at this example for N class classification if you want to see how the knowledge is used:

AI:How to create a current sensing classifier using NanoEdge AI Studio - stm32mcu

 

Learning is only available for Anomaly detection, which mean that in all other kinds of project, you need to use the knowledge.

 

Could you also please tell me, what your previous issue was in the end and how you managed to solve it please?

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Dear Julian E.,

Thank you for your previous reply. I have resolved the issue with linking the knowledge file—the error was due to an incorrect path for the libneai.a file, as I had mistakenly selected an old file path.

Regarding the earlier problem, I believe there’s an 80% chance it was caused by the small difference between normal and abnormal data. However, it’s also possible that the data storage type was incorrect, as I recently encountered the same issue due to this mistake.

Now, I have a new question. Currently, I have two types of data:

  1. Accelerometer triaxial data for fall detection.

  2. VL53L5CX data for obstacle detection.

The training processes for these are separate. However, the generated .zip files contain detection functions with the same name. How can I distinguish (or separately use) them, especially since their data buffer sizes are different?

I’d appreciate your help with this latest issue. Looking forward to your reply, and I hope you have a great day!

Best regards,

dw1.

Hello @dw1,

 

Thank you for the additional information.

 

To use multiples library, you can give them names in the last NanoEdge step:

JulianE_0-1751617620146.png

Then the functions will have a prefixe, like fall_detection_anomalydetection_init().

 

So you need to give them names, get the new files and uses these new ones in your project.

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Dear Julian,

Thank you so much for your prompt and helpful reply! Your guidance has been incredibly valuable, and I truly appreciate your support. I will try your suggested solution right away.

Wishing you a wonderful day!

Best regards,
dw1.