-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetupCNN.m
60 lines (43 loc) · 2.18 KB
/
setupCNN.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
function [train, test, layers, options] = setupCNN(root_folder)
% This function setups a convolutional neural network and return the
% training/testing sets, net layers, and net options. PlEASE NOTE: we've
% found errors between different computers when trying to perform parallel
% processing. If problems exist on your device, just remove it from the
% options var (on line 15).
original_imgs = imageDatastore(fullfile(root_folder, ...
Constants.CATEGORIES), 'LabelSource','foldernames', ...
'IncludeSubfolders', true, 'FileExtensions', '.jpg');
imgs = preprocessImages(original_imgs);
[train, test] = splitEachLabel(imgs, Constants.TRAINING_SIZE, 'randomize');
options = trainingOptions('sgdm', 'MaxEpochs', 10,'shuffle', ...
'every-epoch','InitialLearnRate', .00001, 'ExecutionEnvironment', 'parallel');
layers = [imageInputLayer([Constants.IMG_SIZE Constants.IMG_SIZE 3])
convolution2dLayer([11,11], Constants.NUM_OF_FILTERS_1, 'Padding',0,'Stride',4)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(3,'Stride',2,'Padding',0)
convolution2dLayer([5,5],Constants.NUM_OF_FILTERS_2,'Padding',2,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(3,'Stride',2,'Padding',0)
convolution2dLayer([3,3],Constants.NUM_OF_FILTERS_3,'Padding',1,'Stride',1)
reluLayer
convolution2dLayer([3,3],Constants.NUM_OF_FILTERS_3,'Padding',1,'Stride',1)
reluLayer
convolution2dLayer([3,3],Constants.NUM_OF_FILTERS_3,'Padding',1,'Stride',1)
reluLayer
maxPooling2dLayer(3,'Stride',2,'Padding',0)
convolution2dLayer([2,2],Constants.NUM_OF_FILTERS_4,'Padding',1,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(3,'Stride',2,'Padding',0)
fullyConnectedLayer(2)
reluLayer
dropoutLayer(.50)
fullyConnectedLayer(2)
reluLayer
dropoutLayer(.50)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
end