How to compute the misclassification rate for (KFold & Leave-one-out) cross validation in the linear SVM classfier?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource','foldernames');
% Notice that each set now has exactly the same number of images.
countEachLabel(imds)
waitbar(.1,f,'Loading Pre-trained Network');
%------------------------------Load Pretrained Network----------------------
% Load pretrained network
net = alexnet();
featureLayer = 'fc8';
% Inspect the first layer
net.Layers(1)
% Inspect the last layer
net.Layers(end)
% Number of class names for ImageNet classification task
numel(net.Layers(end).ClassNames)
%----------------------------Prepare Training & Test Image Sets------------
[trainingSet, testSet] = splitEachLabel(imds, 0.7, 'randomize');
waitbar(.2,f,'Images Pre-processing');
%-----------------------------Pre-processing Images For CNN----------------
% Create augmentedImageDatastore from training and test sets to resize images in imds to the size required by the network.
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize, trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize, testSet, 'ColorPreprocessing', 'gray2rgb');
waitbar(.3,f,'Features Extraction');
%---------------------------Extract Training Features---------------------
% Get the network weights for the second convolutional layer
w1 = net.Layers(2).Weights;
% Scale and resize the weights for visualization
w1 = mat2gray(w1);
w1 = imresize(w1,5);
trainingFeatures = activations(net, augmentedTrainingSet, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns');
% Get training labels from the trainingSet
trainingLabels = trainingSet.Labels;
waitbar(.6,f,'SVM Classifier Training');
%-------------------Train a Linear SVM------------------------------
% Train linear SVM classifier using a fast linear solver, and set 'ObservationsIn' to 'columns' to match the arrangement used for training features.
classifier = fitclinear(trainingFeatures, trainingLabels, ...
'Learner','svm','ObservationsIn','columns');
waitbar(.8,f,'Classifier Evaluation');
%-------------------Evaluate the Classifier-----------------------------
% Extract test features using the CNN
testFeatures = activations(net, augmentedTestSet, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns');
% Pass CNN image features to trained classifier
[predictedLabels,scores] = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
% Get the known labels
testLabels = testSet.Labels;
% Tabulate the results using a confusion matrix.
confMat = confusionmat(testLabels, predictedLabels)
% Convert confusion matrix into percentage form
confMat = bsxfun(@rdivide,confMat,sum(confMat,2));
% Display the mean accuracy
mean(diag(confMat))
0 Commenti
Risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!