Back to paper library
arXiv 2019ICCV 2019SegmentationPaper notes

YOLACT

Just Arithmetic

Original paper

YOLACT: Real-time Instance Segmentation

Paper authors

Daniel Bolya · Chong Zhou · Fanyi Xiao · Yong Jae Lee

Publication
ICCV 2019Conference
First submitted to arXiv
arXiv:1904.02689
Cite this paper · BibTeX

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

@misc{segmentationyolact2019,
  title = {{YOLACT: Real-time Instance Segmentation}},
  author = {Daniel Bolya and Chong Zhou and Fanyi Xiao and Yong Jae Lee},
  year = {2019},
  eprint = {1904.02689},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1904.02689}
}
On this page

The YOLOv3 paper contains a famous gripe. Joseph Redmon wrote, "Boxes are stupid anyway though, I'm probably a true believer in masks except I can't get YOLO to learn them."

Daniel Bolya, Chong Zhou, Fanyi Xiao and Yong Jae Lee of UC Davis put that line at the very top of YOLACT's introduction, and then answered it.

In 2019, the strongest instance segmentation method was Mask R-CNN, which handles about 8 images per second on COCO. Object detection had long had single-stage models like SSD and YOLO running at dozens of images per second, but instance segmentation still had no real-time counterpart. The authors say YOLACT is the first method on COCO to run at over 30 images per second with mask AP close to 30.

The name is short for You Only Look At CoefficienTs. Why it looks only at the coefficients will become clear shortly.

Defining the Problem

Object detection got fast by removing the second stage. Faster R-CNN first proposes candidate boxes, then classifies and refines each one; SSD and YOLO predict boxes and classes directly at every position of the feature map, all in one go.

Instance segmentation can't simply copy this. Mask R-CNN's mask branch has to wait for the first stage to propose candidate boxes, then use RoIAlign to cut out the features inside each box and resize them to a fixed size, and only then can it hand them to the mask branch. This "localize first, then cut out features" pipeline is sequential and hard to speed up. Single-stage methods like FCIS can run in parallel, but their post-processing is heavy, and they're still far from real time.

Why not have every position output a mask directly, as in detection? The authors' explanation is that masks are spatially coherent; neighboring pixels mostly belong to the same object. Convolutional layers naturally exploit this coherence. But the classes and box coordinates a single-stage detector outputs at each anchor form a vector, essentially the output of a fully connected layer, which isn't good at producing anything with spatial structure.

The authors actually tried it. Having each anchor output a 16×16 mask directly in fully connected fashion (fc-mask) gets only 20.7 AP on COCO.

So what needs solving is how a single-stage detector can also output good masks without cutting out RoI features.

Solving the Problem

Split Into Two Parallel Jobs

Original Figure 2: the YOLACT architecture. On the left are the ResNet backbone (C1 to C5) and the feature pyramid (P3 to P7). P3 connects to Protonet, which produces k prototype masks (k = 4 in the figure, blue for low, yellow for high); the features at each level connect to the prediction head, which, besides classes and boxes, outputs a set of mask coefficients for each detection (the figure shows coefficient bars for two detections, Person and Racket). After NMS, the coefficients are used to combine the prototypes by addition and subtraction (Assembly), and the results are cropped by box and thresholded to give masks for the tennis player and the racket.
Original Figure 2, Bolya et al., from PDF page 3, arXiv license, scholarly quotation.

YOLACT splits instance segmentation into two independent parts that can be computed at the same time:

  1. Prototype masks. A fully convolutional network (protonet) outputs kk masks for the whole image, and these masks don't belong to any particular object.
  2. Mask coefficients. At each anchor, the detection head outputs kk more numbers on top of the class and box.

To get an object's mask, use its kk coefficients to take a weighted sum of the kk prototypes:

M=σ(PC⊤)M = \sigma(P C^\top)

PP holds the h×w×kh \times w \times k prototypes, CC is the n×kn \times k coefficient matrix, and nn is the number of objects that survive NMS and the score threshold. The whole assembly is a single matrix multiplication plus a sigmoid.

With this division of labor, convolution handles what it's good at, spatial structure (the prototypes), the fully connected-style outputs handle what they're good at, semantic vectors (the coefficients), and the two can be computed in parallel.

Look at the assembly in the upper right of Figure 2. The tennis player's mask is the first three prototypes added together, minus the fourth; the racket is minus the first, plus the second, minus the third, plus the fourth. Both objects use the same set of prototypes, just with different coefficients.

Following a 550×550 Image

YOLACT's default input is 550×550, with a ResNet-101 plus FPN backbone.

The prototype branch. Protonet attaches to FPN's P3, which is 69×69×256.

Original Figure 3: the Protonet architecture. Starting from P3 (69×69×256), it goes through three 3×3 convolutions (still 69×69×256), is upsampled and convolved to 138×138×256, and a final 1×1 convolution outputs 138×138×k.
Original Figure 3, Bolya et al., from PDF page 4, arXiv license, scholarly quotation.

After three 3×3 convolutions, the map is upsampled 2× to 138×138, 1/4 of the input, and a 1×1 convolution outputs kk channels. The paper uses k=32k = 32, so the prototypes are 138×138×32.

The authors chose P3 because it has the highest resolution in the FPN while still carrying deep semantics; upsampling further to 1/4 is for small objects. The prototype outputs are unbounded (ReLU, or no nonlinearity at all), which lets the network give very strong responses to things it's very sure about (such as obvious background).

The prototypes have no loss of their own; all their supervision comes from the masks assembled at the end.

The coefficient branch. RetinaNet-style prediction heads attach to every level from P3 to P7, with 3 anchors per position. Each anchor originally outputs cc class scores and 4 box coordinates; YOLACT adds kk coefficients, for 4+c+k4 + c + k in all. COCO has 80 classes plus background, so that's 4+81+32=1174 + 81 + 32 = 117 numbers.

Working from the network's strides, a 550 input gives grids of 69, 35, 18, 9 and 5 on a side from P3 to P7, 6,416 positions and 19,248 anchors in total.

The coefficients go through tanh, so they range from −1 to 1. The authors stress this point because assembly has to be able to "subtract" a prototype; both masks in Figure 2 need subtraction to come together.

Assembly. Suppose 5 objects remain after NMS, so the coefficient matrix CC is 5×32. Flatten the prototypes into 19,044×3219{,}044 \times 32 (138×138=19,044138 \times 138 = 19{,}044), multiply by C⊤C^\top to get 19,044×519{,}044 \times 5, reshape that into 5 masks of 138×138, and apply the sigmoid.

Finally, the predicted box crops away the part of the mask outside it, and the result is thresholded. The authors say the whole mask branch takes only about 5 milliseconds.

Cropping During Training

There are three loss terms: classification, box, and the per-pixel binary cross-entropy between the assembled mask and the annotated mask, weighted 1, 1.5 and 6.125 respectively.

Cropping differs between training and inference. At inference, the predicted box is used; during training, the annotated box is used, and the mask loss is divided by the annotated box's area. This keeps the pixel counts of large objects from drowning out small objects, so the prototypes learn small things too.

The Prototypes Learned Localization on Their Own

This is the most interesting part of the whole paper.

The usual line is that fully convolutional networks are translation invariant: put the same thing on the left or the right of the frame, and the convolution computes the same response. But instance segmentation needs to tell "the person on the left" from "the person on the right," so FCIS deliberately added position-sensitive feature maps, while Mask R-CNN put masks in the second stage and let RoIAlign handle localization.

YOLACT's only means of localization is cropping by box at the very end. Yet the authors found that for medium and large objects, the masks are still usable even without cropping. The prototypes learned localization on their own.

Original Figure 5: responses of six prototypes (rows 1 to 6) on six images (columns a to f). a is a solid red image, b and c are white backgrounds with a red star (in different positions), and d to f are real photos (a cluster of small colorful paper umbrellas, a flock of sheep, two vases). The responses of prototypes 1 to 3 are split into two sides by a dashed line and light up on only one side; prototype 4 lights up at the bottom left of objects; prototype 5 on the background and object edges; prototype 6 on the ground.
Original Figure 5, Bolya et al., from PDF page 5, arXiv license, scholarly quotation.

Start with column a, an image that's solid red all over. If a convolutional network were really translation invariant, an input that's the same everywhere should give an output that's the same everywhere. But the prototypes' responses on this image are clearly partitioned, bright in some places and dark in others.

The reason is padding. Every convolution layer in ResNet pads the edges with 0, and this ring of zeros tells the network how far a pixel is from the image edge. The authors' example is that a kernel like [1,0][1, 0] applied over several layers can push the edge's 0 inward one layer at a time. So ResNet is inherently not translation invariant, and YOLACT makes heavy use of that. In columns b and c, the red star merely changes position, and the prototypes respond completely differently.

Now the real photos. Prototypes 1 to 3 light up only on one side of an invisible line, effectively cutting the frame into partitions. Combining these partition prototypes makes it possible to separate objects of the same class, even overlapping ones. In image d, the green umbrella's mask can be obtained by subtracting prototype 3 from prototype 2, which separates it from the red one.

The other prototypes each have their own job: prototype 4 lights up at the bottom-left corner of objects, prototype 5 at the border between background and objects, and prototype 6 picks out the ground.

Prototypes can also do several jobs at once. Prototype 2 is a partition prototype and also responds especially strongly to objects in the bottom-left corner. This explains why 32 prototypes are enough.

Fast NMS

The last engineering detail is NMS. Traditional NMS processes each class in sequence: sort the boxes by score, start from the highest, remove the lower-scoring boxes that overlap it beyond a threshold, then move on to the next surviving box. This process is sequential.

In a 5 fps model, NMS being 10 milliseconds slower hardly matters, only 0.26 fps; in a 30 fps model, saving 10 milliseconds gains 12.9 fps.

Fast NMS turns it into matrix operations. For each class, take the top nn boxes by score, compute the n×nn \times n IoU matrix XX, set the diagonal and the lower triangle to 0 (keeping only the IoU of "boxes that score higher than me" with me), and take the maximum of each column:

Kkj=max⁡iXkijK_{kj} = \max_i X_{kij}

KkjK_{kj} is the largest IoU between box jj of class kk and all boxes that score higher than it; the box is kept if this is below the threshold tt.

The difference is that in traditional NMS, a box that has already been removed can't remove others; Fast NMS ignores this and compares every box with all the boxes that score higher than it, so it removes slightly more. In exchange, everything is computed in one go on the GPU, 11.8 milliseconds faster than a Cython version of traditional NMS, at a cost of only 0.1 AP.

Discussion

Results on COCO

Training uses a single GPU, batch size 8, and SGD for 800,000 iterations, which takes 4 to 6 days on one Titan Xp. All speeds were remeasured on the same Titan Xp.

COCO test-dev results (from the paper's Table 1):

MethodBackboneFPSAPAP50AP75APSAPL
FCISR-1016.629.551.530.28.049.7
Mask R-CNNR-101-FPN8.635.758.037.815.552.4
YOLACT-550R-101-FPN33.529.848.531.29.947.7
YOLACT-700R-101-FPN23.431.250.632.812.147.1

YOLACT-550 runs 3.9 times as fast as Mask R-CNN, with AP 5.9 lower.

Now look at where the gap lies. AP50 is 9.5 apart, AP75 only 6.6; at an extremely strict threshold like IoU 95%, YOLACT scores 1.6 and actually beats Mask R-CNN's 1.3. Roughly, these numbers say that YOLACT misses or misidentifies objects more often, but once it catches one, its mask boundaries are more accurate.

Original Figure 7: masks from FCIS (top), Mask R-CNN (middle) and YOLACT (bottom) on five images, with close-ups. For the tennis player on the far right, YOLACT's mask hugs the outline of the arm, while the mask edges of FCIS and Mask R-CNN are messier.
Original Figure 7, Bolya et al., from PDF page 7, arXiv license, scholarly quotation.

The authors credit two things: YOLACT's masks are 138×138, much larger than Mask R-CNN's 28×28, and they're assembled directly from the original features, with no misalignment from RoI cropping. The arm of the tennis player on the far right shows it most clearly.

Small objects, on the other hand, are YOLACT's weak spot, with a small-object AP of only 9.9 against Mask R-CNN's 15.5.

The Gap Comes From the Detector

The authors also made a clever comparison. YOLACT's mask AP is 29.8 and its box AP 32.3, a difference of 2.5; Mask R-CNN's are 35.7 and 38.2, also 2.5 apart. Both lose the same amount going from boxes to masks, so the 6 points YOLACT trails by come mainly from the detector itself, not from how the masks are produced.

Ablations

A few key ablations (COCO val2017):

  • Number of prototypes. Increasing kk from 8 to 32 raises AP from 26.8 to 27.7; going on to 64, 128 and 256 leaves AP between 27.6 and 27.8, while speed drops. The authors' explanation is that coefficients are hard to predict; if just one coefficient is badly off, the linearly combined mask can vanish entirely or leak into other objects; with more prototypes, this balance gets harder to strike, and the extra prototypes are mostly duplicates that differ a little at the edges.
  • Fast NMS. YOLACT goes from 24.0 fps to 33.5 fps, with AP slipping from 30.0 to 29.9. The same swap in Mask R-CNN costs 0.3 AP and saves about 15 milliseconds.
  • Sped-up baselines. Shrinking Mask R-CNN's input to 550×550 still gives only 13.5 fps; FCIS without mask voting runs at 9.5 fps. Even trading away accuracy, these methods can't reach real time.
  • Semantic segmentation auxiliary loss. During training, an extra 1×1 convolution on P3 does semantic segmentation (allowing multiple classes per pixel), and it's removed at inference; AP rises 0.4 with no change in speed.

Two Kinds of Failure

In the discussion, the authors honestly list two kinds of errors peculiar to YOLACT.

The first is localization failure. When too many objects crowd into one spot, the prototypes can't separate each of them, and some objects get a mask that looks more like generic foreground than their own. The paper's example is a blue truck parked under a red airplane.

The second is leakage. The network knows the mask will be cropped by the box in the end, so it doesn't bother suppressing noise outside the box. That's fine when the box is accurate; when the box is too big, the mask of another object farther away gets wrapped in too. The example is three skiers; the network decides they're far enough apart not to need separating, and once a box is too large, the masks stick together.

Stability on Video

YOLACT is trained only on still images and has no temporal smoothing of any kind, yet the authors found its masks on video to be more stable than Mask R-CNN's. Mask R-CNN's masks hang on the first stage's candidate boxes and jitter even when the objects stand still; YOLACT's prototypes aren't affected by the boxes, so when a box jitters, the mask doesn't jitter much along with it.

Conclusion

Rather than computing a separate mask for each object, YOLACT first computes a set of prototypes everyone shares, and each object only has to decide how to add and subtract them. Assembly is just one matrix multiplication, so the masks come almost for free, and instance segmentation ran above 30 fps on COCO for the first time.

This design went a long way. In Ultralytics' YOLO segmentation models, the segmentation head outputs 32 prototypes by default, each box gets 32 coefficients, and after masks_in @ protos the masks are cropped by box, exactly as in YOLACT. When the YOLOE note on this site mentions "a segmentation head that produces masks the YOLACT way," this is what it refers to.

These descendants also inherited YOLACT's two weaknesses. One is small objects, since the prototypes are only 1/4 the size of the input; the other is leakage, since the masks are cropped by boxes, and when a box is off, the mask goes off with it. When same-class objects crowd together, check whether the boxes are accurate before blaming the masks.