From 1c0f3b676f90fd2169fa897b9e4a38a0c6b32c70 Mon Sep 17 00:00:00 2001 From: Qiea <1310371422@qq.com> Date: Wed, 13 Nov 2024 12:30:23 +0800 Subject: [PATCH] save --- PORTING/CNN/cnn.c | 39 +++++++++++++++++++++++++-------------- PORTING/CNN/cnn.h | 1 + PORTING/CNN/cnn_model.c | 34 +++++++++++++++++++--------------- PORTING/CNN/cnn_model.h | 13 +++++-------- PORTING/CNN/debug.c | 2 +- PORTING/CNN/tools.c | 20 +++++++++++--------- PORTING/CNN/tools.h | 1 + 7 files changed, 63 insertions(+), 47 deletions(-) diff --git a/PORTING/CNN/cnn.c b/PORTING/CNN/cnn.c index 00706de..e4a5a5b 100644 --- a/PORTING/CNN/cnn.c +++ b/PORTING/CNN/cnn.c @@ -169,13 +169,14 @@ float* hidden(const float* input_matrix){ return affine1_rslt; } -float* output(const float* input_matrix){ +float* output(Model model_w, const float* input_matrix){ + u8 num = model_w.num_kernels; float affine2_temp; // 临时变量,用于存储输出层的中间结果 - float *affine2_rslt = (float *) mymalloc(SRAMEX,sizeof(float)*7); - memset(affine2_rslt, 0, sizeof(float)*7); + float *affine2_rslt = (float *) mymalloc(SRAMEX,(sizeof(float)*num)); + memset(affine2_rslt, 0, sizeof(float)*num); // 遍历10个输出神经元(假设有10个类别) - for(int n=0; n<7; n++) + for(int n=0; n #include #include "tools.h" +#include "Function.h" void cnn_run(void); void _cnn_run(void); diff --git a/PORTING/CNN/cnn_model.c b/PORTING/CNN/cnn_model.c index 520f0ca..02befb6 100644 --- a/PORTING/CNN/cnn_model.c +++ b/PORTING/CNN/cnn_model.c @@ -14,7 +14,7 @@ Model fc2_bias; Model fc2_weight; Model data; - +float data_array[DATA_ARRSIZE] __attribute__((at(0XC0009C40))); float* modelmym_init(char* model_name){ if(conv1_bias.array == NULL && strcmp(model_name, "conv1_bias") == 0) @@ -37,8 +37,8 @@ float* modelmym_init(char* model_name){ return fc2_bias.array = (float*)mymalloc(SRAMEX, FC2_BIAS_ARRSIZE * sizeof(float)); else if(fc2_weight.array == NULL && strcmp(model_name, "fc2_weight") == 0) return fc2_weight.array = (float*)mymalloc(SRAMEX, FC2_WEIGHT_ARRSIZE * sizeof(float)); - else if(data.array == NULL && strcmp(model_name, "data") == 0) - return data.array = (float*)mymalloc(SRAMEX, DATA_ARRSIZE * sizeof(float)); +// else if(data.array == NULL && strcmp(model_name, "data") == 0) +// return data.array = (float*)mymalloc(SRAMEX, DATA_ARRSIZE * sizeof(float)); else if(strcmp(model_name, "all") == 0){ if(conv1_bias.array == NULL)conv1_bias.array = (float*)mymalloc(SRAMEX, CONV1_BIAS_ARRSIZE * sizeof(float)); if(conv1_weight.array == NULL)conv1_weight.array = (float*)mymalloc(SRAMEX, CONV1_WEIGHT_ARRSIZE * sizeof(float)); @@ -50,7 +50,7 @@ float* modelmym_init(char* model_name){ if(fc1_weight.array == NULL)fc1_weight.array = (float*)mymalloc(SRAMEX, FC1_WEIGHT_ARRSIZE * sizeof(float)); if(fc2_bias.array == NULL)fc2_bias.array = (float*)mymalloc(SRAMEX, FC2_BIAS_ARRSIZE * sizeof(float)); if(fc2_weight.array == NULL)fc2_weight.array = (float*)mymalloc(SRAMEX, FC2_WEIGHT_ARRSIZE * sizeof(float)); - if(data.array == NULL)data.array = (float*)mymalloc(SRAMEX, DATA_ARRSIZE * sizeof(float)); +// if(data.array == NULL)data.array = (float*)mymalloc(SRAMEX, DATA_ARRSIZE * sizeof(float)); } return NULL; } @@ -119,9 +119,9 @@ u8 modelmym_free(char* model_name){ return 1; } else if(data.array != NULL && strcmp(model_name, "data") == 0){ - myfree(SRAMEX,data.array); - data.array = NULL; - data.realength = 0; +// myfree(SRAMEX,data.array); + memset(data.array, 0 ,data.maxlength); +// data.realength = 0; return 1; } else if(strcmp(model_name, "all") == 0){ @@ -173,7 +173,7 @@ u8 model_write(char* model_name) Model *_model = model(model_name); if(_model == NULL || strcmp(model_name, "data") == 0){ - sprintf(_path, "0:/dataset/_data/%s.txt", model_name); + sprintf(_path, "dataset/_data/%s.txt", model_name); if(f_open(file, (const TCHAR *)_path, 1)){ DEBUG_PRINTF("\r\n输入了一个无效的模型或Data数据集的名字\r\n"); return 199; @@ -184,7 +184,7 @@ u8 model_write(char* model_name) } if(_model -> dname == NULL){ - sprintf(_path, "0:/dataset/%s.txt", _model -> name); + sprintf(_path, "dataset/%s.txt", _model -> name); if(f_open(file, (const TCHAR *)_path, 1)){ DEBUG_PRINTF("预设里没有这个模型:[%s]\r\n", _path); return 4; @@ -197,7 +197,7 @@ u8 model_write(char* model_name) } if(_model -> dname)sprintf(_datapath, "_data/%s", _model -> dname); - sprintf(_path, "0:/dataset/%s.txt", _model -> dname ? _datapath : _model -> name); + sprintf(_path, "dataset/%s.txt", _model -> dname ? _datapath : _model -> name); if(f_open(file, (const TCHAR *)_path, 1)){ DEBUG_PRINTF("文件[%s]无法打开\r\n", _model -> dname ? _model -> dname : _model -> name); return 199; @@ -286,7 +286,7 @@ u8 model_switchdata(char* data_name){ u8 _len = strlen(data_name); char _path[_len+1+7+35]; if(data.array != NULL)modelmym_free("data"); - sprintf(_path, "0:/dataset/_data/%s.txt",data_name); + sprintf(_path, "dataset/_data/%s.txt",data_name); if(f_open(file,(const TCHAR*)_path,1)){ DEBUG_PRINTF("\r\nData数据集[%s]不存在\r\n",data_name); return 0; @@ -361,7 +361,7 @@ void model_init(){ conv1_weight.array = modelmym_init(conv1_weight.name); conv1_weight.maxlength = CONV1_WEIGHT_ARRSIZE; conv1_weight.channel = 1; - conv1_weight.num_kernels = 32; + conv1_weight.num_kernels = CONV1_BIAS_ARRSIZE; conv2_bias.name = "conv2_bias"; conv2_bias.array = modelmym_init(conv2_bias.name); @@ -371,7 +371,7 @@ void model_init(){ conv2_weight.array = modelmym_init(conv2_weight.name); conv2_weight.maxlength = CONV2_WEIGHT_ARRSIZE; conv2_weight.channel = 32; - conv2_weight.num_kernels = 64; + conv2_weight.num_kernels = CONV2_BIAS_ARRSIZE; conv3_bias.name = "conv3_bias"; conv3_bias.array = modelmym_init(conv3_bias.name); @@ -381,7 +381,7 @@ void model_init(){ conv3_weight.array = modelmym_init(conv3_weight.name); conv3_weight.maxlength = CONV3_WEIGHT_ARRSIZE; conv3_weight.channel = 64; - conv3_weight.num_kernels = 128; + conv3_weight.num_kernels = CONV3_BIAS_ARRSIZE; fc1_bias.name = "fc1_bias"; fc1_bias.array = modelmym_init(fc1_bias.name); @@ -398,10 +398,14 @@ void model_init(){ fc2_weight.name = "fc2_weight"; fc2_weight.array = modelmym_init(fc2_weight.name); fc2_weight.maxlength = FC2_WEIGHT_ARRSIZE; + fc2_weight.num_kernels = FC2_BIAS_ARRSIZE; data.name = "data"; - data.array = modelmym_init(data.name); + data.array = data_array; data.maxlength = DATA_ARRSIZE; + data.realength = DATA_ARRSIZE; data.dname = "data"; + + memset(data.array, 0, sizeof(float)*DATA_ARRSIZE); } diff --git a/PORTING/CNN/cnn_model.h b/PORTING/CNN/cnn_model.h index 67f8d92..f33f4ce 100644 --- a/PORTING/CNN/cnn_model.h +++ b/PORTING/CNN/cnn_model.h @@ -36,15 +36,12 @@ typedef struct { #define CONV3_WEIGHT_ARRSIZE (128*64*3*3) //73728 #define FC1_BIAS_ARRSIZE (128) #define FC1_WEIGHT_ARRSIZE (128*18432) //2359296 -#define FC2_BIAS_ARRSIZE (7) -#define FC2_WEIGHT_ARRSIZE (7*128) //896 -#define is1250000 1 -#if is1250000 - #define DATA_ARRSIZE (1250000) -#else - #define DATA_ARRSIZE (100 * 100) -#endif + + +#define FC2_BIAS_ARRSIZE (4) //4个卷积核 +#define FC2_WEIGHT_ARRSIZE (4*128) +#define DATA_ARRSIZE (1300000) //原始数据长度 1300000 diff --git a/PORTING/CNN/debug.c b/PORTING/CNN/debug.c index 4b73186..c49678b 100644 --- a/PORTING/CNN/debug.c +++ b/PORTING/CNN/debug.c @@ -100,6 +100,6 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim==(&TIM3_Handler)) { - LED1=!LED1; //LED1反转 + LED_R=!LED_R; //LED1反转 } } diff --git a/PORTING/CNN/tools.c b/PORTING/CNN/tools.c index 0142bd9..67bd52d 100644 --- a/PORTING/CNN/tools.c +++ b/PORTING/CNN/tools.c @@ -26,7 +26,7 @@ void send_blocks(float* arr, char* uuid_str){ for (int j = 0; j < 100; ++j) { int n = snprintf(block + block_index, 100 * 100 - block_index, "%.6f|", arr[i * 100 + j]); block_index += n; - if (block_index >= 9000) { + if (block_index >= 7000) { block_index = 0; // 重置块 _total_len++; } @@ -39,13 +39,15 @@ void send_blocks(float* arr, char* uuid_str){ for (int j = 0; j < 100; ++j) { int n = snprintf(block + block_index, 100*100 - block_index, "%.6f|", arr[i*100+j]); block_index += n; - if (block_index >= 9000) { + if (block_index >= 7000) { char* _block = (char*) mymalloc(SRAMEX,sizeof(char)*100*100); memset(_block, 0 ,sizeof(char)*100*100); block_index = 0; // 重置块 - sprintf(_block, "{\"uuid\":\"%s\",\"Bid\":\"%d\",\"Eid\":\"%d\",\"Block\":\"%s\"}\n", uuid_str, total_len++, _total_len, block); - DEBUG_PRINTF("%s", _block); - myfree(SRAMEX,_block); + sprintf(_block, "{\\\"uuid\\\":\\\"%s\\\",\\\"Bid\\\":\\\"%d\\\",\\\"Eid\\\":\\\"%d\\\",\\\"Block\\\":\\\"%s\\\"}\n", uuid_str, total_len++, _total_len, block); + //DEBUG_PRINTF("%s", _block); + + CSTX_4G_ALiYunIOTSenddata_string(_block,"data_string"); + myfree(SRAMEX,_block); _block = NULL; } } @@ -56,13 +58,13 @@ void send_blocks(float* arr, char* uuid_str){ char* _block = (char*) mymalloc(SRAMEX,sizeof(char)*100*100); memset(_block, 0 ,sizeof(char)*100*100); block_index = 0; - sprintf(_block, "{\"uuid\":\"%s\",\"Bid\":\"%d\",\"Eid\":\"%d\",\"Block\":\"%s\"}\n", uuid_str, total_len, _total_len, block); - DEBUG_PRINTF("%s", _block); - myfree(SRAMEX,_block); + sprintf(_block, "{\\\"uuid\\\":\\\"%s\\\",\\\"Bid\\\":\\\"%d\\\",\\\"Eid\\\":\\\"%d\\\",\\\"Block\\\":\\\"%s\\\"}\n", uuid_str, total_len, _total_len, block); + //DEBUG_PRINTF("%s", _block); + CSTX_4G_ALiYunIOTSenddata_string(_block,"data_string"); + myfree(SRAMEX,_block); _block = NULL; } myfree(SRAMEX,block); block = NULL; } - diff --git a/PORTING/CNN/tools.h b/PORTING/CNN/tools.h index 6264833..aebf442 100644 --- a/PORTING/CNN/tools.h +++ b/PORTING/CNN/tools.h @@ -1,5 +1,6 @@ #include "cnn_model.h" #include "debug.h" +#include "EC800M.h" char* uuid(void);