-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateDeepNN.m
More file actions
180 lines (153 loc) · 5.48 KB
/
Copy pathcreateDeepNN.m
File metadata and controls
180 lines (153 loc) · 5.48 KB
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
%% Deep learning neural network just infection
digitDatasetPath = fullfile('C:\Users\MainUser\Pictures\Catagories');
imds2 = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
% Convert lables to '0' and '1'
fullLabel = imds2.Labels;
strgs = cell(length(fullLabel),1);
for loop = 1:length(fullLabel)
temp = char(fullLabel(loop));
switch temp
case 'None'
strgs(loop) = {'None'};
case 'High'
strgs(loop) = {'Infection'};
case 'Low'
strgs(loop) = {'Infection'};
case 'Moderate'
strgs(loop) = {'Infection'};
case 'OutOfFocus'
strgs(loop) = {'None'};
otherwise
end
end
catLabel = categorical(strgs);
imds2.Labels = catLabel;
% noNum = length(find(numLabel==0))
% yesNum = length(find(numLabel==1))
% End convert labels
T = countEachLabel(imds2)
img = readimage(imds2,1);
[rowNum,colNum,colors] = size(img);
numTrainFiles = .3;
[imdsValidation2,imdsTrain2] = splitEachLabel(imds2,numTrainFiles,'randomize');
% trainCount = countEachLabel(imdsTrain2);
% mincount = min(trainCount.Count);
% [imdsTrain2,~] = splitEachLabel(imdsTrain2,mincount,'randomize');
gNet = googlenet;
lgraph = layerGraph(gNet);
lgraph = removeLayers(lgraph, {'loss3-classifier','prob','output'});
numClasses = numel(categories(imdsTrain2.Labels));
newLayers = [
fullyConnectedLayer(numClasses,'Name','fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10)
softmaxLayer('Name','softmax')
classificationLayer('Name','classoutput')];
lgraph = addLayers(lgraph,newLayers);
lgraph = connectLayers(lgraph,'pool5-drop_7x7_s1','fc');
inputSize = lgraph.Layers(1).InputSize;
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true,...
'RandYReflection',true);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain2, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation2);
options2 = trainingOptions('sgdm', ...
'MaxEpochs',8, ...
'ValidationData',augimdsValidation, ...
'InitialLearnRate',1e-6, ...
'ValidationFrequency',15, ...
'ValidationPatience',20,...
'Verbose',true, ...
'Plots','training-progress',...
'ExecutionEnvironment','auto',...
'MiniBatchSize',10); % 32
% analyzeNetwork(lgraph);
countEachLabel(imdsTrain2)
countEachLabel(imdsValidation2)
net2 = trainNetwork(augimdsTrain,lgraph,options2);
%
YPred = classify(net2,imdsValidation2);
YValidation = imdsValidation2.Labels;
% accuracy = sum(YPred == YValidation)/numel(YValidation)
plotconfusion(YValidation,YPred)
% Save network
save('CNN7_08June2018.mat','net2')
%% Deep learning neural network Level of infection
digitDatasetPath = fullfile('C:\Users\MainUser\Pictures\Catagories');
imds2 = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
% Convert lables to '0' and '1'
fullLabel = imds2.Labels;
strgs = cell(length(fullLabel),1);
for loop = 1:length(fullLabel)
temp = char(fullLabel(loop));
switch temp
case 'None'
strgs(loop) = {'None'};
case 'High'
strgs(loop) = {'High'};
case 'Low'
strgs(loop) = {'Low'};
case 'Moderate'
strgs(loop) = {'Moderate'};
case 'OutOfFocus'
strgs(loop) = {'None'};
otherwise
end
end
catLabel = categorical(strgs);
imds2.Labels = catLabel;
% noNum = length(find(numLabel==0))
% yesNum = length(find(numLabel==1))
% End convert labels
T = countEachLabel(imds2)
img = readimage(imds2,1);
[rowNum,colNum,colors] = size(img);
numTrainFiles = .3;
[imdsValidation2,imdsTrain2] = splitEachLabel(imds2,numTrainFiles,'randomize','Exclude','None');
trainCount = countEachLabel(imdsTrain2);
mincount = min(trainCount.Count);
[imdsTrain2,~] = splitEachLabel(imdsTrain2,mincount,'randomize');
gNet = googlenet;
lgraph = layerGraph(gNet);
lgraph = removeLayers(lgraph, {'loss3-classifier','prob','output'});
numClasses = numel(categories(imdsTrain2.Labels));
newLayers = [
fullyConnectedLayer(numClasses,'Name','fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10)
softmaxLayer('Name','softmax')
classificationLayer('Name','classoutput')];
lgraph = addLayers(lgraph,newLayers);
lgraph = connectLayers(lgraph,'pool5-drop_7x7_s1','fc');
layers = lgraph.Layers;
connections = lgraph.Connections;
layers(1:110) = freezeWeights(layers(1:110));
lgraph = createLgraphUsingConnections(layers,connections);
inputSize = lgraph.Layers(1).InputSize;
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true,...
'RandYReflection',true);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain2, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation2);
options2 = trainingOptions('sgdm', ...
'MaxEpochs',8, ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',15, ...
'ValidationPatience',20,...
'Verbose',true, ...
'Plots','training-progress',...
'ExecutionEnvironment','auto',...
'MiniBatchSize',10); % 32
% analyzeNetwork(lgraph);
countEachLabel(imdsTrain2)
countEachLabel(imdsValidation2)
net3 = trainNetwork(augimdsTrain,lgraph,options2);
%
YPred = classify(net3,imdsValidation2);
YValidation = imdsValidation2.Labels;
% accuracy = sum(YPred == YValidation)/numel(YValidation)
plotconfusion(YValidation,YPred)
% Save network
save('CNN8_08June2018.mat','net3')