Train ACF object detector
returns a trained aggregate channel features (ACF) object detector. The function
uses positive instances of objects in images given in the
detector
= trainACFObjectDetector(trainingData
)trainingData
table and automatically collects negative
instances from the images during training. To create a ground truth table, use
the Image
Labeler or Video
Labeler app.
returns
a detector
= trainACFObjectDetector(trainingData
,Name,Value
)detector
object with additional options specified
by one or more Name,Value
pair arguments.
Use the trainACFObjectDetector
with training images to create an ACF object detector that can detect stop signs. Test the detector with a separate image.
Load the training data.
load('stopSignsAndCars.mat')
Select the ground truth for stop signs. These ground truth is the set of known locations of stop signs in the images.
stopSigns = stopSignsAndCars(:,1:2);
Add the full path to the image files.
stopSigns.imageFilename = fullfile(toolboxdir('vision'),... 'visiondata',stopSigns.imageFilename);
Train the ACF detector. You can turn off the training progress output by specifying 'Verbose',false
as a Name,Value
pair.
acfDetector = trainACFObjectDetector(stopSigns,'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 19.2848 seconds.
Test the ACF detector on a test image.
img = imread('stopSignTest.jpg');
[bboxes,scores] = detect(acfDetector,img);
Display the detection results and insert the bounding boxes for objects into the image.
for i = 1:length(scores) annotation = sprintf('Confidence = %.1f',scores(i)); img = insertObjectAnnotation(img,'rectangle',bboxes(i,:),annotation); end figure imshow(img)
trainingData
— Labeled ground truth imagesLabeled ground truth images, specified as a table with two columns. The first column must contain paths and file names to grayscale or truecolor (RGB) images. Although, ACF-based detectors work best with truecolor images. The second column contains M-by-4 matrices, that contain the locations of the bounding boxes related to the corresponding image. The locations are in the format, [x,y,width,height]. The second column represents a positive instance of a single object class, such as a car, dog, flower, or stop sign. Negative instances are automatically collected from images during the training process.
Each bounding box must be in the format [x,y,width,height]. The format specifies the upper-left corner location and the size of the object in the corresponding image. The table variable (column) name defines the object class name. To create the ground truth table, use the Image Labeler app.
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
.
'ObjectTrainingSize'
, [100
100]
'ObjectTrainingSize'
— Size of training images'Auto'
(default) | [height width] vectorSize of training images, specified as the comma-separated pair consisting of
'ObjectTrainingSize
' and either
'Auto'
or a [height
width] vector. The minimum value of
height and width is
8
. During the training process, all images are
resized to this height and width. Increasing the size can improve
detection accuracy, but also increases training and detection
times.
When you specify 'Auto'
, the size is set
based on the median width-to-height ratio of the positive instances.
Example: [100,100]
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
'NumStages'
— Number of training stages4
(default) | positive integerNumber of training stages for the iterative training process,
specified as the comma-separated pair consisting of 'NumStages
'
and a positive integer. Increasing this number can improve the detector
and reduce training errors, at the expense of longer training time.
Data Types: double
'NegativeSamplesFactor'
— Negative sample factor5
(default) | real-valued scalarNegative sample factor, specified as the comma-separated pair
consisting of 'NegativeSamplesFactor
' and a real-valued
scalar. The number of negative samples to use at each stage is equal
to
NegativeSamplesFactor
× number
of positive samples used at each stage
Data Types: double
'MaxWeakLearners'
— Maximum number of weak learners2048
(default) | positive integer scalar | vector of positive integersMaximum number of weak learners for the last stage, specified
as the comma-separated pair consisting of 'MaxWeakLearners
'
and a positive integer scalar or vector of positive integers. If the
input is a scalar, MaxWeakLearners
specifies
the maximum number for the last stage. If the input is a vector, MaxWeakLearners
specifies
the maximum number for each of the stages and must have a length equal
to 'NumStages
'. These values typically increase
throughout the stages. The ACF object detector uses the boosting algorithm
to create an ensemble of weaker learners. You can use higher values
to improve the detection accuracy, at the expense of reduced detection
performance speeds. Recommended values range from 300 to 5000.
Data Types: double
'Verbose'
— Display progress informationtrue
(default) | false
Option to display progress information for the training process,
specified as the comma-separated pair consisting of 'Verbose
'
and true
or false
.
Data Types: logical
detector
— Trained ACF-based object detectoracfObjectDetector
objectTrained ACF-based object detector, returned as an acfObjectDetector
object.
You have a modified version of this example. Do you want to open this example with your edits?