Evaluating semantic segmentation results error
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I used the documentation to evaluate my results but I get this error, can someone tell me what's wrong please , my input is a .mat file
Evaluating semantic segmentation results
----------------------------------------
* Selected metrics: global accuracy, class accuracy, IoU, weighted IoU, BF score.
* Processed 0 images.Error using semanticSegmentationMetrics>iAssertCategoricalsHaveSameCategories
The categorical data returned by dsResults and dsTruth must have the same
categories.
my code is below
dataSetDir = fullfile('LungTS','preprocessedDataset');
testImagesDir = fullfile(dataSetDir,'imagesTest');
imdReader = @(x) matRead(x);
imds = imageDatastore(testImagesDir, ...
'FileExtensions','.mat','ReadFcn',imdReader);
%imds = imageDatastore(testImagesDir);
classNames = ["nodule" "background"];
labelIDs = [255 0];
labelReader = @(x) matRead(x);
testLabelsDir = fullfile(dataSetDir,'labelsTest');
pxdsTruth = pixelLabelDatastore(testLabelsDir,classNames,labelIDs, ...
'FileExtensions','.mat','ReadFcn',labelReader);
net = load('trained3DUNet-16-Sep-2022-14-34-13-Epoch-250.mat');
net = net.net;
pxdsResults = semanticseg(imds,net,"WriteLocation",tempdir);
metrics = evaluateSemanticSegmentation(pxdsResults,pxdsTruth);
metrics.ClassMetrics
metrics.ConfusionMatrix
cm = confusionchart(metrics.ConfusionMatrix.Variables, ...
classNames, Normalization ='row-normalized');
cm.Title = 'Normalized Confusion Matrix (%)';
imageIoU = metrics.ImageMetrics.MeanIoU;
figure
histogram(imageIoU)
title('Image Mean IoU')
[minIoU, worstImageIndex] = min(imageIoU);
minIoU = minIoU(1);
worstImageIndex = worstImageIndex(1);
worstTestImage = readimage(imds,worstImageIndex);
worstTrueLabels = readimage(pxdsTruth,worstImageIndex);
worstPredictedLabels = readimage(pxdsResults,worstImageIndex);
worstTrueLabelImage = im2uint8(worstTrueLabels == classNames(1));
worstPredictedLabelImage = im2uint8(worstPredictedLabels == classNames(1));
worstMontage = cat(4,worstTestImage,worstTrueLabelImage,worstPredictedLabelImage);
worstMontage = imresize(worstMontage,4,"nearest");
figure
montage(worstMontage,'Size',[1 3])
title(['Test Image vs. Truth vs. Prediction. IoU = ' num2str(minIoU)])
[minIoU, worstImageIndex] = min(imageIoU);
minIoU = minIoU(1);
worstImageIndex = worstImageIndex(1);
worstTestImage = readimage(imds,worstImageIndex);
worstTrueLabels = readimage(pxdsTruth,worstImageIndex);
worstPredictedLabels = readimage(pxdsResults,worstImageIndex);
worstTrueLabelImage = im2uint8(worstTrueLabels == classNames(1));
worstPredictedLabelImage = im2uint8(worstPredictedLabels == classNames(1));
worstMontage = cat(4,worstTestImage,worstTrueLabelImage,worstPredictedLabelImage);
worstMontage = imresize(worstMontage,4,"nearest");
figure
montage(worstMontage,'Size',[1 3])
title(['Test Image vs. Truth vs. Prediction. IoU = ' num2str(minIoU)])
[maxIoU, bestImageIndex] = max(imageIoU);
maxIoU = maxIoU(1);
bestImageIndex = bestImageIndex(1);
bestTestImage = readimage(imds,bestImageIndex);
bestTrueLabels = readimage(pxdsTruth,bestImageIndex);
bestPredictedLabels = readimage(pxdsResults,bestImageIndex);
bestTrueLabelImage = im2uint8(bestTrueLabels == classNames(1));
bestPredictedLabelImage = im2uint8(bestPredictedLabels == classNames(1));
bestMontage = cat(4,bestTestImage,bestTrueLabelImage,bestPredictedLabelImage);
bestMontage = imresize(bestMontage,4,"nearest");
figure
montage(bestMontage,'Size',[1 3])
title(['Test Image vs. Truth vs. Prediction. IoU = ' num2str(maxIoU)])
evaluationMetrics = ["accuracy" "iou"];
metrics = evaluateSemanticSegmentation(pxdsResults,pxdsTruth,"Metrics",evaluationMetrics);
metrics.ClassMetrics
0 Commenti
Risposte (1)
Shantanu Dixit
il 15 Gen 2025
Modificato: Shantanu Dixit
il 15 Gen 2025
Hi Amira,
The error message you encountered suggests that the ground truth and predicted labels in the "pixelLabelDatastore" ('pxdsTruth' and 'pxdsResults') do not have matching categories. Specifically, the categories of the predicted data ('pxdsResults') and the true labels ('pxdsTruth') must be identical for the "evaluateSemanticSegmentation" function to work correctly.
You can verify the categories of the ground truth ('pxdsTruth') and predicted labels ('pxdsResults') through
I = read(pxdsTruth);
R = read(pxdsResults);
CR = categories(I{1});
cI = categories(C{1});
Additionally check that '.mat' files are being correctly read. You can verify that the custom 'matRead' function correctly reads the data and converts it into categorical labels with the expected classes.
function data = matRead(filePath)
matData = load(filePath);
fieldName = fieldnames(matData);
data = matData.(fieldName{1});
% Ensure the data is categorical and matches the class names
data = categorical(data, categoryValues, categoryNames); %% based on specific values and category names
end
You can also refer to the following MathWorks documentation which may be useful:
'pixelLabelDatastore': https://www.mathworks.com/help/vision/ref/pixellabeldatastore.html
'categorical': https://www.mathworks.com/help/matlab/ref/categorical.html
'evaluateSemanticSegmentation': https://www.mathworks.com/help/vision/ref/evaluatesemanticsegmentation.html#d126e288402
Hope this helps!
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!