将卷积实现为矩阵乘法

Implementing convolution as a matrix multiplication

Pithy:帮助使用 Matlab 脚本,该脚本从 Caffe 和 returns 卷积中获取 ImageData 数组和卷积权重。请。


我正在尝试在 Matlab 中重新创建由 Caffe 生成的卷积。

我们做如下定义

W**2 = Size of input
F**2 = Size of filter
P = Size of padding
S = Stride
K = Number of filters

以下文字描述了如何将卷积概括为矩阵乘法:

The local regions in the input image are stretched out into columns in an operation commonly called im2col. For example, if the input is [227x227x3] and it is to be convolved with 11x11x3 filters at stride 4, then we would take [11x11x3] blocks of pixels in the input and stretch each block into a column vector of size 11*11*3 = 363. Iterating this process in the input at stride of 4 gives (227-11)/4+1 = 55 locations along both width and height, leading to an output matrix X_col of im2col of size [363 x 3025], where every column is a stretched out receptive field and there are 55*55 = 3025 of them in total. Note that since the receptive fields overlap, every number in the input volume may be duplicated in multiple distinct columns.

由此,可以得出结论,im2col 函数调用看起来像这样:

input = im2col( input, [3*F*F, ((W-F)/S+1)**2)])

但是,如果我使用以下参数值

W = 5
F = 3
P = 1
S = 2
K = 2

我得到以下尺寸

>> size(input)

ans =

     1     3     5     5

>> size(output)

ans =

     1     2     3     3

>> size(filter)

ans =

     2     3     3     3

如果我使用上面的 im2col 函数调用,我最终会得到一个空矩阵。

如果我在上面的示例中将步幅更改为 1,则输入、输出和过滤器的大小保持不变。如果我使用 Matlab 的 'convn' 命令,大小与 Caffe 的实际输出不一样。

>> size(convn(input,filter))                                  

ans =

     2     5     7     7

为矩阵乘法调整数组大小的一般方法是什么?

您使用 im2col 的第二个参数是错误的,参见 the documentation

你应该给它你试图在图像上滑动的过滤器 window 的大小,即:

cols = im2col( input, [F, F])