Main Content

yolov2ObjectDetector

Detect objects using YOLO v2 object detector

Since R2019a

Description

The yolov2ObjectDetector object creates a you only look once version 2 (YOLO v2) object detector for detecting objects in an image. Using this object, you can:

  • Create a pretrained YOLO v2 object detector by using YOLO v2 deep learning networks trained on COCO dataset.

  • Create a custom YOLO v2 object detector by using a custom trained YOLO v2 deep learning network.

You can also create a yolov2ObjectDetector object by calling the trainYOLOv2ObjectDetector function with training data (requires Deep Learning Toolbox™).

detector = trainYOLOv2ObjectDetector(trainingData,____)
The YOLO v2 object detector recognizes specific objects in images, based on the training images and ground truth data used with the trainYOLOv2ObjectDetector function.

To detect objects in an image, pass the YOLO v2 object detector to the detect object function.

Creation

Description

Pretrained YOLO v2 Object Detector

example

detector = yolov2ObjectDetector(name) creates a pretrained YOLO v2 object detector by using YOLO v2 deep learning networks trained on a COCO dataset.

To use the YOLO v2 networks trained on COCO dataset, you must download and install the Computer Vision Toolbox™ Model for YOLO v2 Object Detection support package. You can download the Computer Vision Toolbox Model for YOLO v2 Object Detection from the Add-On Explorer. For more information, see Get and Manage Add-Ons.

Custom YOLO v2 Object Detector

detector = yolov2ObjectDetector(network) creates a YOLO v2 object detector by using a custom pretrained YOLO v2 network specified at the input.

The input network can also be an imported network from ONNX™ (Open Neural Network Exchange). For more information on how to create YOLO v2 object detector from an imported ONNX YOLO v2 network, see Import Pretrained ONNX YOLO v2 Object Detector.

detector = yolov2ObjectDetector(___,'TrainingImageSize',trainingSizes) specify image sizes used during training by using a name-value pair in addition to the input argument in the previous syntax.

Input Arguments

expand all

Name of the pretrained YOLO v2 deep learning network, specified as one of these:

  • 'darknet19-coco' — A pretrained YOLO v2 deep learning network created using DarkNet-19 as the base network and trained on COCO dataset.

  • 'tiny-yolov2-coco' — A pretrained YOLO v2 deep learning network created using a small base network and trained on COCO dataset.

Data Types: char | string

Custom trained YOLO v2 network, specified as a DAGNetwork object. The DAGNetwork must have an image input layer, YOLO v2 transform layer that is connected to a YOLO v2 output layer.

Set of image sizes used for training, specified as a comma-separated pair consisting of 'TrainingImageSize' and a M-by-2 matrix. Each row is of the form [height width]. The default value is the size of the image input layer of the network.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Properties

expand all

Name for object detector, specified as a character vector or string scalar.

  • If input to the yolov2ObjectDetector object is a pretrained YOLO v2 network, the default value of this property is set to the name of the pretrained YOLO v2 network specified at the input.

  • If the yolov2ObjectDetector object is created using the trainYOLOv2ObjectDetector function, the default value of this property is set to the heading of the second column of the trainingData table specified in the trainYOLOv2ObjectDetector function.

You can modify this name after creating the yolov2ObjectDetector object.

This property is read-only.

Trained YOLO v2 object detection network, specified as a DAGNetwork (Deep Learning Toolbox) object. This object stores the layers that define the YOLO v2 object detection network.

This property is read-only.

Names of object classes that the YOLO v2 object detector was trained to find, specified as a cell array of character vectors. This property is set by the trainingData input argument in the trainYOLOv2ObjectDetector function. Specify the class names as part of the trainingData table.

This property is read-only.

Set of anchor boxes, specified as an N-by-2 matrix defining the width and the height of N anchor boxes. This property is set by the AnchorBoxes property of the output layer in the YOLO v2 network.

The anchor boxes are defined when creating the YOLO v2 network by using the yolov2Layers function. Alternatively, if you create the YOLO v2 network layer-by-layer, the anchor boxes are defined by using the yolov2OutputLayer function.

This property is read-only.

Set of image sizes used for training, specified as an M-by-2 matrix, where each row is of the form [height width]. This property is set by the trainingSizes input argument.

If trainingSizes is not specified at the input, then this property is set by the trainingSizes argument in the trainYOLOv2ObjectDetector function. In this case, the yolov2ObjectDetector object is created by calling the trainYOLOv2ObjectDetector function.

Object Functions

detectDetect objects using YOLO v2 object detector

Examples

collapse all

To run this example, you must download and install the Computer Vision Toolbox Model™ for YOLO v2 Object Detection support package.

Specify the name of a pretrained YOLO v2 deep learning network.

name = "tiny-yolov2-coco";

Create YOLO v2 object detector by using the pretrained YOLO v2 network.

detector = yolov2ObjectDetector(name);

Display and inspect the properties of the YOLO v2 object detector.

disp(detector)
  yolov2ObjectDetector with properties:

            ModelName: 'tiny-yolov2-coco'
              Network: [1×1 DAGNetwork]
    TrainingImageSize: [416 416]
          AnchorBoxes: [5×2 double]
           ClassNames: [person    bicycle    car    motorbike    aeroplane    bus    train    truck    boat    traffic light    …    ]

Detect objects in an unknown image by using the pretrained YOLO v2 object detector.

img = imread("highway.png");
[bboxes,scores,labels] = detect(detector,img);

Display the detection results.

detectedImg = insertObjectAnnotation(img,"Rectangle",bboxes,labels);
figure
imshow(detectedImg)

Extended Capabilities

Version History

Introduced in R2019a