V.Vocor
V.Vocor

Reputation: 449

Implementing convolution as a matrix multiplication

Pithy: Help with Matlab script that takes ImageData array and Convolution weights from Caffe and returns convolution. Please.


I am trying to recreate a convolution generated by Caffe in Matlab.

Let's make the following definitions

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

The following text describes how to generalize the convolution as a matrix multiplication:

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.

From this, one could draw the conclusion that the im2col function call would look something like this:

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

However, if I use the following parameter-values

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

I get the following dimensions

>> size(input)

ans =

     1     3     5     5

>> size(output)

ans =

     1     2     3     3

>> size(filter)

ans =

     2     3     3     3

And if I use the im2col function call from above, I end up with an empty matrix.

If I change the stride to 1 in the above example, the size of the input, the output and the filter remains the same. If I use Matlab's 'convn' command, the size is not the same as the actual output from Caffe.

>> size(convn(input,filter))                                  

ans =

     2     5     7     7

What would be the general way to resize your array for matrix multiplication?

Upvotes: 1

Views: 2968

Answers (1)

cfh
cfh

Reputation: 4666

You are using the second argument to im2col wrong, see the documentation.

You should give it the size of the filter window that you are trying to slide over the image, i.e.:

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

Upvotes: 0

Related Questions