# Masked-attention Mask Transformer for Universal Image Segmentation

Mask2Former uses one architecture for panoptic, instance and semantic segmentation

Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar · arXiv · 2021-12-2

- **Problem** Panoptic, instance and semantic segmentation each have specialized architectures. Universal architectures that cover all three have trailed them in accuracy and training cost.
- **Method** Mask2Former is a Transformer decoder that replaces standard cross-attention with masked attention. Each query attends only inside its own predicted mask region, instead of every image location.
- **Results** With a Swin-L backbone, it reaches 57.8 PQ on COCO panoptic and 57.7 mIoU on ADE20K semantic segmentation, beating the best specialized models.

## Why universal segmentation has not replaced specialized models

Panoptic, instance and semantic segmentation differ in the semantics used to group pixels, yet each has been handled by its own specialized architecture. Semantic segmentation has mostly used per-pixel classification networks, while instance-level segmentation is dominated by mask classification networks, so the design and optimization effort is duplicated across tasks.

Universal architectures handle all three tasks with one design trained by an end-to-end set prediction objective, but each task and dataset still gets its own training run. The best reported universal result on instance segmentation is more than 9 AP below the specialized Swin-HTC++ model.

Training is also heavier: MaskFormer needs 300 epochs to reach 40.1 AP and can fit only one image on a 32G GPU, whereas Swin-HTC++ reaches higher accuracy in 72 epochs.

## Masked attention as a localized cross-attention

The authors hypothesize that local features are enough to update the query features and that global context can instead be gathered through self-attention.

In standard cross-attention with a residual path, the query features of layer l are updated as \\( X\_l=\\operatorname{softmax}(Q\_lK\_l^{T})V\_l+X\_{l-1} \\), where \\( Q\_l=f\_Q(X\_{l-1}) \\) and \\( K\_l, V\_l \\) are projections of the image features over \\( H\_lW\_l \\) locations. Every query therefore reads from every image location.

Masked attention adds a mask \\( M\_{l-1} \\) to the logits before the softmax, giving \\( X\_l=\\operatorname{softmax}(M\_{l-1}+Q\_lK\_l^{T})V\_l+X\_{l-1} \\). The mask entry at each location is 0 where the binarized prediction from the previous layer equals 1 and negative infinity elsewhere, so positions outside the predicted region receive no attention weight.

That binary mask is the previous layer's mask prediction thresholded at 0.5 and resized to the resolution of \\( K\_l \\); \\( M\_0 \\) is taken from the initial query features \\( X\_0 \\) before the decoder runs.

## Feeding high-resolution features one scale per layer

High-resolution features improve performance on small objects, but attending over them in every layer is costly. Instead of using the finest map throughout, the decoder draws on a feature pyramid and feeds one resolution to one decoder layer at a time.

The pixel decoder supplies feature maps at 1/32, 1/16 and 1/8 of the input resolution. Each scale receives a sinusoidal positional embedding and a learnable scale-level embedding, and the three scales are consumed from lowest to highest resolution in a cycle that is repeated L times, giving 3L decoder layers in total.

In the ResNet-50 ablation, a single 1/32 scale reaches 41.5 AP, 50.2 PQ and 46.1 mIoU at 218G FLOPs, while a single 1/8 scale reaches 44.0 AP, 51.8 PQ and 47.4 mIoU at 239G FLOPs. The round-robin schedule reaches 43.7 AP, 51.9 PQ and 47.2 mIoU at 226G FLOPs, while feeding all three scales to every layer costs 247G FLOPs for 44.0 AP, 51.9 PQ and 46.3 mIoU.

## Three decoder changes that add no computation

A standard decoder layer applies self-attention, then cross-attention, then a feed-forward network, starts from zero-initialized query features, and applies dropout to residual connections and attention maps.

The first change moves self-attention after masked attention, since queries entering the first self-attention carry no image signal and are unlikely to gain information from it. The second makes the query features learnable and supervises them before the decoder, so that their predicted masks act like region proposals. The third removes dropout, which the authors found to lower performance more often than not.

## Sampled-point mask losses

High-resolution mask prediction makes training memory heavy, so the mask loss is computed on K randomly sampled points rather than on the full mask, following PointRend and Implicit PointRend. In bipartite matching, one set of K points is drawn uniformly and shared by all predicted and ground-truth masks, while the final loss draws a different set of K points for each prediction and ground-truth pair by importance sampling.

K is set to 12544, that is 112 by 112 points. Per image, training memory falls from 18GB to 6GB, a 3x reduction.

Changing only the final loss to sampled points cuts memory from 18G to 6G, with AP at 41.0 and mIoU at 45.9 in both settings while PQ rises from 50.3 to 50.8. Sampling in the matching loss as well raises the full model to 43.7 AP, 51.9 PQ and 47.2 mIoU at 6G, so the matching change adds accuracy without changing memory.

## Architecture and training settings

The default pixel decoder is MSDeformAttn rather than FPN, with 6 MSDeformAttn layers over the 1/8, 1/16 and 1/32 feature maps and an upsampling step with a lateral connection that produces the 1/4 per-pixel embedding. The Transformer decoder uses L = 3, which gives 9 layers, and 100 queries by default, with auxiliary losses on each intermediate layer and on the learnable query features.

The mask loss combines binary cross-entropy and dice losses, each with weight 5.0, and the classification loss has weight 2.0 for predictions matched to a ground truth and 0.1 for unmatched predictions.

Panoptic and instance training uses AdamW with an initial learning rate of 0.0001, weight decay of 0.05 and a backbone learning rate multiplier of 0.1, with 50 epochs and batch size 16 by default. Large-scale jittering samples scales from 0.1 to 2.0 and crops to 1024x1024, and inference resizes the shorter side to 800 with the longer side capped at 1333.

Semantic segmentation models follow the MaskFormer settings, except that the 0.1 learning rate multiplier applies to both CNN and Transformer backbones.

FLOPs are averaged over 100 validation images, and fps is measured on a V100 GPU with batch size 1, including post-processing.

## Panoptic segmentation on COCO

Table 1 compares panoptic models on COCO val2017 with 133 categories. With a ResNet-50 backbone, Mask2Former reaches 51.9 PQ in 50 epochs, against 46.5 PQ for MaskFormer trained for 300 epochs. The authors report that Mask2Former converges 6 times faster than MaskFormer.

With Swin-L and 200 queries, Mask2Former reaches 57.8 PQ, 5.1 above MaskFormer and 3.2 above K-Net. The K-Net row is listed with 36 epochs and the MaskFormer row with 300, so the training budgets differ across the compared rows.

The R50 Mask2Former row uses 226G FLOPs at 8.6 fps, against 181G FLOPs at 17.6 fps for MaskFormer, so the accuracy gain comes with more inference compute.

For the Swin-L model, the thing-category AP reaches 48.6 and the mIoU computed from panoptic annotations reaches 67.4, against 40.1 and 64.8 for MaskFormer. These two numbers come from the same panoptic-trained model and show that it also yields instance and semantic outputs.

## Instance segmentation on COCO

With ResNet-50 and 50 epochs, Mask2Former reaches 43.7 AP, above the 42.5 AP of a Mask R-CNN baseline trained with large-scale jittering for 400 epochs. Its AP\_S of 23.4 is below that baseline's 23.8, while its AP\_L of 64.8 is above the baseline's 60.0. The authors describe the training as using 8 times fewer iterations than that baseline.

With Swin-L, Mask2Former reaches 50.1 AP against 49.5 AP for Swin-HTC++, which was trained for 72 epochs.

This comparison uses single-scale inference and models trained only on COCO train2017 data.

## Semantic segmentation on ADE20K

Table 3 reports ADE20K val with 150 categories. With ResNet-50 at crop size 512, Mask2Former reaches 47.2 mIoU single-scale, against 44.5 for MaskFormer, and with Swin-T it reaches 47.7 against 46.7.

With Swin-L, the default MSDeformAttn version reaches 56.1 single-scale and 57.3 multi-scale. Using FaPN as the pixel decoder raises the multi-scale result to 57.7 mIoU, which is above the 57.0 multi-scale mIoU of BEiT-UperNet in the same table.

## Which components account for the gains

All ablations use a ResNet-50 backbone and cover instance, panoptic and semantic segmentation. Removing masked attention lowers AP from 43.7 to 37.8, PQ from 51.9 to 47.1 and mIoU from 47.2 to 45.5, which is the largest drop in the table.

Among the cross-attention variants, masked attention reaches 43.7 AP, 51.9 PQ and 47.2 mIoU, against 37.8, 47.1 and 45.5 for standard cross-attention. The SMCA variant reaches 37.9 AP, 47.2 PQ and 46.6 mIoU, and mask pooling reaches 43.1 AP, 51.5 PQ and 46.0 mIoU. Mask pooling is the operation K-Net uses in place of cross-attention.

## Learnable queries as region proposals

Because learnable queries are supervised by the mask loss, their predictions can be read as mask proposals before they enter the decoder. The authors visualize mask predictions of four selected learnable queries and measure class-agnostic AR@100 on COCO val2017.

The learnable queries reach an AR@100 of 50.3, and the recall rises with depth to 56.8 at layer 3, 57.4 at layer 6 and 57.7 at layer 9. The proposals therefore score well before any decoder layer runs, and the decoder layers add recall on top of them.

## Cityscapes and other datasets

On Cityscapes val, Table 6 lists Mask2Former with R50 at 62.1 PQ, with Swin-B at 66.1 PQ and with Swin-L at 66.6 PQ. For Swin-L, the thing-category AP is 43.6 and the panoptic-derived mIoU is 82.9, while single-scale semantic mIoU is 83.3.

The table also lists specialized models such as Panoptic-DeepLab, SETR and SegFormer, and those values come from their own setups and backbones, so they are not a controlled comparison with this table. Results on ADE20K and Mapillary Vistas appear in an appendix that is not part of this excerpt.

## Limits of task-specific training and small objects

The authors state that Mask2Former still needs to be trained for each specific task, and a single model trained once for multiple tasks and datasets is left for future work. Mask2Former struggles with small objects, with AP\_S of 23.4 for ResNet-50, and it does not fully use the multi-scale features.

## Paper info

- **Authors**: Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar
- **Venue**: arXiv · Preprint
- **First published**: 2021-12-2
- **Added**: 2026-11-12
- **Updated**: 2022-6-15
- **Version**: v3
- **arXiv**: 2112.01527
- **Category**: cs.CV · cs.AI · cs.LG
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2112.01527v3) · [PDF ↗](https://arxiv.org/pdf/2112.01527v3)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar (2021). Masked-attention Mask Transformer for Universal Image Segmentation. arXiv:2112.01527v3. https://arxiv.org/abs/2112.01527v3

**BibTeX**

```
@misc{cheng2021masked,
  title = {{Masked-attention Mask Transformer for Universal Image Segmentation}},
  author = {Bowen Cheng and Ishan Misra and Alexander G. Schwing and Alexander Kirillov and Rohit Girdhar},
  year = {2021},
  eprint = {2112.01527},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2112.01527v3}
}
```

## Related papers

- [Panoptic Segmentation](https://refivu.com/en/papers/dd0aa8a4-d18c-432a-aa71-a94345749b15-panoptic-segmentation.md)
- [Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics](https://refivu.com/en/papers/7546f3a0-585b-406e-b199-f4b06c78a42f-multi-task-learning-using-uncertainty-to-weigh-losses.md)
- [Stochastic Backpropagation and Approximate Inference in Deep Generative Models](https://refivu.com/en/papers/c970a8cd-f376-4280-925f-4a55101bd127-stochastic-backpropagation-and-approximate-inference-in-deep.md)
- [Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond](https://refivu.com/en/papers/11ff1363-cafd-421d-bece-bfc246ea3170-qwen-vl-a-versatile-vision-language-model-for.md)
- [CCNet: Criss-Cross Attention for Semantic Segmentation](https://refivu.com/en/papers/0b5bae85-f19c-40ec-85a8-6d588ce0497b-ccnet-criss-cross-attention-for-semantic-segmentation.md)
- [Semantic Image Synthesis with Spatially-Adaptive Normalization](https://refivu.com/en/papers/7c8cce40-a7a4-49f2-a589-a9b074c36caa-semantic-image-synthesis-with-spatially-adaptive.md)

## Other paper commentary

- [Transformers in Vision: A Survey](https://refivu.com/en/papers/5380a1d5-fae3-41ad-ab72-e650e9db12fe-transformers-in-vision-a-survey.md)
- [Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour](https://refivu.com/en/papers/c430ecf3-8156-45a7-a3f7-eafc308c0d49-accurate-large-minibatch-sgd-training-imagenet-in-1.md)
- [Image Segmentation Using Deep Learning: A Survey](https://refivu.com/en/papers/c5b4071c-1e13-4bda-97cd-37556fea547e-image-segmentation-using-deep-learning-a-survey.md)
- [Residual Attention Network for Image Classification](https://refivu.com/en/papers/a2dac1b2-ffde-41af-9f9c-c9914f383632-residual-attention-network-for-image-classification.md)
- [DeCAF: A Deep Convolutional Activation Feature for Generic Visual Recognition](https://refivu.com/en/papers/b018056b-58d2-42cc-9184-ed3588d7abd4-decaf-a-deep-convolutional-activation-feature-for-generic.md)
- [Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers](https://refivu.com/en/papers/c0f3cbcf-d34f-4a38-a72f-084577c0ea46-rethinking-semantic-segmentation-from-a-sequence-to-sequence.md)
- [A Broad-Coverage Challenge Corpus for Sentence Understanding through Inference](https://refivu.com/en/papers/6a9a87c0-1034-4d6f-8def-43a3279e4725-a-broad-coverage-challenge-corpus-for-sentence-understanding.md)
- [Supervised Contrastive Learning](https://refivu.com/en/papers/a5818cfe-00fa-4e85-a38e-cdce6aa6cad5-supervised-contrastive-learning.md)
- [The Future of Digital Health with Federated Learning](https://refivu.com/en/papers/1562dfe5-7a60-4996-ad5c-7fe34a7d94e7-the-future-of-digital-health-with-federated-learning.md)
- [Past, Present, and Future of Simultaneous Localization And Mapping: Towards the Robust-Perception Age](https://refivu.com/en/papers/52381e81-8cdd-4551-89ee-d6c7630fb938-past-present-and-future-of-simultaneous-localization-and.md)
- [Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation](https://refivu.com/en/papers/e938eef4-a963-441d-bfef-194cc6a9a9e3-estimating-or-propagating-gradients-through-stochastic.md)

## Terms in this paper

- [COCO](https://refivu.com/en/terms/41f3215c4f4a-coco)
- [Semantic segmentation](https://refivu.com/en/terms/00dfce7ec404-semantic-segmentation)
- [mIoU](https://refivu.com/en/terms/901dcc5384c2-miou)
- [Cityscapes](https://refivu.com/en/terms/1067bb006500-cityscapes)
- [Mask R-CNN](https://refivu.com/en/terms/6da881f24c0c-mask-r-cnn)
- [AP](https://refivu.com/en/terms/476f7c8deadf-ap)
- [ADE20K](https://refivu.com/en/terms/5adea765479c-ade20k)
- [instance segmentation](https://refivu.com/en/terms/c62f5b6c52fb-instance-segmentation)
- [cross-attention](https://refivu.com/en/terms/83e3067f3ffa-cross-attention)
- [DETR](https://refivu.com/en/terms/86857f2f379d-detr)
- [panoptic segmentation](https://refivu.com/en/terms/0fdb08320aa0-panoptic-segmentation)
- [Transformer decoder](https://refivu.com/en/terms/c3364183b850-transformer-decoder)
- [Mapillary Vistas](https://refivu.com/en/terms/3bbe7487e044-mapillary-vistas)
- [masked attention](https://refivu.com/en/terms/50764ce6f2ac-masked-attention)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/50cd7b17-dd9f-4dc4-9cef-f0a5bc0e4924-masked-attention-mask-transformer-for-universal-image)
