此教程为目前全网为数不多用于使用STM32实时跑神经网络结果输出的教程,不涉及原理讲解,只为帮助想在stm32上使用神经网络的朋友
基本步骤为先跑出神经网络得到xxx.h5文件,然后用cubemx把h5文件移植到stm32中,然后初始化设置输入输出,实时输入数据进行神经网络检测,需要做的工作主要有:会写或者会改神经网络代码,输出.h5文件(使用Tesoroflow产生得有版本要求,太高得可能不行),使用cubemx来移植到stm32上;初始化输入输出来使之能把数据输入到神经网络进行运算。
1、 自行上网查询stm32AI教程,网上90%的教程会教到能把官方例程跑出来,使用cubemx进行验证(跑不出来,检查步骤有无错误,时钟设置是否正确,串口设置是否正确)
2、 但如果想实时跑数据进行神经网络检测,在Cubemx上能验证是没有用的,需要在代码中设置输入输出。
3、 根据我给得输入输出初始化函数来进行初始化自己得输入输出参数,然后进行验证。
使用STM32跑神经网络需要的初始化代码:
static void AI_Run(float* pIn, float* pOut) //神经网络实时计算代码,填入输入输出数组即可
{ai_i32 batch;ai_error err;/* 1 - Create the AI buffer IO handlers with the default definition */ai_buffer ai_input[AI_NETWORK_IN_NUM] = AI_NETWORK_IN;ai_buffer ai_output[AI_NETWORK_OUT_NUM] = AI_NETWORK_OUT;/* 2 - Update IO handlers with the data payload */ai_input[0].n_batches = 1;ai_input[0].data = AI_HANDLE_PTR(pIn);ai_output[0].n_batches = 1;ai_output[0].data = AI_HANDLE_PTR(pOut);batch = ai_network_run(network, ai_input, ai_output);if (batch != 1) {err = ai_network_get_error(network);printf("AI ai_network_run error - type=%d code=%d\r\n", err.type, err.code);
// Error_Handler();}
}static void AI_Init(ai_handle w_addr, ai_handle act_addr) //初始化代码
{ai_error err;printf("AI_NETWORK_IN_NUM=%d AI_NETWORK_OUT_NUM=%d\r\n", AI_NETWORK_IN_NUM, AI_NETWORK_OUT_NUM);/* 1 - Create an instance of the model */err = ai_network_create(&network, AI_NETWORK_DATA_CONFIG);if (err.type != AI_ERROR_NONE) {printf("ai_network_create error - type=%d code=%d\r\n", err.type, err.code);
// Error_Handler();}/* 2 - Initialize the instance */const ai_network_params params = AI_NETWORK_PARAMS_INIT(AI_NETWORK_DATA_WEIGHTS(w_addr),AI_NETWORK_DATA_ACTIVATIONS(act_addr));if (!ai_network_init(network, ¶ms)) {err = ai_network_get_error(network);printf("ai_network_init error - type=%d code=%d\r\n", err.type, err.code);
// Error_Handler();}
}
給出一个可参考的伪代码(参考使用方式,以下代码无法直接跑,只是示意)实例:
#include "sys.h"
#include "delay.h"/*bsp*/
#include "usart.h"
#include "led.h"
#include "key.h"
#include "timer.h"
#include "adc.h"
#include "arc_de.h"
#include "main.h"
#include "crc.h"
#include "usart.h"
#include "gpio.h"
#include "app_x-cube-ai.h"/*宏定义*//**********************全局变量****************************/
float team_data[3] = { 0 };
float Arc_data[DATASIZE][3] = { 0 }, FFt_Out[DATASIZE / 2] = { 0 }, minValue = 3, maxValue = 0, F_maxValue = 0, FFt_Out1[DATASIZE / 2] = { 0 }, Freqy = 0;
u16 adcx, i = 0;
u16 n = 0, m = 0;
float F_rate[3] = { 0 };
/* Reference index at which max energy of bin ocuurs */
uint32_t Index = 0;struct arc_eigenvalue arc_eig;
ai_handle network = AI_HANDLE_NULL;
/**********************函数申明****************************/
static void AI_Init(ai_handle w_addr, ai_handle act_addr);
static void AI_Run(float* pIn, float* pOut);void hard_init(void)
{}
int main(void)
{arm_rfft_fast_instance_f32 S;float aiInData[row][AI_NETWORK_IN_1_SIZE] = {};float aiOutData[AI_NETWORK_OUT_1_SIZE];ai_u8 activations[AI_NETWORK_DATA_ACTIVATIONS_SIZE];hard_init();
//初始化AIMX_CRC_Init();MX_X_CUBE_AI_Init();AI_Init(ai_network_data_weights_get(), activations);delay_ms(1);while (1){ AI_Run(aiInData[i], aiOutData);//实时运行}}#ifdef USE_FULL_ASSERTstatic void AI_Run(float* pIn, float* pOut)
{ai_i32 batch;ai_error err;/* 1 - Create the AI buffer IO handlers with the default definition */ai_buffer ai_input[AI_NETWORK_IN_NUM] = AI_NETWORK_IN;ai_buffer ai_output[AI_NETWORK_OUT_NUM] = AI_NETWORK_OUT;/* 2 - Update IO handlers with the data payload */ai_input[0].n_batches = 1;ai_input[0].data = AI_HANDLE_PTR(pIn);ai_output[0].n_batches = 1;ai_output[0].data = AI_HANDLE_PTR(pOut);batch = ai_network_run(network, ai_input, ai_output);if (batch != 1) {err = ai_network_get_error(network);printf("AI ai_network_run error - type=%d code=%d\r\n", err.type, err.code);}
}static void AI_Init(ai_handle w_addr, ai_handle act_addr)
{ai_error err;printf("AI_NETWORK_IN_NUM=%d AI_NETWORK_OUT_NUM=%d\r\n", AI_NETWORK_IN_NUM, AI_NETWORK_OUT_NUM);/* 1 - Create an instance of the model */err = ai_network_create(&network, AI_NETWORK_DATA_CONFIG);if (err.type != AI_ERROR_NONE) {printf("ai_network_create error - type=%d code=%d\r\n", err.type, err.code);}/* 2 - Initialize the instance */const ai_network_params params = AI_NETWORK_PARAMS_INIT(AI_NETWORK_DATA_WEIGHTS(w_addr),AI_NETWORK_DATA_ACTIVATIONS(act_addr));if (!ai_network_init(network, ¶ms)) {err = ai_network_get_error(network);printf("ai_network_init error - type=%d code=%d\r\n", err.type, err.code);}
}