Evaluating semantic segmentation results error

1 回表示 (過去 30 日間)
Amira Youssef
Amira Youssef 2022 年 10 月 5 日
編集済み: Shantanu Dixit 2025 年 1 月 15 日
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

回答 (1 件)

Shantanu Dixit
Shantanu Dixit 2025 年 1 月 15 日
編集済み: Shantanu Dixit 2025 年 1 月 15 日
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:
Hope this helps!

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by