-
Notifications
You must be signed in to change notification settings - Fork 2
/
makeObjFcn2_MaxObj1.m
191 lines (153 loc) · 7.7 KB
/
makeObjFcn2_MaxObj1.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function ObjFcn = makeObjFcn2(XTrain,YTrain,XValidation,YValidation,Parm)
measure=Parm.ObjFcnMeasure;
ObjFcn = @valErrorFun;
function [valError,cons,fileName] = valErrorFun(optVars)
imageSize = [size(XTrain,1) size(XTrain,2) size(XTrain,3)];
numClasses = numel(unique(YTrain));
%initialNumFilters = round((max(imageSize)/2)/sqrt(optVars.NetworkDepth));
numMaxPools=3;
PoolSizeAvg = floor(max(imageSize)/(2^(numMaxPools)));
%filterSize = 5;
layers = [
imageInputLayer(imageSize,'Name','input')
convolution2dLayer(Parm.filterSize,Parm.initialNumFilters,'Padding','same','Name','conv_1')%3,8
batchNormalizationLayer('Name','BN_1');
reluLayer('Name','relu_1');
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_1')
convolution2dLayer(Parm.filterSize,2*Parm.initialNumFilters,'Padding','same','Name','conv_2')%3,16
batchNormalizationLayer('Name','BN_2');
reluLayer('Name','relu_2');
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_2')
convolution2dLayer(Parm.filterSize,4*Parm.initialNumFilters,'Padding','same','Name','conv_3')%3,32
batchNormalizationLayer('Name','BN_3')
reluLayer('Name','relu_3')
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_3')
convolution2dLayer(Parm.filterSize,8*Parm.initialNumFilters,'Padding','same','Name','conv_4')%3,32
batchNormalizationLayer('Name','BN_4')
reluLayer('Name','relu_4')
% maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_4')
%
% convolution2dLayer(filterSize,16*filterNum,'Padding','same','Name','conv_5')%3,32
% batchNormalizationLayer('Name','BN_5')
% reluLayer('Name','relu_5')
additionLayer(2,'Name','add')
averagePooling2dLayer(2,'Stride',2,'Name','avpool')
fullyConnectedLayer(numClasses,'Name','FC')
softmaxLayer('Name','softmax')
classificationLayer('Name','ClassOut')];
layers2 = [
convolution2dLayer(Parm.filterSize2,Parm.initialNumFilters,'Padding','same','Name','l2_conv_1')%3,8
batchNormalizationLayer('Name','l2_BN_1');
reluLayer('Name','l2_relu_1');
maxPooling2dLayer(2,'Stride',2,'Name','l2_MaxPool_1')
convolution2dLayer(Parm.filterSize2,2*Parm.initialNumFilters,'Padding','same','Name','l2_conv_2')%3,16
batchNormalizationLayer('Name','l2_BN_2');
reluLayer('Name','l2_relu_2');
maxPooling2dLayer(2,'Stride',2,'Name','l2_MaxPool_2')
convolution2dLayer(Parm.filterSize2,4*Parm.initialNumFilters,'Padding','same','Name','l2_conv_3')%3,32
batchNormalizationLayer('Name','l2_BN_3')
reluLayer('Name','l2_relu_3')
maxPooling2dLayer(2,'Stride',2,'Name','l2_MaxPool_3')
convolution2dLayer(Parm.filterSize2,8*Parm.initialNumFilters,'Padding','same','Name','l2_conv_4')%3,32
batchNormalizationLayer('Name','l2_BN_4')
reluLayer('Name','l2_relu_4')
% maxPooling2dLayer(2,'Stride',2,'Name','l2_MaxPool_4')
%
% convolution2dLayer(8,160,'Padding','same','Name','l2_conv_5')%3,32
% batchNormalizationLayer('Name','l2_BN_5')
% reluLayer('Name','l2_relu_5')
];
lgraph = layerGraph(layers);
lgraph = addLayers(lgraph,layers2);
lgraph = connectLayers(lgraph,'input','l2_conv_1');
lgraph = connectLayers(lgraph,'l2_relu_4','add/in2');
%figure; plot(lgraph)
% layers = [
% imageInputLayer(imageSize)
%
% % The spatial input and output sizes of these convolutional
% % layers are 32-by-32, and the following max pooling layer
% % reduces this to 16-by-16.
% convBlock(optVars.filterSize,initialNumFilters,optVars.NetworkDepth)
% maxPooling2dLayer(2,'Stride',2)
% % 1. maxPool
%
% % The spatial input and output sizes of these convolutional
% % layers are 16-by-16, and the following max pooling layer
% % reduces this to 8-by-8.
% convBlock(optVars.filterSize,2*initialNumFilters,optVars.NetworkDepth)
% maxPooling2dLayer(2,'Stride',2)
% % 2. maxPool
%
% % The spatial input and output sizes of these convolutional
% % layers are 8-by-8. The global average pooling layer averages
% % over the 8-by-8 inputs, giving an output of size
% % 1-by-1-by-4*initialNumFilters. With a global average
% % pooling layer, the final classification output is only
% % sensitive to the total amount of each feature present in the
% % input image, but insensitive to the spatial positions of the
% % features.
% convBlock(optVars.filterSize,4*initialNumFilters,optVars.NetworkDepth)
% maxPooling2dLayer(2,'Stride',2)
% % 3. maxPool
%
% convBlock(optVars.filterSize,8*initialNumFilters,optVars.NetworkDepth)
% %averagePooling2dLayer(PoolSizeAvg)
%
% % Add the fully connected layer and the final softmax and
% % classification layers.
% fullyConnectedLayer(numClasses)
% softmaxLayer
% classificationLayer];
augimdsTrain = augmentedImageDatastore(imageSize(1:2),XTrain,YTrain);
augimdsValidation = augmentedImageDatastore(imageSize(1:2),XValidation,YValidation);
miniBatchSize = Parm.miniBatchSize;
validationFrequency = floor(numel(YTrain)/miniBatchSize);
if validationFrequency<1
validationFrequency=1;
end
options = trainingOptions('sgdm',...
'InitialLearnRate',Parm.InitialLearnRate,...
'Momentum',Parm.Momentum,...
'ExecutionEnvironment',Parm.ExecutionEnvironment,...
'MaxEpochs',Parm.MaxEpochs, ...
'LearnRateSchedule','piecewise',...
'LearnRateDropPeriod',35,...
'LearnRateDropFactor',0.1,...
'MiniBatchSize',miniBatchSize,...
'L2Regularization',Parm.L2Regularization,...
'Shuffle','every-epoch',...
'Verbose',false,...
'Plots','training-progress',...
'ValidationData',{XValidation,YValidation},...
'ValidationPatience',Inf,...
'ValidationFrequency',validationFrequency);
%'Plots','none',...
%'MaxEpochs',100,...
% 'Plots','training-progress',...
% imageAugmenter = imageDataAugmenter( ...
% 'RandRotation',[-5,5], ...
% 'RandXTranslation',[-3 3], ...
% 'RandYTranslation',[-3 3]);
%
% datasource = augmentedImageDatastore(imageSize,XTrain,YTrain,...
% 'DataAugmentation',imageAugmenter,...
% 'OutputSizeMode','randcrop');
% trainedNet = trainNetwork(datasource,lgraph,options);
rng('default');
trainedNet = trainNetwork(augimdsTrain,lgraph,options);
close(findall(groot,'Tag','NNET_CNN_TRAININGPLOT_FIGURE'))
[YPredicted,probs] = classify(trainedNet,augimdsValidation);
if strcmp(measure,'accuracy')
valError = 1 - mean(YPredicted == YValidation);
%display('accuracy');
else
[a,b,c,auc] = perfcurve(YValidation,probs(:,2),'2');
valError = 1 - auc;
%display('auc based');
end
fileName = num2str(valError) + ".mat";
save(fileName,'trainedNet','valError','options')
cons = [];
end
end