-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnnPool.m
55 lines (44 loc) · 2.01 KB
/
cnnPool.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function pooledFeatures = cnnPool(poolDim, convolvedFeatures)
%cnnPool Pools the given convolved features
%
% Parameters:
% poolDim - dimension of pooling region
% convolvedFeatures - convolved features to pool (as given by cnnConvolve)
% convolvedFeatures(imageRow, imageCol, featureNum, imageNum)
%
% Returns:
% pooledFeatures - matrix of pooled features in the form
% pooledFeatures(poolRow, poolCol, featureNum, imageNum)
%
numImages = size(convolvedFeatures, 4);
numFilters = size(convolvedFeatures, 3);
convolvedDim = size(convolvedFeatures, 1);
pooledFeatures = zeros(convolvedDim / poolDim, ...
convolvedDim / poolDim, numFilters, numImages);
% Instructions:
% Now pool the convolved features in regions of poolDim x poolDim,
% to obtain the
% (convolvedDim/poolDim) x (convolvedDim/poolDim) x numFeatures x numImages
% matrix pooledFeatures, such that
% pooledFeatures(poolRow, poolCol, featureNum, imageNum) is the
% value of the featureNum feature for the imageNum image pooled over the
% corresponding (poolRow, poolCol) pooling region.
%
% Use mean pooling here.
%%% YOUR CODE HERE %%%
for imageNum = 1 : numImages
for filterNum = 1 : numFilters
pooledImage = zeros(convolvedDim / poolDim, convolvedDim / poolDim);
convolvedImage = squeeze(convolvedFeatures(:, :, filterNum, imageNum));
x = (floor(size(convolvedImage,1)/poolDim)) * poolDim;
convolvedImage = convolvedImage(1:x,1:x);
for i = 1:size(pooledImage,1)
for j = 1:size(pooledImage,2)
pooledImage(i,j) = sum (sum (convolvedImage((((i-1)*poolDim)+1):((((i-1)*poolDim)+1)+poolDim-1), (((j-1)*poolDim)+1):((((j-1)*poolDim)+1)+poolDim-1))));
end
end
pooledImage = pooledImage ./ (poolDim*poolDim);
pooledFeatures(:, :, filterNum, imageNum) = pooledImage;
end
end
end