Main Content

Train YOLO v2 Network for Vehicle Detection

Load the training data for vehicle detection into the workspace.

data = load("vehicleTrainingData.mat");
trainingData = data.vehicleTrainingData;

Specify the directory in which training samples are stored. Add full path to the file names in training data.

dataDir = fullfile(toolboxdir("vision"),"visiondata");
trainingData.imageFilename = fullfile(dataDir,trainingData.imageFilename);

Randomly shuffle data for training.

rng(0)
shuffledIdx = randperm(height(trainingData));
trainingData = trainingData(shuffledIdx,:);

Create an imageDatastore using the files from the table.

imds = imageDatastore(trainingData.imageFilename);

Create a boxLabelDatastore using the label columns from the table.

blds = boxLabelDatastore(trainingData(:,2:end));

Combine the datastores.

ds = combine(imds,blds);

Specify the class names using the label columns from the table.

classes = trainingData.Properties.VariableNames(2:end);

Specify anchor boxes.

anchorBoxes = [8 8; 32 48; 40 24; 72 48];

Load a preinitialized YOLO v2 object detection network.

load("yolov2VehicleDetectorNet.mat","net");

Create the YOLO v2 object detection network.

detector = yolov2ObjectDetector(net,classes,anchorBoxes)
detector = 
  yolov2ObjectDetector with properties:

                  Network: [1×1 dlnetwork]
                InputSize: [128 128 3]
        TrainingImageSize: [128 128]
              AnchorBoxes: [4×2 double]
               ClassNames: vehicle
    ReorganizeLayerSource: ''
              LossFactors: [5 1 1 1]
                ModelName: ''

Configure the network training options.

options = trainingOptions("sgdm", ...
    InitialLearnRate=0.001, ...
    Verbose=true, ...
    MiniBatchSize=16, ...
    MaxEpochs=30, ...
    Shuffle="never", ...
    VerboseFrequency=30, ...
    CheckpointPath=tempdir);

Train the YOLO v2 network.

[trainedDetector,info] = trainYOLOv2ObjectDetector(ds,detector,options);
*************************************************************************
Training a YOLO v2 Object Detector for the following object classes:

* vehicle

Training on single CPU.
|========================================================================================|
|  Epoch  |  Iteration  |  Time Elapsed  |  Mini-batch  |  Mini-batch  |  Base Learning  |
|         |             |   (hh:mm:ss)   |     RMSE     |     Loss     |      Rate       |
|========================================================================================|
|       1 |           1 |       00:00:00 |         7.13 |         50.8 |          0.0010 |
|       2 |          30 |       00:00:13 |         1.18 |          1.4 |          0.0010 |
|       4 |          60 |       00:00:25 |         0.98 |          1.0 |          0.0010 |
|       5 |          90 |       00:00:35 |         0.59 |          0.3 |          0.0010 |
|       7 |         120 |       00:00:46 |         0.53 |          0.3 |          0.0010 |
|       9 |         150 |       00:00:57 |         0.63 |          0.4 |          0.0010 |
|      10 |         180 |       00:01:07 |         0.45 |          0.2 |          0.0010 |
|      12 |         210 |       00:01:18 |         0.39 |          0.2 |          0.0010 |
|      14 |         240 |       00:01:28 |         0.60 |          0.4 |          0.0010 |
|      15 |         270 |       00:01:39 |         0.42 |          0.2 |          0.0010 |
|      17 |         300 |       00:01:49 |         0.35 |          0.1 |          0.0010 |
|      19 |         330 |       00:02:00 |         0.47 |          0.2 |          0.0010 |
|      20 |         360 |       00:02:10 |         0.36 |          0.1 |          0.0010 |
|      22 |         390 |       00:02:21 |         0.34 |          0.1 |          0.0010 |
|      24 |         420 |       00:02:32 |         0.44 |          0.2 |          0.0010 |
|      25 |         450 |       00:02:42 |         0.54 |          0.3 |          0.0010 |
|      27 |         480 |       00:02:52 |         0.39 |          0.2 |          0.0010 |
|      29 |         510 |       00:03:03 |         0.44 |          0.2 |          0.0010 |
|      30 |         540 |       00:03:13 |         0.37 |          0.1 |          0.0010 |
|========================================================================================|
Training finished: Max epochs completed.
Detector training complete.
*************************************************************************

Verify the training accuracy by inspecting the training loss for each iteration.

figure
plot(info.TrainingLoss)
grid on
xlabel("Number of Iterations")
ylabel("Training Loss for Each Iteration")

Figure contains an axes object. The axes object with xlabel Number of Iterations, ylabel Training Loss for Each Iteration contains an object of type line.

Read a test image into the workspace.

img = imread("detectcars.png");

Run the trained YOLO v2 object detector on the test image for vehicle detection.

[bboxes,scores] = detect(trainedDetector,img);

Display the detection results.

if(~isempty(bboxes))
    img = insertObjectAnnotation(img,"rectangle",bboxes,scores);
end
figure
imshow(img)

Figure contains an axes object. The hidden axes object contains an object of type image.