-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining_code.m
More file actions
109 lines (82 loc) · 2.42 KB
/
Copy pathtraining_code.m
File metadata and controls
109 lines (82 loc) · 2.42 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
%%
clc
clear all
close all
%% Transfer learning Approach
% Create Image data store from Processed data (3 channel).
imds = imageDatastore('ProcessedData2', ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
% Dividing data into train and validation. 70-30 Split
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%Display some sample images.
numTrainImages = numel(imdsTrain.Labels);
idx = randperm(numTrainImages,16);
figure
for i = 1:16
subplot(4,4,i)
I = readimage(imdsTrain,idx(i));
imshow(I)
end
%Load Pretrained Network
net = alexnet;
% Extracting all layers except last 3.
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels));
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
%% Network Training
%Uncomment the section if training is required.
options = trainingOptions('sgdm',...
'MiniBatchSize',5,...
'MaxEpochs',10,...
'InitialLearnRate',0.0001);
% % Fine-tune the network using |trainNetwork| on the new layer array.
netTransfer = trainNetwork(imdsTrain,layers,options);
save('netTransfer.mat', 'netTransfer');
%%
load('netTransfer');
% Classify the test images using |classify|.
predictedLabels = classify(netTransfer,imdsValidation);
%%
% Display four sample test images with their predicted labels.
idx = [1 33 20 41];
figure
for i = 1:numel(idx)
subplot(2,2,i)
I = readimage(imdsValidation,idx(i));
label = predictedLabels(idx(i));
imshow(I)
title(char(label))
drawnow
end
%%
% Calculate the classification accuracy.
testLabels = imdsValidation.Labels;
accuracy = sum(predictedLabels==testLabels)/numel(predictedLabels)
%% Feature Extraction Approach
%Load Pretrained Network
net = alexnet;
layer = 'fc7';
featuresTrain = activations(net,imdsTrain,layer,'OutputAs','rows');
featuresTest = activations(net,imdsValidation,layer,'OutputAs','rows');
%Extract the class labels from the training and test data.
YTrain = imdsTrain.Labels;
YTest = imdsValidation.Labels;
%Fit svm
classifier = fitcecoc(featuresTrain,YTrain);
YPred = predict(classifier,featuresTest);
idx = [1 5 10 15];
figure
for i = 1:numel(idx)
subplot(2,2,i)
I = readimage(imdsValidation,idx(i));
label = YPred(idx(i));
imshow(I)
title(char(label))
end
accuracy = mean(YPred == YTest)
save('main.mat');