-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask4_1.m
259 lines (214 loc) · 8.54 KB
/
task4_1.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
%% Preparation of data
clear
close all
clc
% Config variables
% who_to_train: 0-> only two_classes CNN, 1->only four_classes CNN, 2->both
who_to_train = 2;
% Preparing labels
anger_cell = cell(1,250);
anger_cell(:) = {'Anger'};
disgust_cell = cell(1,250);
disgust_cell(:) = {'Disgust'};
fear_cell = cell(1,250);
fear_cell(:) = {'Fear'};
happiness_cell = cell(1,250);
happiness_cell(:) = {'Happiness'};
emotions_labels_four = categorical([anger_cell disgust_cell fear_cell happiness_cell]);
emotions_labels_two = categorical([disgust_cell fear_cell]);
% Base image to get the proper size for the first layer of the CNN
base_img = imread('datasets/images/10011.jpg');
base_img_size = size(base_img);
% Creation of the two datastore, first one is for a 4-classes
% classification, second for a 2-classes classification
imds_four = imageDatastore('datasets/images/images_to_use_experiment_2', 'labels', emotions_labels_four);
imds_two = imageDatastore('datasets/images/images_to_use_experiment_2_two_classes', 'labels', emotions_labels_two);
% Data split in training and testing set
fracTrain = 0.8;
[imdsTrain_two,imdsTest_two] = splitEachLabel(imds_two,fracTrain,'randomize');
[imdsTrain_four,imdsTest_four] = splitEachLabel(imds_four,fracTrain,'randomize');
%Method for image augmentation
imageAugmenter = imageDataAugmenter('RandRotation',[-20,20],...
'RandXReflection', true, ...
'RandXTranslation', [-10 10], ...
'RandYTranslation', [-10 10]);
augimds_two = augmentedImageDatastore(base_img_size,imdsTrain_two,'DataAugmentation',imageAugmenter);
augimds_four = augmentedImageDatastore(base_img_size,imdsTrain_four,'DataAugmentation',imageAugmenter);
%CNN layers for two-classes classification (fear and disgust emotions)
leaky_layers_two = [
imageInputLayer(size(base_img))
convolution2dLayer(16,6,'Stride',4,'Padding','same')
batchNormalizationLayer
leakyReluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(8,12,'Stride',2,'Padding','same')
batchNormalizationLayer
leakyReluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(40)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer
];
%CNN layers for four-classes classification
leaky_layers_four = [
imageInputLayer(size(base_img))
convolution2dLayer(16,8,'Stride',4,'Padding','same')
batchNormalizationLayer
leakyReluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(8,16,'Stride',2,'Padding','same')
batchNormalizationLayer
leakyReluLayer
maxPooling2dLayer(2,'Stride', 1)
convolution2dLayer(4,32,'Stride',2,'Padding','same')
batchNormalizationLayer
leakyReluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(100)
fullyConnectedLayer(100)
fullyConnectedLayer(4)
softmaxLayer
classificationLayer
];
% Traces of other experiments
% ---------------------------
% layers = [
% imageInputLayer(size(base_img))
%
% convolution2dLayer(10,24,'Stride',4,'Padding','same')
% batchNormalizationLayer
% reluLayer
%
% maxPooling2dLayer(2,'Stride',2)
%
% convolution2dLayer(5,48,'Stride',2,'Padding','same')
% batchNormalizationLayer
% reluLayer
%
% maxPooling2dLayer(2,'Stride',2)
%
% convolution2dLayer(3,96,'Stride',1,'Padding','same')
% batchNormalizationLayer
% reluLayer
%
% maxPooling2dLayer(2,'Stride',2)
%
% convolution2dLayer(3,96,'Stride',1,'Padding','same')
% batchNormalizationLayer
% leakyReluLayer
%
% maxPooling2dLayer(2,'Stride',2)
%
% convolution2dLayer(3,96,'Stride',1,'Padding','same')
% batchNormalizationLayer
% leakyReluLayer
%
% fullyConnectedLayer(100)
% fullyConnectedLayer(4)
%
% softmaxLayer
%
% classificationLayer
% ];
%% Training
%Training options for two CNNs
options_sgdm_two = trainingOptions('sgdm', ...
'InitialLearnRate', 0.01, ...
'MiniBatchSize', 40, ...
'MaxEpochs', 200, ...
'Shuffle','every-epoch', ...
'ValidationData', imdsTest_two, ...
'ValidationFrequency', 20, ...
'Verbose', false, ...
'Plots', 'training-progress');
options_sgdm_four = trainingOptions('sgdm', ...
'InitialLearnRate', 0.01, ...
'MiniBatchSize', 40, ...
'MaxEpochs', 100, ...
'Shuffle','every-epoch', ...
'ValidationData', imdsTest_four, ...
'ValidationFrequency', 20, ...
'Verbose', false, ...
'Plots', 'training-progress');
if who_to_train == 2
net_sgdm_leaky_two = trainNetwork(augimds_two,leaky_layers_two,options_sgdm_two);
net_sgdm_leaky_four = trainNetwork(augimds_four,leaky_layers_four,options_sgdm_four);
elseif who_to_train == 1
net_sgdm_leaky_four = trainNetwork(augimds_four,leaky_layers_four,options_sgdm_four);
else
net_sgdm_leaky_two = trainNetwork(augimds_two,leaky_layers_two,options_sgdm_two);
end
% Traces of other experiments
% ---------------------------
% options_adam = trainingOptions('adam', ...
% 'InitialLearnRate', 0.01, ...
% 'MiniBatchSize', 20, ...
% 'MaxEpochs', 40, ...
% 'Shuffle','every-epoch', ...
% 'ValidationData', imdsTest, ...
% 'ValidationFrequency', 4, ...
% 'Verbose', false, ...
% 'Plots', 'training-progress');
%
% options_rmsprop = trainingOptions('rmsprop', ...
% 'InitialLearnRate', 0.01, ...
% 'MiniBatchSize', 20, ...
% 'MaxEpochs', 40, ...
% 'Shuffle','every-epoch', ...
% 'ValidationData', imdsTest, ...
% 'ValidationFrequency', 20, ...
% 'Verbose', false, ...
% 'Plots', 'training-progress');
% net_sgdm = trainNetwork(augimds,layers,options_sgdm);
% net_adam = trainNetwork(augimds,layers,options_adam);
% net_rmsprop = trainNetwork(augimds,layers,options_rmsprop);
% net_adam_leaky = trainNetwork(augimds,leaky_layers,options_adam);
% net_rmsprop_leaky = trainNetwork(augimds,leaky_layers,options_rmsprop);
%% Performance Assessment
%Plotting of confusion matrix for given test set
if who_to_train == 2
predLabels_sgdm_leaky_two = classify(net_sgdm_leaky_two,imdsTest_two);
predLabels_sgdm_leaky_four = classify(net_sgdm_leaky_four,imdsTest_four);
testLabels_two = imdsTest_two.Labels;
testLabels_four = imdsTest_four.Labels;
accuracy_sgdm_leaky_two = sum(predLabels_sgdm_leaky_two == testLabels_two)/numel(testLabels_two);
fprintf('Accuracy sgdm leaky 2 classes is %8.2f%%\n',accuracy_sgdm_leaky_two*100);
accuracy_sgdm_leaky_four = sum(predLabels_sgdm_leaky_four == testLabels_four)/numel(testLabels_four);
fprintf('Accuracy sgdm leaky 4 classes is %8.2f%%\n',accuracy_sgdm_leaky_four*100);
figure
plotconfusion(testLabels_two, predLabels_sgdm_leaky_two, 'Confusion Matrix 2 Classes');
figure
plotconfusion(testLabels_four, predLabels_sgdm_leaky_four, 'Confusion Matrix 4 Classes');
elseif who_to_train == 1
predLabels_sgdm_leaky_four = classify(net_sgdm_leaky_four,imdsTest_four);
testLabels_four = imdsTest_four.Labels;
accuracy_sgdm_leaky_four = sum(predLabels_sgdm_leaky_four == testLabels_four)/numel(testLabels_four);
fprintf('Accuracy sgdm leaky 4 classes is %8.2f%%\n',accuracy_sgdm_leaky_four*100);
figure
plotconfusion(testLabels_four, predLabels_sgdm_leaky_four, 'Confusion Matrix 4 Classes');
else
predLabels_sgdm_leaky_two = classify(net_sgdm_leaky_two,imdsTest_two);
testLabels_two = imdsTest_two.Labels;
accuracy_sgdm_leaky_two = sum(predLabels_sgdm_leaky_two == testLabels_two)/numel(testLabels_two);
fprintf('Accuracy sgdm leaky 2 classes is %8.2f%%\n',accuracy_sgdm_leaky_two*100);
figure
plotconfusion(testLabels_two, predLabels_sgdm_leaky_two, 'Confusion Matrix 2 Classes');
end
% Traces of other experiments
% ---------------------------
% predLabels_sgdm = classify(net_sgdm,imdsTest);
% predLabels_adam = classify(net_adam,imdsTest);
% predLabels_rmsprop = classify(net_rmsprop,imdsTest);
% predLabels_adam_leaky = classify(net_adam_leaky,imdsTest);
% predLabels_rmsprop_leaky = classify(net_rmsprop_leaky,imdsTest);
% accuracy_sgdm = sum(predLabels_sgdm == testLabels)/numel(testLabels);
% fprintf('Accuracy sgdm is %8.2f%%\n',accuracy_sgdm*100);
% accuracy_adam = sum(predLabels_adam == testLabels)/numel(testLabels);
% fprintf('Accuracy adam is %8.2f%%\n',accuracy_adam*100);
% accuracy_rmsprop = sum(predLabels_rmsprop == testLabels)/numel(testLabels);
% fprintf('Accuracy rmsprop is %8.2f%%\n',accuracy_rmsprop*100);
% accuracy_adam_leaky = sum(predLabels_adam_leaky == testLabels)/numel(testLabels);
% fprintf('Accuracy adam leaky is %8.2f%%\n',accuracy_adam_leaky*100);
% accuracy_rmsprop_leaky = sum(predLabels_rmsprop_leaky == testLabels)/numel(testLabels);
% fprintf('Accuracy rmsprop leaky is %8.2f%%\n',accuracy_rmsprop_leaky*100);