Getting Accuracy from a Confusion Chart

9 visualizaciones (últimos 30 días)
Richard Harris
Richard Harris el 28 de Abr. de 2022
Respondida: Sai Pavan el 11 de Oct. de 2023
Hello,
I can't seem to find the right documentation or i i misunderstand something but i want to try to find the accuracy of a confusion chart using "confusionchart". I know the algorithms and etc. and i can easily find the algorthms needed but i can't seem to get what i need. code and image included. The code is from in class lectures
clear
close all
clc
% Load training and test data using |imageDatastore|.
Training_Dir = fullfile(toolboxdir('vision'),'visiondata','digits','synthetic');
Testing_Dir = fullfile(toolboxdir('vision'),'visiondata','digits','handwritten');
% imageDatastore recursively scans the directory tree containing the
%
trainingSet = imageDatastore(Training_Dir,'IncludeSubfolders',true,'LabelSource','foldernames');
testingSet = imageDatastore(Testing_Dir,'IncludeSubfolders',true,'LabelSource','foldernames');
%% Show a few of the training and test images
close all
figure('Name','Training Image Examples','NumberTitle','off');
movegui("northwest")
subplot(3,3,1);
imshow(trainingSet.Files{12});
subplot(3,3,2);
imshow(trainingSet.Files{315});
subplot(3,3,3);
imshow(trainingSet.Files{810});
subplot(3,3,4);
imshow(trainingSet.Files{1000});
subplot(3,3,5);
imshow(trainingSet.Files{500});
subplot(3,3,6);
imshow(testingSet.Files{10});
subplot(3,3,7);
imshow(testingSet.Files{35});
subplot(3,3,8);
imshow(testingSet.Files{80});
subplot(3,3,9);
imshow(testingSet.Files{105});
%% Show pre-processing and Feature Extraction results
exTestImage = readimage(testingSet,12);
Gray_IM=im2gray(exTestImage);
Binary_IM = imbinarize(Gray_IM);
figure('Name','Preprocessed Image','NumberTitle','off');
movegui("north")
subplot(1,3,1)
imshow(exTestImage)
subplot(1,3,2)
imshow(Gray_IM)
subplot(1,3,3)
imshow(Binary_IM)
%% Extract HOG features from image
[img, imgInfo] = readimage(trainingSet, 800);
imgInfo
cellSize = [2 2];
[hog_4x4, vis4x4] = extractHOGFeatures(img,'CellSize',cellSize);
figure('Name','Histogram of Gradients','NumberTitle','off')
movegui("northeast")
plot(vis4x4);
figure('Name','Training Image Example with HOG','NumberTitle','off');
movegui("southwest")
imshow(img, 'InitialMagnification',2000)
hold on
plot(vis4x4,'color', 'b')
hold off
%% Extracting HOG Features
% HOG features from each image in training set.
hogFeatureSize = length(hog_4x4);
numImages = numel(trainingSet.Files);
trainingFeatures = zeros(numImages,hogFeatureSize,'single');
for i = 1:numImages
img = readimage(trainingSet,i);
img = im2gray(img); %convertign the specified truecolor The RGB Images to a grayscale intensity image
img = imbinarize(img); % pre-processing step to conver the Imges into black & white
trainingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
% similar procedure will be used with testing set.
numImages_test = numel(testingSet.Files);
testingFeatures = zeros(numImages_test,hogFeatureSize,'single');
for i = 1:numImages_test
img = readimage(testingSet,i);
img = im2gray(img); %convertign the specified truecolor The RGB Images to a grayscale intensity image
img = imbinarize(img); % pre-processing step to conver the Imges into black & white
testingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
%% Train and Testing a Decision Tree Classifier
% Get labels for each image.
trainingLabels = trainingSet.Labels;
tree = fitctree(trainingFeatures,trainingLabels);
% Evaluating the the Classifier
% Get labels for each image.
testLabels = testingSet.Labels;
predictedLabels = predict(tree, testingFeatures);
% Tabulate the results using a confusion matrix.
figure("Name","Confusion Matrix",'NumberTitle','off')
movegui("south")
cm = confusionchart(testLabels,predictedLabels,'RowSummary','row-normalized','ColumnSummary','column-normalized');
%% Testing the performance of Trained model on some testing images
M=20;
predicted_Image = predict(tree, testingFeatures(M,:));
figure("Name","Predicted Digit", "NumberTitle","off")
imshow(testingSet.Files{M}, 'InitialMagnification',800);
caption = sprintf('Predicted Digit is: %c', predicted_Image );
title(caption, 'FontSize', 18);

Respuestas (1)

Sai Pavan
Sai Pavan el 11 de Oct. de 2023
Hi Richard,
I understand that you are trying to get the accuracy of the model from the confusion chart.
Please follow the below workflow to get the accuracy of the model:
  • Generate the confusion chart using the confusionchart function which requires the true labels and predicted labels as inputs.
  • Access the confusion matrix from the chart object using the NormalizedValues property. 
  • Calculate the accuracy using the elements of the confusion matrix. The accuracy is the sum of the diagonal elements divided by the sum of all elements.
Please refer to the below code snippet to get the accuracy of the model from the confusion chart:
chart = confusionchart(testLabels,predictedLabels); % Generate confusion chart
confusionMatrix = chart.NormalizedValues; % Get the normalized confusion matrix
accuracy = sum(diag(confusionMatrix)) / sum(confusionMatrix(:)); % Calculate accuracy
Hope it helps.
Regards,
Sai Pavan

Categorías

Más información sobre Get Started with Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by