Main Content

yolov4ObjectDetector

Detect objects using YOLO v4 object detector

Since R2022a

Description

The yolov4ObjectDetector object creates a you only look once version 4 (YOLO v4) one-stage object detector for detecting objects in an image. Using this object, you can:

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

  • Create a custom YOLO v4 object detector by using any pretrained or untrained YOLO v4 deep learning network.

Creation

Description

Pretrained YOLO v4 Object Detector

example

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

Custom YOLO v4 Object Detector

detector = yolov4ObjectDetector(name,classes,aboxes) creates a pretrained YOLO v4 object detector and configures it to perform transfer learning using a specified set of object classes and anchor boxes. For optimal results, you must train the detector on new training images before performing detection. Use the trainYOLOv4ObjectDetector function for training the detector.

detector = yolov4ObjectDetector(net,classes,aboxes) creates an object detector by using the deep learning network net.

If net is a pretrained YOLO v4 deep learning network, the function creates a pretrained YOLO v4 object detector. The classes and aboxes are values used for training the network.

If net is an untrained YOLO v4 deep learning network, the function creates a YOLO v4 object detector to use for training and inference. classes and aboxes specify the object classes and the anchor boxes, respectively, for training the YOLO v4 network.

Use the trainYOLOv4ObjectDetector function to train the network before performing object detection.

example

detector = yolov4ObjectDetector(baseNet,classes,aboxes,'DetectionNetworkSource',layer) creates a YOLO v4 object detector by adding detection heads to a base network, baseNet.

The function adds detection heads to the specified feature extraction layers layer in the base network. To specify the names of the feature extraction layers, use the name-value argument 'DetectionNetworkSource',layer.

If baseNet is a pretrained deep learning network, the function creates a YOLO v4 object detector and configures it to perform transfer learning with the specified object classes and anchor boxes.

If baseNet is an untrained deep learning network, the function creates a YOLO v4 object detector and configures it for object detection. classes and aboxes specify the object classes and the anchor boxes, respectively, for training the YOLO v4 network.

You must train the detector on a training dataset before performing object detection. Use the trainYOLOv4ObjectDetector function for training the detector.

detector = yolov4ObjectDetector(___,Name=Value) sets the InputSize and ModelName properties of the object detector by using name, value pair arguments. Name is the property name and Value is the corresponding value. You must enclose each property name in quotes.

Note

To use the pretrained YOLO v4 object detection networks trained on COCO dataset, you must install the Computer Vision Toolbox™ Model for YOLO v4 Object Detection. You can download and install the Computer Vision Toolbox Model for YOLO v4 Object Detection from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons. To run this function, you will require the Deep Learning Toolbox™.

Input Arguments

expand all

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

  • "csp-darknet53-coco" — A pretrained YOLO v4 deep learning network created using CSP-DarkNet-53 as the base network and trained on COCO dataset.

  • "tiny-yolov4-coco" — A pretrained YOLO v4 deep learning network created using a small base network and trained on COCO dataset.

Data Types: char | string

Names of object classes for training the detector, specified as a string vector, cell array of character vectors, or categorical vector. This argument sets the ClassNames property of the yolov4ObjectDetector object.

Data Types: char | string | categorical

Anchor boxes for training the detector, specified as an N-by-1 column vector cell array or a 1-by-N row vector cell array. N is the number of output layers in the YOLO v4 deep learning network. Each cell contains an M-by-2 matrix, where M is the number of anchor boxes in that layer. Each row in the M-by-2 matrix denotes the size of an anchor box in the form [height width].

The first element in the cell array specifies the anchor boxes to associate with the first output layer, the second element in the cell array specifies the anchor boxes to associate with the second output layer, and so on. For accurate detection results, specify large anchor boxes for the first output layer and small anchor boxes for the last output layer. That is, the anchor box sizes must decrease for each output layer in the order in which the layers appear in the YOLO v4 deep learning network.

This argument sets the AnchorBoxes property of the yolov4ObjectDetector object.

Data Types: cell

YOLO v4 deep learning network, specified as a dlnetwork (Deep Learning Toolbox) object. The input network can be either an untrained or a pretrained deep learning network. The input network must have an image input layer and the Normalization property value of the image input layer must be set to "None".

Base network for creating the YOLO v4 deep learning network, specified as a dlnetwork (Deep Learning Toolbox) object. The network can be either an untrained or a pretrained deep learning network. The input network must have an image input layer and the Normalization property value of the image input layer must be set to "None".

Names of the feature extraction layers in the base network, specified as a cell array of character vectors, or a string array. The function creates a YOLO v4 network by adding detection head layers to the output of the feature extraction layers in the base network.

Example: layer = {'conv10','fire9-concat','fire8-concat'}

Example: layer = ["conv10","fire9-concat","fire8-concat"]

Data Types: char | string | cell

Properties

expand all

YOLO v4 deep learning network to use for object detection, stored as a dlnetwork (Deep Learning Toolbox) object.

Names of object classes to detect, stored as a categorical vector. You can set this property by using the input argument classes.

This property is read-only.

Set of anchor boxes, stored as a N-by-1 cell array. Each element in the cell is a M-by-2 matrix. Each row in the M-by-2 matrix denotes the size of the anchor box in the form of [height width]. M denotes the number of anchor boxes. N is the number of output layers in the YOLO v4 deep learning network for which the anchor boxes are defined. The first element in the cell array specifies the anchor boxes for the first output layer, the second element in the cell array specifies the anchor boxes for the second output layer, and so on. You must specify same number of anchor boxes for each of the output layer.

You can set this property by using the input argument aboxes.

This property is read-only.

Image size used for training, stored as a vector of form [height width] or [height width channels]. To set this property, specify it at object creation. The size of the training images must be a multiple of 32.

For example, detector = yolov4ObjectDetector(net,classes,aboxes,InputSize=[220 220]).

Name for the object detector, stored as a character vector or string scalar. To set this property, specify it at object creation.

For example, yolov4ObjectDetector(net,classes,aboxes,ModelName="customDetector") sets the name for the object detector to "customDetector".

This property is read-only.

Bounding box format for an object detector, stored as "axis-aligned" or "rotated". When the PredictedBoxType is "axis-aligned", the object detector will train and perform inference on only axis-aligned bounding boxes. If it is set to "rotated", the object detector will train and perform inference on only rotated bounding boxes. Set this property when you create the object.

Object Functions

detectDetect objects using YOLO v4 object detector

Examples

collapse all

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

name = "tiny-yolov4-coco";

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

detector = yolov4ObjectDetector(name);

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

disp(detector)  
  yolov4ObjectDetector with properties:

        Network: [1×1 dlnetwork]
    AnchorBoxes: {2×1 cell}
     ClassNames: {80×1 cell}
      InputSize: [416 416 3]
      ModelName: 'tiny-yolov4-coco'

Use analyzeNetwork to display the YOLO v4 network architecture and get information about the network layers.

analyzeNetwork(detector.Network)

Detect objects in an unknown image by using the pretrained YOLO v4 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)

This example shows how to create a YOLO v4 object detection network by using a pretrained ResNet -50 convolutional neural network as the base network.

Load a pretrained deep learning network to use as the base network. This example uses ResNet-50 pretrained network as the base network. For information about other available pretrained networks, see Pretrained Deep Neural Networks (Deep Learning Toolbox).

basenet = resnet50;

Use analyzeNetwork to display the architecture of the base network.

analyzeNetwork(basenet)

The first layer in the base network is the image input layer. Inspect the property of the image input layer in the base network.

basenet.Layers(1)
ans = 
  ImageInputLayer with properties:

                      Name: 'input_1'
                 InputSize: [224 224 3]

   Hyperparameters
          DataAugmentation: 'none'
             Normalization: 'zerocenter'
    NormalizationDimension: 'auto'
                      Mean: [224×224×3 single]

To create a YOLO v4 deep learning network you must make these changes to the base network:

  1. Set the Normalization property of the ImageInputLayer in the base network to 'none'.

  2. Remove the fully connected classification layer.

Define an image input layer with Normalization property value as 'none' and other property values same as that of the base network.

imageSize = basenet.Layers(1).InputSize;
layerName = basenet.Layers(1).Name;
newinputLayer = imageInputLayer(imageSize,'Normalization','none','Name',layerName);

Extract the layer graph of the base network to use for creating YOLO v4 deep learning network..

lgraph = layerGraph(basenet);

Remove the fully connected layer in the base network.

lgraph = removeLayers(lgraph,'ClassificationLayer_fc1000');

Replace the image input layer in the base network with the new input layer.

lgraph = replaceLayer(lgraph,layerName,newinputLayer);

Create a dlnetwork object from the layer

dlnet = dlnetwork(lgraph);

Specify the names of the feature extraction layers in the base network to use as the detection heads.

featureExtractionLayers = ["activation_22_relu","activation_40_relu"];

Specify the class names and anchor boxes to use for training the YOLO v4 deep learning network created using resnet50 as the base network.

classes = {'car','person'};
anchorBoxes = {[122,177;223,84;80,94];...
               [111,38;33,47;37,18]};

Create a YOLO v4 object detector by using the specified base network and the detection heads.

detector = yolov4ObjectDetector(dlnet,classes,anchorBoxes,DetectionNetworkSource=featureExtractionLayers);

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

disp(detector) 
  yolov4ObjectDetector with properties:

        Network: [1×1 dlnetwork]
    AnchorBoxes: {2×1 cell}
     ClassNames: {2×1 cell}
      InputSize: [224 224 3]
      ModelName: ''

Use analyzeNetwork to display the YOLO v4 network architecture and get information about the network layers.

analyzeNetwork(detector.Network)

This example shows how to detect people using a pretrained YOLO v4 object detector.

Load the pretrained YOLO v4 detector that can detect 80 common object classes.

detector = yolov4ObjectDetector();

Read image to process.

I = imread("visionteam.jpg");

Run object detector.

[bboxes, scores, labels] = detect(detector, I, Threshold=0.4);

Select detections for the person class.

personBoxes = bboxes(labels=="person", :);

Display results.

detectedImg = insertObjectAnnotation(I, "Rectangle", personBoxes, "person");
figure
imshow(detectedImg)

Figure contains an axes object. The axes object contains an object of type image.

Extended Capabilities

Version History

Introduced in R2022a