# Exercise:Convolution and Pooling

Jump to: navigation, search
 Revision as of 02:00, 23 May 2011 (view source)Jngiam (Talk | contribs) (→Step 2a: Implement convolution)← Older edit Revision as of 06:48, 23 May 2011 (view source)Jngiam (Talk | contribs) (→Step 2a: Implement convolution)Newer edit → Line 42: Line 42: First, conv2 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 imageNum, feature number featureNum, and the channel number of the image channel, with the 2-D convolution of the weight matrix for the featureNum-th feature and channel-th channel with the image matrix for the imageNum-th image going inside. First, conv2 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 imageNum, feature number featureNum, and the channel number of the image channel, with the 2-D convolution of the weight matrix for the featureNum-th feature and channel-th channel with the image matrix for the imageNum-th image going inside. - Second, because of the mathematical definition of convolution, the feature matrix must be "flipped" before passing it to conv2. 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 conv2. The following implementation tip explains the "flipping" of feature matrices when using MATLAB's convolution functions: - + - Concretely, the code to do the convolution using conv2 will look something like the following: + - + - + - 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 + - + - + - The following implementation tip explains the "flipping" of feature matrices when using MATLAB's convolution functions: +