-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNN CNN Depth.m
128 lines (119 loc) · 3.69 KB
/
KNN CNN Depth.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
%% Fruit Kinect Code
clear;
clc;
close all;
warning ('off');
%% Data Reading and Pre-Processing
path='DepthDB';
fileinfo = dir(fullfile(path,'*.png'));
filesnumber=size(fileinfo);
for i = 1 : filesnumber(1,1)
images{i} = imread(fullfile(path,fileinfo(i).name));
disp(['Loading image No : ' num2str(i) ]);
end;
%% Feature Extraction
% Extract SURF Features
% imset = imageSet('DepthCNN','recursive');
% % Create a bag-of-features from the image database
% bag = bagOfFeatures(imset,'VocabularySize',40,'PointSelection','Detector');
% % Encode the images as new features
% SURF = encode(bag,imset);
%-------------------------------------------------
% Extract LPQ Features
% More value for winsize, better result
winsize=19;
for i = 1 : filesnumber(1,1)
tmp{i}=lpq(images{i},winsize);
disp(['Extract LPQ : ' num2str(i) ]);end;
for i = 1 : filesnumber(1,1)
LPQ(i,:)=tmp{i};end;
% Combining Feature Matrixes
FinalReady=LPQ;
% Labeling for Supervised Learning
sizefinal=size(FinalReady);
sizefinal=sizefinal(1,2);
FinalReady(1:200,sizefinal+1)=1;
FinalReady(201:400,sizefinal+1)=2;
FinalReady(401:600,sizefinal+1)=3;
FinalReady(601:800,sizefinal+1)=4;
%% KNN Classification
lblknn=FinalReady(:,end);
dataknn=FinalReady(:,1:end-1);
Mdl = fitcknn(dataknn,lblknn,'NumNeighbors',5,'Standardize',1)
rng(1); % For reproducibility
knndat = crossval(Mdl);
classError = kfoldLoss(knndat);
Lknn = resubLoss(Mdl,'LossFun','classiferror');
KNNAccuracy = 1 - kfoldLoss(knndat, 'LossFun', 'ClassifError');
% Predict the labels of the training data.
predictedknn = resubPredict(Mdl);
sizenet=size(FinalReady);
sizenet=sizenet(1,1);
ct=0;
for i = 1 : sizenet(1,1)
if lblknn(i) ~= predictedknn(i)
ct=ct+1;
end;end;
% Compute Accuracy
finsvm=ct*100/ sizenet;
SVMAccuracy=(100-finsvm);
% Confusion Matrix
figure
cmknn = confusionchart(lblknn,predictedknn);
cmknn.Title = ['KNN Classification = ' num2str(SVMAccuracy) '%'];
cmknn.RowSummary = 'row-normalized';
cmknn.ColumnSummary = 'column-normalized';
% ROC
[~,scoreknn] = resubPredict(Mdl);
diffscoreknn = scoreknn(:,2) - max(scoreknn(:,1),scoreknn(:,3));
[Xknn,Yknn,T,~,OPTROCPTknn,suby,subnames] = perfcurve(lblknn,diffscoreknn,1);
figure;
plot(Xknn,Yknn)
hold on
plot(OPTROCPTknn(1),OPTROCPTknn(2),'ro')
xlabel('False positive rate')
ylabel('True positive rate')
title('ROC Curve for KNN')
hold off
%
%% Deep Neural Network
% CNN
deepDatasetPath = fullfile('DepthCNN');
imds = imageDatastore(deepDatasetPath, ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
% Number of training (less than number of each class)
numTrainFiles = 160;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
% Input image size for instance: 512 512 3
imageInputLayer([256 256 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
% Number of classes
fullyConnectedLayer(4)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.001, ...
'MaxEpochs',15, ...
'MiniBatchSize',32, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',9, ...
'Verbose',false, ...
'Plots','training-progress');
netmacro = trainNetwork(imdsTrain,layers,options);
YPred = classify(netmacro,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation) *100;
disp(['CNN Macro Recognition Accuracy Is = ' num2str(accuracy) ]);