-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDL_classifier.m
64 lines (35 loc) · 1.31 KB
/
DL_classifier.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
%train and validation using alex net
DatasetPath=('E:\UNL\semester 1\CSI 8300 cv and ıp\PA_B\leaf\Test\Pongamia Pinnata (P7)');
%Read images from the image folder
images=imageDatastore(DatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames');
%disturbuting images in the set of training and testing
numTrainFiles=230;
[TrainImages,TestImages]=splitEachLabel(images,numTrainFiles,'randomize');
net= alexnet;
layersTransfer=net.Layers(1:end-3);
%transfer learning
numClasses=2;
%define layers of Alexnet
layers=[
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
%Training options
options=trainingOptions( 'sgdm',...
'MiniBatchSize',20,...
'MaxEpochs',8, ...
'InitialLearnRate', 0.0001,...
'Shuffle','every-epoch', ...
'ValidationData',TestImages,...
'ValidationFrequency', 10, ...
'Verbose', false,...
'Plots','training-progress');
%training the Alexnet
netTransfer=trainNetwork(TrainImages, layers,options);
%classifying the Alexnet
Ypred=classify(netTransfer, TestImages);
YValidation=TestImages.Labels;
accuracy = sum(Ypred==YValidation)/ numel(YValidation)
% plot condusion matrix
plotconfusion(YValidation, Ypred);