Main Content

maskrcnn

Detect objects using Mask R-CNN instance segmentation

Description

The maskrcnn object performs instance segmentation of objects in an image using a Mask R-CNN (regions with convolution neural networks) object detector. To detect objects in an image, pass the trained detector to the segmentObjects function.

Note

This function requires the Computer Vision Toolbox™ Model for Mask R-CNN Instance Segmentation. You can install the Computer Vision Toolbox Model for Mask R-CNN Instance Segmentation 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™.

Creation

Description

example

detector = maskrcnn("resnet50-coco") loads a pretrained Mask R-CNN object detector trained on the COCO data set with a ResNet-50 network as the feature extractor.

detector = maskrcnn("resnet50-coco",classNames) creates a pretrained Mask R-CNN object detector and configures it to perform transfer learning using a specified set of object classes. The classNames argument sets the ClassNames property. For optimal results, train the detector on new training images before performing detection.

detector = maskrcnn("resnet50-coco",classNames,anchorBoxes) creates a pretrained Mask R-CNN object detector and configures it to perform transfer learning using a specified set of object classes and anchor boxes. The classNames argument sets the ClassNames property. The anchorBoxes argument sets the AnchorBoxes property.

detector = maskrcnn(___,Name=Value) uses name-value arguments to specify ROI pooling sizes or to set the ModelName or InputSize properties. Specify name-value arguments in addition to the input arguments from any of the previous syntaxes.

For example, maskrcnn("resnet50-coco",classNames,anchorBoxes,PoolSize=[11 11]) specifies the ROI pooling size for the detection head as 11-by-11 pixels.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional pairs of arguments as Name1=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.

Example: maskrcnn("resnet50-coco",classNames,anchorBoxes,PoolSize=[11 11]) specifies the ROI pooling size for the detection head as 11-by-11 pixels.

ROI pooling size for the detection head, specified as a 1-by-2 vector in the format [height width].

ROI pooling size for the mask segmentation head, specified as a 1-by-2 vector in the format [height width].

Properties

expand all

Name of the trained Mask R-CNN object detection network, specified as a string scalar or character vector.

Size of anchor boxes, specified as an M-by-2 matrix, where each row is in the format [height width]. The default value consists of 15 anchor boxes defined by the MS-COCO data set. When you specify the anchor boxes, the maskrcnn object reinitializes the final convolution layers in the region proposal subnetwork to the correct size based on the number of anchor boxes.

You cannot modify the value of this property after you create the object.

Names of the object classes that the Mask R-CNN detector is trained to detect, specified as a cell array. The default value consists of the 80 object class names in the MS-COCO data set, such as "person", "bicycle", and "car". When you specify the class names, the maskrcnn object reinitializes the final convolution layers in the detection head and mask segmentation head to the correct size based on the number of classes.

You cannot modify the value of this property after you create the object.

Image size to use for detection, specified as a 1-by-3 vector of positive integers in the format [height width 3]. The detector resizes input images to this size while maintaining the aspect ratio. The default value is the network input size.

You cannot modify the value of this property after you create the object.

Object Functions

forwardRun forward pass on Mask R-CNN network
segmentObjectsSegment objects using Mask R-CNN instance segmentation

Examples

collapse all

Load a pretrained Mask R-CNN object detector.

detector = maskrcnn("resnet50-coco")
detector = 
  maskrcnn with properties:

      ModelName: 'maskrcnn'
     ClassNames: {1×80 cell}
      InputSize: [800 1200 3]
    AnchorBoxes: [15×2 double]

Read a test image that includes objects that the network can detect, such as people.

I = imread("visionteam.jpg");

Segment instances of objects using the Mask R-CNN object detector.

[masks,labels,scores,boxes] = segmentObjects(detector,I,Threshold=0.95);

Overlay the detected object masks in blue on the test image. Display the bounding boxes in red and the object labels.

overlayedImage = insertObjectMask(I,masks);
imshow(overlayedImage)
showShape("rectangle",boxes,Label=labels,LineColor=[1 0 0])

Introduced in R2021b