# Exercise:Convolution and Pooling

- - - === Part II: Convolution and pooling === - === Step 3: Convolution and pooling === + === Step 2: Convolution and pooling === Now that you have learned features for small patches, you will convolved these learned features with the large images, and pool these convolved features for use in a classifier later. Now that you have learned features for small patches, you will convolved these learned features with the large images, and pool these convolved features for use in a classifier later. - ==== Step 3a: Convolution ==== + ==== Step 2a: Convolution ==== Implement convolution, as described in [[feature extraction using convolution]], in the function cnnConvolve in cnnConvolve.m. Implementing convolution is somewhat involved, so we will guide you through the process below. Implement convolution, as described in [[feature extraction using convolution]], in the function cnnConvolve in cnnConvolve.m. Implementing convolution is somewhat involved, so we will guide you through the process below. Line 150: Line 125: Taking the preprocessing steps into account, the feature activations that you should compute is $\sigma(W(T(x-\bar{x})) + b)$, where $T$ is the whitening matrix and $\bar{x}$ is the mean patch. Expanding this, you obtain $\sigma(WTx - WT\bar{x} + b)$, which suggests that you should convolve the images with $WT$ rather than $W$ as earlier, and you should add $(b - WT\bar{x})$, rather than just $b$ to convolvedFeatures, before finally applying the sigmoid function. Taking the preprocessing steps into account, the feature activations that you should compute is $\sigma(W(T(x-\bar{x})) + b)$, where $T$ is the whitening matrix and $\bar{x}$ is the mean patch. Expanding this, you obtain $\sigma(WTx - WT\bar{x} + b)$, which suggests that you should convolve the images with $WT$ rather than $W$ as earlier, and you should add $(b - WT\bar{x})$, rather than just $b$ to convolvedFeatures, before finally applying the sigmoid function. - ==== Step 3b: Checking ==== + ==== Step 2b: Checking ==== We have provided some code for you to check that you have done the convolution correctly. The code randomly checks the convolved values for a number of (feature, row, column) tuples by computing the feature activations for the selected features and patches directly using the sparse autoencoder. We have provided some code for you to check that you have done the convolution correctly. The code randomly checks the convolved values for a number of (feature, row, column) tuples by computing the feature activations for the selected features and patches directly using the sparse autoencoder. - ==== Step 3c: Pooling ==== + ==== Step 2c: Pooling ==== Implement [[pooling]] in the function cnnPool in cnnPool.m. Implement [[pooling]] in the function cnnPool in cnnPool.m. - === Step 4: Use pooled features for classification === + === Step 3: Use pooled features for classification === Once you have implemented pooling, you will use the pooled features to train a softmax classifier to map the pooled features to the class labels. The code in this section uses softmaxTrain from the softmax exercise to train a softmax classifier on the pooled features for 500 iterations, which should take around 5 minutes. Once you have implemented pooling, you will use the pooled features to train a softmax classifier to map the pooled features to the class labels. The code in this section uses softmaxTrain from the softmax exercise to train a softmax classifier on the pooled features for 500 iterations, which should take around 5 minutes. - === Step 5: Test classifier === + === Step 4: Test classifier === Now that you have a trained softmax classifier, you can see how well it performs on the test set. This section contains code that will load the test set (which is a smaller part of the STL10 dataset, specifically, 3200 rescaled 64x64 images from 4 different classes) and obtain the pooled, convolved features for the images using the functions cnnConvolve and cnnPool which you wrote earlier, as well as the preprocessing matrices ZCAWhite and meanImage which were computed earlier in preprocessing the training images. These pooled features will then be run through the softmax classifier, and the accuracy of the predictions will be computed. You should expect to get an accuracy of around 77-78%. Now that you have a trained softmax classifier, you can see how well it performs on the test set. This section contains code that will load the test set (which is a smaller part of the STL10 dataset, specifically, 3200 rescaled 64x64 images from 4 different classes) and obtain the pooled, convolved features for the images using the functions cnnConvolve and cnnPool which you wrote earlier, as well as the preprocessing matrices ZCAWhite and meanImage which were computed earlier in preprocessing the training images. These pooled features will then be run through the softmax classifier, and the accuracy of the predictions will be computed. You should expect to get an accuracy of around 77-78%.