本来想走捷径但发现网络上很多理解是错误的,自己调试了下。
从这里就完全可以知道,这个函数其实是为每个卷积核变量需要做乘法做准备,基本上就是把N*N的卷积运算
转换成N*N个的1*1卷积运算。
这段代码可以直接拷贝过去使用,我是在VC2017里面调试过。
#include <stdio.h>
#define tchannel 2
#define theight 5
#define twidth 5
#define tksize 3
#define tstride 2
#define tpad 0
int im_data[tchannel][theight][twidth];
int data_col[tchannel*theight*twidth];
void data_init(void)
{
static int i, j, k;
int m = 0;
for (size_t i = 0; i < tchannel; i++)
{
printf("{");
for (size_t j = 0; j < theight; j++)
{
printf("\t");
for (size_t k = 0; k < twidth; k++)
{
m++;
im_data[i][j][k] = m;
printf( "%d,",im_data[i][j][k]);
}
printf("\n");
}
printf("}");
printf("\n");
}
}
int im2col_get_pixel(int *im, int height, int width, int channels,
int row, int col, int channel, int pad)
{
row -= pad;
col -= pad;
if (row < 0 || col < 0 ||
row >= height || col >= width) return 0;
return im[col + width * (row + height * channel)];
}
//From Berkeley Vision's Caffe!
//https://github.com/BVLC/caffe/blob/master/LICENSE
void im2col_cpu(int* data_im,
int channels, int height, int width,
int ksize, int stride, int pad, int* data_col)
{
int c, h, w;
int height_col = (height + 2 * pad - ksize) / stride + 1; //output size height
int width_col = (width + 2 * pad - ksize) / stride + 1; //output size height
printf("------%d--%d----------\n", height_col,width_col);
int channels_col = channels * ksize * ksize;
//最外层循环是每个卷积核的参数个数:
for (c = 0; c < channels_col; ++c)
{
int w_offset = c % ksize;
int h_offset = (c / ksize) % ksize;
int c_im = c / ksize / ksize;
//这两层循环是用卷积核把图像遍历一遍
printf("{" );
for (h = 0; h < height_col; ++h)
{
for (w = 0; w < width_col; ++w)
{
int im_row = h_offset + h * stride;
int im_col = w_offset + w * stride;
int col_index = (c * height_col + h) * width_col + w;
data_col[col_index] = im2col_get_pixel(data_im,
height, width, channels,im_row, im_col, c_im, pad);
printf("%d,", data_col[col_index]);
}
}
printf("}-->x%d\n",c);
}
}
int main()
{
data_init();
im2col_cpu((int*)im_data, tchannel, theight, twidth,
tksize, tstride, tpad, (int*)data_col);
while (1);
}
运行结果