Main Content

ssdObjectDetector

Detect objects using SSD deep learning detector

Since R2020a

Description

The ssdObjectDetector detects objects from an image, using a single shot detector (SSD) object detector. To detect objects in an image, pass the trained detector to the detect function. You can also use the trained detector for multiclass object detection. For information about SSD deep learning network, see Getting Started with SSD Multibox Detection.

Creation

Description

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

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

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

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

example

detector = ssdObjectDetector(baseNet,classes,aboxes,'DetectionNetworkSource',layer) creates a SSD 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 SSD 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 SSD object detector and configures it for object detection. classes and aboxes specify the object classes and the anchor boxes, respectively, for training the SSD network.

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

detector = ssdObjectDetector(___,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.

Input Arguments

expand all

SSD deep learning network, specified as a LayerGraph (Deep Learning Toolbox) object. The input network can be either an untrained or a pretrained deep learning network. The input network must not have loss layers.

Base network for creating the SSD deep learning network, specified as a LayerGraph (Deep Learning Toolbox) object. The network can be either an untrained or a pretrained deep learning network. When you specify this argument, you must also specify the value for DetectionNetworkSource name-value argument.

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 ssdObjectDetector object.

Data Types: char | string | categorical

Anchor boxes for training the detector, specified as an N-by-1 cell array. N is the number of output layers in the SSD 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 SSD deep learning network.

This argument sets the AnchorBoxes property of the ssdObjectDetector object.

Data Types: cell

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 SSD network by adding classification and regression layers to the output of the feature extraction layers in the base network.

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

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

Data Types: char | string | cell

Properties

expand all

This property is read-only.

Name of the classification model, specified as a character vector or string scalar. By default, the name is set to the heading of the second column of the trainingData table specified in the trainSSDObjectDetector function. You can modify this name after creating your ssdObjectDetector object.

This property is read-only.

Trained SSD multibox object detection network, specified as a DAGNetwork (Deep Learning Toolbox) object. This object stores the layers that define the convolutional neural network used within the SSD detector.

This property is read-only.

Size of anchor boxes, specified as a P-by-1 cell array for P number of feature extraction layers used for object detection in the SSD network. Each element of the array contains an M-by-2 matrix of anchor box sizes, in the format [height width]. Each cell can contain a different number of anchor boxes. This value is set during training.

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

This property is read-only.

Names of the object classes that the SSD detector was trained to find, specified as a cell array. You can set this property by using the input argument classes.

Object Functions

detectDetect objects using SSD multibox object detector

Examples

collapse all

This example shows how to create a SSD 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) (Deep Learning Toolbox).

basenet = resnet50;

Use analyzeNetwork to display the architecture of the base network.

analyzeNetwork(basenet)

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

classNames = ["person" "car" "dog"];
anchorBoxes = {[30 60;60 30;50 50;100 100], ...
               [40 70;70 40;60 60;120 120], ...
               [50 80;80 60;70 70;140 140]};

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

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

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

basenet = layerGraph(basenet);
detector = ssdObjectDetector(basenet,classNames,anchorBoxes,DetectionNetworkSource=featureExtractionLayers);

Display and inspect the properties of the SSD object detector.

disp(detector)
  ssdObjectDetector with properties:

        Network: [1x1 DAGNetwork]
    AnchorBoxes: {3x1 cell}
     ClassNames: [3x1 string]
      InputSize: [224 224 3]
      ModelName: ''

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

analyzeNetwork(detector.Network)

Load a pretrained single shot detector (SSD) object to detect vehicles in an image. The detector is trained with images of cars on a highway scene.

vehicleDetector = load('ssdVehicleDetector.mat','detector');
detector = vehicleDetector.detector;

Read a test image into the workspace.

I = imread('highway.png');

Display the test image.

imshow(I);

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

Run the pretrained SSD object detector by using the detect function. The output contains the bounding boxes, scores, and the labels for vehicles detected in the image. The labels are derived from the ClassNames property of the detector.

[bboxes,scores,labels] = detect(detector,I)
bboxes = 2×4

   139    78    96    81
    99    67   165   146

scores = 2x1 single column vector

    0.8349
    0.6302

labels = 2x1 categorical
     vehicle 
     vehicle 

Annotate the image with the detection results.

if ~isempty(bboxes)
    detectedI = insertObjectAnnotation(I,'rectangle',bboxes,cellstr(labels));
else
   detectedI = insertText(I,[10 10],'No Detections');
end
   
figure
imshow(detectedI)

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

Extended Capabilities

Version History

Introduced in R2020a