Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

How Mask R-CNN detects, classifies and masks individual objects

1. Frame

You now understand what instance identity means at the data level. This page covers how Mask R-CNN actually produces it, building directly on the Faster R-CNN architecture from L06. If any of the L06 detection-architecture vocabulary feels distant, it is worth a quick review before continuing.


2. Motivation

geoai’s instance segmentation functions expose very few parameters directly, num_classes, num_channels, instance_labels, and a handful of training settings, which makes the architecture itself feel like a black box unless you know roughly what happens inside. Understanding the four main components here is what lets you make sense of training behavior, loss curves, and failure modes on the following pages, without needing to implement any of it yourself.


3. Core idea

Mask R-CNN extends Faster R-CNN by adding one more parallel branch: alongside the existing class label and bounding box outputs, a new branch predicts a pixel-level mask for each detected object. Everything Faster R-CNN already does, proposing regions and classifying them, stays the same; instance segmentation is what you get by asking the same detector to also answer “and what is this object’s exact shape.”


4. Core architecture and components

Mask R-CNN extends object detection into instance segmentation by adding a dedicated mask branch alongside traditional bounding box detection. To understand how an input satellite image becomes a set of individually segmented features, follow the end-to-end processing pipeline below:

input image (e.g., 4-band RGB + NIR)
       ↓
backbone + Feature Pyramid Network (FPN)
       ↓
multiscale feature maps
       ↓
Region Proposal Network (RPN)
       ↓
candidate object regions (RoIs)
       ↓
RoI Align (bilinear interpolation)
       ↓
 ┌──────────────┬──────────────┐
 ↓              ↓
detection head  mask head
 ↓              ↓
class + box     28×28 binary mask
 └───────┬──────┘
         ↓
one complete prediction per instance

A. Backbone and Feature Pyramids

The Backbone (typically ResNet-50 or ResNet-101) converts raw image pixels into rich, learned feature maps. Rather than analyzing pixel values directly, deeper convolutional layers extract increasingly abstract visual patterns such as edges, textures, and object structures.


B. Region Proposal Network (RPN)

Inherited directly from Faster R-CNN, the Region Proposal Network (RPN) scans the multiscale feature maps to answer a single question: Where might an object be located?

  1. Candidate Generation: The RPN slides across feature maps and evaluates predefined anchor boxes of different sizes and aspect ratios.

  2. Scoring and Refinement: For each anchor, it estimates an “objectness” score (the likelihood that a feature is present) and proposes an initial, coarse bounding box.

  3. Filtering: Proposals with low objectness scores are dropped. Overlapping candidates are filtered using non-maximum suppression (NMS) so that only top-scoring, distinct regions move forward.


C. RoI Align: spatial precision for masks

Before candidate regions pass to the prediction heads, each proposed region of interest (RoI) must be extracted from the feature map and converted into a standard, fixed-size matrix.

candidate bounding box
         ↓
sample precisely via bilinear interpolation (RoI Align)
         ↓
fixed-size feature matrix

D. Detection head

Extracted RoI features pass into the detection head, which performs two standard object-detection tasks in parallel:

This branch operates identically to Faster R-CNN and determines what the object is and where its overall envelope lies.


E. Mask head

The mask head is the defining addition of Mask R-CNN. While the detection head processes bounding boxes, the mask head branch uses a small Fully Convolutional Network (FCN) to predict an exact pixel geometry for each detected object.

Instance Output
├── Class Label (e.g., field = 1)
├── Confidence Score (e.g., 0.94)
├── Bounding Box ([xmin, ymin, xmax, ymax])
└── Pixel Mask (binary footprint)

F. Detection quality affects mask quality

Mask R-CNN relies on a top-down sequence:

detect candidate object (box)
         ↓
segment target object (mask)

Because segmentation happens inside proposed bounding boxes, errors in the detection stage propagate directly into the final segmentation output:


G. Multi-task loss functions

Mask R-CNN trains all components simultaneously using a combined multi-task loss function (LtotalL_{\text{total}}):

Ltotal=Lrpn_cls+Lrpn_box+Lcls+Lbox+LmaskL_{\text{total}} = L_{\text{rpn\_cls}} + L_{\text{rpn\_box}} + L_{\text{cls}} + L_{\text{box}} + L_{\text{mask}}

During model training, monitoring individual loss terms helps pinpoint specific weaknesses:


H. Model alternatives and trade-offs

While Mask R-CNN is a standard baseline for two-stage instance segmentation, other architecture families address different operational priorities:

Other model families
  • Two-Stage Models (e.g., Mask R-CNN): Isolate detection before mask prediction. They offer strong boundary accuracy and robust performance on small features, but require higher computational time.

  • Single-Stage Models (e.g., YOLO-Seg): Predict bounding boxes and mask coefficients simultaneously in a single pass. They offer significantly faster inference speeds, making them ideal for real-time applications or massive scene processing, though occasionally at the expense of fine boundary detail.

  • Transformer-Based Models (e.g., Mask2Former): Replace region proposals with learned object queries and attention mechanisms. They treat semantic, instance, and panoptic segmentation under a single framework, excelling at complex scenes with overlapping objects.

Mask R-CNN remains an ideal learning architecture because its modular structure clearly illustrates how detection and segmentation components interact.


5. Putting the pipeline together

When Mask R-CNN processes an image, the backbone extracts features, the RPN proposes candidate regions, RoI Align samples each region precisely, the detection head classifies and refines each box, and the mask head predicts a shape for each detected object in parallel. After non-maximum suppression removes duplicates, the final output for each detected instance is a class label, a confidence score, a bounding box, and a pixel-level mask, everything needed to convert predictions into georeferenced vector features on a later page.


6. Common pitfalls


7. Mini task

A colleague says, “Mask R-CNN is just Faster R-CNN with an extra step at the end that draws the mask.” Identify what this description gets right and what it oversimplifies.

Sample solution

It is right that Mask R-CNN keeps Faster R-CNN’s backbone, RPN, and detection head essentially unchanged, and that the mask head is the genuinely new addition. It oversimplifies by implying the mask is just a final drawing step tacked onto an existing box: the mask head runs as its own parallel branch, and RoI Align, itself a precision upgrade over the original architecture, is what makes the resulting masks usable at pixel-level accuracy in the first place, not merely a supporting detail.


8. Key takeaways

Further reading