Main Content

yolov3ObjectDetector

Detect objects using YOLO v3 object detector

Since R2021a

Description

Theyolov3ObjectDetectorobject creates a you only look once version 3 (YOLO v3) object detector for detecting objects in an image. Using this object, you can:

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

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

Creation

Description

Pretrained YOLO v3 Object Detector

example

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

Note

To use the pretrained YOLO v3 deep learning networks trained on COCO dataset, you must install theComputer Vision Toolbox™ Model for YOLO v3 Object Detectionfrom Add-On Explorer. For more information about installing add-ons, seeGet and Manage Add-Ons. To run this function, you will require the Deep Learning Toolbox™.

Custom YOLO v3 Object Detector

detector= yolov3ObjectDetector(name,classes,aboxes)creates a pretrained YOLO v3 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.

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

Ifnetis a pretrained YOLO v3 deep learning network, the function creates a pretrained YOLO v3 object detector. Theclassesandaboxesare values used for training the network.

Ifnetis an untrained YOLO v3 deep learning network, the function creates a YOLO v3 object detector to use for training and inference.classesandaboxesspecify the object classes and the anchor boxes, respectively, for training the YOLO v3 network.

You must train the detector on a training dataset before performing object detection. For information about how to train a YOLO v3 object detector, seePreprocess Training DataandTrain Modelsections in the对象检测使用YOLO v3意思深入学习example.

example

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

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

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

IfbaseNetis an untrained deep learning network, the function creates a YOLO v3 object detector and configures it for object detection.classesandaboxesspecify the object classes and the anchor boxes, respectively, for training the YOLO v3 network.

You must train the detector on a training dataset before performing object detection.

detector= yolov3ObjectDetector(___,Name,Value)sets theInputSizeandModelNameproperties of the object detector by using name-value pair arguments.Nameis the property name andValue相应的价值。你必须附上每个property name in quotes.

Input Arguments

expand all

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

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

  • 'tiny-yolov3-coco'— A pretrained YOLO v3 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 theClassNamesproperty of theyolov3ObjectDetectorobject.

Data Types:char|string|categorical

Anchor boxes for training the detector, specified as anN-by-1 cell array.Nis the number of output layers in the YOLO v3 deep learning network. Each cell contains anM-by-2 matrix, whereMis the number of anchor boxes in that layer. Each row in theM-by-2 matrix denotes the size of an anchor box in the form [heightwidth].

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 v3 deep learning network.

This argument sets theAnchorBoxesproperty of theyolov3ObjectDetectorobject.

Data Types:cell

YOLO v3 deep learning network, specified as adlnetwork(Deep Learning Toolbox)object. The input network can be either an untrained or a pretrained deep learning network.

Base network for creating the YOLO v3 deep learning network, specified as adlnetwork(Deep Learning Toolbox)object, orDAGNetwork(Deep Learning Toolbox)object. The network can be either an untrained or a pretrained deep learning network.

Names of the feature extraction layers in the base network, specified as a cell array of character vectors, or a string array.

函数创建一个YOLO v3意思网络通过添加detection head layers to the output of the feature extraction layers in the base network. The feature extraction layers must be specified in the reverse of the order in which they appear in the network architecture. For example, given a base network with four feature extraction layers, you must add the first detection head to the fourth feature extraction layer, the second detection head to the third feature extraction layer, and so on.

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

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

Data Types:char|string|cell

Properties

expand all

This property is read-only.

YOLO v3 deep learning network to use for object detection, stored as adlnetwork(Deep Learning Toolbox)object.

This property is read-only.

Set of anchor boxes, stored as aN-by-1 cell array. Each element in the cell is aM-by-2 matrix. Each row in theM-by-2 matrix denotes the size of the anchor box in the form of [heightwidth].Mdenotes the number of anchor boxes.Nis the number of output layers in the YOLO v3 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 can set this property by using the input argumentaboxes.

This property is read-only.

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

This property is read-only.

Set of image sizes used for training, stored as anM-by-2 matrix of typedouble. Each row is of the form [heightwidth]. To set this property, specify it at object creation.

For example,detector = yolov3ObjectDetector(net,classes,aboxes,'InputSize',[220 220; 440 440]).

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

For example,yolov3ObjectDetector(net,classes,aboxes,'ModelName','customDetector')sets the name for the object detector to'customDetector'.

Object Functions

detect Detect objects using YOLO v3 object detector
preprocess Preprocess training and test images
forward Compute YOLO v3 deep learning network output for training
predict Compute YOLO v3 deep learning network outputs for inference

Examples

collapse all

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

name ='tiny-yolov3-coco';

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

detector = yolov3ObjectDetector(name);

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

disp(detector)
yolov3ObjectDetector with properties: Network: [1x1 dlnetwork] AnchorBoxes: {2x1 cell} ClassNames: [80x1 categorical] InputSize: [416 416 3] Learnables: [48x3 table] State: [22x3 table] ModelName: 'tiny-yolov3-coco'

UseanalyzeNetworkto display the YOLO v3 network architecture and get information about the network layers. The network has two detection heads attached to the feature extraction network.

analyzeNetwork(detector.Network)

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

img = imread('sherlock.jpg'); img = preprocess(detector,img); img = im2single(img); [bboxes,scores,labels] = detect(detector,img,'DetectionPreprocessing','none');

Display the detection results.

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

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

This example shows how to create a custom YOLO v3 object detector by using a pretrained SqueezeNet as the base network.

Load a pretrained SqueezeNet network. The SqueezeNet network is a convolutional neural network that you can use as the base network for creating a YOLO v3 object detector.

net = squeezenet
net = DAGNetwork with properties: Layers: [68x1 nnet.cnn.layer.Layer] Connections: [75x2 table] InputNames: {'data'} OutputNames: {'ClassificationLayer_predictions'}

Inspect the architecture of the base network by usinganalyzeNetwork(Deep Learning Toolbox)function.

analyzeNetwork(net)

Specify the anchor boxes and the classes to use to train the YOLO v3 network.

aboxes = {[150,127;97,90;68,67];[38,42;41,29;31,23]}; classes = {'Car','Truck'};

Select two feature extraction layers in the base network to serve as the source for detection subnetwork.

layer = {'fire9-concat','fire8-concat'};

Create a custom YOLO v3 object detector by adding detection heads to the feature extraction layers of the base network. Specify the model name, classes, and the anchor boxes.

detector = yolov3ObjectDetector(net,classes,aboxes,'ModelName','Custom YOLO v3','DetectionNetworkSource',layer);

Inspect the architecture of the YOLO v3 deep learning network by usinganalyzeNetwork(Deep Learning Toolbox)function.

analyzeNetwork(detector.Network)

Inspect the properties of the YOLO v3 object detector. You can now train the YOLO v3 object detector on a custom training dataset and perform object detection.

disp(detector)
yolov3ObjectDetector with properties: Network: [1x1 dlnetwork] AnchorBoxes: {2x1 cell} ClassNames: [2x1 categorical] InputSize: [227 227 3] Learnables: [66x3 table] State: [6x3 table] ModelName: 'Custom YOLO v3'

For information about how to train a YOLO v3 object detector, see the对象检测使用YOLO v3意思深入学习example.

Extended Capabilities

中on History

Introduced in R2021a