Skip to content

Commit faa69fb

Browse files
committed
Do convolution and pooling instead of using pre-computed values
1 parent 0025880 commit faa69fb

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

cnn_exercise/cnnExercise.m

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
% ZCAWhite = zeros(visibleSize, visibleSize);
4646
% meanPatch = zeros(visibleSize, 1);
4747

48-
load '../linear_decoder_exercise/STL10Features.mat'
48+
load '../linear_decoder_exercise/STL10Features.mat';
4949

5050
% --------------------------------------------------------------------
5151

@@ -68,13 +68,13 @@
6868
% Note that we have to preprocess the images in the exact same way
6969
% we preprocessed the patches before we can obtain the feature activations.
7070

71-
load stlTrainSubset.mat % loads numTrainImages, trainImages, trainLabels
71+
load stlTrainSubset.mat; % loads numTrainImages, trainImages, trainLabels
7272

7373
%% Use only the first 8 images for testing
7474
convImages = trainImages(:, :, :, 1:8);
7575

7676
% % 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);
7878

7979
% %% STEP 2b: Checking your convolution
8080
% % To ensure that you have convolved the features correctly, we have
@@ -109,11 +109,11 @@
109109

110110
% disp('Congratulations! Your convolution code passed the test.');
111111

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
114114

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);
117117

118118
% %% STEP 2d: Checking your pooling
119119
% % To ensure that you have implemented pooling, we will use your pooling
@@ -137,64 +137,64 @@
137137
% disp('Congratulations! Your pooling code passed the test.');
138138
% end
139139

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)
167167

168-
% featureStart = (convPart - 1) * stepSize + 1;
169-
% featureEnd = convPart * stepSize;
168+
featureStart = (convPart - 1) * stepSize + 1;
169+
featureEnd = convPart * stepSize;
170170

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);
174174

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;
182182

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();
189189

190-
% clear convolvedFeaturesThis pooledFeaturesThis;
190+
clear convolvedFeaturesThis pooledFeaturesThis;
191191

192-
% end
192+
end
193193

194194

195195
% You might want to save the pooled features since convolution and pooling takes a long time
196196
% save('cnnPooledFeatures.mat', 'pooledFeaturesTrain', 'pooledFeaturesTest');
197-
load('cnnPooledFeatures.mat');
197+
% load('cnnPooledFeatures.mat');
198198
toc();
199199

200200
%%======================================================================

0 commit comments

Comments
 (0)