Sub topic 15
Unified Mask Classification with Transformers
Assume you know convolutional neural networks, object detection, and the difference between semantic, instance, and panoptic segmentation. This chapter explains how transformer-based models turn these tasks into one mask-prediction problem.
The unified idea
Traditional semantic segmentation predicts a class independently for every pixel. Instance segmentation often detects objects first and then predicts a mask for each detected box. Unified mask classification takes another approach:
- Predict a set of masks.
- Assign one class label to each mask.
- Combine the masks according to the task.
A mask can represent a thing, such as one car, or a stuff region, such as road. The model does not need a separate output head for semantic, instance, and panoptic segmentation.
| Task | Mask meaning | Output handling |
|---|---|---|
| Semantic | Regions belonging to each class | Combine masks with the same class |
| Instance | One individual object | Keep separate masks for each object |
| Panoptic | Objects and background regions | Keep thing instances and stuff regions together |
MaskFormer’s working model
MaskFormer uses a convolutional backbone and a pixel decoder to produce dense image features at useful resolutions. A transformer decoder then processes a fixed set of learned queries. Each query represents a possible segment.
For every query, the model predicts:
- A class distribution, including a “no object” class.
- A mask embedding.
- A mask by comparing the embedding with the pixel features.
The number of queries is usually larger than the expected number of segments. Unused queries predict “no object.”
Training must match predicted masks to the correct target masks because query 1 does not have a fixed meaning. MaskFormer uses bipartite matching, commonly implemented with the Hungarian algorithm. The matching cost can include classification error, mask binary cross-entropy, and Dice loss. The matched predictions receive target labels. Unmatched queries receive the “no object” label.
Mask2Former’s improvement
Mask2Former extends this design with multi-scale features and masked attention. Instead of allowing every query to attend equally to every image location, masked attention focuses a query on the region suggested by its current mask. This reduces irrelevant attention and helps the model refine small or crowded regions.
A practical benefit is one architecture for all three segmentation tasks. The differences mainly appear during post-processing:
- For semantic segmentation, combine masks by class.
- For instance segmentation, retain separate masks for thing classes.
- For panoptic segmentation, keep thing instances and assign stuff masks without overlaps.
Trade-offs and checks
Transformer mask classification avoids dependence on bounding-box proposals, but it still has costs. It can require substantial memory, especially with high-resolution features and many queries. Small objects may be missed when the pixel decoder loses fine detail. Too few queries limit the number of segments the model can represent.
During evaluation, inspect both mask quality and query behavior. Check whether many queries predict “no object,” whether nearby objects merge, and whether stuff regions leave gaps. For panoptic output, enforce non-overlap and define how low-confidence or unmatched pixels become void.
Exercise
Why can the same Mask2Former model support semantic, instance, and panoptic segmentation?
Show answer
It predicts a set of class-labeled masks. The task determines how those masks are grouped, retained, and combined during post-processing.