|
45 | 45 | % ZCAWhite = zeros(visibleSize, visibleSize); |
46 | 46 | % meanPatch = zeros(visibleSize, 1); |
47 | 47 |
|
48 | | -load '../linear_decoder_exercise/STL10Features.mat' |
| 48 | +load '../linear_decoder_exercise/STL10Features.mat'; |
49 | 49 |
|
50 | 50 | % -------------------------------------------------------------------- |
51 | 51 |
|
|
68 | 68 | % Note that we have to preprocess the images in the exact same way |
69 | 69 | % we preprocessed the patches before we can obtain the feature activations. |
70 | 70 |
|
71 | | -load stlTrainSubset.mat % loads numTrainImages, trainImages, trainLabels |
| 71 | +load stlTrainSubset.mat; % loads numTrainImages, trainImages, trainLabels |
72 | 72 |
|
73 | 73 | %% Use only the first 8 images for testing |
74 | 74 | convImages = trainImages(:, :, :, 1:8); |
75 | 75 |
|
76 | 76 | % % NOTE: Implement cnnConvolve in cnnConvolve.m first! |
77 | | -% convolvedFeatures = cnnConvolve(patchDim, hiddenSize, convImages, W, b, ZCAWhite, meanPatch); |
| 77 | +convolvedFeatures = cnnConvolve(patchDim, hiddenSize, convImages, W, b, ZCAWhite, meanPatch); |
78 | 78 |
|
79 | 79 | % %% STEP 2b: Checking your convolution |
80 | 80 | % % To ensure that you have convolved the features correctly, we have |
|
109 | 109 |
|
110 | 110 | % disp('Congratulations! Your convolution code passed the test.'); |
111 | 111 |
|
112 | | -% %% STEP 2c: Implement pooling |
113 | | -% % Implement pooling in the function cnnPool in cnnPool.m |
| 112 | +%% STEP 2c: Implement pooling |
| 113 | +% Implement pooling in the function cnnPool in cnnPool.m |
114 | 114 |
|
115 | | -% % NOTE: Implement cnnPool in cnnPool.m first! |
116 | | -% pooledFeatures = cnnPool(poolDim, convolvedFeatures); |
| 115 | +% NOTE: Implement cnnPool in cnnPool.m first! |
| 116 | +pooledFeatures = cnnPool(poolDim, convolvedFeatures); |
117 | 117 |
|
118 | 118 | % %% STEP 2d: Checking your pooling |
119 | 119 | % % To ensure that you have implemented pooling, we will use your pooling |
|
137 | 137 | % disp('Congratulations! Your pooling code passed the test.'); |
138 | 138 | % end |
139 | 139 |
|
140 | | -% %%====================================================================== |
141 | | -% %% STEP 3: Convolve and pool with the dataset |
142 | | -% % In this step, you will convolve each of the features you learned with |
143 | | -% % the full large images to obtain the convolved features. You will then |
144 | | -% % pool the convolved features to obtain the pooled features for |
145 | | -% % classification. |
146 | | -% % |
147 | | -% % Because the convolved features matrix is very large, we will do the |
148 | | -% % convolution and pooling 50 features at a time to avoid running out of |
149 | | -% % memory. Reduce this number if necessary |
150 | | - |
151 | | -% stepSize = 50; |
152 | | -% assert(mod(hiddenSize, stepSize) == 0, 'stepSize should divide hiddenSize'); |
153 | | - |
154 | | -% load stlTrainSubset.mat % loads numTrainImages, trainImages, trainLabels |
155 | | -% load stlTestSubset.mat % loads numTestImages, testImages, testLabels |
156 | | - |
157 | | -% pooledFeaturesTrain = zeros(hiddenSize, numTrainImages, ... |
158 | | -% floor((imageDim - patchDim + 1) / poolDim), ... |
159 | | -% floor((imageDim - patchDim + 1) / poolDim) ); |
160 | | -% pooledFeaturesTest = zeros(hiddenSize, numTestImages, ... |
161 | | -% floor((imageDim - patchDim + 1) / poolDim), ... |
162 | | -% floor((imageDim - patchDim + 1) / poolDim) ); |
163 | | - |
164 | | -% tic(); |
165 | | - |
166 | | -% for convPart = 1:(hiddenSize / stepSize) |
| 140 | +%%====================================================================== |
| 141 | +%% STEP 3: Convolve and pool with the dataset |
| 142 | +% In this step, you will convolve each of the features you learned with |
| 143 | +% the full large images to obtain the convolved features. You will then |
| 144 | +% pool the convolved features to obtain the pooled features for |
| 145 | +% classification. |
| 146 | +% |
| 147 | +% Because the convolved features matrix is very large, we will do the |
| 148 | +% convolution and pooling 50 features at a time to avoid running out of |
| 149 | +% memory. Reduce this number if necessary |
| 150 | + |
| 151 | +stepSize = 50; |
| 152 | +assert(mod(hiddenSize, stepSize) == 0, 'stepSize should divide hiddenSize'); |
| 153 | + |
| 154 | +load stlTrainSubset.mat % loads numTrainImages, trainImages, trainLabels |
| 155 | +load stlTestSubset.mat % loads numTestImages, testImages, testLabels |
| 156 | + |
| 157 | +pooledFeaturesTrain = zeros(hiddenSize, numTrainImages, ... |
| 158 | + floor((imageDim - patchDim + 1) / poolDim), ... |
| 159 | + floor((imageDim - patchDim + 1) / poolDim) ); |
| 160 | +pooledFeaturesTest = zeros(hiddenSize, numTestImages, ... |
| 161 | + floor((imageDim - patchDim + 1) / poolDim), ... |
| 162 | + floor((imageDim - patchDim + 1) / poolDim) ); |
| 163 | + |
| 164 | +tic(); |
| 165 | + |
| 166 | +for convPart = 1:(hiddenSize / stepSize) |
167 | 167 |
|
168 | | -% featureStart = (convPart - 1) * stepSize + 1; |
169 | | -% featureEnd = convPart * stepSize; |
| 168 | + featureStart = (convPart - 1) * stepSize + 1; |
| 169 | + featureEnd = convPart * stepSize; |
170 | 170 |
|
171 | | -% fprintf('Step %d: features %d to %d\n', convPart, featureStart, featureEnd); |
172 | | -% Wt = W(featureStart:featureEnd, :); |
173 | | -% bt = b(featureStart:featureEnd); |
| 171 | + fprintf('Step %d: features %d to %d\n', convPart, featureStart, featureEnd); |
| 172 | + Wt = W(featureStart:featureEnd, :); |
| 173 | + bt = b(featureStart:featureEnd); |
174 | 174 |
|
175 | | -% fprintf('Convolving and pooling train images\n'); |
176 | | -% convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ... |
177 | | -% trainImages, Wt, bt, ZCAWhite, meanPatch); |
178 | | -% pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis); |
179 | | -% pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis; |
180 | | -% toc(); |
181 | | -% clear convolvedFeaturesThis pooledFeaturesThis; |
| 175 | + fprintf('Convolving and pooling train images\n'); |
| 176 | + convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ... |
| 177 | + trainImages, Wt, bt, ZCAWhite, meanPatch); |
| 178 | + pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis); |
| 179 | + pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis; |
| 180 | + toc(); |
| 181 | + clear convolvedFeaturesThis pooledFeaturesThis; |
182 | 182 |
|
183 | | -% fprintf('Convolving and pooling test images\n'); |
184 | | -% convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ... |
185 | | -% testImages, Wt, bt, ZCAWhite, meanPatch); |
186 | | -% pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis); |
187 | | -% pooledFeaturesTest(featureStart:featureEnd, :, :, :) = pooledFeaturesThis; |
188 | | -% toc(); |
| 183 | + fprintf('Convolving and pooling test images\n'); |
| 184 | + convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ... |
| 185 | + testImages, Wt, bt, ZCAWhite, meanPatch); |
| 186 | + pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis); |
| 187 | + pooledFeaturesTest(featureStart:featureEnd, :, :, :) = pooledFeaturesThis; |
| 188 | + toc(); |
189 | 189 |
|
190 | | -% clear convolvedFeaturesThis pooledFeaturesThis; |
| 190 | + clear convolvedFeaturesThis pooledFeaturesThis; |
191 | 191 |
|
192 | | -% end |
| 192 | +end |
193 | 193 |
|
194 | 194 |
|
195 | 195 | % You might want to save the pooled features since convolution and pooling takes a long time |
196 | 196 | % save('cnnPooledFeatures.mat', 'pooledFeaturesTrain', 'pooledFeaturesTest'); |
197 | | -load('cnnPooledFeatures.mat'); |
| 197 | +% load('cnnPooledFeatures.mat'); |
198 | 198 | toc(); |
199 | 199 |
|
200 | 200 | %%====================================================================== |
|
0 commit comments