Académique Documents
Professionnel Documents
Culture Documents
This example shows how to train an object detector using a deep learning technique named Faster R-CNN
(Regions with Convolutional Neural Networks).
Overview
Deep learning is a powerful machine learning technique that automatically learns image features for training
robust object detectors. There are several techniques for object detection using deep learning such as Faster
R-CNN and you only look once (YOLO) v2. This example trains a Faster R-CNN vehicle detector using the
trainFasterRCNNObjectDetector function.
Note: This example requires Computer Vision Toolbox™ and Deep Learning Toolbox™. Parallel Computing
Toolbox™ is recommended to train the detector using a CUDA-capable NVIDIA™ GPU with compute capability
3.0.
Load Dataset
This example uses a small vehicle data set that contains 295 images. Each image contains one or two labeled
instances of a vehicle. A small data set is useful for exploring the Faster R-CNN training procedure, but in
practice, more labeled images are needed to train a robust detector.
The ground truth data is stored in a table. The first column contains the path to the image files. The remaining
columns contain the ROI labels for vehicles.
1 'C:\Users\IPN\... [960,58...
2 'C:\Users\IPN\... [602,55...
3 'C:\Users\IPN\... [473,55...
4 'C:\Users\IPN\... [563,55...
Display one of the images from the data set to understand the type of images it contains.
1
I = imread(DOYMADataset.imageFileName{10});
Split the data set into a training set for training the detector, and a test set for evaluating the detector. Select
60% of the data for training. Use the rest for evaluation.
2
trainFasterRCNNObjectDetector trains the detector in four steps. The first two steps train the region
proposal and detection networks used in Faster R-CNN. The final two steps combine the networks from the
first two steps such that a single network is created for detection [1]. Specify the network training options for all
steps using trainingOptions.
The 'MiniBatchSize' property is set to 1 because the vehicle dataset has images with different sizes. The
prevents them from being batched together for processing. Choose a MiniBatchSize greater than 1 if the
training images are all the same size to reduce training time.
The 'CheckpointPath' property is set to a temporary location for all the training options. This name-value pair
enables the saving of partially trained detectors during the training process. If training is interrupted, such as
from a power outage or system failure, you can resume training from the saved checkpoint.
This example uses a pretrained ResNet-50 for feature extraction. Other pretrained networks such
as MobileNet v2 or ResNet-18 can also be used depending on application requirements. The
trainFasterRCNNObjectDetector function automatically adds the sub-networks required for object detection.
You many also create a custom Faster R-CNN network.
Train Faster R-CNN object detector if doTrainingAndEval is true. Otherwise, you can load a pretrained
network.
*************************************************************************
Training a Faster R-CNN Object Detector for the following object classes:
* DOYMA
3
Warning: Invalid bounding boxes from 1 out of 252 training images were removed. The following rows in
trainingData have invalid bounding box data:
Invalid Rows
____________
52
Bounding boxes must be fully contained within their associated image and must have positive width and height.
Training on single GPU.
|=======================================================================================================|
| Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Mini-batch | Base Learning |
| | | (hh:mm:ss) | Loss | Accuracy | RMSE | Rate |
|=======================================================================================================|
| 1 | 1 | 00:00:02 | 1.3094 | 67.19% | 0.86 | 0.0010 |
| 1 | 50 | 00:00:13 | 0.5515 | 91.41% | 0.48 | 0.0010 |
| 1 | 100 | 00:00:24 | 0.7343 | 89.76% | 0.56 | 0.0010 |
| 1 | 150 | 00:00:34 | 0.8839 | 54.69% | 0.66 | 0.0010 |
| 1 | 200 | 00:00:45 | 0.8803 | 72.44% | 0.68 | 0.0010 |
| 1 | 250 | 00:00:55 | 0.6347 | 86.72% | 0.55 | 0.0010 |
| 2 | 300 | 00:01:14 | 0.7146 | 85.83% | 0.77 | 0.0010 |
| 2 | 350 | 00:01:25 | 1.5682 | 61.42% | 1.01 | 0.0010 |
| 2 | 400 | 00:01:36 | 0.6590 | 83.59% | 1.16 | 0.0010 |
| 2 | 450 | 00:01:46 | 0.7015 | 79.69% | 1.00 | 0.0010 |
| 2 | 500 | 00:01:57 | 0.8008 | 86.72% | 0.77 | 0.0010 |
| 3 | 550 | 00:02:15 | 0.3830 | 96.09% | 0.53 | 0.0010 |
| 3 | 600 | 00:02:26 | 1.0613 | 73.23% | 1.27 | 0.0010 |
| 3 | 650 | 00:02:36 | 0.5723 | 82.03% | 0.54 | 0.0010 |
| 3 | 700 | 00:02:47 | 0.5809 | 89.06% | 0.68 | 0.0010 |
| 3 | 750 | 00:02:58 | 0.7283 | 92.19% | 0.74 | 0.0010 |
| 4 | 800 | 00:03:16 | 0.3657 | 99.22% | 0.58 | 0.0010 |
| 4 | 850 | 00:03:27 | 0.3398 | 90.63% | 0.70 | 0.0010 |
| 4 | 900 | 00:03:38 | 0.3847 | 90.63% | 0.39 | 0.0010 |
| 4 | 950 | 00:03:49 | 0.4135 | 91.34% | 0.52 | 0.0010 |
| 4 | 1000 | 00:04:00 | 0.5336 | 90.48% | 0.52 | 0.0010 |
| 5 | 1050 | 00:04:18 | 0.4953 | 71.65% | 0.49 | 0.0010 |
| 5 | 1100 | 00:04:29 | 1.3643 | 92.19% | 1.20 | 0.0010 |
| 5 | 1150 | 00:04:40 | 0.4657 | 94.53% | 0.53 | 0.0010 |
| 5 | 1200 | 00:04:51 | 0.3389 | 93.75% | 0.39 | 0.0010 |
| 5 | 1250 | 00:05:02 | 0.6125 | 89.76% | 0.66 | 0.0010 |
| 5 | 1255 | 00:05:03 | 0.6255 | 73.44% | 0.65 | 0.0010 |
|=======================================================================================================|
Step 2 of 4: Training a Fast R-CNN Network using the RPN from step 1.
--> Extracting region proposals from 251 training images...done.
4
| 3 | 700 | 00:03:04 | 0.0395 | 100.00% | 0.79 | 0.0010 |
| 4 | 750 | 00:03:23 | 0.0729 | 99.22% | 1.01 | 0.0010 |
| 4 | 800 | 00:03:35 | 0.0518 | 100.00% | 0.88 | 0.0010 |
| 4 | 850 | 00:03:47 | 0.1180 | 100.00% | 1.06 | 0.0010 |
| 4 | 900 | 00:03:59 | 0.0634 | 99.22% | 0.77 | 0.0010 |
| 4 | 950 | 00:04:11 | 0.0068 | 100.00% | 0.44 | 0.0010 |
| 5 | 1000 | 00:04:30 | 0.1325 | 96.88% | 0.91 | 0.0010 |
| 5 | 1050 | 00:04:43 | 0.0575 | 99.22% | 1.37 | 0.0010 |
| 5 | 1100 | 00:04:55 | 0.0316 | 100.00% | 0.81 | 0.0010 |
| 5 | 1150 | 00:05:07 | 0.0884 | 100.00% | 0.73 | 0.0010 |
| 5 | 1200 | 00:05:19 | 0.0935 | 98.44% | 0.81 | 0.0010 |
| 5 | 1230 | 00:05:26 | 0.0574 | 99.22% | 0.64 | 0.0010 |
|=======================================================================================================|
5
| 2 | 400 | 00:01:32 | 0.1029 | 99.22% | 0.89 | 0.0010 |
| 2 | 450 | 00:01:43 | 0.0625 | 100.00% | 0.72 | 0.0010 |
| 2 | 500 | 00:01:54 | 0.0536 | 100.00% | 0.68 | 0.0010 |
| 3 | 550 | 00:02:11 | 0.0309 | 100.00% | 0.85 | 0.0010 |
| 3 | 600 | 00:02:22 | 0.0919 | 97.66% | 0.71 | 0.0010 |
| 3 | 650 | 00:02:33 | 0.0935 | 100.00% | 0.87 | 0.0010 |
| 3 | 700 | 00:02:43 | 0.0589 | 98.44% | 0.62 | 0.0010 |
| 3 | 750 | 00:02:54 | 0.0687 | 97.66% | 0.60 | 0.0010 |
| 4 | 800 | 00:03:12 | 0.0310 | 100.00% | 0.83 | 0.0010 |
| 4 | 850 | 00:03:22 | 0.0684 | 99.22% | 0.72 | 0.0010 |
| 4 | 900 | 00:03:33 | 0.1031 | 99.22% | 0.86 | 0.0010 |
| 4 | 950 | 00:03:44 | 0.0451 | 99.22% | 0.60 | 0.0010 |
| 4 | 1000 | 00:03:55 | 0.0327 | 100.00% | 0.56 | 0.0010 |
| 5 | 1050 | 00:04:12 | 0.0273 | 100.00% | 0.76 | 0.0010 |
| 5 | 1100 | 00:04:23 | 0.0457 | 100.00% | 0.70 | 0.0010 |
| 5 | 1150 | 00:04:34 | 0.0908 | 100.00% | 0.90 | 0.0010 |
| 5 | 1200 | 00:04:44 | 0.0418 | 100.00% | 0.62 | 0.0010 |
| 5 | 1250 | 00:04:55 | 0.0363 | 100.00% | 0.59 | 0.0010 |
|=======================================================================================================|
Invalid Rows
____________
52
Bounding boxes must be fully contained within their associated image and must have positive width and height.
*******************************************************************
6
Evaluate Detector Using Test Set
Evaluate the detector on a large set of images to measure the trained detector's performance. Computer
Vision Toolbox™ provides object detector evaluation functions to measure common metrics such as average
precision (evaluateDetectionPrecision) and log-average miss rates (evaluateDetectionMissRate). Here,
the average precision metric is used. The average precision provides a single number that incorporates the
ability of the detector to make correct classifications (precision) and the ability of the detector to find all relevant
objects (recall).
The first step for detector evaluation is to collect the detection results by running the detector on the test set.
numImages = height(testData);
results = table('Size',[numImages 3],...
'VariableTypes',{'cell','cell','cell'},...
'VariableNames',{'Boxes','Scores','Labels'});
% Run detector on each image in the test set and collect results.
for i = 1:numImages
7
% Collect the results.
% Collect the results.
results.Boxes{i} = bboxes;
results.Scores{i} = scores;
results.Labels{i} = labels;
end
% Extract expected bounding box locations from test data.
expectedResults = testData(:, 2:end);
The precision/recall (PR) curve highlights how precise a detector is at varying levels of recall. Ideally, the
precision would be 1 at all recall levels. The use of additional layers in the network can help improve the
average precision, but might require additional training data and longer training time.
% Run detector on each image in the test set and show results.
8
% for i = 1:numImages
%
% % Read the image.
% I = imread(testData.imageFileName{i});
%
% % Run the detector.
% [bboxes, scores, labels] = detect(detector, I);
%
% I = insertObjectAnnotation(I,'rectangle',bboxes,scores);
% figure
% imshow(I)
% input('')
% end
% % Extract expe
Summary
This example showed how to train a vehicle detector using Faster R-CNN. You can follow similar steps to train
detectors for traffic signs, pedestrians, or other objects.
References
[1] Ren, S., K. He, R. Gershick, and J. Sun. "Faster R-CNN: Towards Real-Time Object Detection with Region
Proposal Networks." IEEE Transactions of Pattern Analysis and Machine Intelligence. Vol. 39, Issue 6, June
2017, pp. 1137-1149.
[2] Girshick, R., J. Donahue, T. Darrell, and J. Malik. "Rich Feature Hierarchies for Accurate Object Detection
and Semantic Segmentation." Proceedings of the 2014 IEEE Conference on Computer Vision and Pattern
Recognition. Columbus, OH, June 2014, pp. 580-587.
[3] Girshick, R. "Fast R-CNN." Proceedings of the 2015 IEEE International Conference on Computer Vision.
Santiago, Chile, Dec. 2015, pp. 1440-1448.
[4] Zitnick, C. L., and P. Dollar. "Edge Boxes: Locating Object Proposals from Edges." European Conference on
Computer Vision. Zurich, Switzerland, Sept. 2014, pp. 391-405.
[5] Uijlings, J. R. R., K. E. A. van de Sande, T. Gevers, and A. W. M. Smeulders. "Selective Search for Object
Recognition."_ International Journal of Computer Vision_. Vol. 104, Number 2, Sept. 2013, pp. 154-171.