Exercise:Convolution and Pooling

From Ufldl

Jump to: navigation, search
(Step 2a: Implement convolution)
(Step 2a: Implement convolution)
Line 42: Line 42:
First, <tt>conv2</tt> performs a 2-D convolution, but you have 5 "dimensions" - image number, feature number, row of image, column of image, and channel of image - that you want to convolve over. Because of this, you will have to convolve each feature and image channel separately for each image, using the row and column of the image as the 2 dimensions you convolve over. This means that you will need three outer loops over the image number <tt>imageNum</tt>, feature number <tt>featureNum</tt>, and the channel number of the image <tt>channel</tt>, with the 2-D convolution of the weight matrix for the <tt>featureNum</tt>-th feature and <tt>channel</tt>-th channel with the image matrix for the <tt>imageNum</tt>-th image going inside.  
First, <tt>conv2</tt> performs a 2-D convolution, but you have 5 "dimensions" - image number, feature number, row of image, column of image, and channel of image - that you want to convolve over. Because of this, you will have to convolve each feature and image channel separately for each image, using the row and column of the image as the 2 dimensions you convolve over. This means that you will need three outer loops over the image number <tt>imageNum</tt>, feature number <tt>featureNum</tt>, and the channel number of the image <tt>channel</tt>, with the 2-D convolution of the weight matrix for the <tt>featureNum</tt>-th feature and <tt>channel</tt>-th channel with the image matrix for the <tt>imageNum</tt>-th image going inside.  
-
Second, because of the mathematical definition of convolution, the feature matrix must be "flipped" before passing it to <tt>conv2</tt>. This is explained in greater detail in the implementation tip section following the code.
+
Second, because of the mathematical definition of convolution, the feature matrix must be "flipped" before passing it to <tt>conv2</tt>. The following implementation tip explains the "flipping" of feature matrices when using MATLAB's convolution functions:
-
 
+
-
Concretely, the code to do the convolution using <tt>conv2</tt> will look something like the following:
+
-
 
+
-
<syntaxhighlight lang="matlab">
+
-
convolvedFeatures = zeros(hiddenSize, numImages, imageDim - patchDim + 1, imageDim - patchDim + 1);
+
-
for imageNum = 1:numImages
+
-
  for featureNum = 1:hiddenSize
+
-
    % Obtain the feature matrix for this feature
+
-
    Wfeat = W(featureNum, :);
+
-
    Wfeat = reshape(Wfeat, patchDim, patchDim, 3);
+
-
   
+
-
    % Get convolution of image with feature matrix for each channel
+
-
    convolvedImage = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1);
+
-
    for channel = 1:3
+
-
 
+
-
      % Flip the feature matrix because of the definition of convolution, as explained later
+
-
      feature = flipud(fliplr(squeeze(Wfeat(:, :, channel))));
+
-
      im = squeeze(images(:, :, channel, imageNum));
+
-
 
+
-
      % Convolve "filter" with "im", adding the result to convolvedImage
+
-
      % ---- YOUR CODE HERE ----
+
-
 
+
-
 
+
-
      % ------------------------
+
-
 
+
-
    end
+
-
   
+
-
    % The convolved feature is the sum of the convolved values for all channels
+
-
    convolvedFeatures(featureNum, imageNum, :, :) = convolvedImage;
+
-
  end
+
-
end
+
-
</syntaxhighlight>
+
-
 
+
-
The following implementation tip explains the "flipping" of feature matrices when using MATLAB's convolution functions:
+
   
   
<div style="border:1px solid black; padding: 5px">
<div style="border:1px solid black; padding: 5px">

Revision as of 06:48, 23 May 2011

Personal tools