YOLOv2 Object Detection from ONNX Model in MATLAB

Evaluation & Result

Once I had the trained network, I then evaluated its performance on the Test Data as per the below steps.

Step 1:Create a table to hold the results and initialize a Deployable Video Player to view the image stream.

results = table('Size',[height(TestData) 3],...
'VariableNames',{'Boxes','Scores', 'Labels'});
depVideoPlayer = vision.DeployableVideoPlayer;

Step 2:Loop through all the images in the Test set.

for i = 1:height(TestData)
% Read the image
I = imread(TestData.imageFilename{i});

% Run the detector.
[bboxes,scores,labels] = detect(detectorTinyYolo2,I);
if ~isempty(bboxes)
I = insertObjectAnnotation(I,'Rectangle',bboxes,cellstr(labels))

% Collect the results in the results table
results.Boxes{i} = floor(bboxes);
results.Scores{i} = scores;
results.Labels{i} = labels;
threshold =0.3;
expectedResults = TestData;
Looping through test data images to visualize the network detection, using deployable video player. (Image Courtesy Neha Goel)

Step 3: Evaluate the precision metrics.

I used evaluateDetectionPrecision function to calculate the data points for plotting the precision-recall curve using the given input arguments and the threshold value.

[ap, recall, precision] = evaluateDetectionPrecision(results, expectedResults(:,2:end),threshold);

Plot the precision metrics for each class.

plot(recall{1,1},precision{1,1},'g-','LineWidth',2, "DisplayName",'greenBuoy');
hold on;
plot(recall{2,1},precision{2,1},'b-','LineWidth',2, "DisplayName",'navGate');
hold on;
plot(recall{3,1},precision{3,1},'r-','LineWidth',2, "DisplayName",'redBuoy');
hold on;
plot(recall{4,1},precision{4,1},'y-','LineWidth',2, "DisplayName",'yellowBuoy');
hold off;xlabel('Recall');
title(sprintf('Average Precision = %.2f\n', ap)
legend('Location', 'best');
Image Courtesy Neha Goel

Step 4: Evaluate miss rate metric for object detection.

Using the function evaluateDetectionMissRate I calculated the log average -miss rate of the results, compared to the groundTruthData and data points.

[am,fppi,missRate] = evaluateDetectionMissRate(results, expectedResults(:,2:end),threshold);

Plot the log miss rate metrics for each class to false positives per image.

loglog(fppi{1,1}, missRate{1,1},'-g','LineWidth',2, "DisplayName",'greenBuoy');
hold on;
loglog(fppi{2,1}, missRate{2,1}, 'b','LineWidth',2,"DisplayName",'navGate');
hold on;
loglog(fppi{3,1}, missRate{3,1},'-r','LineWidth',2, "DisplayName",'redBuoy');
hold on;
loglog(fppi{4,1}, missRate{4,1},'-y','LineWidth',2, "DisplayName",'yellowBuoy');
hold off;xlabel('False Positives Per Image');
ylabel('Log Average Miss Rate');
title(sprintf('Log Average Miss Rate = %.2f\n', am))
legend('Location', 'best');
Image Courtesy Neha Goel

As observed due to the imbalanced classes the metrics are more accurate for the “ navGate ” class. One can solve the unbalancing of the classes using oversampling and under-sampling techniques .

Final Remarks

This story’s main purpose was to walk through an approach of importing onnx models and retraining on customized data in MATLAB. You can watch the video of this story here .

As a part of next step I deployed this network on a NVIDIA Jetson and detected objects on a live camera. To learn more about the entire project you can check this GitHub repository : https://github.com/mathworks-robotics/deep-learning-for-object-detection-yolov2 .