-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.m
36 lines (24 loc) · 1.1 KB
/
source.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
% add all subfolders to the path
folder = fileparts(which(mfilename));
addpath(genpath(folder));
% --------------- training ------------------------------------------------
if exist('classifier.mat', 'file') ~= 2
imdsTrain = imageDatastore(fullfile('data','training'),...
'IncludeSubfolders',true,'FileExtensions','.png','LabelSource','foldernames');
imdsTest = imageDatastore(fullfile('data','publicTest'),...
'IncludeSubfolders',true,'FileExtensions','.png','LabelSource','foldernames');
net = alexnet;
inputSize = net.Layers(1).InputSize;
layer = 'fc7';
augimdsTrain = augmentedImageDatastore([227, 227, 3],imdsTrain);
featuresTrain = activations(net,augimdsTrain,layer,'OutputAs','rows');
augimdsTest = augmentedImageDatastore(inputSize,imdsTest);
featuresTest = activations(net,augimdsTest,layer,'OutputAs','rows');
YTrain = imdsTrain.Labels;
YTest = imdsTest.Labels;
classifier = fitcecoc(featuresTrain,YTrain);
YPred = predict(classifier,featuresTest);
accuracy = mean(YPred == YTest);
else
load classifier.mat;
end