Segmentation Transformer: Object-Contextual Representations for Semantic Segmentation
- Publication
- ECCV 2020Conference
- First submitted to arXiv
- arXiv:1909.11065
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{segmentationocrnet2019,
title = {{Segmentation Transformer: Object-Contextual Representations for Semantic Segmentation}},
author = {Yuhui Yuan and Xiaokang Chen and Xilin Chen and Jingdong Wang},
year = {2019},
eprint = {1909.11065},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1909.11065}
}
This paper's name on arXiv changed once. The first version, in September 2019, was called "Object-Contextual Representations for Semantic Segmentation," and it kept that name through ECCV 2020. The sixth version, in April 2021, gained the words "Segmentation Transformer" in front, plus a whole new section retelling the original method in Transformer language.
The rename came right after ViT and DETR took off. Still, the paper's core idea was already in the first version from September 2019, earlier than both DETR and ViT, and it's quite close to the queries of the later MaskFormer.
The authors are Yuhui Yuan, Xiaokang Chen, Xilin Chen and Jingdong Wang of the Institute of Computing Technology, Chinese Academy of Sciences, and Microsoft Research Asia. Their HRNet + OCR (plus their own boundary post-processing, SegFix) was first on the Cityscapes leaderboard at the ECCV 2020 submission deadline.
Defining the Problem
In semantic segmentation, a pixel's own features are almost never enough; it needs context from its surroundings. The question is where to get it.
There were two mainstream answers at the time.
The first is multi-scale context. ASPP uses convolutions with different atrous rates, and PSPNet uses pooling of different sizes, to gather information from ranges of different sizes. These approaches decide where to look based only on "position," regardless of what the pixels being looked at belong to.

On the left is ASPP. The red dot is on the car, and its context is ring after ring of sparsely sampled points, some landing on the car and some on the people and wall beside it.
The second is relational context, the self-attention approach of DANet, OCNet and the like. Each pixel looks at all other pixels, weighted by similarity. This is smarter, but it has two problems. One is that it's expensive, with pixels needing relations; the other is that every pixel is treated on its own, without being organized into "objects."
The authors' starting point is simple. A pixel's label is the class of the object it belongs to. So the most useful context should be "the object this pixel belongs to" itself, the whole blue car in the right image.
They first ran an idealized experiment to test the idea. If the annotations told the network directly which object region each pixel belongs to, and that region's representation were used to augment the pixel, how much better would it get?

The three bars are the baseline (dilated ResNet-101), OCR, and GT-OCR, which forms object regions from the annotations. GT-OCR is this method's upper bound. It jumps from 75.8 to 88.8 on Cityscapes and from 39.7 to 54.3 on ADE20K, 13 to 18 points more on all four datasets.
With an upper bound that high, the next question is whether the network, without annotations, can split pixels into object regions by itself, and split them well enough.
Solving the Problem
Three Steps

The OCR module has three steps, matching the three dashed boxes in the figure. Let's walk through it on Cityscapes: training crops are 769×769, the backbone is a dilated ResNet-101 with output stride 8, so the feature map is 97×97, pixels in total; the number of classes is .
Step 1: soft object regions (pink box). From the features of the backbone's third stage, a 1×1 convolution directly predicts a coarse segmentation map of size . This map is supervised by the annotations (cross-entropy), and its -th channel says how much each pixel belongs to class .
Step 2: object region representations (purple box). The fourth stage's features pass through a 3×3 convolution to become 512-channel pixel representations . For each region , a softmax over space first normalizes the weights of the 9,409 pixels so that they sum to 1, and the pixel representations are then averaged with these weights:
Each of the 19 classes gets a 512-dimensional vector , representing what "the road in this image" or "the car in this image" looks like. Note that it's specific to this image; a red sports car and a white van are both cars, but in different images they get different .
Step 3: object contextual representations (orange box). Each pixel computes a relation with each region:
and are both 1×1 convolution plus BatchNorm plus ReLU, projecting each side to 256 dimensions before the inner product. The softmax is taken over the 19 regions, so can be read as "how much pixel looks like an object of class ."
These weights then combine the 19 region representations into pixel 's object contextual representation:
Finally, the original and are concatenated and passed through a transformation to get the augmented representation , which a linear layer classifies. , and are likewise 1×1 convolution plus BatchNorm plus ReLU.
Why This Is Cheaper
A comparison with DANet makes it clear. In DANet's position attention, every pixel computes a relation with every pixel, a matrix of about 89 million values. OCR's relation matrix is , under 180,000 values, nearly 500 times smaller.
The savings come from the extra layer of "object regions" in between. DANet lets each pixel hunt for similar pixels on its own; OCR first groups the pixels into 19 regions, and each pixel only has to look at those 19 regions.
Region Supervision Is Essential
The method has two key design choices, and the authors ran ablations on both (Cityscapes validation set, dilated ResNet-101).
The first is that the object regions have to be supervised. Remove the coarse segmentation loss from step 1 (putting an ordinary auxiliary loss on the third stage instead), and mIoU drops from 79.58 to 77.31. If the regions don't correspond to real classes, the idea of "describing pixels by objects" falls apart.
The second is that the relations have to be computed from both pixel and region representations. The authors implemented two approaches of their own that predict relations from pixels only. The Double Attention-style one scores 79.01 and the ACFNet-style one (using the intermediate segmentation map directly as the relation) 78.02, both below OCR's 79.58. The authors' explanation is that region representations describe the object "in this image," so the relations computed from them fit this image better.
Retold in Transformer Language
§3.3, added in the sixth version, translates these three steps into a Transformer encoder–decoder.

The decoder on the right corresponds to the first two steps. Its input is category queries, one per class. The queries do cross-attention with the image features. The inner product of a query with each pixel is the coarse segmentation that step 1's 1×1 convolution computes (the 1×1 convolution's weights are exactly one vector per class); the softmax over space is step 2's ; and the weighted average of the image features is .
The encoder on the left corresponds to step 3. This time the pixels serve as queries, and the decoder's output (the region representations) serves as keys and values, in one cross-attention, which gives and ; the FFN after it corresponds to .
The authors also point out that these category queries are a lot like ViT's class token, except that there's one per class. Doing the two cross-attentions and the two self-attentions at the same time amounts to putting the image features and the category queries together into a single self-attention.
Seen from today, this retelling is interesting. A year later, MaskFormer used exactly the architecture of "each query corresponds to a region, and the inner product of a query with the pixel features gives a mask," except that MaskFormer's queries aren't tied to classes, and there can be more of them than there are classes.
Discussion
A Fair Comparison With Other Context Approaches
With the same backbone (dilated ResNet-101) and the same training setup, the authors reimplemented PPM and ASPP, getting results better than their original papers reported, and then compared OCR with them (the paper's Table 2; test set for Cityscapes, validation sets for ADE20K and LIP):
| Method | Cityscapes | + coarse | ADE20K | LIP |
|---|---|---|---|---|
| PPM (reimpl.) | 80.3 | 81.6 | 44.50 | 54.76 |
| ASPP (reimpl.) | 81.0 | 81.7 | 44.60 | 55.01 |
| OCR | 81.8 | 82.4 | 45.28 | 55.60 |
OCR wins every column, but by small margins, about 0.5 to 1.5 points. The authors' view is that on baselines this strong, such gains already count as significant, and OCR uses much less computation.
The same goes for relational context (the paper's Table 3). DANet scores 81.5 on the Cityscapes test set and CCNet's criss-cross attention 81.4; the authors' reimplementations of self-attention and Double Attention land at 81.1 to 81.2, while OCR gets 81.8.
Cost Is What Really Sets It Apart
The accuracy gaps are only a few tenths of a point; the cost gap is the real story. On a feature map, the authors measured the cost of each context module alone on a P40 GPU, excluding the backbone (the paper's Table 4):
| Module | Params | Memory | FLOPs | Time |
|---|---|---|---|---|
| PPM | 23.1M | 792M | 619G | 99ms |
| ASPP | 15.5M | 284M | 492G | 97ms |
| DANet | 10.6M | 2,339M | 1,110G | 121ms |
| CCNet | 10.6M | 427M | 804G | 131ms |
| Self-attention | 10.5M | 2,168M | 619G | 96ms |
| OCR | 10.5M | 202M | 340G | 45ms |
OCR's memory is only 1/10 of DANet's, its FLOPs under 1/3, and its time under half. Against multi-scale PPM, it has half the parameters and only 1/4 of the memory.
This table also shows the problem with the DANet paper, whose attention eats 2.3GB of memory at this size.
First on the Leaderboard
When comparing best results with other methods, the authors split methods into two groups: those on a simple baseline (dilated ResNet-101), and those on advanced baselines (stronger backbones, higher-resolution outputs, multi-grid and so on).
On the simple baseline, OCR scores 81.8 (without coarse annotations) and 82.4 (with coarse annotations) on the Cityscapes test set, already on par with some methods that use advanced baselines (DANet, for example).
Switching to the advanced baseline HRNet-W48, pretrained on Mapillary first, brings the Cityscapes test set to 84.2; adding SegFix, the boundary post-processing from the authors' other paper, brings it to 84.5, first on the leaderboard at submission time. Later, NVIDIA's HMSA combined HRNet + OCR with multi-scale attention and pushed it to 85.4.
One interesting detail is that the authors tried attaching PPM or ASPP directly to HRNet-W48, and not only did it not help, it sometimes made things worse; OCR helped every time.
On other datasets, OCR scores 45.66 on the ADE20K validation set, 56.65 on the LIP validation set, 56.2 on the PASCAL-Context test set and 40.5 on the COCO-Stuff test set. The authors also attached OCR to Panoptic FPN for panoptic segmentation, raising PQ on COCO val from 43.0 to 44.2, with the gain coming mainly from stuff classes (2.3 more PQ).
Conclusion
OCRNet's idea is to first roughly ask each pixel "which class are you," average the pixels of each class into one representative, then let every pixel look back at these representatives.
This "group first, then consult" step cuts the number of relations from the square of the pixel count to the pixel count times the number of classes; accuracy comes out a little better than DANet, PPM and ASPP, while memory and time drop several times over.
It's also a Transformer that arrived a little early. One query per class, with the inner product of queries and pixels giving the regions; this structure became mainstream in the MaskFormer and Mask2Former line.
If you move the OCR module into another network, the object region branch must have its segmentation loss attached. The unsupervised version only reaches 77.31, more than two points below the supervised one; that branch is more than an auxiliary loss, since the whole module relies on it to sort the pixels into the right groups.