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

조회 수: 28 (최근 30일)
HK
HK 2021년 11월 30일
댓글: Arya Faturrahman 2022년 10월 11일
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);

채택된 답변

Pratyush Roy
Pratyush Roy 2021년 12월 3일
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
Arya Faturrahman
Arya Faturrahman 2022년 10월 11일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by