How to creat and plot a confusion matrix of the trained CNN netowork?

I have trained a CNN and result showed down but I would need help to find the confusion matrix of the network validation accuracy outcome

Answers (1)

Hi
To find the confusion matrix, first you have to test the model on testing data and then use plotconfusion function. For more information, you can refer this example. Hope it will help!

10 Comments

I already tried this example but did not help me. Do you have any other sources that make it easy?
Thanks
Can you share the model prediction and the ground truth data? Or can you share your code?
I have 5 classes of image data.store and the model split Label are 80% for training, 10% for validation, 10% for testing . the model validation accuracy is 88.24%
layers = [
imageInputLayer([227 227 3],"Name","data")
convolution2dLayer([11 11],116,"Name","conv1","BiasLearnRateFactor",2,"Stride",[4 4])
reluLayer("Name","relu1")
crossChannelNormalizationLayer(5,"Name","norm1","K",1)
maxPooling2dLayer([3 3],"Name","pool1","Stride",[2 2])
groupedConvolution2dLayer([5 5],132,2,"Name","conv2","BiasLearnRateFactor",2,"Padding",[2 2 2 2])
reluLayer("Name","relu2")
fullyConnectedLayer(250,"Name","fc8","BiasLearnRateFactor",2)
fullyConnectedLayer(5,"Name","new fc","BiasLearnRateFactor",10,"WeightLearnRateFactor",10)
softmaxLayer("Name","prob")
classificationLayer("Name","classoutput")];
miniBatchSize = 25;
valFrequency = floor(numel(augimdsTrain.Files)/miniBatchSize);
options = trainingOptions('sgdm', ...%sgdm
'MiniBatchSize',38, ...
'MaxEpochs',8, ...%12
'InitialLearnRate',0.001, ...
'L2Regularization',0.3,...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',valFrequency, ...
'ValidationPatience',4,'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(augimdsTrain,layers,options); % train the network
[YPred] = classify(trainedNet,augimdsValidation); % classify the validation images
accuracy = mean(YPred == imdsValidation.Labels); % calculate the classification accurcy
fracCorrect = accuracy/numel(YPred);
Try this:
plotconfusion(imdsValidation.Labels,YPred)
Undefined function or variable 'trainedNet'.
I tried and recieved this error , since trainedNet is image store , not variable. This is the only problem. if we could pass it , the confusion matrix will be plotted . Any idea?
Are you able to train the network? The trainedNet is the trained network which you have defined as:
trainedNet = trainNetwork(augimdsTrain,layers,options); % train the network
Once training completes, it will be a defined object.
How do I get the confusion matrix for each set seperatly, such as, confusion matrix for tested data, trained date, validated data, then the total confusion matrix for the whole network ?
You can pass the labels and predictions separately to plotConfusion function for each validation,test and training data. For example, consider below:
[YPredValidation] = classify(trainedNet,augimdsValidation); % classify the validation images
plotconfusion(imdsValidation.Labels,YPredValidation)
[YPredTraining] = classify(trainedNet,augimdsTrain); % classify the training images
plotconfusion(imdsValidation.Labels,YPredTraining)
% Similary for testing dataset
How do I get the confusion matrix for all trained data at one set instead of finding a confusion matrix for each set seperatly?
The code is above in a pervious comment

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 21 Feb 2021

Commented:

on 19 Jan 2022

Community Treasure Hunt

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

Start Hunting!