一个图像 input_num=1;
图像通道 input_channel=1;
图像高 input_h=4;
图像宽 input_w=4;
kernel高 kernel_h=3;
kernel宽 kernel_w=3;
stride=1;pad=0;
卷积后,输出图像的计算公式:
output_h=(input_h-kernel_h)/stride+1;
output_w=(input_w-kernel_w)/stride+1;
原图(图a)按照从左到右、从上到下的过程,将(a)中大小为33(因为kernel大小为33)的矩阵拉成右图(图b)中的一列。具体过程如下图所示:
二:多通道的im2col
假设有三个通道(R、G、B)图像通道 input_channel=3;
图像在内存中的存储是:首先是连续存储第一通道的数据,然后再存储第二通道的数据,最后存储第三通道的数据。如下图:
多通道的im2col的过程如下图所示
内存中的存储是首先im2col第一通道,然后在im2col第二通道,最后im2col第三通道。各通道im2col的数据在内存中也是连续存储的。如下图:
三:kernel
图像的每个通道对应一个kernel通道,如下图(注:为计算简单,将kernel的值设置为1,同样此值不代表颜色数值。)
kernel的通道数据在内存中也是连续存储的。所以上面的kernel图像也可以表示为下图:
四:矩阵乘 sgemm
在caffe中图像与kernel的矩阵乘,是kernel*img。即:在矩阵乘法中
M=1 ,
N=output_h * output_w
K=input_channels * kernel_h * kernel_w
如下图所示:
图像数据是连续存储,因此输出图像也可以如下图所示【output_h * output_w】=【2*2】:
五:多通道图像输出
在caffe中图像与kernel的矩阵乘中:
M=output_channels ,
N=output_h * output_w
K=input_channels * kernel_h * kernel_w
如下图:
同样,多个输出通道图像的数据是连续存储,因此输出图像也可以如下图所示【output_channelsoutput_h * output_w】=【32*2】,
六、实现程序
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
int conv_out_height(h, pad, size, stride) {
return (h + 2 * pad - size) / stride + 1;
}
int conv_out_width(w, pad, size, stride) {
return (w + 2 * pad - size) / stride + 1;
}
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;//防止越界
//根据channel定位通道,width*(row + height*channel)用来定位行,例如当channel=0时,代表输入图像的第一个通道,此时row表示了此通道图像的第row行,由于图像是用一维存放的,所以width*row则是使其指向了实际存放的那一行元素,图像然后通过col来定位列,即那一行元素的第几个,从而找到图像像素值;
//而当channel=1时,代表了指向第二个通道图像,,row代表了此通道图像的第row行,由于图像是用一维存放的,所以width*(row + height)则是使其指向了实际存放的那一行元素,图像然后通过col来定位列,即那一行元素的第几个,从而找到图像像素值;感觉和上面一样有点啰嗦了;
return im[col + width*(row + height*channel)];
}
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;
int width_col = (width + 2 * pad - ksize) / stride + 1;
//卷积核参数个数
int channels_col = channels * ksize * ksize;
//遍历所有的卷积核元素
for (c = 0; c < channels_col; ++c) {
//通过下面3个值可以定位到内存里的卷积核元素位置
int w_offset = c % ksize;//卷积核横坐标w = c % 卷积核尺寸
int h_offset = (c / ksize) % ksize;//卷积核纵坐标h = (c / 卷积核尺寸) % 卷积核尺寸
int c_im = c / ksize / ksize;//卷积核通道c_im = c /卷积核尺寸 / 卷积核尺寸;也是其对应的输入图像通道,每个通道的卷积核只卷积对应通道的图像;
//遍历的每个卷积核元素对应的图像像素值
for (h = 0; h < height_col; ++h) {
for (w = 0; w < width_col; ++w) {
//表示第c_im个通道卷积核上坐标为(h_offset,w_offset)的元素对应第c_im个输入通道图像中元素的坐标(im_row,im_col);例如上图中第一个通道卷积核的第一个元素就对应了图像第一个输入通道上的(0,1,4,5)四个元素,四个坐标分别为输入图像第一个通道(c_im=0)时的(0,0),(0,1),(1,0),(1,1)
int im_row = h_offset + h * stride;//每个卷积核元素对图像进行卷积时图像元素的行
int im_col = w_offset + w * stride;//每个卷积核元素对图像进行卷积时图像元素的列
//每个卷积核元素都会对应out_w*out_h个像素值,所以整个卷积核对应的像素数量应该为:
//输出特征图高 x 输出特征图宽 x 卷积核尺寸x 卷积核尺寸 x卷积核通道
//内存中的存储是首先im2col第一通道,然后在im2col第二通道,最后im2col第三通道。各通道im2col的数据在内存中也是连续存储的。
//下面的目录col_index就是按照通道顺序存放的,先是存放第一的通道的卷积核的第一个元素所对应的图像像素,然后依次存放;接着放第二个通道的,最后放最后一个通道的;
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);
}
}
}
}
int main(int argc, char* argv[]) {
int *data_im = NULL;
int *data_col = NULL;
int channels = 3, height = 4, width = 4;
int ksize = 3, stride = 1, pad = 0;
int out_w, out_h;
int workspace_size;
int inputs = height * width * channels;
data_im = (int*)malloc(inputs * sizeof(int));
if (!data_im) {
printf("malloc error\n");
exit(EXIT_FAILURE);
}
out_w = conv_out_width(width, pad, ksize, stride);
out_h = conv_out_width(height, pad, ksize, stride);
workspace_size = out_h * out_w * ksize * ksize * channels;
data_col = (int*)malloc(workspace_size * sizeof(int));
if (!data_col) {
printf("malloc error\n");
exit(EXIT_FAILURE);
}
//init image
for (int i = 0; i<inputs; i++) data_im[i] = i;
im2col_cpu(data_im, channels, height, width, ksize, stride, pad, data_col);
printf("data_im:\n");
for (int i = 0; i<inputs; i++) {
printf("%-3d", data_im[i]);
if( (i+1) % 4 == 0) printf("\n");
}
printf("\ndata_col:\n");
for (int i = 0; i<workspace_size; i++) {
printf("%-3d", data_col[i]);
if( (i+1) % 4 == 0) printf("\n");
}
printf("\n");
free(data_im);
free(data_col);
system("pause");
exit(EXIT_SUCCESS);
}