XM-01/XM-01/Core/Src/myEdge_ai_app.c
2025-05-19 17:28:19 +08:00

137 lines
6.9 KiB
C

#include "usart.h"
#include "gpio.h"
#include "math.h"
#include "rtthread.h"
#include "myEdge_ai_app.h"
#include "app_x-cube-ai.h"
ai_handle network;
AI_ALIGNED(32)
float aiInData[AI_MODEL_IN_1_SIZE];
AI_ALIGNED(32)
float aiOutData[AI_MODEL_OUT_1_SIZE];
AI_ALIGNED(32)
ai_u8 activations[AI_MODEL_DATA_ACTIVATIONS_SIZE];
ai_buffer *ai_input;
ai_buffer *ai_output;
AI_ALIGNED(32)
const uint8_t test[1024] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 46, 99, 64, 42, 20, 7, 0, 0, 3, 5, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 18, 46, 178, 171, 223, 198, 210, 147, 30, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 56, 78, 79, 84, 62, 77, 109, 188, 138, 97, 61, 18, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 35, 60, 78, 55, 30, 35, 33, 35, 6, 0, 7, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 17, 25, 20, 16, 20, 5, 0, 5, 18, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 60, 32, 27, 23, 10, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 16, 46, 37, 46, 38, 12, 6, 0, 0, 0, 15, 4, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 7, 17, 51, 40, 45, 43, 49, 28, 15, 5, 6, 9, 11, 32, 11, 10, 37, 54, 15, 9, 24, 15, 10, 4, 4, 7, 10, 61, 0, 0, 0, 5, 12, 43, 52, 39, 61, 62, 36, 24, 13, 9, 5, 9, 18, 33, 30, 17, 55, 33, 37, 97, 100, 93, 37, 73, 53, 45, 24, 105, 0, 0, 0, 7, 23, 62, 85, 50, 58, 42, 38, 28, 18, 21, 11, 14, 20, 49, 59, 46, 68, 103, 118, 184, 72, 47, 24, 9, 26, 120, 0, 170, 0, 0, 4, 14, 53, 79, 88, 56, 80, 48, 43, 41, 23, 17, 13, 33, 39, 12, 6, 15, 45, 53, 31, 90, 105, 125, 59, 36, 32, 10, 9, 6, 0, 0, 3, 14, 25, 57, 83, 43, 26, 23, 16, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 39, 55, 26, 25, 24, 20, 17, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 29, 77, 58, 45, 29, 23, 38, 30, 8, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 14, 32, 45, 51, 46, 56, 49, 32, 31, 26, 29, 7, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 95, 96, 93, 99, 104, 108, 88, 63, 130, 64, 35, 13, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 21, 64, 89, 90, 90, 94, 104, 99, 84, 72, 31, 19, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 24, 64, 154, 128, 150, 179, 150, 167, 120, 76, 68, 33, 12, 10, 7, 14, 0, 0, 5, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 34, 85, 119, 70, 75, 131, 160, 152, 88, 54, 40, 28, 8, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 42, 79, 51, 46, 59, 73, 72, 72, 100, 75, 89, 74, 42, 12, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 15, 18, 18, 29, 40, 40, 36, 43, 56, 88, 57, 22, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 40, 46, 57, 52, 53, 56, 65, 35, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 36, 51, 85, 72, 73, 88, 67, 61, 49, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 19, 56, 55, 53, 28, 36, 39, 24, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 14, 35, 64, 79, 64, 89, 44, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 24, 53, 95, 92, 69, 20, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 20, 73, 159, 109, 98, 34, 14, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 22, 55, 51, 69, 49, 49, 23, 11, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 33, 54, 64, 72, 66, 21, 13, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 18, 32, 35, 76, 64, 18, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 15, 25, 25, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
AI_ALIGNED(32)
float inputBuf[1024] = {0.0f};
void PictureCharArrayToFloat(const uint8_t *srcBuf, float *dstBuf, int len)
{
for (int i = 0; i < len; i++)
{
dstBuf[i] = srcBuf[i]; //==1?0:1;
}
}
#define CLIP_MIN 0.0f
#define CLIP_MAX 300.0f
#define EPSILON 1e-6f
#define INPUT_SIZE 1024
// Preprocess a single sample (input: array of 1024 floats, output: array of 1024 floats)
float clipped[INPUT_SIZE] = {0};
float sqrted[INPUT_SIZE] = {0};
void preprocess_data(const float *input, float *output, size_t length)
{
float mean = 0.0f, stddev = 0.0f;
// 1. Clip
for (size_t i = 0; i < length; ++i)
{
float val = input[i];
if (val < CLIP_MIN)
val = CLIP_MIN;
if (val > CLIP_MAX)
val = CLIP_MAX;
clipped[i] = val;
}
// 2. Sqrt(x+1)
for (size_t i = 0; i < length; ++i)
{
sqrted[i] = sqrtf(clipped[i] + 1.0f);
}
// 3. Local Normalization (mean/std)
// Compute mean
for (size_t i = 0; i < length; ++i)
{
mean += sqrted[i];
}
mean /= (float)length;
// Compute stddev
for (size_t i = 0; i < length; ++i)
{
float diff = sqrted[i] - mean;
stddev += diff * diff;
}
stddev = sqrtf(stddev / (float)length) + EPSILON;
// 4. Normalize, tanh, scale to [0,1]
for (size_t i = 0; i < length; ++i)
{
float normed;
if (stddev > EPSILON)
{
normed = (sqrted[i] - mean) / stddev;
}
else
{
normed = sqrted[i] - mean;
}
// 4. Tanh
float tanh_val = tanhf(normed);
// 5. Scale [0,1]
output[i] = (tanh_val + 1.0f) / 2.0f;
}
}
////////////////////////////////////////////////////////////////////////////////////////
int aiInit(void)
{
ai_error err;
/* Create and initialize the c-model */
const ai_handle acts[] = {activations};
err = ai_model_create_and_init(&network, acts, NULL);
if (err.type != AI_ERROR_NONE)
{
rt_kprintf("ai_network_create error - type=%d code=%d\r\n", err.type, err.code);
}
/* Reteive pointers to the model's input/output tensors */
ai_input = ai_model_inputs_get(network, NULL);
ai_output = ai_model_outputs_get(network, NULL);
return 0;
}
/*
* Run inference
*/
void aiRun(const void *in_data, void *out_data)
{
ai_i32 batch;
ai_error err;
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////模型运算/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/* Update IO handlers with the data payload */
ai_input[0].data = AI_HANDLE_PTR(in_data);
ai_output[0].data = AI_HANDLE_PTR(out_data);
batch = ai_model_run(network, ai_input, ai_output);
if (batch != 1)
{
err = ai_model_get_error(network);
rt_kprintf("AI ai_network_run error - type=%d code=%d\r\n", err.type, err.code);
}
///////////////////////////////////////////////////////////////////////////////////
}