Back to paper library
arXiv 2019ECCV 2020SegmentationPaper notes

OCRNet

First, Who Are You?

Original paper

Segmentation Transformer: Object-Contextual Representations for Semantic Segmentation

Paper authors

Yuhui Yuan · Xiaokang Chen · Xilin Chen · Jingdong Wang

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}
}
On this page

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.

Original Fig. 2: a car-show photo from ADE20K, with a red dot marking the pixel to classify, located on a car. (a) ASPP: the context is several groups of sparsely sampled pixels, with different colors for different atrous rates; these points fall both on the car and on the background (people, walls). (b) OCR: the context is the whole car (the blue region).
Original Fig. 2, Yuan et al., from PDF page 4, arXiv license, scholarly quotation.

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 NN pixels needing N×NN \times N 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?

Original Fig. 1: bar charts of mIoU for Baseline, OCR and GT-OCR on four datasets. Cityscapes: 75.8, 79.6, 88.8; ADE20K: 39.7, 44.3, 54.3; PASCAL-Context: 45.8, 53.3, 63.7; COCO-Stuff: 32.6, 38.4, 46.1.
Original Fig. 1, Yuan et al., from PDF page 2, arXiv license, scholarly quotation.

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

Original Fig. 3: the OCR pipeline. The backbone outputs pixel representations; the pink box is the soft object regions (with a loss attached); the purple box is the object region representations, obtained by weighting the pixel representations with the soft object regions; in the orange box, the pixel representations and object region representations give pixel–region relations, which weight the object region representations to give object contextual representations, which are then combined with the pixel representations into augmented representations.
Original Fig. 3, Yuan et al., from PDF page 5, arXiv license, scholarly quotation.

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, N=9,409N = 9{,}409 pixels in total; the number of classes is K=19K = 19.

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 19×97×9719 \times 97 \times 97. This map is supervised by the annotations (cross-entropy), and its kk-th channel MkM_k says how much each pixel belongs to class kk.

Step 2: object region representations (purple box). The fourth stage's features pass through a 3×3 convolution to become 512-channel pixel representations xix_i. For each region MkM_k, a softmax over space first normalizes the weights m~ki\tilde{m}_{ki} of the 9,409 pixels so that they sum to 1, and the pixel representations are then averaged with these weights:

fk=∑i∈Im~ki xif_k = \sum_{i \in \mathcal{I}} \tilde{m}_{ki} \, x_i

Each of the 19 classes gets a 512-dimensional vector fkf_k, 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 fcarf_{\text{car}}.

Step 3: object contextual representations (orange box). Each pixel computes a relation with each region:

wik=eκ(xi,fk)∑j=1Keκ(xi,fj),κ(x,f)=ϕ(x)⊤ψ(f)\begin{aligned} w_{ik} &= \frac{e^{\kappa(x_i, f_k)}}{\sum_{j=1}^{K} e^{\kappa(x_i, f_j)}}, \\ \kappa(x, f) &= \phi(x)^\top \psi(f) \end{aligned}

ϕ\phi and ψ\psi 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 wikw_{ik} can be read as "how much pixel ii looks like an object of class kk."

These weights then combine the 19 region representations into pixel ii's object contextual representation:

yi=ρ(∑k=1Kwik δ(fk))y_i = \rho\Big(\sum_{k=1}^{K} w_{ik} \, \delta(f_k)\Big)

Finally, the original xix_i and yiy_i are concatenated and passed through a transformation gg to get the augmented representation ziz_i, which a linear layer classifies. δ\delta, ρ\rho and gg 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 9,409×9,4099{,}409 \times 9{,}409 matrix of about 89 million values. OCR's relation matrix is 9,409×199{,}409 \times 19, 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.

Original Fig. 4: the segmentation transformer. On the left, the encoder takes image features as input and runs (optional) self-attention, cross-attention (Q from the image features, K and V from the output of the decoder on the right) and an FFN, followed by a linear predictor that outputs the segmentation map. On the right, the decoder takes category queries as input and runs self-attention, cross-attention (Q is the category queries, K and V are the image features) and an FFN.
Original Fig. 4, Yuan et al., from PDF page 8, arXiv license, scholarly quotation.

The decoder on the right corresponds to the first two steps. Its input is KK 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 m~ki\tilde{m}_{ki}; and the weighted average of the image features is fkf_k.

The encoder on the left corresponds to step 3. This time the pixels serve as queries, and the decoder's output (the KK region representations) serves as keys and values, in one cross-attention, which gives wikw_{ik} and yiy_i; the FFN after it corresponds to ρ\rho.

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):

MethodCityscapes+ coarseADE20KLIP
PPM (reimpl.)80.381.644.5054.76
ASPP (reimpl.)81.081.744.6055.01
OCR81.882.445.2855.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 2048×128×1282048 \times 128 \times 128 feature map, the authors measured the cost of each context module alone on a P40 GPU, excluding the backbone (the paper's Table 4):

ModuleParamsMemoryFLOPsTime
PPM23.1M792M619G99ms
ASPP15.5M284M492G97ms
DANet10.6M2,339M1,110G121ms
CCNet10.6M427M804G131ms
Self-attention10.5M2,168M619G96ms
OCR10.5M202M340G45ms

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 N×NN \times N 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.