Perform sensitivity, specificity, precision, recall, f_measure in CNN

39 views (last 30 days)
Hello experts,
I want to perform [sensitivity, specificity, precision, recall, f_measure] in the following script, but I dont' know how.
Please help me how to write code to evaluate them!
outputFolder = fullfile('Caltech')
rootFolder = fullfile(outputFolder, '101_ObjectCategories')
categories = {'data1', 'data2'}
imds = imageDatastore(fullfile(rootFolder,categories), 'LabelSource','foldernames')
tbl = countEachLabel(imds)
minSetCount = min(tbl{:,2})
imds = splitEachLabel(imds, minSetCount, 'randomize')
net = resnet50();
lgraph = layerGraph(net);
clear net;
numClasses = 2;
[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', ...
% 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',...
'Shuffle','every-epoch', ...
'ValidationData', augmentedTestSet, ...
'ValidationFrequency', 30, ...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
netTransfer = trainNetwork(augmentedTrainingSet,lgraph,options);

Accepted Answer

Pratyush Roy
Pratyush Roy on 3 Dec 2021
The predict function might be helpful to predict the labels for the test images using the following command:
YPred = predict(netransfer, imds_test) %imds_test is the image dastore containing the test images.
After obtaining the predicted labels "YPred" the function perfcurve can be used the get the precision and recall values using the following command:
[Xpr,Ypr,Tpr,AUCpr] =perfcurve(targets, scores, 1, 'xCrit', 'reca', 'yCrit', 'prec');
Here Xpr and YPr represents recall and pres=cision respectively.
You can also use the confusion function to obtain the "Matrix of percentages" using the following command:
[c,cm,ind,per] = confusion(targets,outputs) %per represents the Matrix of percentages. Please refer to the doc for more details.
Hope this helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!