-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification.m
149 lines (125 loc) · 3.97 KB
/
classification.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%% Flower Classification
clear all; close all; clc;
%% Load Data
%myFolder = "H:\Matlab\ComputerVisionCoursework\17flowers";
myFolder = "~/Documents/MATLAB/ComputerVisionCoursework1/17flowers"
%%
imds = imageDatastore(myFolder, ...
'IncludeSubfolders', true, ...
'LabelSource','foldernames');
%imds = imresize(imds, [256 256 3]);
%imds = transform(rimds,@(x) imresize(x,[256 256 3]));
% resize the images
imds.ReadFcn = @(loc)imresize(imread(loc),[256 256]);
%% Divide data into training and validation sets
% Could do 70%-15%-15% train-validate-test -> 56-12-12
numTrain = 64;
numVal = 8;
numTest = 8;
[imgTrain, imgVal, imgTest] = splitEachLabel(imds,numTrain, numVal, numTest, 'randomized');
%%
% disp(imgTrain.Labels(240))
%% Data Augmentation
%imgTrain2 = transform(imgTrain, 'ReadFcn', @(x)classificationAugmentationPipeline(imread(x), ''));
imageAugmenter = imageDataAugmenter(...
'RandRotation',[-30 30], ...
'RandScale',[0.5 4], ...
'RandXShear',[-45 45], ...
'RandYShear',[-45 45])
auimds = augmentedImageDatastore([256 256], imgTrain, 'ColorPreprocessing','gray2rgb', 'DataAugmentation',imageAugmenter)
%%
% disp(numel(imgTrain.Files))
% imgTrain3 = imageDatastore(imgTrain2)
%% Define Network Architecture
inputSize = [256 256 3];
numClasses = 17;
layers = [
imageInputLayer(inputSize)
convolution2dLayer(3, 32)
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 32)
batchNormalizationLayer
reluLayer
%dropoutLayer(0.2)
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 64)
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 64)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 128)
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 128)
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 128)
batchNormalizationLayer
reluLayer
%maxPooling2dLayer(2, 'Stride', 2)
groupedConvolution2dLayer(3,1,'channel-wise','Stride',2,'Padding','same')
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 64)
batchNormalizationLayer
reluLayer
reluLayer
convolution2dLayer(3, 64)
batchNormalizationLayer
reluLayer
%maxPooling2dLayer(2, 'Stride', 2)
groupedConvolution2dLayer(3,1,'channel-wise','Stride',2,'Padding','same')
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 32)
batchNormalizationLayer
reluLayer
convolution2dLayer(3, 32)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
fullyConnectedLayer(16)
reluLayer
dropoutLayer(0.3)
fullyConnectedLayer(16)
reluLayer
%dropoutLayer(0.2)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
%% Hyperparameters of Network
options = trainingOptions('adam', 'MaxEpochs', 50, 'ValidationData', imgVal, ...
'Verbose', true, 'Plots', 'training-progress', 'ValidationFrequency', 10)
%% Train Network
cNet = trainNetwork(auimds, layers, options);
%% Save Network
% save('H:\Matlab\ComputerVisionCoursework\myNet.mat', 'cNet');
load("~/Documents/MATLAB/ComputerVisionCoursework1/classnet.mat");
%% Test Network
YPred = classify(cNet, imgTest);
YTesting = imgTest.Labels;
accuracy = mean(YPred == YTesting)
%%
%className = ['bluebell '; 'buttercup '; 'coltsfoot '; 'cowslip '; 'crocus '; 'daffodil '; 'daisy '; 'dandelion '; 'fritillary'; 'iris '; 'lilyvalley'; 'pansy '; 'snowdrop '; 'sunflower '; 'tigerlily '; 'tulip '; 'windflower'];
size(className3)
%% Evaluation
label = predict(cNet,imgTest);
%%
%label1 = predict(cNet,imgTest.Files(1));
I4 = readimage(imgTest, 1);
m = classify(cNet, I4);
figure
imshow(I4)
title(string(m))
%%
lablR = (imgTest.Files(1))
%%
t = rms(label, "all");
t
%%
size(label)
%%
rocObj = rocmetrics(YTesting, label, imds.Labels);