-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCosineKNNClassifierScript.m
More file actions
146 lines (109 loc) · 4.52 KB
/
CosineKNNClassifierScript.m
File metadata and controls
146 lines (109 loc) · 4.52 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
%% CAP 4630 - Intro to AI - FAU - Dr. Marques - Fall 2016
% Group number 12
% Members: Ernso Jean-Louis, Willene Nazaire, Aaron Reich
%% Download/load Pre-trained Convolutional Neural Network (CNN)
% 1: Location of pre-trained "AlexNet"
cnnURL = 'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-caffe-alex.mat';
% Specify folder for storing CNN model
cnnFolder = './networks';
cnnMatFile = 'imagenet-caffe-alex.mat';
cnnFullMatFile = fullfile(cnnFolder, cnnMatFile);
% Check that the code is only downloaded once
if ~exist(cnnFullMatFile, 'file')
disp('Downloading pre-trained CNN model...');
websave(cnnFullMatFile, cnnURL);
end
% Load MatConvNet network into a SeriesNetwork
convnet = helperImportMatConvNet(cnnFullMatFile);
%% Set up image data
% Load simplified dataset and build image store
dataFolder = './data/SomePetImages';
categories = {'cat', 'dog'};
imds = imageDatastore(fullfile(dataFolder, categories), 'LabelSource', 'foldernames');
tbl = countEachLabel(imds);
% Use the smallest overlap set
minSetCount = min(tbl{:,2});
% Use splitEachLabel method to trim the set.
imds = splitEachLabel(imds, minSetCount, 'randomize');
%% Pre-process Images For CNN
imds.ReadFcn = @(filename)readAndPreprocessImage(filename);
%% Divide data into training and testing sets
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
%% Feature Extraction
% Get the network weights for the second convolutional layer
w1 = convnet.Layers(2).Weights;
% Scale and resize the weights for visualization
w1 = mat2gray(w1);
w1 = imresize(w1,5);
% Use features from one of the deeper layers
featureLayer = 'fc7';
trainingFeaturesFolder = './';
trainingFeaturesFile = 'trainingSomeFeatures.mat';
trainingFeaturesFullMatFile = fullfile(trainingFeaturesFolder, trainingFeaturesFile);
% Check that the code is only downloaded once
if ~exist(trainingFeaturesFullMatFile, 'file')
disp('Building training features... This will take a while...');
trainingFeatures = activations(convnet, trainingSet, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns');
save(trainingFeaturesFullMatFile, 'trainingFeatures');
else
load trainingSomeBlurryFeatures.mat
end
% Extract features from images in the test set
testFeaturesFolder = './';
testFeaturesFile = 'testSomeFeatures.mat';
testFeaturesFullMatFile = fullfile(testFeaturesFolder, testFeaturesFile);
% Check that the code is only downloaded once
if ~exist(testFeaturesFullMatFile, 'file')
disp('Extracting features... This will take a while...');
% Extract test features using the CNN
testFeatures = activations(convnet, testSet, featureLayer, 'MiniBatchSize',32);
% Save features for future use
save(testFeaturesFullMatFile, 'testFeatures');
else
load testSomeBlurryFeatures.mat
end
%% Load model
load CosineKNNClassifier.mat
rng(32);
%% Train classifier
Model = CosineKNNClassifier.ClassificationKNN;
predictedLabels = predict(Model, trainingFeatures);
% Get the known labels
trainingLabels = trainingSet.Labels;
% Tabulate the results using a confusion matrix.
confMat = confusionmat(trainingLabels, predictedLabels);
% Convert confusion matrix into percentage form
confMat = bsxfun(@rdivide,confMat,sum(confMat,2))
%% Display and Compute ROC curve
% Compute the ROC curve.
% Label the test sample observations.
% Display the results for the observations in the test sample.
[label_train,score_train] = predict(Model,trainingFeatures);
[X_train,Y_train,T_train,AUC_train] = perfcurve(trainingLabels,score_train(:,1),'cat');
% Plot the ROC curve
figure(1), plot(X_train,Y_train)
xlabel('False positive rate')
ylabel('True positive rate')
title('ROC for Classification by KNN, Training Data Set')
disp(AUC_train)
%% Test classifier's prediction accuracy and produce confusion matrix
predictedLabels1 = predict(Model, testFeatures);
% Get the known labels
testLabels = testSet.Labels;
% Tabulate the results using a confusion matrix.
confMat1 = confusionmat(testLabels, predictedLabels1);
% Convert confusion matrix into percentage form
confMat1 = bsxfun(@rdivide,confMat1,sum(confMat1,2))
%% Display and Compute ROC curve
% Compute the ROC curve.
% Label the test sample observations.
% Display the results for the observations in the test sample.
[label_test,score_test] = predict(Model,testFeatures);
[X_test,Y_test,T_test,AUC_test] = perfcurve(testLabels,score_test(:,1),'cat');
% Plot the ROC curve
figure(2), plot(X_test,Y_test)
xlabel('False positive rate')
ylabel('True positive rate')
title('ROC for Classification by KNN, Test Data Set')
disp(AUC_test)