detect - Detect objects using YOLO v2 object detector - MATLAB (original) (raw)
Detect objects using YOLO v2 object detector
Syntax
Description
[bboxes](#mw%5Fb37a078f-053a-423f-9133-1eebc34b17fa) = detect([detector](#mw%5F19de3c90-cd14-4a88-bec4-2ef967d0902d),[I](#mw%5F9aaec1f6-72d8-4537-9ba6-79ac24fa5d0b%5Fsep%5Fmw%5F4eb34e25-e00e-4069-90dd-a1ee7ef516b6))
detects objects within a single image or an array of images, I
, using you only look once version 2 (YOLO v2) object detector. The input size of the image must be greater than or equal to the network input size of the pretrained detector. The locations of objects detected are returned as a set of bounding boxes.
Note
The use of a CUDA®-enabled NVIDIA® GPU is highly recommended. The GPU reduces computation time significantly. Usage of the GPU requires Parallel Computing Toolbox™. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).
[[bboxes](#mw%5Fb37a078f-053a-423f-9133-1eebc34b17fa),[scores](#mw%5F4f102b2d-c959-40b5-b04b-ebfae9aaab67)] = detect([detector](#mw%5F19de3c90-cd14-4a88-bec4-2ef967d0902d),[I](#mw%5F9aaec1f6-72d8-4537-9ba6-79ac24fa5d0b%5Fsep%5Fmw%5F4eb34e25-e00e-4069-90dd-a1ee7ef516b6))
also returns the class-specific confidence scores for each bounding box.
[___,[labels](#mw%5F9fe9a2a1-b0ef-4caf-ad54-3a2682cb7a93)] = detect([detector](#mw%5F19de3c90-cd14-4a88-bec4-2ef967d0902d),[I](#mw%5F9aaec1f6-72d8-4537-9ba6-79ac24fa5d0b%5Fsep%5Fmw%5F4eb34e25-e00e-4069-90dd-a1ee7ef516b6))
returns a categorical array of labels assigned to the bounding boxes in addition to the output arguments from the previous syntax. The labels used for object classes are defined during training using the trainYOLOv2ObjectDetector function.
[___,[labels](#mw%5F9fe9a2a1-b0ef-4caf-ad54-3a2682cb7a93),[info](#mw%5F6c8886c8-f440-4602-b3af-b7824d274af4)] = detect([detector](#mw%5F19de3c90-cd14-4a88-bec4-2ef967d0902d),[I](#mw%5F9aaec1f6-72d8-4537-9ba6-79ac24fa5d0b%5Fsep%5Fmw%5F4eb34e25-e00e-4069-90dd-a1ee7ef516b6))
also returns information about the class probabilities and objectness scores for each detection.
[detectionResults](#mw%5F37a05e22-5160-415c-8013-28b8d9ec16b5) = detect([detector](#mw%5F19de3c90-cd14-4a88-bec4-2ef967d0902d),[ds](#mw%5F9aaec1f6-72d8-4537-9ba6-79ac24fa5d0b%5Fsep%5Fmw%5Fdf05771b-5b5c-475e-aaf6-1856dc6dc91b))
detects objects within all the images returned by the read function of the input datastore.
[___] = detect(___,[roi](#mw%5F9aaec1f6-72d8-4537-9ba6-79ac24fa5d0b%5Fsep%5Fmw%5F9d414605-716f-4b3a-8102-0d485423b247))
detects objects within the rectangular search region specified byroi
. Use output arguments from any of the previous syntaxes. Specify input arguments from any of the previous syntaxes.
[___] = detect(___,[Name=Value](#namevaluepairarguments))
specifies options using one or more name-value arguments in addition to any combination of arguments from previous syntaxes. For example, Threshold=0.75
sets the threshold to remove detections to 0.75
.
Examples
Load a YOLO v2 object detector pretrained to detect vehicles.
vehicleDetector = load('yolov2VehicleDetector.mat','detector'); detector = vehicleDetector.detector;
Read a test image into the workspace.
I = imread('highway.png');
Display the input test image.
Run the pretrained YOLO v2 object detector on the test image. Inspect the results for vehicle detection. The labels are derived from the ClassNames
property of the detector.
[bboxes,scores,labels] = detect(detector,I)
labels = categorical vehicle
Annotate the image with the bounding boxes for the detections.
if ~isempty(bboxes) detectedI = insertObjectAnnotation(I,'rectangle',bboxes,cellstr(labels)); end figure imshow(detectedI)
Input Arguments
Input image, specified as an_H_-by-_W_-by-_C_-by-B numeric array of images. Images must be real, nonsparse, grayscale or RGB image.
- H — Height in pixels.
- W — Width in pixels
- C — The channel size in each image must be equal to the network's input channel size. For example, for grayscale images,C must be equal to
1
. For RGB color images, it must be equal to3
. - B — Number of images in the array.
The detector is sensitive to the range of the input image. Therefore, ensure that the input image range is similar to the range of the images used to train the detector. For example, if the detector was trained on uint8
images, rescale this input image to the range [0, 255] by using the im2uint8 or rescale function. The size of this input image should be comparable to the sizes of the images used in training. If these sizes are very different, the detector has difficulty detecting objects because the scale of the objects in the input image differs from the scale of the objects the detector was trained to identify. Consider whether you used the SmallestImageDimension
property during training to modify the size of training images.
Data Types: uint8
| uint16
| int16
| double
| single
| logical
Datastore, specified as a datastore object containing a collection of images. Each image must be a grayscale, RGB, or multichannel image. The function processes only the first column of the datastore, which must contain images and must be cell arrays or tables with multiple columns.
Search region of interest, specified as a four-element vector of the form [x y width _height_]. The vector specifies the upper left corner and size of a region in pixels.
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: Threshold=0.25
sets the threshold to remove detections to0.25
.
Detection threshold, specified as a scalar in the range [0, 1]. Detections that have scores less than this threshold value are removed. To reduce false positives, increase this value.
Select the strongest bounding box for each detected object, specified astrue
or false
.
true
— Returns the strongest bounding box per object. The method calls the selectStrongestBboxMulticlass function, which uses nonmaximal suppression to eliminate overlapping bounding boxes based on their confidence scores.
By default, the selectStrongestBboxMulticlass function is called as follows
selectStrongestBboxMulticlass(bbox,scores,...
RatioType="Union",...
OverlapThreshold=0.5);false
— Return all the detected bounding boxes. You can then write your own custom method to eliminate overlapping bounding boxes.
Minimum region size, specified as a vector of the form [height _width_]. Units are in pixels. The minimum region size defines the size of the smallest region containing the object.
By default, MinSize
is 1-by-1.
Maximum region size, specified as a vector of the form [height _width_]. Units are in pixels. The maximum region size defines the size of the largest region containing the object.
By default, MaxSize
is set to the height and width of the input image, I. To reduce computation time, set this value to the known maximum region size for the objects that can be detected in the input test image.
Minimum batch size, specified as a scalar value. Use theMiniBatchSize
to process a large collection of image. Images are grouped into minibatches and processed as a batch, which can improve computational efficiency at the cost of increased memory demand. Decrease the size to use less memory.
Hardware resource on which to run the detector, specified as"auto"
, "gpu"
, or "cpu"
.
"auto"
— Use a GPU if it is available. Otherwise, use the CPU."gpu"
— Use the GPU. To use a GPU, you must have Parallel Computing Toolbox and a CUDA-enabled NVIDIA GPU. If a suitable GPU is not available, the function returns an error. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox)."cpu"
— Use the CPU.
Performance optimization, specified one of the following:
"auto"
— Automatically apply a number of optimizations suitable for the input network and hardware resource."mex"
— Compile and execute a MEX function. This option is available when using a GPU only. Using a GPU requires Parallel Computing Toolbox and a CUDA enabled NVIDIA GPU. If Parallel Computing Toolbox or a suitable GPU is not available, then the function returns an error. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox)."none"
— Disable all acceleration.
The default option is "auto"
. If "auto"
is specified, MATLAB® applies a number of compatible optimizations. If you use the"auto"
option, MATLAB does not ever generate a MEX function.
Using the Acceleration
options "auto"
and"mex"
can offer performance benefits, but at the expense of an increased initial run time. Subsequent calls with compatible parameters are faster. Use performance optimization when you plan to call the function multiple times using new input data.
The "mex"
option generates and executes a MEX function based on the network and parameters used in the function call. You can have several MEX functions associated with a single network at one time. Clearing the network variable also clears any MEX functions associated with that network.
The "mex"
option is only available for input data specified as a numeric array, cell array of numeric arrays, table, or image datastore. No other types of datastore support the "mex"
option.
The "mex"
option is only available when you are using a GPU. You must also have a C/C++ compiler installed. For setup instructions, see Set Up Compiler (GPU Coder).
"mex"
acceleration does not support all layers. For a list of supported layers, see Supported Layers (GPU Coder).
Output Arguments
Location of objects detected within the input image or images, returned as an_M_-by-4 matrix or a B_-by-1 cell array.M is the number of bounding boxes in an image, and_B is the number of _M_-by-4 matrices when the input contains an array of images.
Each row of bboxes
contains a four-element vector of the form [x y width _height_]. This vector specifies the upper left corner and size of that corresponding bounding box in pixels.
Detection confidence scores for each bounding box, returned as one of these options:
- _M_-by-1 numeric vector — The input is a single test image.M is the number of bounding boxes detected in the image.
- _B_-by-1 cell array — The input is a batch of test images, where B is the number of test images in the batch. Each cell in the array contains an _M_-element row vector, where each element indicates the detection score for a bounding box in the corresponding image.
A higher score indicates higher confidence in the detection. The confidence score for each detection is a product of the corresponding objectness score and maximum class probability. The objectness score is the probability that the object in the bounding box belongs to a class in the image. The maximum class probability is the largest probability that a detected object in the bounding box belongs to a particular class.
Labels for bounding boxes, returned as one of these options:
- _M_-by-1 categorical vector if the input is a single test image.
- _T_-by-1 cell array if the input is an array of test images.T is the number of test images in the array. Each cell in the array contains a _M_-by-1 categorical vector containing the names of the object classes.
M is the number of bounding boxes detected in an image.
Detection results, returned as a 3-column table with variable names,Boxes, Scores, and Labels. The Boxes column contains M_-by-4 matrices, of_M bounding boxes for the objects found in the image. Each row contains a bounding box as a 4-element vector in the format [x,y,width,_height_]. The format specifies the upper-left corner location and size in pixels of the bounding box in the corresponding image.
Class probabilities and objectness scores of the detections, returned as a structure array with these fields.
ClassProbabilities
— Class probabilities for each of the detections, returned as a _B_-by-1 cell array. B is the number of images in the input batch of images,I
. Each cell in the array contains the class probabilities as an_M_-by-N numeric matrix. M is the number of bounding boxes and N is the number of classes. Each class probability is a numeric scalar, indicating the probability that the detected object in the bounding box belongs to a class in the image.ObjectnessScores
— Objectness scores for each of the detections, returned as a _B_-by-1 cell array. B is the number of images in the input batch of images,I
. Each cell in the array contains the objectness score for each bounding box as an_M_-by-1 numeric vector. M is the number of bounding boxes. Each objectness score is a numeric scalar, indicating the probability that the bounding box contains an object belonging to one of the classes in the image.
More About
By default, the detect
function preprocesses the test image for object detection by:
- Resizing it to a nearest possible image size used for training the YOLO v2 network. The function determines the nearest possible image size from the
TrainingImageSize
property of the yolov2ObjectDetector object. - Normalizing its pixel values to lie in same range as that of the images used to train the YOLO v2 object detector. For example, if the detector was trained on
uint8
images, the test image must also have pixel values in the range [0, 255]. Otherwise, use the im2uint8 or rescale function to rescale the pixel values in the test image.
Extended Capabilities
Usage notes and limitations:
- The
roi
argument to thedetect
method must be a code generation constant (coder.const()
) and a 1x4 vector. - Only the
Threshold
,SelectStrongest
,MinSize
, andMaxSize
name-value pairs fordetect
are supported.
Usage notes and limitations:
- The
roi
argument to thedetect
method must be a codegen constant (coder.const()
) and a 1x4 vector. - Only the
Threshold
,SelectStrongest
,MinSize
,MaxSize
, andMiniBatchSize
Name-Value pairs are supported. - The height, width, channel, and batch size of the input image must be fixed size.
- The minimum batch size value passed to detect method must be fixed size.
Version History
Introduced in R2019a
Specify the info output argument to return information about the class probability and objectness score for each detection.
See Also
Apps
Functions
Objects
Topics
- Create Custom YOLO v2 Object Detection Network
- Object Detection Using YOLO v2 Deep Learning
- Multiclass Object Detection Using YOLO v2 Deep Learning
- Estimate Anchor Boxes from Training Data
- Code Generation for Object Detection by Using YOLO v2
- Getting Started with YOLO v2
- Get Started with Object Detection Using Deep Learning
- Choose an Object Detector
- Anchor Boxes for Object Detection
- Datastores for Deep Learning (Deep Learning Toolbox)