From edd76c2871913a2bc202814b40d256dd7286fe6c Mon Sep 17 00:00:00 2001 From: Qiea <1310371422@qq.com> Date: Tue, 12 Nov 2024 09:37:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E6=A4=8Dc-cnn=EF=BC=9A=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ok,但代码未精简 --- PORTING/CNN/cnn.c | 49 +++++++++++++++++++++++++++-------------- PORTING/CNN/cnn_model.c | 6 ++--- PORTING/CNN/debug.c | 2 +- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/PORTING/CNN/cnn.c b/PORTING/CNN/cnn.c index 0b268bf..0fbabe7 100644 --- a/PORTING/CNN/cnn.c +++ b/PORTING/CNN/cnn.c @@ -41,14 +41,15 @@ float* expand(const float* old_matrix, int old_matrix_length, int layer){ //c_rl ͼı߳100 //ؾĽ float* convolution(Model model_w, Model model_b, const float* input_matrix, int input_matrix_length){ + DEBUG_PRINTF("ʼ\r\n"); // ʼ int im_l = input_matrix_length; int cr_l = input_matrix_length - 2; float conv_temp; // ʱڴ洢м //ںϲǰ飬32*64*50*50(ڶ)ĴС - float* _conv_rlst = (float *) mymalloc(SRAMEX,sizeof (float) * model_w.channel * model_w.num_kernels * (cr_l * cr_l)); - memset(_conv_rlst, 0, sizeof (float) * model_w.channel * model_w.num_kernels * (cr_l * cr_l)); + float* _conv_rlst = (float *) mymalloc(SRAMEX,sizeof (float) * model_w.num_kernels * (cr_l * cr_l)); + memset(_conv_rlst, 0, sizeof (float) * model_w.num_kernels * (cr_l * cr_l)); //ͼϲ float* conv_rlst = (float *) mymalloc(SRAMEX,sizeof (float) * model_w.num_kernels * (cr_l * cr_l)); memset(conv_rlst, 0, sizeof (float) * model_w.num_kernels * (cr_l * cr_l)); @@ -67,32 +68,36 @@ float* convolution(Model model_w, Model model_b, const float* input_matrix, int * model_w.array[((c+k*model_w.channel)*3*3) + (x*3+y)]; } } - _conv_rlst[(c*model_w.num_kernels*cr_l*cr_l) + (k*cr_l*cr_l) + (row*cr_l+col)] = conv_temp; + _conv_rlst[(k*cr_l*cr_l) + (row*cr_l+col)] = conv_temp; } } } - } - //ϲͼ - { + //ϲͼ for(int k=0; k < model_w.num_kernels; k++) { for (int row = 0; row < cr_l; row++) { for (int col = 0; col < cr_l; col++) { - conv_temp = 0; // ÿسʼΪ0 - for (int c = 0; c < model_w.channel; c++) { - conv_temp += _conv_rlst[(c * model_w.num_kernels * cr_l * cr_l) + (k * cr_l * cr_l) + (row * cr_l + col)]; - } - // ϶Ӧ˵ƫ - conv_temp += model_b.array[k]; - // ReLUС0ֵΪ0 - if (conv_temp > 0) - conv_rlst[(k * (cr_l * cr_l)) + (row * cr_l) + (col)] = conv_temp; // 0 - else - conv_rlst[(k * (cr_l * cr_l)) + (row * cr_l) + (col)] = 0; // 0 + conv_rlst[(k*cr_l*cr_l) + (row*cr_l+col)] += _conv_rlst[(k*cr_l*cr_l) + (row*cr_l+col)]; } } } } + for(int k=0; k < model_w.num_kernels; k++) { + for (int row = 0; row < cr_l; row++) { + for (int col = 0; col < cr_l; col++) { + conv_temp = conv_rlst[(k * cr_l * cr_l) + (row * cr_l + col)]; + // ϶Ӧ˵ƫ + conv_temp += model_b.array[k]; + // ReLUС0ֵΪ0 + if (conv_temp > 0) + conv_rlst[(k * (cr_l * cr_l)) + (row * cr_l) + (col)] = conv_temp; // 0 + else + conv_rlst[(k * (cr_l * cr_l)) + (row * cr_l) + (col)] = 0; // 0 + } + } + } myfree(SRAMEX,_conv_rlst); + _conv_rlst = NULL; + DEBUG_PRINTF("\r\n"); return conv_rlst; } @@ -268,11 +273,14 @@ float* generateMatrix(Model model, const float* value) } void cnn_run(){ + DEBUG_PRINTF("ʼCNN\r\n"); float value[3] = {0}; calculate_statistics(data,&value[0]); if (check_threshold(data,&value[0])){ float* _data = generateMatrix(data,&value[0]); + + DEBUG_PRINTF("ʼһ\r\n"); //һ㣺102 * 102 float* expand_matrix_1 = expand(_data, 100, 1); float* conv_rlst_1 = convolution(conv1_weight,conv1_bias,expand_matrix_1, 102); @@ -285,6 +293,8 @@ void cnn_run(){ myfree(SRAMEX,conv_rlst_1); conv_rlst_1 = NULL; + + DEBUG_PRINTF("ʼڶ\r\n"); //ڶ㣺32 * 52 * 52 float* expand_matrix_2 = expand(pool_rslt_1, 50, 32); float* conv_rlst_2 = convolution(conv2_weight,conv2_bias,expand_matrix_2, 52); @@ -297,6 +307,8 @@ void cnn_run(){ myfree(SRAMEX,conv_rlst_2); conv_rlst_2 = NULL; + + DEBUG_PRINTF("ʼ\r\n"); //㣺 64 * 27 * 27 float* expand_matrix_3 = expand(pool_rslt_2, 25, 64); float* conv_rlst_3 = convolution(conv3_weight,conv3_bias,expand_matrix_3, 27); @@ -309,7 +321,10 @@ void cnn_run(){ myfree(SRAMEX,conv_rlst_3); conv_rlst_3 = NULL; + DEBUG_PRINTF("ʼIJ\r\n"); float* affine1_rslt = hidden(pool_rslt_3); + + DEBUG_PRINTF("ʼ\r\n"); float* affine2_rslt = output(affine1_rslt); myfree(SRAMEX,pool_rslt_3); diff --git a/PORTING/CNN/cnn_model.c b/PORTING/CNN/cnn_model.c index d749571..ce6ddb8 100644 --- a/PORTING/CNN/cnn_model.c +++ b/PORTING/CNN/cnn_model.c @@ -165,8 +165,8 @@ u8 model_write(char* model_name) u8 _times=0; u32 _larr = 0; u8 _len = strlen(model_name); - char _path[_len+1+7+30]; - char _datapath[_len+1+7+30]; + char _path[_len+1+7+35]; + char _datapath[_len+1+7+35]; char _fstr[READLENGTH+1] = {0}; char _sstr[2]; int progress; @@ -284,7 +284,7 @@ u8 model_read(char* model_name, u32 start, u32 end, u32 gap){ u8 model_switchdata(char* data_name){ u8 _len = strlen(data_name); - char _path[_len+1+7]; + char _path[_len+1+7+35]; if(data.array != NULL)modelmym_free("data"); sprintf(_path, "0:/dataset/_data/%s.txt",data_name); if(f_open(file,(const TCHAR*)_path,1)){ diff --git a/PORTING/CNN/debug.c b/PORTING/CNN/debug.c index 00ab347..d3c1b99 100644 --- a/PORTING/CNN/debug.c +++ b/PORTING/CNN/debug.c @@ -2,7 +2,7 @@ #include "led.h" -u8 _DEBUG = 0; +u8 _DEBUG = 1;