Analysis of the realization of Im2col in Caffe

Source: Internet
Author: User

Here, I am the Caffe in the im2col of the parsing process directly pulled out, using C + + output, easy to understand. The code is as follows:

1#include <iostream>2 3 using namespacestd;4 5 BOOLIs_a_ge_zero_and_a_lt_b (intAintb)6 {7     if(a>=0&& a <b)return true;8     return false;9 }Ten  One voidIM2COL_CPU (Const float* Data_im,Const intchannels, A     Const intHeightConst intWidthConst intKernel_h,Const intKernel_w, -     Const intPad_h,Const intPad_w, -     Const intStride_h,Const intStride_w, the     Const intDilation_h,Const intDilation_w, -     float*Data_col) { -   Const intOutput_h = (height +2* Pad_h- -(Dilation_h * (Kernel_h-1) +1))/Stride_h +1; +   Const intOutput_w = (width +2* Pad_w- -(Dilation_w * (Kernel_w-1) +1))/Stride_w +1; +   Const intchannel_size = Height *width; A    for(intChannel = channels; channel--; Data_im + =channel_size) { at      for(intKernel_row =0; Kernel_row < Kernel_h; kernel_row++) { -        for(intKernel_col =0; Kernel_col < Kernel_w; kernel_col++) { -         intInput_row =-pad_h + Kernel_row *Dilation_h; -          for(intOutput_rows = Output_h; Output_rows; output_rows--) { -           if(!is_a_ge_zero_and_a_lt_b (input_row, height)) { -              for(intOutput_cols = Output_w; Output_cols; output_cols--) { in* (data_col++) =0; -             } to}Else { +             intInput_col =-pad_w + Kernel_col *Dilation_w; -              for(intOutput_col = Output_w; Output_col; output_col--) { the               if(Is_a_ge_zero_and_a_lt_b (input_col, width)) { ** (data_col++) = Data_im[input_row * Width +Input_col]; $}Else {Panax Notoginseng* (data_col++) =0; -               } theInput_col + =Stride_w; +             } A           } theInput_row + =Stride_h; +         } -       } $     } $   } - } -  the  - intMain ()Wuyi { the      float*Data_im; -     intheight=5; Wu     intWidth=5;  -     intKernel_h=3;  About     intkernel_w=3; $     intPad_h=1;  -     intpad_w=1; -     intStride_h=1;  -     intstride_w=1; A     intDilation_h=1;  +     intdilation_w=1; the     float*Data_col; -     intChannels =3; $     Const intOutput_h = (height +2* Pad_h- the(Dilation_h * (Kernel_h-1) +1))/Stride_h +1; the       Const intOutput_w = (width +2* Pad_w- the(Dilation_w * (Kernel_w-1) +1))/Stride_w +1; theData_im =New float[channels*height*width]; -Data_col =New float[channels*output_h*output_w*kernel_h*Kernel_w]; in      the     //init input image Data the      for(intm=0; m<channels;++m) About     { the        for(intI=0; ii) the       { the          for(intj=0; j<width;++j) +         { -DATA_IM[M*WIDTH*HEIGHT+I*WIDTH+J] = m*width*height+ I*width +J; thecout <<data_im[m*width*height+i*width+j] <<' ';Bayi         } thecout <<Endl; the       } -     } -      the im2col_cpu (data_im, channels, the height,width, Kernel_h, Kernel_w, the Pad_h, Pad_w, the Stride_h, Stride_w, - Dilation_h, Dilation_w, the data_col); thecout <<channels<<Endl; thecout <<output_h<<Endl;94cout <<output_w<<Endl; thecout <<kernel_h<<Endl; thecout <<kernel_w<<Endl; thecout <<"Error"<<Endl;98      for(intI=0; i<kernel_w*kernel_h*channels;++i) About     {     -          for(intj=0; j<output_w*output_h;++j)101         {102cout <<data_col[i*output_w*output_h+j]<<' ';103         }104cout <<Endl; the     }106 107     return 0;108}

Multi-channel convolution image others have given a lot of, we can search for the basic is from a piece. Here is an understanding of my own process, and the output of the program is exactly the same

Analysis of the realization of Im2col in Caffe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.