-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNNCounter.m
65 lines (53 loc) · 1.6 KB
/
CNNCounter.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
% Convolutional Neural Network Counter
% Authors:
% Daniel Rodriguez
% Sebastian Salazar
% Reference: https://youtu.be/lK9YyX-q32k
close all
clear all
clc
%% Get images from folder
imgFolder = '/imgBinary';
imds = imageDatastore(imgFolder,...
'IncludeSubFolders', true, 'LabelSource', 'foldernames');
%% Split data in 60% training, 20% validation and 20% test
fracTrainFiles = 0.6;
fracValFiles = 0.2;
fracTestFiles = 0.2;
[imdsTrain, imdsValidation, imdsTest] = splitEachLabel(imds, ...
fracTrainFiles, fracValFiles, fracTestFiles, 'randomize');
layers = [
imageInputLayer([150 150 1])
convolution2dLayer(5,10,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(5,10,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(3)
softmaxLayer
classificationLayer
];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',100, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
%% Get accuracy
YPred = classify(net,imdsTest);
YTest = imdsTest.Labels;
accuracy = sum(YPred == YTest)/numel(YTest)
%% Show images data couldn't get predicted
ind = find(YPred ~= YTest);
figure;
for ii = 1:length(ind)
subplot(3,3,ii);
imagesc(imdsValidation.readimage(ind(ii)));
title([num2str(double(YPred(ind(ii)))-1), ' predicted, ', ...
num2str(double(YTest(ind(ii)))-1), ' actual'])
end