Back to paper library
arXiv 2022CVPR 2023SegmentationPaper notes

OneFormer

Once Is Enough

Original paper

OneFormer: One Transformer to Rule Universal Image Segmentation

Paper authors

Jitesh Jain · Jiachen Li · MangTik Chiu · Ali Hassani · Nikita Orlov · Humphrey Shi

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

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

@misc{segmentationoneformer2022,
  title = {{OneFormer: One Transformer to Rule Universal Image Segmentation}},
  author = {Jitesh Jain and Jiachen Li and MangTik Chiu and Ali Hassani and Nikita Orlov and Humphrey Shi},
  year = {2022},
  eprint = {2211.06220},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2211.06220}
}
On this page

Mask2Former is often called a "universal" segmentation architecture, since the same design does semantic, instance and panoptic segmentation. Look closely at its experiments, though, and every task is trained separately. On ADE20K, semantic segmentation trains for 160k iterations, instance segmentation for another 160k and panoptic segmentation for yet another 160k, ending up with three sets of weights.

The architecture is universal; the model isn't.

Jitesh Jain, Jiachen Li, Humphrey Shi and colleagues at SHI Labs at the University of Oregon set out to build a truly universal model: one architecture, one set of weights, only panoptic annotations, a single training run, and it has to beat separately trained Mask2Former on all three tasks. The title borrows a line from The Lord of the Rings: One Transformer to Rule Universal Image Segmentation.

Defining the Problem

Original Figure 1: three stages on the road to universal segmentation. (a) The traditional approach: semantic, instance and panoptic each have their own specialized architecture, model and data. (b) Recent panoptic architectures (such as Mask2Former and K-Net): the three tasks share one architecture but are still trained into three models, each on its own data. (c) OneFormer: one universal architecture, one universal model and one dataset, plus a "task" input telling the model which task to do.
Original Figure 1, Jain et al., from PDF page 1, arXiv license, scholarly quotation.

The authors divide the development of segmentation into three stages. (a) is the traditional approach, with a separate architecture for each task. (b) covers "panoptic architectures" like K-Net, MaskFormer and Mask2Former; one architecture can do all three tasks, but getting the best results still means training once per task. The authors call this "semi-universal."

So why not just mix the three tasks and train one model? The authors tried, jointly training Mask2Former on all three tasks on ADE20K (each image gets the annotation of one randomly drawn task), and got 48.7 PQ, 33.7 AP and 56.2 mIoU. The paper says semi-universal architectures "face a significant drop in performance" under joint training, but against the separately trained Mask2Former in its own table (48.7, 34.9, 56.1), PQ and mIoU are actually flat, and the only real drop is 1.2 in AP ("significant" is resting almost entirely on AP's shoulders).

The authors believe the problem is that Mask2Former's queries don't know which task they're doing. The same set of queries should pick up only thing classes, one per object, for instance segmentation; one region per class, stuff included, for semantic segmentation; and a mix of the two for panoptic segmentation. What the queries mean changes with the task, yet the model has no information to tell which task it is.

So OneFormer has two questions to answer. How do you let the model know which task it's doing? And how do you get it to learn the differences between tasks, and between classes, in a single joint training run?

Solving the Problem

Overall Architecture

Original Figure 2: the OneFormer architecture. (a) Multi-scale features: the image passes through the backbone and pixel decoder to give multi-scale features from 1/4 to 1/32. (b) Task-conditioned queries: the task input "the task is {task}" is tokenized and passed through an MLP to give the task token Q_task, which is repeated N−1 times as the initial queries, passed through a Transformer together with the 1/4 features, and concatenated with Q_task to give N queries; separately, the panoptic, instance and semantic text lists pass through the Text Mapper to give the text queries Q_text, and a contrastive loss is computed between the two sets of queries. (c) Task-dynamic prediction: the queries pass through L Transformer decoder layers (each consisting of cross-attention, self-attention and an FFN, in that order) and output masks and classes, which are compared with that task's annotations during training.
Original Figure 2, Jain et al., from PDF page 3, arXiv license, scholarly quotation.

(a) and (c) are essentially Mask2Former: a backbone plus a pixel decoder with multi-scale deformable attention, followed by a 9-layer Transformer decoder in which the queries take a dot product with the pixel features to give masks and also predict classes. Everything new in OneFormer is in (b).

One Set of Annotations, Three Tasks

Start with how the training data is prepared. OneFormer uses only panoptic annotations, because they contain information about both stuff and things, and semantic and instance annotations can both be derived from them.

For each training image, one of the three tasks (panoptic, semantic or instance) is first drawn uniformly at random, and then that task's annotation is derived from the panoptic annotation:

  • Semantic: one mask per class present, however many individuals there are.
  • Instance: thing classes only, one mask per object, with all stuff ignored.
  • Panoptic: one mask per stuff class and one per thing object.
Original Figure 3: how three kinds of annotation of the same street scene are turned into text lists. (a) The panoptic, instance and semantic annotations. (b) The number of masks per class: the panoptic annotation has 5 cars, 2 persons, 1 rider, 1 bicycle, 1 pole, 1 road, 1 building, 1 traffic sign, 1 vegetation and 1 sidewalk; the instance annotation has only 5 cars, 2 persons, 1 rider and 1 bicycle; the semantic annotation has just 1 for each class. (c) Each mask is written as the sentence "a photo with a {CLS}", forming the text list. (d) The list is padded to a fixed length with "a panoptic photo", "an instance photo" and "a semantic photo".
Original Figure 3, Jain et al., from PDF page 4, arXiv license, scholarly quotation.

Look at column (b). For the same image, the panoptic annotation has 5 cars and 2 people, plus stuff such as road, building and vegetation; the instance annotation is left with only cars, people, the rider and the bicycle; the semantic annotation counts just one mask per class, so the 5 cars merge into "1 car."

Each mask is then written as the sentence "a photo with a {CLS}"; 5 cars, for example, become 5 sentences of "a photo with a car," giving a text list. The list's length varies from image to image, so it's padded to a fixed length with "a/an {task} photo," and these padding sentences stand for "no object."

Task Token and Task-Guided Queries

How does the model know which task it's doing? Through a single sentence, "the task is {task}." After tokenization, this sentence passes through a linear layer and LayerNorm to give a task token QtaskQ_\text{task}.

Mask2Former's object queries are a set of learnable parameters, the same for every image. OneFormer's object queries are initialized from the task token instead. QtaskQ_\text{task} is repeated N−1N - 1 times and passed through a two-layer Transformer in which the copies interact with the flattened 1/4-resolution image features; finally QtaskQ_\text{task} itself is concatenated back on, giving NN queries (N=250N = 250 by default).

This way the queries carry the information "which task this is" from the start, along with the context of this particular image.

Contrastive Learning Between Queries and Text

A task token alone isn't enough. The authors also want the queries to actually learn to "stand for different things under different tasks," and for that they use the text list from earlier.

The text list passes through a 6-layer Transformer text encoder (the tokenizer and encoder are the same as CLIP's), giving one embedding per sentence; 16 learnable text context embeddings are then concatenated to make up NN text queries QtextQ_\text{text}. These text queries describe "what should be segmented out of this image under this task."

Contrastive learning is then done between the object queries and the text queries, the same way CLIP trains on image–text pairs:

LQ↔Qtext=LQ→Qtext+LQtext→Q\mathcal{L}_{Q \leftrightarrow Q_\text{text}} = \mathcal{L}_{Q \rightarrow Q_\text{text}} + \mathcal{L}_{Q_\text{text} \rightarrow Q}

Within a batch, matching object queries and text queries are pulled together, and the rest pushed apart.

This does two things. The first is telling tasks apart. For instance segmentation the text list contains only things, so the queries are pushed toward attending only to things; for semantic segmentation the list has one sentence per class, so the queries are pushed toward one region per class. The second is telling classes apart. The text carries class names, so contrastive learning helps the queries distinguish similar classes more clearly, and the paper shows examples where OneFormer confuses similar classes less often than Mask2Former.

The text queries are used only in training. At inference the whole text mapper can be dropped; give the model "the task is semantic" or "the task is instance" and you get the output for that task.

The final loss is the contrastive loss and the classification cross-entropy, plus binary cross-entropy and Dice loss on the masks; the last three are the same as in Mask2Former.

Discussion

One Model Beats Three

ADE20K validation set (Table 1 of the paper, Swin-L, 640×640 crops, single-scale testing):

ModelTrainingPQAPmIoU
Mask2Former (panoptic)Separate48.7——
Mask2Former (instance)Separate—34.9—
Mask2Former (semantic)Separate——56.1
OneFormerJoint, once49.835.957.0

Trained once, with one set of weights, OneFormer beats the separately trained Mask2Former versions on all three metrics, with PQ 1.1 higher, AP 1.0 higher and mIoU 0.9 higher. Its total training is a third of Mask2Former's.

On Cityscapes, OneFormer is also better on panoptic and instance segmentation (Table 2 of the paper, Swin-L). Its PQ of 67.2 and AP of 45.6 are both above Mask2Former's (66.6 and 43.7); its mIoU of 83.0 is slightly below Mask2Former's semantic-only model (83.3), while with multi-scale testing it's 84.4 against 84.3. With ConvNeXt-L, PQ reaches 68.5 and AP 46.5.

On COCO the gap is smaller (Table 3 of the paper). The Swin-L OneFormer gets 57.9 PQ against Mask2Former's 57.8, and both get 67.4 mIoU. The authors note that COCO's panoptic and instance annotations are inconsistent with each other, so AP is computed against instance annotations derived from the panoptic ones.

With a stronger backbone such as DiNAT-L (a Transformer with neighborhood attention), mIoU on ADE20K reaches 58.3, and with large 1280 crops PQ reaches 51.5.

Ablations

All ablations are on Cityscapes with Swin-L (Tables 4 and 5 of the paper):

SettingPQAPmIoU
OneFormer67.245.683.0
No task token66.543.382.9
No learnable text context62.745.082.8
Zero-initialized queries65.844.583.1
Query classification loss instead66.444.782.6
No contrastive loss58.842.482.5

The contrastive loss has the biggest effect; without it, PQ drops by 8.4 and AP by 3.2. The authors also tried replacing the contrastive loss with "having each query directly classify the classes on the text list," which also does worse. The task token mainly affects instance segmentation AP; the learnable text context mainly affects PQ, which drops by 4.5 without it.

Interestingly, mIoU barely responds to any of these components, with every change within 0.5. Semantic segmentation is the simplest of the three tasks to begin with.

The text template makes a difference too. "a photo with a {CLS}" is best; the bare class name "{CLS}" costs 0.6 PQ, and "a photo with a {CLS} {TYPE}," which adds whether it's stuff or a thing, actually costs 1.8.

The Task Token Really Works

The most direct evidence is the same model given different task tokens (Table 8 of the paper, ADE20K):

Task tokenPQAPmIoU
the task is panoptic49.335.857.0
the task is instance33.135.926.4
the task is semantic40.425.357.0

Given "instance," the model stops segmenting stuff; stuff PQ falls to just 1.5 and mIoU drops with it to 26.4, but instance AP is the highest of the three. Given "semantic," AP drops to 25.3, because objects of the same class are merged into one region. The same set of weights really does change its behavior according to the task token.

Conclusion

OneFormer takes Mask2Former from a "universal architecture" to a "universal model." It uses only panoptic annotations and draws a random task for each training image, turns the sentence "the task is {task}" into a task token that initializes the queries, and uses contrastive learning between queries and text so that the queries learn to stand for different things under different tasks. The result is one set of weights that, with a third of the training, beats separately trained Mask2Former on all three tasks on ADE20K. On Cityscapes it also wins on panoptic and instance segmentation, while on semantic segmentation it edges ahead only with multi-scale testing.

In practice, you maintain a single model in deployment and get semantic, instance and panoptic segmentation from it, switching tasks by changing one sentence.

Keep in mind, though, that most of OneFormer's gains are in AP and PQ; its semantic segmentation mIoU is about the same as specialized models'. If you need only semantic segmentation, Mask2Former or another specialized semantic segmentation model will do. If you need several kinds of output at once, what OneFormer saves you is training and maintaining two extra models.