Back to paper library
arXiv 2022CVPR 2023SegmentationPaper notes

Mask DINO

Boxes and Masks Are Family

Original paper

Mask DINO: Towards A Unified Transformer-based Framework for Object Detection and Segmentation

Paper authors

Feng Li · Hao Zhang · Huaizhe xu …

Show all 7 authors

Feng Li · Hao Zhang · Huaizhe xu · Shilong Liu · Lei Zhang · Lionel M. Ni · Heung-Yeung Shum

Publication
CVPR 2023Conference
First submitted to arXiv
arXiv:2206.02777
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{segmentationmaskdino2022,
  title = {{Mask DINO: Towards A Unified Transformer-based Framework for Object Detection and Segmentation}},
  author = {Feng Li and Hao Zhang and Huaizhe xu and Shilong Liu and Lei Zhang and Lionel M. Ni and Heung-Yeung Shum},
  year = {2022},
  eprint = {2206.02777},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2206.02777}
}
On this page

By mid-2022, the DETR family had a champion on each of two tracks. On detection there was DINO, the first DETR-like model to take first place on the COCO detection leaderboard; on segmentation there was Mask2Former, a single architecture handling instance, panoptic and semantic segmentation.

Both are "a set of queries plus a Transformer decoder," and they look very close. In the convolutional era, Mask R-CNN and HTC had long since shown that detection and instance segmentation help each other when trained together. So the natural thought is that adding a mask head to DINO, or a box head to Mask2Former, should do well on both.

Feng Li, Hao Zhang, Lei Zhang and colleagues at the Hong Kong University of Science and Technology and IDEA actually tried it, and both directions failed. This paper first answers why, then proposes Mask DINO, which adds a mask branch to DINO so that detection and segmentation genuinely help each other.

Defining the Problem

The authors start with two simple sets of experiments, using ResNet-50 and 50 epochs of training (Tables 1 and 2 of the paper):

ModelBox APMask AP
DINO50.7—
Mask2Former46.2 (box from mask)43.7
Mask2Former + det. head21.641.3
DINO + DETR seg. head—35.8
DINO + Mask2Former seg. head49.940.2

Bolt a detection head onto Mask2Former and box AP is only 21.6, with the masks dropping 2.4 along with it. The other way around, DINO with Mask2Former's segmentation head gets only 40.2 mask AP, 3.5 below the original Mask2Former, and DINO's own boxes slip a little too. Simple multi-task training ends up with each side dragging the other down.

Why? The authors analyze it from the nature of the two tasks. Segmentation is pixel-level classification; detection is region-level regression.

Mask2Former's queries get their masks just by taking a dot product with high-resolution pixel features and checking how similar each pixel is, so they never need to know where the object is as a whole. Its positional queries are just a set of learnable vectors, without the "anchor box" kind of positional prior DAB-DETR has; its masked attention uses the previous layer's high-resolution mask as a hard constraint, which is neither efficient nor flexible for box prediction; and it can't refine boxes layer by layer the way DINO does.

DINO's queries, conversely, are designed for detection. Its positional queries are 4-D anchor boxes, its content queries handle box offsets and classes, and designs like query selection and denoising training all strengthen region-level representations; they never interact with high-resolution pixel features. DETR's original segmentation head isn't ideal either. It computes attention only on the smallest feature map and then upsamples, and it's too heavy to add an auxiliary mask loss at every decoder layer.

So can one design a unified architecture in which detection and segmentation share their queries, while each still gets everything it needs?

Solving the Problem

Adding a Mask Branch to DINO

Original Figure 1: the Mask DINO framework. The part on the blue background is the original DINO: multi-scale features pass through the encoder, query selection (Unified & Enhanced QS) yields the initial content queries and anchor boxes, and these go into the decoder, which uses Hybrid Matching and denoising training (Unified DN) and outputs boxes and classes. In red are the parts added for segmentation: the backbone's 1/4 features and the encoder's 1/8 features upsampled 2× are added to give a pixel embedding map, which then takes a dot product with the query embeddings output by the decoder to give masks.
Original Figure 1, Li et al., from PDF page 5, arXiv license, scholarly quotation.

The blue part is DINO, untouched. The red is what's new.

First, a quick recap of DINO. It's a standard DETR-like model with a backbone, a Transformer encoder and a Transformer decoder. The positional part of each query is a 4-D anchor box that every decoder layer refines, and the refined box also guides where multi-scale deformable attention takes its features from. DINO also uses denoising training to speed up convergence and query selection to pick the initial queries from the encoder's output.

Mask DINO's mask branch borrows Mask2Former's approach. The backbone's 1/4-resolution features CbC_b first pass through a convolution to match the channel count, the encoder's 1/8-resolution features CeC_e are upsampled 2×, and the two are added and passed through a segmentation head M\mathcal{M} to get a high-resolution pixel embedding map. The dot product of each query's content embedding qcq_c with this map is that query's mask:

m=qc⊗M(T(Cb)+F(Ce))m = q_c \otimes \mathcal{M}(\mathcal{T}(C_b) + \mathcal{F}(C_e))

The same query outputs a class, a box and a mask together. That alone isn't enough, so the authors go on to extend each of DINO's three key designs to segmentation.

One: Query Selection Looks at Masks Too

DINO's query selection attaches a few prediction heads to the encoder output and picks the highest-scoring positions as the decoder's initial queries. Mask DINO attaches three heads to the encoder output, the same as the decoder's, for classification, boxes and masks. The top-ranked features by classification score predict boxes and masks together, are supervised on both, and serve as the decoder's initial content queries and anchor boxes.

There's a clever observation here. Early in training, masks are actually easier to learn than boxes, since a mask only has to check how similar each pixel is, while a box has to regress four coordinates directly. The authors measured mask AP at each decoder layer. At layer 0, right after query selection, Mask DINO already has 39.6 mask AP; Mask2Former's layer 0 has only 1.1.

Since masks are more accurate than boxes at the start, the authors derive boxes from the selected masks and use them as the decoder's initial anchor boxes. This "mask-enhanced anchor box initialization" raises layer-0 box AP from 25.6 to 41.2 and final box AP from 50.5 to 51.7, 0.8 above the original DINO. Here segmentation returns the favor and helps detection.

Two: Denoising Covers Masks Too

DINO's denoising training adds noise to the ground-truth boxes and classes, feeds them to the decoder as extra queries, and asks the model to recover the correct answers. This speeds up convergence substantially.

Mask DINO extends it to masks. A box can be seen as a coarse version of a mask, so the authors treat the (noised) boxes directly as "noisy masks" and train the model to recover masks from boxes.

Three: Matching Looks at Masks Too

When training DETR-like models, predictions are first paired with annotations by bipartite matching. DINO's matching cost looks only at classes and boxes, but Mask DINO's boxes and masks come from two parallel heads and may disagree with each other. So the authors add the mask loss to the matching cost:

λclsLcls+λboxLbox+λmaskLmask\lambda_\text{cls} \mathcal{L}_\text{cls} + \lambda_\text{box} \mathcal{L}_\text{box} + \lambda_\text{mask} \mathcal{L}_\text{mask}

Panoptic Segmentation: Stuff Needs No Boxes

Panoptic segmentation brings a problem of its own. For stuff classes like sky and road, the boxes derived from the annotated masks are often irregular or even cover the whole image, and training box prediction on them interferes with detecting things.

The authors compute no box loss for stuff classes and leave them out of box matching, while the box prediction pipeline stays as it is and still guides deformable attention to where it takes features. In the 12-epoch setting, this adds 1.1 PQ, with stuff PQ rising from 38.8 to 40.2.

Discussion

Instance Segmentation and Detection

ResNet-50 on the COCO validation set (compiled from Table 3 of the paper; speed measured with detectron2 on an A100):

ModelEpochsMask APBox APFPS
Mask2Former1238.7—8.2
Mask DINO1241.445.714.8
Mask2Former5043.746.28.2
DINO36—50.919.6
Mask DINO5046.050.514.8
Mask DINO (mask-enhanced anchors)5046.351.714.2

With the same 12 epochs of training, Mask DINO's mask AP is 2.7 above Mask2Former's; trained for 24 epochs, it already reaches 44.2, beating the 43.7 of Mask2Former trained for 50. At 50 epochs, masks are 2.6 higher, and box AP is also 0.8 above DINO, a detection-only model. It's faster than Mask2Former too, at 14.8 images per second against 8.2, because it uses deformable attention instead of Mask2Former's high-resolution masked attention.

The gain on small objects is especially clear, with small-object mask AP rising from 23.4 to 26.0. This fits the authors' claim that detection can help segmentation; the regional position information boxes provide is exactly what Mask2Former's queries lack.

With Swin-L, Mask DINO trained for 50 epochs gets 52.3 mask AP and 59.0 box AP, the former 2.2 above Mask2Former (trained for 100 epochs).

Panoptic and Semantic Segmentation

On panoptic segmentation (Table 4 of the paper, ResNet-50), Mask DINO trained for 50 epochs gets 53.0 PQ, 1.1 above Mask2Former's 51.9, and is better on both things and stuff. Interestingly, Mask DINO uses no masked attention at all, taking features with box-guided deformable attention instead, and still does well even on stuff classes, which have no clear shape.

For semantic segmentation, the authors found that results varied a lot, so they ran each setting three times. On ADE20K, Mask DINO averages 47.7 against Mask2Former's 46.1; on Cityscapes it's 79.8 against 78.7.

Detection Data Helps Segmentation

Another advantage of the unified architecture is that detection data, far more plentiful than segmentation data, can now be put to use as well. The authors pretrain on Objects365, a large detection dataset, then fine-tune on each segmentation task (Table 7 of the paper, Swin-L):

TaskMask DINO+ Objects365 pretrainingPrevious best
COCO instance (AP)52.654.553.4 (SwinV2-G, 3B params)
COCO panoptic (PQ)58.459.457.8 (Mask2Former)
ADE20K semantic (mIoU)56.660.8 (test-time aug.)59.9 (SwinV2-G)

With only 223 million parameters plus pretraining on detection data, Mask DINO beats the 3-billion-parameter SwinV2-G on both instance and semantic segmentation, and beats Mask2Former on panoptic segmentation too. That's what the paper means by the best results "among models under one billion parameters."

Ablations

These all use ResNet-50 (Tables 11, 13 and 15 of the paper); the first is trained for 50 epochs, the other two for 12:

  • Boxes and masks help each other. Training the same model on boxes alone gives 50.1 box AP, and on masks alone 43.3 mask AP; training both together gives 50.5 for boxes and 46.0 for masks (50 epochs). Masks gain 2.7 and boxes 0.4.
  • Matching has to look at both. Matching on boxes alone gives 44.4/40.5 (box/mask), on masks alone 40.2/38.4, and on both 44.5/41.4.
  • Every component contributes. The full model gets 45.7/41.4. Removing mask-enhanced anchors costs boxes 1.2; removing unified query selection costs boxes 2.1 and masks 1.1; removing unified denoising costs boxes 1.3 and masks 1.1; removing hybrid matching costs boxes 0.8 and masks 0.9. With all of them removed, 41.7/38.5 is left.

As for decoder depth, Mask DINO keeps improving from 3 layers up to 9; the authors mention that the original DINO actually gets worse with 9 layers.

Conclusion

Mask DINO starts from the fact that the strongest detector and the strongest segmenter of the DETR era look alike, yet neither could simply be added onto the other. The authors trace the cause to query design; segmentation queries don't understand position, and detection queries don't understand pixels.

The fix is to add a mask branch to DINO and extend query selection, denoising training and bipartite matching to masks. The neatest of these is the mask-enhanced anchor box; early in training, masks are more accurate than boxes, so the masks give the boxes their starting point. In the end detection and segmentation really do help each other, with boxes better than DINO's, masks better than Mask2Former's, and higher speed as well.

When you need both detection and segmentation, or have plenty of detection annotations but only a few segmentation ones, a unified architecture like Mask DINO is especially good value. Its best numbers were all bought with pretraining on Objects365 detection data first.