forked from GeoRos/UAS-Vision-2016-TargetDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnnConvolve.m
62 lines (46 loc) · 1.95 KB
/
cnnConvolve.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
56
57
58
59
60
61
function convolvedFeatures = cnnConvolve(filterDim, numFilters, images, W, b)
%cnnConvolve Returns the convolution of the features given by W and b with
%the given images
%
% Parameters:
% filterDim - filter (feature) dimension
% numFilters - number of feature maps
% images - large images to convolve with, matrix in the form
% images(r, c, image number)
% W, b - W, b for features from the sparse autoencoder
% W is of shape (filterDim,filterDim,numFilters)
% b is of shape (numFilters,1)
%
% Returns:
% convolvedFeatures - matrix of convolved features in the form
% convolvedFeatures(imageRow, imageCol, featureNum, imageNum)
numImages = size(images, 3);
imageDim = size(images, 1);
convDim = imageDim - filterDim + 1;
convolvedFeatures = zeros(convDim, convDim, numFilters, numImages);
% Convolves every filter with every image to produce the
% (imageDim - filterDim + 1) x (imageDim - filterDim + 1) x numFeatures
% x numImages matrix convolvedFeatures, such that
% convolvedFeatures(imageRow, imageCol, featureNum, imageNum) is the
% value of the convolved featureNum feature for the imageNum image over
% the region (imageRow, imageCol) to
% (imageRow + filterDim - 1, imageCol + filterDim - 1)
for imageNum = 1:numImages
for filterNum = 1:numFilters
filter = squeeze(W(:,:,filterNum));
% Flips the feature matrix because of the definition of convolution
filter = rot90(squeeze(filter),2);
im = squeeze(images(:, :, imageNum));
% Convolves "filter" with "im", adding the result to convolvedImage
convolvedImage = conv2(im, filter, 'valid');
% Adds the bias unit
convolvedImage = convolvedImage + b(filterNum);
% Then, apply the activation function
%Sigmoid
convolvedImage = sigmf(convolvedImage, [1 0]);
%ReLU
%convolvedImage = max(convolvedImage, 0);
convolvedFeatures(:, :, filterNum, imageNum) = convolvedImage;
end
end
end