objectDetectorTrainingData - Create training data for an object detector - MATLAB (original) (raw)
Create training data for an object detector
Syntax
Description
[___,[arrds](#mw%5F0f98b9c1-ea39-4771-aa74-9608ccba19ed)] = objectDetectorTrainingData([gTruth](#bvkrwem-1-gTruth))
also returns a datastore arrds
, that contains the attributes and sublabels associated with the labels.
[trainingDataTable](#bvkrwem-1-trainingData) = objectDetectorTrainingData([gTruth](#bvkrwem-1-gTruth))
returns a table of training data from the specified ground truth.gTruth
is an array of groundTruth objects. You can use the table to train an object detector using the Computer Vision Toolbox™ training functions.
[___] = objectDetectorTrainingData([gTruth](#bvkrwem-1-gTruth),[Name=Value](#namevaluepairarguments))
specifies options using one or more name-value arguments in addition to any combination of arguments from previous syntaxes. For example,Verbose=True
enables display to the workspace environment.
If you create the groundTruth objects ingTruth
using a video file, a custom data source, or animageDatastore object with different custom read functions, then you can specify any combination of name-value arguments. If you create the groundTruth
objects from an image collection or image sequence data source, then you can specify only the SamplingFactor and theLabelType name-value arguments.
Examples
Train a vehicle detector based on a YOLO v2 network.
Add the folder containing training images to the workspace.
imageDir = fullfile(matlabroot,"toolbox","vision","visiondata","vehicles"); addpath(imageDir);
Load the vehicle ground truth data.
data = load("vehicleTrainingGroundTruth.mat"); gTruth = data.vehicleTrainingGroundTruth;
Create an image datastore and box label datastore using the ground truth object.
[imds,bxds] = objectDetectorTrainingData(gTruth);
Combine the datastores.
cds = combine(imds,bxds);
Load the detector containing the dlnetwork
object for training.
load("yolov2VehicleDetectorNet.mat","net"); classes = "vehicle"; aboxes = [8 8; 32 48; 40 24; 72 48]; detector = yolov2ObjectDetector(net,classes,aboxes);
Configure training options.
options = trainingOptions("sgdm", ... InitialLearnRate=0.001, ... Verbose=true, ... MiniBatchSize=16, ... MaxEpochs=30, ... Shuffle="every-epoch", ... VerboseFrequency=10);
Train the detector.
[detector,info] = trainYOLOv2ObjectDetector(cds,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.17 | 51.4 | 0.0010 | | 1 | 10 | 00:00:04 | 1.78 | 3.2 | 0.0010 | | 2 | 20 | 00:00:07 | 1.57 | 2.5 | 0.0010 | | 2 | 30 | 00:00:11 | 1.40 | 1.9 | 0.0010 | | 3 | 40 | 00:00:16 | 1.11 | 1.2 | 0.0010 | | 3 | 50 | 00:00:19 | 1.28 | 1.6 | 0.0010 | | 4 | 60 | 00:00:23 | 0.96 | 0.9 | 0.0010 | | 4 | 70 | 00:00:27 | 0.90 | 0.8 | 0.0010 | | 5 | 80 | 00:00:31 | 0.79 | 0.6 | 0.0010 | | 5 | 90 | 00:00:36 | 0.83 | 0.7 | 0.0010 | | 6 | 100 | 00:00:40 | 0.84 | 0.7 | 0.0010 | | 7 | 110 | 00:00:44 | 0.57 | 0.3 | 0.0010 | | 7 | 120 | 00:00:49 | 0.62 | 0.4 | 0.0010 | | 8 | 130 | 00:00:52 | 0.59 | 0.3 | 0.0010 | | 8 | 140 | 00:00:56 | 0.56 | 0.3 | 0.0010 | | 9 | 150 | 00:01:01 | 0.54 | 0.3 | 0.0010 | | 9 | 160 | 00:01:05 | 0.54 | 0.3 | 0.0010 | | 10 | 170 | 00:01:09 | 0.62 | 0.4 | 0.0010 | | 10 | 180 | 00:01:12 | 0.53 | 0.3 | 0.0010 | | 11 | 190 | 00:01:16 | 0.58 | 0.3 | 0.0010 | | 12 | 200 | 00:01:19 | 0.45 | 0.2 | 0.0010 | | 12 | 210 | 00:01:22 | 0.52 | 0.3 | 0.0010 | | 13 | 220 | 00:01:26 | 0.53 | 0.3 | 0.0010 | | 13 | 230 | 00:01:29 | 0.49 | 0.2 | 0.0010 | | 14 | 240 | 00:01:33 | 0.50 | 0.2 | 0.0010 | | 14 | 250 | 00:01:36 | 0.44 | 0.2 | 0.0010 | | 15 | 260 | 00:01:40 | 0.50 | 0.3 | 0.0010 | | 15 | 270 | 00:01:44 | 0.56 | 0.3 | 0.0010 | | 16 | 280 | 00:01:47 | 0.55 | 0.3 | 0.0010 | | 17 | 290 | 00:01:50 | 0.46 | 0.2 | 0.0010 | | 17 | 300 | 00:01:54 | 0.40 | 0.2 | 0.0010 | | 18 | 310 | 00:01:57 | 0.50 | 0.3 | 0.0010 | | 18 | 320 | 00:02:01 | 0.51 | 0.3 | 0.0010 | | 19 | 330 | 00:02:05 | 0.44 | 0.2 | 0.0010 | | 19 | 340 | 00:02:12 | 0.47 | 0.2 | 0.0010 | | 20 | 350 | 00:02:20 | 0.43 | 0.2 | 0.0010 | | 20 | 360 | 00:02:24 | 0.51 | 0.3 | 0.0010 | | 21 | 370 | 00:02:27 | 0.46 | 0.2 | 0.0010 | | 22 | 380 | 00:02:31 | 0.49 | 0.2 | 0.0010 | | 22 | 390 | 00:02:34 | 0.53 | 0.3 | 0.0010 | | 23 | 400 | 00:02:38 | 0.49 | 0.2 | 0.0010 | | 23 | 410 | 00:02:42 | 0.38 | 0.1 | 0.0010 | | 24 | 420 | 00:02:46 | 0.48 | 0.2 | 0.0010 | | 24 | 430 | 00:02:50 | 0.40 | 0.2 | 0.0010 | | 25 | 440 | 00:02:56 | 0.44 | 0.2 | 0.0010 | | 25 | 450 | 00:03:02 | 0.41 | 0.2 | 0.0010 | | 26 | 460 | 00:03:08 | 0.46 | 0.2 | 0.0010 | | 27 | 470 | 00:03:12 | 0.51 | 0.3 | 0.0010 | | 27 | 480 | 00:03:16 | 0.49 | 0.2 | 0.0010 | | 28 | 490 | 00:03:20 | 0.48 | 0.2 | 0.0010 | | 28 | 500 | 00:03:24 | 0.44 | 0.2 | 0.0010 | | 29 | 510 | 00:03:28 | 0.27 | 7.3e-02 | 0.0010 | | 29 | 520 | 00:03:31 | 0.37 | 0.1 | 0.0010 | | 30 | 530 | 00:03:35 | 0.32 | 0.1 | 0.0010 | | 30 | 540 | 00:03:38 | 0.33 | 0.1 | 0.0010 | |========================================================================================| Training finished: Max epochs completed. Detector training complete.
Read a test image.
I = imread("detectcars.png");
Run the detector.
[bboxes,scores] = detect(detector,I);
Display the results.
if(~isempty(bboxes)) I = insertObjectAnnotation(I,"rectangle",bboxes,scores); end figure imshow(I)
Use training data to train an ACF-based object detector for stop signs
Add the folder containing images to the MATLAB® path.
imageDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata', 'stopSignImages'); addpath(imageDir);
Load ground truth data, which contains data for stops signs and cars.
load('stopSignsAndCarsGroundTruth.mat','stopSignsAndCarsGroundTruth')
View the label definitions to see the label types in the ground truth.
stopSignsAndCarsGroundTruth.LabelDefinitions
ans=3×3 table
Name Type Group
____________ _________ ________
{'stopSign'} Rectangle {'None'}
{'carRear' } Rectangle {'None'}
{'carFront'} Rectangle {'None'}
Select the stop sign data for training.
stopSignGroundTruth = selectLabelsByName(stopSignsAndCarsGroundTruth,'stopSign');
Create the training data for a stop sign object detector.
trainingData = objectDetectorTrainingData(stopSignGroundTruth); summary(trainingData)
trainingData: 41×2 table
Variables:
imageFilename: cell array of character vectors
stopSign: cell
Statistics for applicable variables:
NumMissing
imageFilename 0
stopSign 0
Train an ACF-based object detector.
acfDetector = trainACFObjectDetector(trainingData,'NegativeSamplesFactor',2);
ACF Object Detector Training The training will take 4 stages. The model size is 34x31. Sample positive examples(~100% Completed) Compute approximation coefficients...Completed. Compute aggregated channel features...Completed.
Stage 1: Sample negative examples(~100% Completed) Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 19 weak learners.
Stage 2: Sample negative examples(~100% Completed) Found 84 new negative examples for training. Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 20 weak learners.
Stage 3: Sample negative examples(~100% Completed) Found 84 new negative examples for training. Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 54 weak learners.
Stage 4: Sample negative examples(~100% Completed) Found 84 new negative examples for training. Compute aggregated channel features...Completed. Train classifier with 42 positive examples and 84 negative examples...Completed. The trained classifier has 61 weak learners.
ACF object detector training is completed. Elapsed time is 13.9558 seconds.
Test the ACF-based detector on a sample image.
I = imread('stopSignTest.jpg'); bboxes = detect(acfDetector,I);
Display the detected object.
annotation = acfDetector.ModelName; I = insertObjectAnnotation(I,'rectangle',bboxes,annotation);
figure imshow(I)
Remove the image folder from the path.
Load image locations, label definitions and label data.
data = load('labelsWithAttributes.mat'); images = fullfile(matlabroot,'toolbox','vision','visiondata','stopSignImages', data.imageFilenames);
Create a ground truth object.
dataSource = groundTruthDataSource(images); gTruth = groundTruth(groundTruthDataSource(images), data.labeldefs, data.labelData);
Create an image datastore, box label datastore, and array datastore using the ground truth object.
[imds, blds, arrds] = objectDetectorTrainingData(gTruth);
Read all attributes.
ans=2×1 cell array {1×1 struct} {1×1 struct}
Input Arguments
Ground truth data, specified as a scalar or an array of groundTruth objects. You can create ground truth objects from existing ground truth data by using thegroundTruth
object.
If you use custom data sources in groundTruth
with parallel computing enabled, then the reader function is expected to work with a pool of MATLAB workers to read images from the data source in parallel.
Note
If you specify gTruth
as an array ofgroundTruth
objects, all label definitions must have the same label names.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: (SamplingFactor
=5
) sets the subsampling factor to 5
.
Factor for subsampling images in the ground truth data source, specified as "auto"
, an integer, or a vector of integers. For a sampling factor of N, the returned training data includes every _N_th image in the ground truth data source. The function ignores ground truth images with empty label data. To set the SamplingFactor
with projected cuboid data, you must specify theLabelType name-value argument tolabelType.ProjectedCuboid
.
Use sampled data to reduce repeated data, such as a sequence of images with the same scene and labels. It can also help in reducing training time.
Value | Sampling Factor |
---|---|
"auto" | The function samples data sources with timestamps, such as a video, with a factor of5, and 1 for a collection of images. |
Integer | Manually set the sampling factor to apply to all data. |
Vector of integers | When you input an array of ground truth objects, the function uses the sampling factor specified by the corresponding vector element. |
Type of label to extract from ground truth data, specified as"labelType.Rectangle"
,"labelType.RotatedRectangle"
, or"labelType.ProjectedCuboid"
. Use the type of label consistent with the type of object detector you want to train.
Folder name to write extracted images to, specified as a string scalar or character vector. The specified folder must exist and have write permissions.
This argument applies only for:
- groundTruth objects created using a video file or a custom data source.
- An array of groundTruth objects created using imageDatastore , with different custom
read
functions.
The function ignores this argument when:
- The input groundTruth object was created from an image sequence data source.
- The array of input groundTruth objects all contain image datastores using the same custom
read
function. - Any of the input groundTruth objects containing datastores, use the default
read
functions.
Image file format, specified as a string scalar or character vector. File formats must be supported by imwrite.
This argument applies only for:
- groundTruth objects created using a video file or a custom data source.
- An array of groundTruth objects created using imageDatastore with different custom
read
functions.
The function ignores this argument when:
- The input groundTruth object was created from an image sequence data source.
- The array of input groundTruth objects all contain image datastores using the same custom
read
function. - Any of the input groundTruth objects containing datastores, use the default
read
functions.
Prefix for output image file names, specified as a string scalar or character vector. The image files are named as:
_.
The default value uses the name of the data source that the images were extracted from, strcat(sourceName,"_")
, for video and a custom data source, or "datastore"
, for an image datastore.
This argument applies only for:
- groundTruth objects created using a video file or a custom data source.
- An array of groundTruth objects created using imageDatastore with different custom
read
functions.
The function ignores this argument when:
- The input groundTruth object was created from an image sequence data source.
- The array of input groundTruth objects all contain image datastores using the same custom
read
function. - Any of the input groundTruth objects containing datastores, use the default
read
functions.
Flag to display training progress at the MATLAB command line, specified as either true
(1
) orfalse
(0
). This property applies only for groundTruth objects created using a video file or a custom data source.
Output Arguments
Image datastore, returned as an imageDatastore object containing images extracted from the gTruth objects. The images in imds
contain at least one class of annotated labels. The function ignores images that are not annotated.
Box label datastore, returned as a boxLabelDatastore object. The datastore contains categorical vectors for ROI label names and M_-by-4 matrices of_M bounding boxes. The locations and sizes of the bounding boxes are represented as double _M_-by-4 element vectors in the format [x,y,width,_height_].
Array datastore, returned as a struct
array. The fields of the struct
contain the attributes and sublabel names for the corresponding labels in the box label datastoreblds. The sublabel data is packaged into thestruct
with a Position
field along with the fields that correspond to the sublabel attributes.
Training data table, returned as a table with two or more columns. The first column of the table contains image file names with paths. The images can be grayscale or truecolor (RGB) and in any format supported by imread. Each of the remaining columns correspond to an ROI label and contains the locations of bounding boxes in the image (specified in the first column), for that label. The bounding boxes are specified as M_-by-4 matrices of_M bounding boxes in the format [x,y,width,_height_]. [x,_y_] specifies the upper-left corner location. To create a ground truth table, you can use the Image Labeler app or Video Labeler app.
The output table ignores any sublabel or attribute data present in the input gTruth object.
Version History
Introduced in R2017a
- Updated to support 3-D projected cuboid labels.
- Returns extracted attributes and sublabels as a third output. The attributes and sublabels are packaged as an array datastore.
See Also
Apps
Functions
- trainACFObjectDetector | trainYOLOv2ObjectDetector | trainYOLOv3ObjectDetector | trainYOLOv4ObjectDetector | estimateAnchorBoxes
Objects
Topics
- Datastores for Deep Learning (Deep Learning Toolbox)
- Training Data for Object Detection and Semantic Segmentation