what is the difference between using SVM classifier and normal CNN for image classification?
Show older comments
Hello there
I'm was trying to build an image classification program and I was following these examples:
First one: https://www.mathworks.com/help/vision/ug/image-category-classification-using-deep-learning.html
I used Alexnet for both examples and the accuracy was really different
for the first example I got 99.14% accuracy , and for the second example I got 90%
and the second example was taking so long to train the network, I don't know why is that
also, the first example didn't give me the plots or anything for the training process, how could I do that?
I notice the different is using something called SVM classifier in the first example, but actually I don't know what is that mean, could someone explain it to me, please?
First example code:
tic
clear;
clc;
outputFolder = fullfile('Spilt_Dataset');
rootFolder = fullfile(outputFolder , 'Categories');
categories = {'front_or_left' , 'front_or_right','hump' , 'left_turn','narrow_from_left' , 'narrows_from_right',...
'no_horron' , 'no_parking','no_u_turn' , 'overtaking_is_forbidden','parking' , 'pedestrian_crossing',...
'right_or_left' , 'right_turn','rotor' , 'slow','speed_30' , 'speed_40',...
'speed_50','speed_60','speed_80','speed_100','stop','u_turn'};
imds = imageDatastore(fullfile(rootFolder,categories),'LabelSource','Foldernames');
tbl = countEachLabel(imds);
minSetCount = min(tbl{:,2});
imds = splitEachLabel(imds, minSetCount, 'randomize');
countEachLabel(imds);
net = alexnet();
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[TrainingSet, TestSet] = splitEachLabel(imds,0.8, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize,...
TrainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize,...
TestSet, 'ColorPreprocessing', 'gray2rgb');
wl = net.Layers(2).Weights;
wl = mat2gray(wl);
featureLayer = 'fc6';
trainingFeatures = activations(net,...
augmentedTrainingSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
TrainingLables = TrainingSet.Labels;
classifier = fitcecoc(trainingFeatures, TrainingLables, 'Learner',...
'Linear', 'Coding', 'onevsall' , 'ObservationsIn', 'columns');
TestFeatures = activations(net, ...
augmentedTestSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
predictLabels = predict(classifier, TestFeatures, 'ObservationsIn', 'columns');
TestLabels = TestSet.Labels;
confMat = confusionmat(TestLabels, predictLabels);
confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
mean(diag(confMat))
toc
The second example code:
tic
clear;
clc;
outputFolder = fullfile('Spilt_Dataset');
rootFolder = fullfile(outputFolder , 'Categories');
categories = {'front_or_left' , 'front_or_right','hump' , 'left_turn','narrow_from_left' , 'narrows_from_right',...
'no_horron' , 'no_parking','no_u_turn' , 'overtaking_is_forbidden','parking' , 'pedestrian_crossing',...
'right_or_left' , 'right_turn','rotor' , 'slow','speed_30' , 'speed_40',...
'speed_50','speed_60','speed_80','speed_100','stop','u_turn'};
imds = imageDatastore(fullfile(rootFolder,categories),'LabelSource','Foldernames');
tbl = countEachLabel(imds);
minSetCount = min(tbl{:,2});
imds = splitEachLabel(imds, minSetCount, 'randomize');
countEachLabel(imds);
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.8,'randomized');
net = alexnet;
analyzeNetwork(net);
inputSize = net.Layers(1).InputSize;
layersTransfer = net.Layers(1:end-3);
numClasses = 24;
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',128, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-3, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',160, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,layers,options);
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!