Object detection based on CNN in matlab

S
samira-bellil · Jun 15, 2021 · 1.9K views
Question
I want to build an object recognition system based on CNN. I've collected a database, and I want to apply the steps mentioned in the following example:   https://www.mathworks.com/help/vision/examples/object-detection-using-faster-r-cnn-deep-learning.html   In this example, there is this code that aims to load the vehicle data :   data = load('fasterRCNNVehicleTrainingData.mat'); vehicleDataset = data.vehicleTrainingData; I want to know how can I create this file for my dataset.
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Sep 9, 2026

To implement object detection using a Convolutional Neural Network (CNN) in MATLAB, you typically use a deep learning detection framework such as Faster R-CNN, YOLOv4, or SSD. The standard engineering workflow requires labeling ground truth bounding boxes, generating anchor boxes matching your target object sizes, configuring a convolutional backbone (such as ResNet-50), and training the detector with trainFasterRCNNObjectDetector.

Complete 4-Step Implementation Workflow

  1. Dataset Preparation and Ground Truth Labeling: Use the built-in MATLAB imageLabeler or trainingImageLabeler app to draw rectangular bounding boxes around your target classes. Export the label annotations as a groundTruth object or a MATLAB table containing image file paths and coordinate vectors [x, y, width, height].
  2. Anchor Box Estimation: Anchor boxes provide prior scales and aspect ratios. Use the estimateAnchorBoxes function on your training labels to calculate cluster centroids (k-means based on Intersection-over-Union distance).
  3. Detector Configuration & Training: Select a pre-trained feature extraction backbone and configure solver parameters (SGD with momentum or Adam) through trainingOptions.
  4. Inference & Evaluation: Run the trained detector on unseen images using detect() and compute Mean Average Precision (mAP) using evaluateDetectionPrecision.

Executable MATLAB Code: Training Faster R-CNN

% Step 1: Load training data table containing image paths and bounding box labels
% dataTable columns: {'imageFilename', 'vehicle'}
data = load('vehicleTrainingData.mat');
trainingData = data.vehicleTrainingData;

% Step 2: Configure pre-trained backbone and estimate anchor boxes
inputSize = [224, 224, 3];
numClasses = 1;

% Calculate 4 prior anchor box sizes from dataset bounding box distributions
numAnchors = 4;
anchorBoxes = estimateAnchorBoxes(trainingData(:, 2), numAnchors);

% Build Faster R-CNN network using ResNet-50 as the feature extractor
featureExtractionNetwork = resnet50;
featureLayer = 'activation_40_relu';

detector = fasterRCNNObjectDetector(inputSize, numClasses, anchorBoxes, ...
    featureExtractionNetwork, 'FeatureLayer', featureLayer);

% Step 3: Define training hyper-parameters
options = trainingOptions('sgdm', ...
    'MiniBatchSize', 4, ...
    'InitialLearnRate', 1e-3, ...
    'LearnRateSchedule', 'piecewise', ...
    'LearnRateDropPeriod', 5, ...
    'LearnRateDropFactor', 0.2, ...
    'MaxEpochs', 10, ...
    'Verbose', true, ...
    'Shuffle', 'every-epoch', ...
    'ExecutionEnvironment', 'auto'); % Uses NVIDIA CUDA GPU if available

% Step 4: Train detector
trainedDetector = trainFasterRCNNObjectDetector(trainingData, detector, options, ...
    'NegativeOverlapRange', [0, 0.3], ...
    'PositiveOverlapRange', [0.6, 1]);

% Step 5: Test detector on a new image
testImage = imread('test_scene.jpg');
[bboxes, scores, labels] = detect(trainedDetector, testImage, 'Threshold', 0.5);

% Annotate and display detections
if ~isempty(bboxes)
    annotatedImage = insertObjectAnnotation(testImage, 'rectangle', bboxes, ...
        cellstr(labels) + ": " + string(round(scores, 2)), 'LineWidth', 2);
    figure;
    imshow(annotatedImage);
    title('Detected Objects via Faster R-CNN');
else
    disp('No target objects detected above the threshold.');
end

Engineering Tips for High Detection Accuracy

  • Handling Small Objects: If your target objects occupy fewer than 32x32 pixels, choose an earlier shallow convolutional layer in the backbone (such as res3d_relu instead of res5c_relu) to preserve fine spatial resolution before excessive downsampling.
  • Preventing GPU Memory Crashes: Faster R-CNN creates region proposals during training which demand substantial VRAM. If MATLAB throws an out-of-memory error on your GPU, drop MiniBatchSize to 2 or 4 and enable 16-bit precision training.
  • Faster Inference Alternative: For real-time applications (above 25 FPS) such as live webcam streams or automated inspection lines, replace Faster R-CNN with yolov4ObjectDetector or tinyYoloV4 in the Computer Vision Toolbox.
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!