what is the difference between using SVM classifier and normal CNN for image classification?
2 views (last 30 days)
Show older comments
Abdulaziz Alotaibi
on 16 Apr 2021
Commented: Abdulaziz Alotaibi
on 28 Apr 2021
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);
0 Comments
Accepted Answer
Pratyush Roy
on 27 Apr 2021
Edited: Pratyush Roy
on 27 Apr 2021
Hi,
In the first case we are using the Alexnet layers as a feature extractor, i.e., to transform the image to its representative feature. The color image is converted to a 1-d feature vector. The feature vectors obtained for multiple images are then fed to a support vector machine(SVM) classifier which classifies the images to their respective classes. Since the Alexnet layers are good feature extractors and the SVM is a strong classifier, the accuracy value is significantly high.We are not training any of the network layers in the first case, so the training process cannot be visualised. The only training occurs here in case of SVM, which we cannot visualize.
On the other hand, in the next case we are using the entire Alexnet as both feature extractor as well as classifier and training them. As a result, we can visualise the training process. However this network is not as strong a classifier as SVM, as a result the accuracy value drops down.
Hope this helps!
More Answers (0)
See Also
Categories
Find more on Image Data Workflows 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!