Implementing convolution as a matrix multiplication
<p><em>Pithy: Help with Matlab script that takes ImageData array and Convolution weights  from Caffe and returns convolution. Please.</em></p>

<hr>

<p>I am trying to recreate a convolution generated by Caffe in Matlab.</p>

<p>Let's make the following definitions </p>

<pre><code>W**2 = Size of input
F**2 = Size of filter
P = Size of padding
S = Stride
K = Number of filters
</code></pre>

<p>The following text describes how to generalize the convolution as a matrix multiplication:</p>

<blockquote>
  <p>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.</p>
</blockquote>

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

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

<p>However, if I use the following parameter-values</p>

<pre><code>W = 5
F = 3
P = 1
S = 2
K = 2
</code></pre>

<p>I get the following dimensions</p>

<pre><code>&gt;&gt; size(input)

ans =

     1     3     5     5

&gt;&gt; size(output)

ans =

     1     2     3     3

&gt;&gt; size(filter)

ans =

     2     3     3     3
</code></pre>

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

<p>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.</p>

<pre><code>&gt;&gt; size(convn(input,filter))                                  

ans =

     2     5     7     7
</code></pre>

<p><strong>What would be the general way to resize your array for matrix multiplication?</strong></p>