This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

objectDetectorTrainingData

Create training data for an object detector

Syntax

trainingData = objectDetectorTrainingData(gTruth)
trainingData = objectDetectorTrainingData(gTruth,Name,Value)

Description

example

trainingData = objectDetectorTrainingData(gTruth) returns a table of training data from the specified ground truth. The table can be used to train an object detector using training functions such as trainACFObjectDetector, trainRCNNObjectDetector, trainFastRCNNObjectDetector, and trainFasterRCNNObjectDetector.

This function supports parallel computing using multiple MATLAB® workers. Enable parallel computing using the Computer Vision System Toolbox Preferences dialog.

trainingData = objectDetectorTrainingData(gTruth,Name,Value) returns a training data table with additional options specified by one or more name-value pair arguments. If the groundTruth objects in gTruth were created using a video file or a custom data source, then you can specify any combination of name-value pair arguments. If the groundTruth objects were created from an image collection or image sequence data source, then you can specify only the SamplingFactor name-value pair argument.

Examples

collapse all

Use training data to train an ACF-based detector.

Add the folder containing images to the MATLAB path.

imageDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata', 'stopSignImages');
addpath(imageDir);

Load groundTruth data. Ground truth contains data for stops signs and cars.

load('stopSignsAndCarsGroundTruth.mat','stopSignsAndCarsGroundTruth')

View label definitions to see label types in the ground truth.

stopSignsAndCarsGroundTruth.LabelDefinitions
ans=3×2 table
       Name         Type   
    __________    _________

    'stopSign'    Rectangle
    'carRear'     Rectangle
    'carFront'    Rectangle

Select the stop sign data for training.

stopSignGroundTruth = selectLabels(stopSignsAndCarsGroundTruth,'stopSign');

Create training data for a vehicle object detector.

trainingData = objectDetectorTrainingData(stopSignGroundTruth);
summary(trainingData)
Variables:

    imageFilename: 41x1 cell array of character vectors

    stopSign: 41x1 cell

Train an ACF object detector for vehicles.

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 48.4379 seconds.

Test the ACF 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.

rmpath(imageDir); 

Add the image folder to the MATLAB path.

imageDir = fullfile(matlabroot,'toolbox','driving','drivingdata','vehiclesSequence');
addpath(imageDir);

Load groundTruth data.

load vehicleGroundTruth.mat

Create training data for a vehicle object detector.

trainingData = objectDetectorTrainingData(gTruth,'SamplingFactor',2);

Train an ACF object detector for vehicles.

acfDetector = trainACFObjectDetector(trainingData,'ObjectTrainingSize',[20 20]);
ACF Object Detector Training
The training will take 4 stages. The model size is 20x20.
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 71 positive examples and 355 negative examples...Completed.
The trained classifier has 68 weak learners.
--------------------------------------------
Stage 2:
Sample negative examples(~100% Completed)
Found 76 new negative examples for training.
Compute aggregated channel features...Completed.
Train classifier with 71 positive examples and 355 negative examples...Completed.
The trained classifier has 120 weak learners.
--------------------------------------------
Stage 3:
Sample negative examples(~100% Completed)
Found 54 new negative examples for training.
Compute aggregated channel features...Completed.
Train classifier with 71 positive examples and 355 negative examples...Completed.
The trained classifier has 170 weak learners.
--------------------------------------------
Stage 4:
Sample negative examples(~100% Completed)
Found 63 new negative examples for training.
Compute aggregated channel features...Completed.
Train classifier with 71 positive examples and 355 negative examples...Completed.
The trained classifier has 215 weak learners.
--------------------------------------------
ACF object detector training is completed. Elapsed time is 19.0148 seconds.

Test the ACF detector on a test image.

I = imread('highway.png');
[bboxes, scores] = detect(acfDetector,I,'Threshold',1);

Select strongest detection.

[~,idx] = max(scores);

Display the detected object.

annotation = acfDetector.ModelName;
I = insertObjectAnnotation(I,'rectangle',bboxes(idx,:),annotation);

figure 
imshow(I)

Remove the image folder from the path.

rmpath(imageDir);

Input Arguments

collapse all

Ground truth data, specified as an array of groundTruth objects. You can create ground truth objects from existing ground truth data by using the groundTruth 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.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'SamplingFactor',5

Factor at which to subsample 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 Nth image in the ground truth data source, ignoring ground truth images with empty label data.

SamplingFactorSampling Factor Applied
'auto'The sampling factor N is 5 for data sources with timestamps, and 1 for a collection of images.
IntegerAll ground truth data sources in gTruth are sampled with the same sampling factor, N.
Vector of integersThe kth ground truth data source in gTruth is sampled with a sampling factor of N(k).

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 property applies only for groundTruth objects created using a video file or a custom data source.

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.

Prefix applied to output image file names, specified as a string scalar or character vector. The image files are named as:

<name_prefix><image_number>.<image_format>

The default value uses the name of the data source are the images where extracted from, strcat(sourceName,'_'). This property applies only for groundTruth objects created using a video file or a custom data source.

Display training progress on the MATLAB command line, specified as either true or false. This property applies only for groundTruth objects created using a video file or a custom data source.

Output Arguments

collapse all

Training data, returned as a table with two or more columns. The first column of the table contains image file names. The remaining columns contain object positions for rectangular ROI labels found in the ground truth object input, gTruth. The rectangular ROI labels must be [x,y,width,height] bounding boxes that specify object positions within each image. Only labels corresponding to rectangle ROI labels are returned in trainingData. Other labels are ignored. The output table ignores any sublabel and/or attribute data present in the input gTruth object.

Introduced in R2017a