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

33 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')
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);

Accepted Answer

Pratyush Roy
Pratyush Roy on 3 Dec 2021
Hi,
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!
  1 Comment
Arya Faturrahman
Arya Faturrahman on 11 Oct 2022
I Get Error code
Error in DAGNetwork/predict (line 118)
Y = predictBatch( ...
Error in test_resnet (line 1)
YPred = predict(netTransfer, testSet.Labels);
%imds_test is the image dastore containing the
test images.
How to fix and run program i need example for this code. Can you give me example to run testing code about this because i very need this outputs.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!