ROC curve Resnet18

2 ビュー (過去 30 日間)
Kanchon Kanti Podder
Kanchon Kanti Podder 2020 年 3 月 30 日
回答済み: Gayathri 2025 年 1 月 2 日
I have classified 37 classes in Resnet18.
I need to plot ROC for each classes. Help with the code. While compling this code, I got several comments that "error in '[X,Y, AUC, OPTROCPT,SUBY,SUBYNAMES] = perfcurve(cgt, cscores(:,1),1);'"
I have tried it in both 2017b and 2020a online trail.
here is the code from predicted class:
%% test data
test = imageDatastore('test',...
'IncludeSubfolders',true,...
'LabelSource','foldernames');
testValidation = augmentedImageDatastore(inputSize(1:2),test);
[YPredTest,probs]= classify(trainedNet,testValidation);
%% accu test
accuracyTest = mean(YPredTest == test.Labels);
display(accuracyTest)
%% Plot ROC
cgt = double(testValidation);
cscores = probs;
figure(1)
[X,Y, AUC, OPTROCPT,SUBY,SUBYNAMES] = perfcurve(cgt, cscores(:,1),1);
plot(X,Y);
grid
xlabel('False positive rate')
ylabel('True positive rate')
title('ROC for classification CNN')

回答 (1 件)

Gayathri
Gayathri 2025 年 1 月 2 日
To plot the ROC curve for each class in a multi-class classification problem using a ResNet18 model, you need to ensure that the inputs to the "perfcurve function" are correctly formatted. The "perfcurve" function expects true class labels and predicted scores for each class. We will have to add a "for" loop to iterate over each class.
Please refer to the below code which shows the implementation of the same.
numClasses = numel(unique(cgt));
% Plot ROC for each class
figure;
hold on;
for classIdx = 1:numClasses
% True binary labels for the current class
trueLabels = (cgt == classIdx);
% Scores for the current class
scores = probs(:, classIdx);
% Compute ROC curve
[X, Y, ~, AUC] = perfcurve(trueLabels, scores, true);
% Plot ROC curve
plot(X, Y, 'DisplayName', sprintf('Class %d (AUC = %.2f)', classIdx, AUC));
end
For more information on "perfcurve", please refer to the documentation link below.
Hope you find this information helpful!

カテゴリ

Help Center および File ExchangeROC - AUC についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by