@@ -593,69 +593,112 @@ Matrix3d conv_test(Matrix3d mid1, int input_dim = 3, int output_channels = 3, in
593593 }
594594}
595595Matrix3d conv_test_with_output (Matrix3d mid1,
596- int input_dim = 3 ,
597- int output_channels = 3 ,
598- int stride = 1 ,
599- int kernel_size = 2 ,
600- int mode = 0 ,
601- int padding = 0 ,
602- bool verbose = false )
603- {
604- // 如果需要padding,先对输入进行padding
605- if (padding > 0 ) {
606- Matrix3d padded_input = CreateMatrix3d (mid1. dep , mid1. wid + 2 *padding, mid1. high + 2 *padding) ;
607- for ( int c = 0 ; c < mid1. dep ; c++) {
608- padded_input. matrix3d [c] = edge_padding (mid1. matrix3d [c],
609- mid1. wid + 2 *padding,
610- mid1. high + 2 *padding);
611- }
612- mid1 = padded_input ;
596+ int input_dim = 3 ,
597+ int output_channels = 3 ,
598+ int stride = 1 ,
599+ int kernel_size = 2 ,
600+ int mode = 0 ,
601+ bool verbose = false )
602+ // padding 暂未实现
603+ {
604+ if (verbose)
605+ {
606+ cout << " Input Matrix3d: " << endl ;
607+ cout_mat3d ( mid1);
608+ cout << " Parameters: input_dim = " << input_dim
609+ << " , output_channels = " << output_channels
610+ << " , stride = " << stride
611+ << " , kernel_size = " << kernel_size
612+ << " , mode = " << mode ;
613613 }
614614
615- // 计算输出尺寸
616- int output_height = ((mid1.wid - kernel_size) / stride) + 1 ;
617- int output_width = ((mid1.high - kernel_size) / stride) + 1 ;
615+ // Compute padding widths and heights
616+ int padding_wid = stride - (mid1.wid - kernel_size) % stride;
617+ if (padding_wid == stride)
618+ {
619+ padding_wid = 0 ;
620+ }
621+ int padding_high = stride - (mid1.high - kernel_size) % stride;
622+ if (padding_high == stride)
623+ {
624+ padding_high = 0 ;
625+ }
626+ if (verbose)
627+ {
628+ cout << " Padding widths: " << padding_wid << " , padding heights: " << padding_high << endl;
629+ }
618630
619- Matrix3d output3d = CreateMatrix3d (output_channels, output_height, output_width);
620-
621- // 构造卷积核
622- Matrix** filters = (Matrix**)malloc (input_dim * sizeof (Matrix*));
623- for (int i = 0 ; i < input_dim; i++) {
624- filters[i] = (Matrix*)malloc (output_channels * sizeof (Matrix));
625- for (int j = 0 ; j < output_channels; j++) {
626- filters[i][j] = ones (kernel_size, kernel_size);
631+ // Pad each RGB channel in the 3D matrix
632+ Matrix mid_rgb[input_dim];
633+ for (int rgb_idx = 0 ; rgb_idx < input_dim; rgb_idx++)
634+ {
635+ mid_rgb[rgb_idx] = edge_padding (mid1.matrix3d [rgb_idx],
636+ mid1.matrix3d [rgb_idx].row + padding_high,
637+ mid1.matrix3d [rgb_idx].col + padding_wid);
638+ if (verbose)
639+ {
640+ cout << " RGB[" << rgb_idx << " ] channel after padding: " << endl;
641+ cout_mat (mid_rgb[rgb_idx]);
627642 }
628643 }
629644
630- // 执行卷积操作
631- for (int out_c = 0 ; out_c < output_channels; out_c++) {
632- for (int h = 0 ; h < output_height; h++) {
633- for (int w = 0 ; w < output_width; w++) {
634- float sum = 0 ;
635- for (int in_c = 0 ; in_c < input_dim; in_c++) {
636- for (int kh = 0 ; kh < kernel_size; kh++) {
637- for (int kw = 0 ; kw < kernel_size; kw++) {
638- int h_in = h * stride + kh;
639- int w_in = w * stride + kw;
640- sum += mid1.matrix3d [in_c].matrix [h_in][w_in] *
641- filters[in_c][out_c].matrix [kh][kw];
642- }
643- }
644- }
645- output3d.matrix3d [out_c].matrix [h][w] = sum;
646- }
645+ // Construct filters
646+ Matrix filters[input_dim][output_channels];
647+ for (int channel_index = 0 ; channel_index < input_dim; channel_index++)
648+ {
649+
650+ for (int filter_index = 0 ; filter_index < output_channels; filter_index++)
651+ {
652+ Matrix kernel = ones (kernel_size, kernel_size);
653+ filters[channel_index][filter_index] = kernel;
647654 }
648655 }
649656
650- // 释放内存
651- for (int i = 0 ; i < input_dim; i++) {
652- for (int j = 0 ; j < output_channels; j++) {
653- free_mat (filters[i][j]);
657+ // Compute convolution results for each filter
658+ Matrix kernel = ones (kernel_size, kernel_size);
659+ Matrix feature_maps[output_channels];
660+ for (int filter_idx = 0 ; filter_idx < output_channels; filter_idx++)
661+ {
662+ Matrix sum_rgb = CreateMatrix (((mid1.wid - kernel_size + 2 * padding_wid) / stride) + 1 ,
663+ ((mid1.high - kernel_size + 2 * padding_high) / stride) + 1 );
664+ for (int channel_idx = 0 ; channel_idx < input_dim; channel_idx++)
665+ {
666+ // Compute convolution result for a single RGB channel and a single filter
667+ Matrix element = conv_element (mid_rgb[channel_idx],
668+ filters[channel_idx][filter_idx],
669+ kernel_size, stride);
670+ if (verbose)
671+ {
672+ cout << " Convolution of RGB[" << channel_idx << " ] channel with Filter["
673+ << filter_idx << " ] : " << endl;
674+ cout_mat (mid_rgb[channel_idx]);
675+ cout << " * " << endl;
676+ cout_mat (filters[channel_idx][filter_idx]);
677+ cout << " = " << endl;
678+ cout_mat (element);
679+ cout << endl;
680+ }
681+ // Sum convolution results for each RGB channel
682+ sum_rgb = add (sum_rgb, element, 0 );
683+ }
684+ feature_maps[filter_idx] = sum_rgb;
685+ if (verbose)
686+ {
687+ cout << " Feature map [" << filter_idx << " ] : " << endl;
688+ cout_mat (feature_maps[filter_idx]);
654689 }
655- free (filters[i]);
656690 }
657- free (filters);
658-
691+ // Construct 3D matrix to store different feature maps at different depths
692+ Matrix3d output3d = CreateMatrix3d (output_channels, feature_maps[0 ].row , feature_maps[0 ].col );
693+ for (int i = 0 ; i < output_channels; i++)
694+ {
695+ output3d.matrix3d [i] = feature_maps[i];
696+ }
697+ if (verbose)
698+ {
699+ cout << " Output Matrix3d: " << endl;
700+ cout_mat3d (output3d);
701+ }
659702 return output3d;
660703}
661704
@@ -686,7 +729,7 @@ Matrix4d batch_conv_test(Matrix4d mid4,
686729 for (int batch_idx = 0 ; batch_idx < mid4.batch ; batch_idx++)
687730 {
688731 Matrix3d mid3 = mid4.matrix4d [batch_idx];
689- Matrix3d output3d = conv_test_with_output (mid3, input_dim, output_channels, stride, kernel_size, mode, 0 , verbose);
732+ Matrix3d output3d = conv_test_with_output (mid3, input_dim, output_channels, stride, kernel_size, mode, verbose);
690733 output3d_arr[batch_idx] = output3d;
691734 }
692735
0 commit comments