How to get Precision, Recall,ROC,F_Mesure?

1 view (last 30 days)
Arya Faturrahman
Arya Faturrahman on 12 Oct 2022
Answered: Deep on 27 Dec 2024
Hello anyone I want to get Precision, Recall, Sensitivity,Sprecificity,ROC. But I don't know how to implements code. i get Error at perfCurve and i don't know to fix it.
This my Train Code
imds = imageDatastore('Dataset', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
tbl = countEachLabel(imds);
minSetCount = min(tbl{:,2});
imds = splitEachLabel(imds, minSetCount, 'randomize');
countEachLabel(imds);
net = resnet50();
lgraph = layerGraph(net);
clear net;
numClasses = 2;
%numel(lgraph.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.7, 'randomize');
imageSize = [224 224 3];
augmentedTrainingSet = augmentedImageDatastore(imageSize,...
trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize,...
testSet, 'ColorPreprocessing', 'gray2rgb');
% New Learnable Layer
newLearnableLayer = fullyConnectedLayer(numClasses, ...
'Name','new_fc', ...
'WeightLearnRateFactor',10,...
'BiasLearnRateFactor',10);
% Replacing the last layers with new layers
lgraph = replaceLayer(lgraph,'fc1000',newLearnableLayer);
newsoftmaxLayer = softmaxLayer('Name','new_softmax');
lgraph = replaceLayer(lgraph,'fc1000_softmax',newsoftmaxLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'ClassificationLayer_fc1000',newClassLayer);
options = trainingOptions('adam',...
'MaxEpochs',6,'MiniBatchSize',8,...
'Shuffle','every-epoch', ...
'ValidationData', augmentedTestSet, ...
'ValidationFrequency', 30, ...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augmentedTrainingSet,lgraph,options);
And This my Test code for get the precision and any parameters
YPred = predict(netTransfer, augmentedTestSet); %imds_test is the image dastore containing the test images.
[Xpr,Ypr,Tpr,AUCpr] =perfcurve(testSet, newClassLayer, 1, 'xCrit', 'reca', 'yCrit', 'prec');
[c,cm,ind,per] = confusion(targets,outputs); %per represents the Matrix of percentages. Please refer to the doc for more details.
Can you help me to fix and get the data?.

Answers (1)

Deep
Deep on 27 Dec 2024
The function "perfcurve" accepts 3 arguments: a vector of classifier predictions, given true class labels, and the positive class label. You can read more about its correct usage in detail from the official documentation by using the following command:
doc perfcurve
Additionally, since you are working with a confusion matrix, you might find the following MATLAB Answer by Sam helpful for calculating the desired performance metrics: https://www.mathworks.com/matlabcentral/answers/2053757-how-to-evaluate-the-performance-metrics-accuracy-precision-recall-f1score-on-the-tuned-fis#answer_1365099.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!