problem while getting precision-recall curve

1 view (last 30 days)
hi
i am ebaluation my faster rcnn detector to get curve and encounter with this problem :
Dot indexing is not supported for variables of this type.
Error in fasterRCNNtestdetect (line 57)
precision = metrics.ClassMetrics.Precision(classID);
MY CODE IS
clear
load('C:\Users\ZBook\OneDrive\Desktop\detector\detectorFasterRCNN.mat');
dataRoad=load("D:\this is.mat");
img=imageDatastore(dataRoad.gTruth.DataSource.Source);
labeldata=dataRoad.gTruth.LabelData;
blds=boxLabelDatastore(labeldata);
cds=combine(img,blds);
preview(cds)
tbl = countEachLabel(blds);
% Define the split ratios (e.g., 70% training, 15% validation, 15% testing)
trainRatio = 0.15;
valRatio = 0.15;
testRatio = 0.7;
% Count the total number of images
numImages = numel(img.Files );
% Calculate the number of images for each split
numTrain = round(trainRatio * numImages);
numVal = round(valRatio * numImages);
numTest = numImages - numTrain - numVal;
% Shuffle the datastore
cds = shuffle(cds);
inputSize = [224 224 3];
% Split the datastore into training, validation, and testing sets
trainingData = subset(cds, 1:numTrain);
validationData = subset(cds, numTrain+1:numTrain+numVal);
testData = subset(cds, numTrain+numVal+1:numTrain+numVal+numTest);
testdata = transform(testData,@(data)preprocessData(data,inputSize));
% Make predictions
% Make predictions
% Set MiniBatchSize to 1 for detection
detectionResults = detect(detector, testData, 'MinibatchSize', 1, 'Threshold', 0.1);
disp(detectionResults);
numClasses = 8; % Replace with the actual number of classes in your dataset
% Loop through all class IDs
for classID = 1:numClasses
% Make predictions
% Set MiniBatchSize to 1 for detection
% Evaluate the model for the current class
metrics = evaluateDetectionPrecision(detectionResults, testData);
% Extract precision and recall for the current class
precision = metrics.ClassMetrics.Precision(classID);
recall = metrics.ClassMetrics.Recall(classID);
% Display or store the precision and recall for the current class
fprintf('Class %d - Precision: %.2f, Recall: %.2f\n', classID, precision, recall);
% Optionally, you can perform further analysis or visualization for each class
% For example, you could plot precision-recall curves or confusion matrices for each class
% Plot precision-recall curve for the current class
figure
plot(recall, precision)
xlabel('Recall')
ylabel('Precision')
grid on
title(sprintf('Precision-Recall Curve for Class %d', classID));
end
function data = preprocessData(data, targetSize)
% Resize image and bounding boxes to the targetSize.
sz = size(data{1}, [1, 2]);
scale = targetSize(1:2) ./ sz;
data{1} = imresize(data{1}, targetSize(1:2));
% Pass imageSize to helperSanitizeBoxes
imageSize = targetSize; % You may adjust this based on your needs
data{2} = helperSanitizeBoxes(data{2}, imageSize);
% Resize boxes to new image size.
data{2} = bboxresize(data{2}, scale);
end

Accepted Answer

Michael
Michael on 28 Nov 2023
Edited: Michael on 28 Nov 2023
Hello, I think you need to modify the code in your for loop where you're accessing the precision and recall values. Instead of using dot notation, you can use curly brackets {} to access the values. Here's the updated code:
matlab
% Loop through all class IDs
for classID = 1:numClasses
% Make predictions
% Set MiniBatchSize to 1 for detection
% Evaluate the model for the current class
metrics = evaluateDetectionPrecision(detectionResults, testData);
% Extract precision and recall for the current class
precision = metrics{1}.ClassMetrics.Precision(classID);
recall = metrics{1}.ClassMetrics.Recall(classID);
% Display or store the precision and recall for the current class
fprintf('Class %d - Precision: %.2f, Recall: %.2f\n', classID, precision, recall);
% Optionally, you can perform further analysis or visualization for each class
% For example, you could plot precision-recall curves or confusion matrices for each class
% Plot precision-recall curve for the current class
figure
plot(recall, precision)
xlabel('Recall')
ylabel('Precision')
grid on
title(sprintf('Precision-Recall Curve for Class %d', classID));
end
By using curly brackets {}, you can access the first element of the metrics cell array and then access the precision and recall values using dot notation.

More Answers (1)

Walter Roberson
Walter Roberson on 28 Nov 2023
metrics = evaluateDetectionPrecision(detectionResults, testData);
The function evaluateDetectionPrecision always returns a numeric value as the first output. The second and third outputs are potentially cell arrays.
Metrics are something returned from evaluateObjectDetection

Categories

Find more on Recognition, Object Detection, and Semantic Segmentation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!