On this chapter we show a way to convert your convolution operation into a matrix multiplication. This has the advantage to compute faster, at the expense of more memory usage. We employ the **im2col** operation that will transform the input image or batch into a matrix, then we multiply this matrix with a reshaped version of our kernel. Then at the end we reshape this multiplied matrix back to an image with the **col2im** operation.

As shown on previous source code, we use a lot for for-loops to implement the convolutions, while this is useful for learning purpose, it's not fast enough. On this section we will learn how to implement convolutions on a vectorized fashion. First, if we inspect closer the code for convolution is basically a dot-product between the kernel filter and the local regions selected by the moving window, that sample a patch with the same size as our kernel. What would happens if we expand all possible windows on memory and perform the dot product as a matrix multiplication. Answer 200x or more speedups, at the expense of more memory consumption.

For example, if the input is [227x227x3] and it is to be convolved with 11x11x3 filters at stride 4 and padding 0, 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.

Calculating with input 227 with stride 4 and padding 0, gives ((227-11)/4)+1 = 55 locations along both width and height, leading to an output matrix X_col of size [363 x 3025]. Here every column is a stretched out receptive field (patch with depth) and there are 55*55 = 3025 of them in total.

To summarize how we calculate the im2col output sizes:

[img_height, img_width, img_channels] = size(img);newImgHeight = floor(((img_height + 2*P - ksize) / S)+1);newImgWidth = floor(((img_width + 2*P - ksize) / S)+1);cols = single(zeros((img_channels*ksize*ksize),(newImgHeight * newImgWidth)));

The weights of the CONV layer are similarly stretched out into rows. For example, if there are 96 filters of size [11x11x3] this would give a matrix W_row of size [96 x 363], where 11x11x3=363

After the image and the kernel are converted, the convolution can be implemented as a simple matrix multiplication, in our case it will be W_col[96 x 363] multiplied by X_col[363 x 3025] resulting as a matrix [96 x 3025], that need to be reshaped back to [55x55x96].

This final reshape can also be implemented as a function called col2im.

Notice that some implementations of im2col will have this result transposed, if this is the case then the order of the matrix multiplication must be changed.

In order to help the usage of im2col with convolution and also to derive the back-propagation, let's show the convolution with im2col as a graph. Here the input tensor is single a 3 channel 4x4 image. That will pass to a convolution layer with S:1 P:0 K:2 and F:1 (Output volume).

Using the im2col technique the computation graph resembles the FC layer with the same format $f(x, \theta, \beta) = (x.\theta^T) + \beta$, the difference that now we have a bunch of reshapes, transposes and the im2col block.

About the reshapes and transposes during back propagation you just need to invert their operations using again another reshape or transpose, the only important thing to remember is that if you use a reshape row major during forward propagation you need to use a reshape row major on the backpropagation.

The only point to pay attention is the im2col backpropagation operation. The issue is that it cannot be implemented as a simple reshape. This is because the patches could actually overlap (depending on the stride), so you need to sum the gradients where the patches intersect.

function [activations] = ForwardPropagation(obj, input, weights, bias)% Tensor format (rows,cols,channels, batch) on matlab[H,W,~,N] = size(input);[HH,WW,C,F] = size(weights);â€‹% Calculate output sizesH_prime = (H+2*obj.m_padding-HH)/obj.m_stride +1;W_prime = (W+2*obj.m_padding-WW)/obj.m_stride +1;â€‹% Alocate memory for outputactivations = zeros([H_prime,W_prime,F,N]);â€‹% Preparing filter weightsfilter_col = reshape(weights,[HH*WW*C F]);filter_col_T = filter_col';â€‹% Preparing biasif ~isempty(bias)bias_m = repmat(bias,[1 H_prime*W_prime]);elseb = zeros(size(filter_col,1),1);bias_m = repmat(b,[1 H_prime*W_prime]);endâ€‹% Here we convolve each image on the batch in a for-loop, but the im2col% could also handle a image batch at the input, so all computations would% be just one big matrix multiplication. We opted now for this to test the% par-for implementation with OpenMP on CPUfor idxBatch = 1:Nim = input(:,:,:,idxBatch);im_col = im2col_ref(im,HH,WW,obj.m_stride,obj.m_padding,1);mul = (filter_col_T * im_col) + bias_m;activations(:,:,:,idxBatch) = reshape_row_major(mul,[H_prime W_prime size(mul,1)]);endâ€‹% Cache results for backpropagationobj.activations = activations;obj.weights = weights;obj.biases = bias;obj.previousInput = input;end

function [gradient] = BackwardPropagation(obj, dout)dout = dout.input;[H,W,~,N] = size(obj.previousInput);[HH,WW,C,F] = size(obj.weights);â€‹% Calculate output sizesH_prime = (H+2*obj.m_padding-HH)/obj.m_stride +1;W_prime = (W+2*obj.m_padding-WW)/obj.m_stride +1;â€‹% Preparing filter weightsfilter_col_T = reshape_row_major(obj.weights,[F HH*WW*C]);â€‹% Initialize gradientsdw = zeros(size(obj.weights));dx = zeros(size(obj.previousInput));% Get the bias gradient which will be the sum of dout over the% dimensions (batches(4), rows(1), cols(2))db = sum(sum(sum(dout, 1), 2), 4);â€‹for idxBatch = 1:Nim = obj.previousInput(:,:,:,idxBatch);im_col = im2col_ref(im,HH,WW,obj.m_stride,obj.m_padding,1);dout_i = dout(:,:,:,idxBatch);â€‹dout_i_reshaped = reshape_row_major(dout_i,[F, H*W]);â€‹dw_before_reshape = dout_i_reshaped * im_col';dw_i = reshape(dw_before_reshape',[HH, WW, C, F]);dw = dw + dw_i;â€‹% We now have the gradient just before the im2colgrad_before_im2col = (dout_i_reshaped' * filter_col_T);% Now we need to backpropagate im2col (im2col_back),% results will padded by one alwaysdx_padded = im2col_back_ref(grad_before_im2col,H_prime, W_prime, obj.m_stride, HH, WW, C);% Now we need to take out the padingdx(:,:,:,idxBatch) = dx_padded(2:end-1, 2:end-1,:);endâ€‹%% Output gradientsgradient.bias = db;gradient.input = dx;gradient.weight = dw;â€‹end

function [ img_matrix ] = im2col_ref( inputImg, k_height, k_width, S , P, isConv )%IM2COL Convert image to a matrix, this step is used to accelerate%convolutions, implementing the convolution as a matrix multiplication% This version currently does not support batch of images, we choose this% because we're first going to use the CPU mode, and we want to relly on% parfor (OpenMP)coder.extrinsic('warning')% Get Image dimensions[imgHeight, imgWidth, imgChannels] = size(inputImg);â€‹% Calculate convolved result size.newImgHeight = ((imgHeight + 2*P - k_height) / S)+1;newImgWidth = ((imgWidth + 2*P - k_width) / S)+1;offset_K_Height = 0;offset_K_Width = 0;â€‹% Check if it is a real numberif rem(newImgHeight,1) ~= 0 || rem(newImgWidth,1) ~= 0warning('warning: Invalid stride or pad for input\n');if isConv% Convolution do a floornewImgHeight = floor(((imgHeight + 2*P - k_height) / S)+1);newImgWidth = floor(((imgWidth + 2*P - k_width) / S)+1);else% Pooling do a ceil and adapt the sampling windownewImgHeight = ceil(((imgHeight + 2*P - k_height) / S)+1);newImgWidth = ceil(((imgWidth + 2*P - k_width) / S)+1);offset_K_Height = k_height - 1;offset_K_Width = k_width - 1;endendâ€‹% Allocate output sizesimg_matrix = zeros(...(imgChannels*k_height*k_width),(newImgHeight * newImgWidth) ...);â€‹% Only pad if neededif P ~= 0inputImg_Padded = padarray(inputImg,[P P]);% Get dimensions again before iterate on padded image, otherwise we will% keep sampling with the old (unpadded size)[imgHeight, imgWidth, ~] = size(inputImg_Padded);endâ€‹% Iterate on the input image like a convolutioncont = 1;for r=1:S:(imgHeight-offset_K_Height)for c=1:S:(imgWidth-offset_K_Width)% Avoid slide out of the image (Security buffer overflow)if (((c+k_width)-1) <= imgWidth) && (((r+k_height)-1) <= imgHeight)% Select window on input volumeif P == 0patch = inputImg(r:(r+k_height)-1,c:(c+k_width)-1,:);elsepatch = inputImg_Padded(r:(r+k_height)-1,c:(c+k_width)-1,:);endâ€‹% Convert patch to a col vector, the matlab reshape order is% row major while other languages (C/C++, python) are col% major, on this particular case (im2col, then matrix mult with% the kernel) this order will not mather, but it's not allways% true...patchRow = reshape(patch,[],1);â€‹% Append the transformed patch into the output matriximg_matrix(:,cont) = patchRow;cont = cont+1;endendendâ€‹end

function [ img_grad ] = im2col_back_ref( dout, dout_H, dout_W, S, HH, WW, C )%IM2COL_BACK_REF Backpropagation of im2col% dout: (% Return% Image gradient (H,W,C)â€‹% Calculate the spatial dimensions of im_grad% Observe that the result will be "padded"H = (dout_H - 1) * S + HH;W = (dout_W - 1) * S + WW;â€‹img_grad = zeros(H,W,C);â€‹for ii=1:(dout_H*dout_W)row = dout(ii,:);â€‹% Create a patch from the rowpatch = reshape_row_major(row,[HH WW C]);%patch = reshape(row,[HH WW C]);â€‹% Calculate indexes on dxh_start = floor(((ii-1) / dout_W) * S);w_start = mod((ii-1),dout_W) * S;h_start = h_start + 1;w_start = w_start + 1;â€‹img_grad(h_start:h_start+HH-1, w_start:w_start+WW-1, :) = img_grad(h_start:h_start+HH-1, w_start:w_start+WW-1, :) + patch;endend

def conv_forward_naive(x, w, b, conv_param):"""A naive implementation of the forward pass for a convolutional layer.â€‹The input consists of N data points, each with C channels, height H and widthW. We convolve each input with F different filters, where each filter spansall C channels and has height HH and width HH.â€‹Input:- x: Input data of shape (N, C, H, W)- w: Filter weights of shape (F, C, HH, WW)- b: Biases, of shape (F,)- conv_param: A dictionary with the following keys:- 'stride': The number of pixels between adjacent receptive fields in thehorizontal and vertical directions.- 'pad': The number of pixels that will be used to zero-pad the input.â€‹Returns a tuple of:- out: Output data, of shape (N, F, H', W') where H' and W' are given byH' = 1 + (H + 2 * pad - HH) / strideW' = 1 + (W + 2 * pad - WW) / stride- cache: (x, w, b, conv_param)"""out = Nonepad_num = conv_param['pad']stride = conv_param['stride']N,C,H,W = x.shapeF,C,HH,WW = w.shapeH_prime = (H+2*pad_num-HH) // stride + 1W_prime = (W+2*pad_num-WW) // stride + 1out = np.zeros([N,F,H_prime,W_prime])#im2colfor im_num in range(N):im = x[im_num,:,:,:]im_pad = np.pad(im,((0,0),(pad_num,pad_num),(pad_num,pad_num)),'constant')im_col = im2col(im_pad,HH,WW,stride)filter_col = np.reshape(w,(F,-1))mul = im_col.dot(filter_col.T) + bout[im_num,:,:,:] = col2im(mul,H_prime,W_prime,1)cache = (x, w, b, conv_param)return out, cache

def conv_backward_naive(dout, cache):"""A naive implementation of the backward pass for a convolutional layer.â€‹Inputs:- dout: Upstream derivatives.- cache: A tuple of (x, w, b, conv_param) as in conv_forward_naiveâ€‹Returns a tuple of:- dx: Gradient with respect to x- dw: Gradient with respect to w- db: Gradient with respect to b"""dx, dw, db = None, None, Noneâ€‹x, w, b, conv_param = cachepad_num = conv_param['pad']stride = conv_param['stride']N,C,H,W = x.shapeF,C,HH,WW = w.shapeH_prime = (H+2*pad_num-HH) // stride + 1W_prime = (W+2*pad_num-WW) // stride + 1â€‹dw = np.zeros(w.shape)dx = np.zeros(x.shape)db = np.zeros(b.shape)â€‹# We could calculate the bias by just summing over the right dimensions# Bias gradient (Sum on dout dimensions (batch, rows, cols)#db = np.sum(dout, axis=(0, 2, 3))â€‹for i in range(N):im = x[i,:,:,:]im_pad = np.pad(im,((0,0),(pad_num,pad_num),(pad_num,pad_num)),'constant')im_col = im2col(im_pad,HH,WW,stride)filter_col = np.reshape(w,(F,-1)).Tâ€‹dout_i = dout[i,:,:,:]dbias_sum = np.reshape(dout_i,(F,-1))dbias_sum = dbias_sum.Tâ€‹#bias_sum = mul + bdb += np.sum(dbias_sum,axis=0)dmul = dbias_sumâ€‹#mul = im_col * filter_coldfilter_col = (im_col.T).dot(dmul)dim_col = dmul.dot(filter_col.T)â€‹dx_padded = col2im_back(dim_col,H_prime,W_prime,stride,HH,WW,C)dx[i,:,:,:] = dx_padded[:,pad_num:H+pad_num,pad_num:W+pad_num]dw += np.reshape(dfilter_col.T,(F,C,HH,WW))return dx, dw, db

This implementation will receive a image on the format of a 3 dimension tensor [channels, rows, cols] and will create a 2d matrix on the format [rows=(new_h*new_w), cols=(kw*kw*C)] notice that this algorithm will output the transposed version of the diagram above.

def im2col(x,hh,ww,stride):â€‹"""Args:x: image matrix to be translated into columns, (C,H,W)hh: filter heightww: filter widthstride: strideReturns:col: (new_h*new_w,hh*ww*C) matrix, each column is a cube that will convolve with a filternew_h = (H-hh) // stride + 1, new_w = (W-ww) // stride + 1"""â€‹c,h,w = x.shapenew_h = (h-hh) // stride + 1new_w = (w-ww) // stride + 1col = np.zeros([new_h*new_w,c*hh*ww])â€‹for i in range(new_h):for j in range(new_w):patch = x[...,i*stride:i*stride+hh,j*stride:j*stride+ww]col[i*new_w+j,:] = np.reshape(patch,-1)return col

def col2im(mul,h_prime,w_prime,C):"""Args:mul: (h_prime*w_prime*w,F) matrix, each col should be reshaped to C*h_prime*w_prime when C>0, or h_prime*w_prime when C = 0h_prime: reshaped filter heightw_prime: reshaped filter widthC: reshaped filter channel, if 0, reshape the filter to 2D, Otherwise reshape it to 3DReturns:if C == 0: (F,h_prime,w_prime) matrixOtherwise: (F,C,h_prime,w_prime) matrix"""F = mul.shape[1]if(C == 1):out = np.zeros([F,h_prime,w_prime])for i in range(F):col = mul[:,i]out[i,:,:] = np.reshape(col,(h_prime,w_prime))else:out = np.zeros([F,C,h_prime,w_prime])for i in range(F):col = mul[:,i]out[i,:,:] = np.reshape(col,(C,h_prime,w_prime))â€‹return out

def col2im_back(dim_col,h_prime,w_prime,stride,hh,ww,c):"""Args:dim_col: gradients for im_col,(h_prime*w_prime,hh*ww*c)h_prime,w_prime: height and width for the feature mapstrid: stridehh,ww,c: size of the filtersReturns:dx: Gradients for x, (C,H,W)"""H = (h_prime - 1) * stride + hhW = (w_prime - 1) * stride + wwdx = np.zeros([c,H,W])for i in range(h_prime*w_prime):row = dim_col[i,:]h_start = (i / w_prime) * stridew_start = (i % w_prime) * stridedx[:,h_start:h_start+hh,w_start:w_start+ww] += np.reshape(row,(c,hh,ww))return dx

To make things simpler on our heads, follow the simple example of convolving X[3x3] with W[2x2]