how can I extract predicted label and testlabel from already trained deep learning model. the code below gives error while running

5 views (last 30 days)
load('MyVGG19Model.mat');
imdsTest = imageDatastore("C:\Users\Bashir\Desktop\Training dataset\Test set", ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
imageAugmenter = imageDataAugmenter( ...
'RandRotation',[-90,90], ...
'RandScale',[1 1.1], ...
'RandXTranslation',[-3 3], ...
'RandYTranslation',[-3 3]);
imageSize = myvgg16.Layers(1).InputSize;
augmentedTestSet = augmentedImageDatastore(imageSize, imdsTest, 'DataAugmentation',imageAugmenter);
predictedLabels = predict(myvgg16, augmentedTestSet);
testLabels = imdsTest.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))
  7 Comments
Bashir Abubakar
Bashir Abubakar on 4 Dec 2021
Thank you walter for support. TestLabels showed categorical While predictedLabels showed single. I want them all to be of class categorical. How do I go about that pls.
Walter Roberson
Walter Roberson on 4 Dec 2021
You could try
categorical(string(round(predictedLabels)))
but I would recommend looking more carefully at the values in predictedLabels. The values of the predictedLabels might possibly be class numbers, in which case you would want to use them to index the categories that were used in TestLabels .

Sign in to comment.

Answers (1)

yanqi liu
yanqi liu on 4 Dec 2021
yes,sir,as Walter Roberson said,may be ensure data class,such as
load('MyVGG19Model.mat');
imdsTest = imageDatastore("C:\Users\Bashir\Desktop\Training dataset\Test set", ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
imageAugmenter = imageDataAugmenter( ...
'RandRotation',[-90,90], ...
'RandScale',[1 1.1], ...
'RandXTranslation',[-3 3], ...
'RandYTranslation',[-3 3]);
imageSize = myvgg16.Layers(1).InputSize;
augmentedTestSet = augmentedImageDatastore(imageSize, imdsTest, 'DataAugmentation',imageAugmenter);
predictedLabels = predict(myvgg16, augmentedTestSet);
testLabels = imdsTest.Labels
% Tabulate the results using a confusion matrix.
confMat = confusionmat(double(testLabels), double(predictedLabels));
% Convert confusion matrix into percentage form
confMat = bsxfun(@rdivide,confMat,sum(confMat,2))

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!