MaskRCNNForObjectDetection
PretrainedModelMaskRCNNForObjectDetection(config: MaskRCNNConfig)Mask R-CNN with a ResNet-50-FPN backbone (He et al., ICCV 2017).
The two-stage instance-segmentation detector in its modern reference
configuration: Faster R-CNN's ResNet-50-FPN backbone, RPN, and Fast
R-CNN box head, plus a parallel FCN mask branch on the RoI heads. The
submodule layout mirrors the reference detector so the COCO box AP 37.9 / mask AP 34.6 checkpoint loads strict (307 keys) and reproduces
inference.
Parameters
configMaskRCNNConfigmask_rcnn_resnet50_fpn factory for the COCO-pretrained
configuration (num_classes = 91).Attributes
configMaskRCNNConfigbackbone_BackboneWithFPNbody + fpn producing five feature maps
[P2, P3, P4, P5, pool] at strides 4/8/16/32/64 (reused
from Faster R-CNN).rpn_RegionProposalNetworkroi_heads_MaskRoIHeadsbox_head + box_predictor (reused) plus mask_head
(MaskRCNNHeads) + mask_predictor (MaskRCNNPredictor).Notes
See He et al., "Mask R-CNN", ICCV 2017 (arXiv:1703.06870), Ren et al., "Faster R-CNN", NeurIPS 2015, and Lin et al., "Feature Pyramid Networks for Object Detection", CVPR 2017. The model expects an already resized
- normalised image batch; final per-instance detections + masks come
from
postprocess.
Examples
>>> import lucid
>>> from lucid.models.vision.mask_rcnn import mask_rcnn_resnet50_fpn
>>> model = mask_rcnn_resnet50_fpn()
>>> model.eval()
>>> x = lucid.randn(1, 3, 224, 224)
>>> out = model(x)
>>> out.logits.shape[-1] # num_classes
91
>>> out.pred_masks.shape[-2:]
(28, 28)Used by 2
Constructors
1Instance methods
2forward(x: Tensor, proposals: list[Tensor] | None = None)Run Mask R-CNN on a (pre-processed) image batch.
Parameters
Returns
InstanceSegmentationOutputInstanceSegmentationOutput with raw RoI-head outputs:
logits : (Σ proposals, num_classes) class logits.
pred_boxes : (Σ proposals, num_classes, 4) per-class boxes.
pred_masks : (Σ proposals, num_classes, 28, 28) mask logits.
postprocess
→list of dictpostprocess(output: InstanceSegmentationOutput, image_sizes: list[tuple[int, int]] | None = None, proposals: list[Tensor] | None = None, features: list[Tensor] | None = None)Box post-process → per-detection mask gather.
Mirrors the reference inference flow: run the box branch
post-processing (softmax → per-class score filter → per-class NMS →
top-max_detections), then sigmoid the per-RoI mask logits and
gather the channel for each detection's predicted class.
Parameters
forward.image_sizeslist of (H, W)= Noneforward used).Returns
list of dictOne dict per image with "boxes" (D, 4), "scores"
(D,), "labels" (D,) int64, and "masks"
(D, 1, 28, 28) sigmoid mask probabilities (the channel of
each detection's predicted class).