Error using semanticSe​gmentation​Metrics The categorical data returned by dsResults and dsTruth must have the same categories.

5 ビュー (過去 30 日間)
I used the documentation to evaluate my results but I get this error, can someone tell me what's wrong please
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 件)

Birju Patel
Birju Patel 2022 年 10 月 6 日
Check the categories of the data coming out of pxdsResults and pxdsTruth:
A = read(pxdsResults);
categories(A{1})
B = read(pxdsTruth);
categories(B{1})
The error message suggests that these are not the same.

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by