2023-04-22 03:18 AM - last edited on 2024-10-01 03:02 AM by Julian E.
I have written my neural network code and converted it into C language code using STM32CubeMX AI tool. I copied the generated three source code (Network. c, network_data.c and Network_data_ams.c) and the relevant header files into my original project. Compile successfully without error message. However, after downloading my code to my hardware device, I failed to initialize AI by using msh command, ai_error_type:51, ai_error_code:65.
Here is the relevant AI execution code I added with the msh command.
/* Global handle to reference the instantiated C-model */
static ai_handle network = AI_HANDLE_NULL;
/* Global c-array to handle the activations buffer */
AI_ALIGNED(32)
static ai_u8 activations[AI_NETWORK_1_DATA_ACTIVATIONS_SIZE];
AI_ALIGNED(32)
static ai_float in_data[AI_NETWORK_1_IN_1_SIZE];
AI_ALIGNED(32)
static ai_float out_data[AI_NETWORK_1_OUT_1_SIZE];
/* Array of pointer to manage the model's input/output tensors */
static ai_buffer *ai_input;
static ai_buffer *ai_output;
static ai_buffer_format fmt_input;
static ai_buffer_format fmt_output;
#define NSIZE 3
void buf_print(void)
{
rt_kprintf("in_data:");
for (int i=0; i<AI_NETWORK_1_IN_1_SIZE; i++)
{
rt_kprintf("%.2f ",((ai_float*)in_data)[i]);
}
rt_kprintf("\n");
rt_kprintf("out_data:");
for (int i=0; i<AI_NETWORK_1_OUT_1_SIZE; i++)
{
rt_kprintf("%.2f ",((ai_float*)out_data)[i]);
}
rt_kprintf("\n");
}
void aiPrintBufInfo(const ai_buffer *buffer)
{
rt_kprintf("(%lu, %lu, %lu, %lu)", AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_BATCH),
AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_HEIGHT),
AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_WIDTH),
AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_CHANNEL));
rt_kprintf(" buffer_size:%d ", (int)AI_BUFFER_SIZE(buffer));
}
void aiPrintDataType(const ai_buffer_format fmt)
{
if (AI_BUFFER_FMT_GET_TYPE(fmt) == AI_BUFFER_FMT_TYPE_FLOAT)
rt_kprintf("float%d ", (int)AI_BUFFER_FMT_GET_BITS(fmt));
else if (AI_BUFFER_FMT_GET_TYPE(fmt) == AI_BUFFER_FMT_TYPE_BOOL) {
rt_kprintf("bool%d ", (int)AI_BUFFER_FMT_GET_BITS(fmt));
} else { /* integer type */
rt_kprintf("%s%d ", AI_BUFFER_FMT_GET_SIGN(fmt)?"i":"u",
(int)AI_BUFFER_FMT_GET_BITS(fmt));
}
}
void aiPrintDataInfo(const ai_buffer *buffer,const ai_buffer_format fmt)
{
if (buffer->data)
rt_kprintf(" @0x%X/%d \n",
(int)buffer->data,
(int)AI_BUFFER_BYTE_SIZE(AI_BUFFER_SIZE(buffer), fmt)
);
else
rt_kprintf(" (User Domain)/%d \n",
(int)AI_BUFFER_BYTE_SIZE(AI_BUFFER_SIZE(buffer), fmt)
);
}
void aiPrintNetworkInfo(const ai_network_report report)
{
rt_kprintf("Model name : %s\n", report.model_name);
rt_kprintf(" model signature : %s\n", report.model_signature);
rt_kprintf(" model datetime : %s\r\n", report.model_datetime);
rt_kprintf(" compile datetime : %s\r\n", report.compile_datetime);
rt_kprintf(" runtime version : %d.%d.%d\r\n",
report.runtime_version.major,
report.runtime_version.minor,
report.runtime_version.micro);
if (report.tool_revision[0])
rt_kprintf(" Tool revision : %s\r\n", (report.tool_revision[0])?report.tool_revision:"");
rt_kprintf(" tools version : %d.%d.%d\r\n",
report.tool_version.major,
report.tool_version.minor,
report.tool_version.micro);
rt_kprintf(" complexity : %lu MACC\r\n", (unsigned long)report.n_macc);
rt_kprintf(" c-nodes : %d\r\n", (int)report.n_nodes);
rt_kprintf(" map_activations : %d\r\n", report.map_activations.size);
for (int idx=0; idx<report.map_activations.size;idx++) {
const ai_buffer *buffer = &report.map_activations.buffer[idx];
rt_kprintf(" [%d] ", idx);
aiPrintBufInfo(buffer);
rt_kprintf("\r\n");
}
rt_kprintf(" map_weights : %d\r\n", report.map_weights.size);
for (int idx=0; idx<report.map_weights.size;idx++) {
const ai_buffer *buffer = &report.map_weights.buffer[idx];
rt_kprintf(" [%d] ", idx);
aiPrintBufInfo(buffer);
rt_kprintf("\r\n");
}
}
/*
* Bootstrap
*/
int aiInit(void) {
ai_error err;
/* Create and initialize the c-model */
const ai_handle acts[] = { activations };
err = ai_network_1_create_and_init(&network, acts, NULL);
if (err.type != AI_ERROR_NONE) {
rt_kprintf("ai_error_type:%d,ai_error_code:%d\r\n",err.type,err.code);
return -1;
}
ai_network_report report;
if (ai_network_1_get_report(network, &report) != true) {
rt_kprintf("ai get report error\n");
return -1;
}
aiPrintNetworkInfo(report);
/* Reteive pointers to the model's input/output tensors */
ai_input = ai_network_1_inputs_get(network, NULL);
ai_output = ai_network_1_outputs_get(network, NULL);
//
fmt_input = AI_BUFFER_FORMAT(ai_input);
fmt_output = AI_BUFFER_FORMAT(ai_output);
rt_kprintf(" n_inputs/n_outputs : %u/%u\r\n", report.n_inputs,
report.n_outputs);
rt_kprintf("input :");
aiPrintBufInfo(ai_input);
aiPrintDataType(fmt_input);
aiPrintDataInfo(ai_input, fmt_input);
//
rt_kprintf("output :");
aiPrintBufInfo(ai_output);
aiPrintDataType(fmt_output);
aiPrintDataInfo(ai_output, fmt_output);
return 0;
}
int acquire_and_process_data(float *in_data,float *feature)
{
for(int i=0;i<32;i++)
{
in_data[i] = feature[i];
}
return 0;
}
/*
* Run inference
*/
int aiRun(const void *in_data, void *out_data) {
ai_i32 n_batch;
ai_error err;
/* 1 - 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);
/* 2 - Perform the inference */
n_batch = ai_network_1_run(network, &ai_input[0], &ai_output[0]);
if (n_batch != 1) {
err = ai_network_1_get_error(network);
rt_kprintf("ai_error_type:%d,ai_error_code:%d\r\n",err.type,err.code);
};
return 0;
}
extern float features34[DEC_WAVE_NUM][34];//input_data
void test_ai(void)
{
aiInit();
buf_print();
// acquire_and_process_data(in_data, features34[0]);
// aiRun(in_data, out_data);
// buf_print();
}
MSH_CMD_EXPORT(test_ai,"test DNN_small_low")
I checked a lot of relevant information online, indicating that hardware CRC was not enabled, but I still confirmed that my hardware CRC was configured through msh command.
rt_uint16_t CheckCRC16(rt_uint8_t *buf, rt_uint16_t len)
{
static struct hwcrypto_crc_cfg crc16ModbusConfig={
.last_val = 0xffff,
.poly = 0x8005,
.width = 16,
.flags = CRC_FLAG_REFIN|CRC_FLAG_REFOUT,
.xorout = 0x0000,
};
static struct rt_hwcrypto_device* cryptoDev = RT_NULL;
struct rt_hwcrypto_ctx* ctx = rt_hwcrypto_crc_create(cryptoDev,HWCRYPTO_CRC_CRC16);
if(cryptoDev==RT_NULL)
cryptoDev = rt_hwcrypto_dev_default();
rt_hwcrypto_crc_cfg(ctx,&crc16ModbusConfig);
rt_free(ctx);
return 0;
// rt_uint32_t result = rt_hwcrypto_crc_update(ctx, buf, len);
// return result;
}
MSH_CMD_EXPORT(CheckCRC16,"initialization CRCr")
However, I use __HAL_RCC_CRC_CLK_ENABLE(); The debug runtime does not execute a breakpoint at this point. I cannot solve my problem.
Please help me, thanks!
2024-10-01 03:00 AM
I also encounter this problem these days. Already enable the CRC and ai_error_type:51, ai_error_code:65. Have you solved this?
BR