diff --git a/PORTING/CNN/cnn.c b/PORTING/CNN/cnn.c index 36b433e..6cbcc2c 100644 --- a/PORTING/CNN/cnn.c +++ b/PORTING/CNN/cnn.c @@ -41,7 +41,6 @@ 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; @@ -97,7 +96,6 @@ float* convolution(Model model_w, Model model_b, const float* input_matrix, int } myfree(SRAMEX,_conv_rlst); _conv_rlst = NULL; - DEBUG_PRINTF("卷积结束\r\n"); return conv_rlst; } @@ -176,10 +174,6 @@ float* output(const float* input_matrix){ float *affine2_rslt = (float *) mymalloc(SRAMEX,sizeof(float)*7); memset(affine2_rslt, 0, sizeof(float)*7); -// 比较输出层的最大值 - float temp = -100; // 用于存储最大值的临时变量,初始化为一个非常小的值 - int predict_num; // 用于存储预测的数字(对应最大值的索引) - // 遍历10个输出神经元(假设有10个类别) for(int n=0; n<7; n++) { @@ -194,18 +188,8 @@ float* output(const float* input_matrix){ // 加上对应神经元的偏置 affine2_temp = affine2_temp + fc2_weight.array[n]; affine2_rslt[n] = affine2_temp; // 存储输出层的结果 - - // 寻找最大值 - if(temp <= affine2_rslt[n]) - { - temp = affine2_rslt[n]; // 更新最大值 - predict_num = n; // 记录最大值对应的类别索引 - } } - print_rslt(affine2_rslt,7,7); - printf("Label is:%d\r\n",predict_num+1); - return affine2_rslt; } @@ -272,74 +256,123 @@ float* generateMatrix(Model model, const float* value) return CNN_data; } +float calculate_probabilities(float *input_array) +{ + float sum = 0; + u8 input_num = 7; + float *result = (float *) mymalloc(SRAMEX,sizeof(float)*input_num); + memset(result, 0, sizeof(float)*input_num); + + float *temp = (float *) mymalloc(SRAMEX,sizeof(float)*input_num); + memset(temp, 0, sizeof(float)*input_num); + + for (int i = 0; i < input_num; i++) + { + temp[i] = expf(input_array[i]); + sum = sum + temp[i]; + } + for (int j = 0; j < input_num; j++) + { + result[j] = temp[j] / sum; + if(isnan(result[j]))result[j] = 1; + } + + int max_index = 0; + float max_value = result[0]; + for (int k = 1; k < input_num; k++) + { + if (result[k] > max_value) + { + max_value = result[k]; + max_index = k; + } + } + + float _tmp = result[max_index] * 100; + myfree(SRAMEX,temp); + temp = NULL; + myfree(SRAMEX,result); + result = NULL; + return _tmp; +} + + +u8 calculate_layer(float *input_array){ + u8 input_num = 7; + u8 predict_num = 0; + float max_temp = -100; + for(int n=0; n