From fb74170c00e8ed839e8a3a5476e41b970b7e396d Mon Sep 17 00:00:00 2001 From: Qiea <1310371422@qq.com> Date: Sun, 10 Nov 2024 21:15:37 +0800 Subject: [PATCH] _tmp --- cnn.c | 92 +++++++++++++++++++++++++---------------------------- cnn_model.c | 6 ++++ cnn_model.h | 4 +++ 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/cnn.c b/cnn.c index 3435f3c..4a3f872 100644 --- a/cnn.c +++ b/cnn.c @@ -19,56 +19,50 @@ float* expand(const float* old_matrix, u8 old_matrix_length, u8 layer){ return new_matrix; } -//num_kernels 卷积核的个数:32 -//area 卷积的面积:3*3 +//model 模型名字 //input_matrix 输入图像 //input_matrix_length 输入图像的边长:102 -//输出图像的边长:100 +//c_rl 输出图像的边长:100 //返回卷积的结果 -float* convolution(u8 num_kernels, u8 area, const float* input_matrix, u8 input_matrix_length, u8 layer){ +float* convolution(Model model_w, Model model_b, const float* input_matrix, u8 input_matrix_length){ // 初始化卷积层参数 + u8 c_rl = input_matrix_length - 2; float conv_temp; // 临时变量,用于存储卷积计算的中间结果 - float* conv_rlst = (float *) malloc(sizeof (float)*layer*num_kernels*(input_matrix_length-2)*(input_matrix_length-2)); - memset(conv_rlst, 0, sizeof (float)*layer*num_kernels*(input_matrix_length-2)*(input_matrix_length-2)); + float* conv_rlst = (float *) malloc(sizeof (float) * model_w.num_kernels * model_w.layer * (c_rl*c_rl)); + memset(conv_rlst, 0, sizeof (float) * model_w.num_kernels * model_w.layer * (c_rl*c_rl)); // 遍历30个卷积核(假设有30个通道) - for(u8 l=0;l 0) + conv_rlst[row * (input_matrix_length - 2) + col + + n * (input_matrix_length - 2) * (input_matrix_length - 2) + + 0 + ] = conv_temp; // 如果卷积结果大于0,存入结果数组 + else + conv_rlst[row * (input_matrix_length - 2) + col + + n * (input_matrix_length - 2) * (input_matrix_length - 2) + + 0 + ] = 0; // 否则存入0 } - // 加上对应卷积核的偏置 - conv_temp += conv1_bias.array[n]; - // 激活函数:ReLU(将小于0的值设为0) - if(conv_temp > 0) - conv_rlst[row*(input_matrix_length-2)+col+n*(input_matrix_length-2)*(input_matrix_length-2)+ - 0 - ] = conv_temp; // 如果卷积结果大于0,存入结果数组 - else - conv_rlst[row*(input_matrix_length-2)+col+n*(input_matrix_length-2)*(input_matrix_length-2)+ - 0 - ] = 0; // 否则存入0 } } } - } return conv_rlst; } @@ -138,17 +132,17 @@ int main(){ //第一层:填充102 * 102 float* expand_matrix_1 = expand(data.array, 100, 1); print_rslt(expand_matrix_1, 102, (0*102*102)); - float* conv_rlst_1 = convolution(32, 3, expand_matrix_1, 102,1); + float* conv_rlst_1 = convolution(conv1_weight,conv1_bias,expand_matrix_1, 102); print_rslt(conv_rlst_1, 32, (1*100)); - float* pool_rslt_1 = pooling(32, 2, conv_rlst_1, 100); - -//第二层:填充102 * 102 - float* expand_matrix_2 = expand(pool_rslt_1, 50, 32); - print_rslt(expand_matrix_2, 52, (0*52*52)); - float* conv_rlst_2 = convolution(64, 3, expand_matrix_2, 52,1); - print_rslt(conv_rlst_2, 64, (1*50*50)); - float* pool_rslt_2 = pooling(64, 2, conv_rlst_2, 50); - +// float* pool_rslt_1 = pooling(32, 2, conv_rlst_1, 100); +// +////第二层:填充102 * 102 +// float* expand_matrix_2 = expand(pool_rslt_1, 50, 32); +// print_rslt(expand_matrix_2, 52, (0*52*52)); +// float* conv_rlst_2 = convolution(conv2_weight,conv2_bias,expand_matrix_2, 102); +// print_rslt(conv_rlst_2, 64, (1*50*50)); +// float* pool_rslt_2 = pooling(64, 2, conv_rlst_2, 50); +// diff --git a/cnn_model.c b/cnn_model.c index d3eb79f..4a81e9d 100644 --- a/cnn_model.c +++ b/cnn_model.c @@ -341,6 +341,8 @@ void model_init(){ conv1_weight.name = "conv1_weight"; conv1_weight.array = modelmym_init(conv1_weight.name); conv1_weight.maxlength = CONV1_WEIGHT_ARRSIZE; + conv1_weight.layer = 1; + conv1_weight.num_kernels = 32; conv2_bias.name = "conv2_bias"; conv2_bias.array = modelmym_init(conv2_bias.name); @@ -349,6 +351,8 @@ void model_init(){ conv2_weight.name = "conv2_weight"; conv2_weight.array = modelmym_init(conv2_weight.name); conv2_weight.maxlength = CONV2_WEIGHT_ARRSIZE; + conv2_weight.layer = 32; + conv2_weight.num_kernels = 64; conv3_bias.name = "conv3_bias"; conv3_bias.array = modelmym_init(conv3_bias.name); @@ -357,6 +361,8 @@ void model_init(){ conv3_weight.name = "conv3_weight"; conv3_weight.array = modelmym_init(conv3_weight.name); conv3_weight.maxlength = CONV3_WEIGHT_ARRSIZE; + conv2_weight.layer = 64; + conv2_weight.num_kernels = 128; fc1_bias.name = "fc1_bias"; fc1_bias.array = modelmym_init(fc1_bias.name); diff --git a/cnn_model.h b/cnn_model.h index aa7ef5a..459f13d 100644 --- a/cnn_model.h +++ b/cnn_model.h @@ -14,6 +14,10 @@ typedef struct { float* array; u32 maxlength; u32 realength; + + u8 layer; + u8 num_kernels; + } Model;