TL;DR
A detector proposes many rectangular regions (region proposals), each with a different size, but the classifier and mask heads, the small networks that make the predictions, need the same small grid of features for every one. RoIAlign produces that grid by sampling the feature map at exact, unrounded positions and blending neighbouring values with bilinear interpolation. It keeps each feature aligned with the part of the image it came from, which matters most when the output must be exact to the pixel.
Builds on: Max Pooling, Bilinear Interpolation, Object Detection · Leads to: the mask head of Mask R-CNN, and keypoint detectors built on it
Where it's used
Instance segmentation. Mask R-CNN predicts a small mask for each detected object, and the mask must line up with the object's outline. The paper introduced RoIAlign for exactly this reason, because its predecessor's rounding error was too large for pixel-accurate masks.
Object detection. Even when no mask is predicted, RoIAlign improves box accuracy. The Mask R-CNN ablations show this, with the larger gains at strict overlap thresholds.
Keypoint detection. Locating the joints of a person needs features that sit exactly on the body part. The paper reports gains for keypoints as well, since each joint must sit on the right pixels.
How it works
Trace one tiny instance in one dimension. The feature map is the grid of numbers a network computes from an image. Here it has cells numbered 0 to 7, and cell holds the value , so the cells read 0, 1, 4, 9, 16, 25, 36, 49. Each cell covers 16 image pixels, and that ratio of 16 is the stride. The region of interest, or RoI, runs from image pixel 37 to pixel 99. We want two output values, one per bin, where a bin is one slot of the fixed-size output grid.
-
Convert the RoI to feature units. Divide the edges by the stride of 16. The left edge is and the right edge is . Nothing is rounded yet. Check: the RoI is 3.875 cells wide.
-
Split the RoI into bins. Two bins of equal width cover cells each. Bin 1 is and bin 2 is . Bin centres are and . Check: the two bin widths add up to the RoI width.
-
Sample each bin with bilinear interpolation. For this tiny case, take one sample at each bin centre. A sample at between cells and is the blend of those two cells, weighted by how far is past the left one.
- At , the cells are and , with values 9 and 16. The fraction past cell 3 is , so the value is .
- At , the cells are and , with values 25 and 36. The fraction is , so the value is . Check: each sample lies between its two neighbouring cell values.
-
Write the output. RoIAlign returns .
For comparison, RoIPool rounds the edges first. becomes 2 and becomes 6, so the RoI covers cells 2 to 5, split into bins of two cells each. Bin 1 holds cells 2 and 3, with values 4 and 9, and its maximum is 9. Bin 2 holds cells 4 and 5, with values 16 and 25, and its maximum is 25. RoIPool returns . The two methods disagree because RoIPool moved the RoI by a fraction of a cell, and the values it reads are no longer the ones under the bin.
flowchart LR
A["RoI in image pixels"] --> B["Divide by stride, keep fractions"]
B --> C["Split into a fixed grid of bins"]
C --> D["Sample each bin with bilinear interpolation"]
D --> E["Aggregate samples per bin, max or average"]
E --> F["Fixed-size feature grid for the head"]
The key idea is step 1. Keeping the fractional edges is what allows each output value to line up with the image.
The math
Let be a feature map with stride , and let an RoI in image coordinates be . RoIAlign maps the RoI to without rounding, divides it into bins, samples each bin at several fixed points, computes each sample by bilinear interpolation of , and aggregates the samples in each bin by their maximum or their average.
Step 1 in symbols: no quantisation
RoIPool computes and rounds the bin edges to whole cells. RoIAlign computes and keeps the fraction. In the example, and , so the edge is .
Step 2 in symbols: the bins
For a bin of width in feature units, the bin starts at for . In the example, , so the second bin starts at .
Step 3 in symbols: bilinear interpolation
For a sample at position , let and . Then
This is the one-dimensional form, and the two-dimensional version blends four cells in the same way. At , and , so .
Step 4 in symbols: the output
For bin with samples , the output is
or the maximum over instead of the average. The aggregation can be a maximum or an average. The paper reports that its results are insensitive to the exact sampling locations and to the number of points sampled, as long as no rounding happens.
Worked examples
Use the feature values 0, 1, 4, 9, 16, 25 for cells 0 to 5, and a stride of 16. The RoI runs from image pixel 16 to pixel 64, with one bin.
- The RoI in feature units is to .
- The single bin is , with centre .
- The sample at lies between cells 2 and 3, with values 4 and 9. Fraction .
- The value is .
Interpretation: when the RoI edges are exact multiples of the stride, RoIPool and RoIAlign agree on the edges, but RoIAlign still interpolates the centre sample, so it can return values that RoIPool would not.
Use the same feature values and stride as the main walkthrough, with the RoI in feature units and two bins. Take two samples per bin, at one quarter and three quarters of the bin's width, and average them.
- Bin 1 is with width .
- Quarter point: . Its cells are 2 and 3, with fraction . Value: .
- Three-quarter point: . Its cells are 3 and 4, with fraction . Value: .
- Bin 1 average: .
- Bin 2 is with width . Quarter point: . Its cells are 4 and 5, with fraction . Value: .
- Bin 2 three-quarter point: . Its cells are 5 and 6, with fraction . Value: .
- Bin 2 average: .
Interpretation: two samples per bin give a value that represents the whole bin, not one point. The output is , close to the walkthrough's , because the feature map changes smoothly across these cells.
A pedestrian's box runs from image pixel 205 to pixel 245, which is 40 pixels wide. The backbone, the network that computes the feature map, has stride 16, and the box head uses a grid of bins, the size the paper uses for boxes.
- In feature units the left edge is and the right edge is . The true width is cells, which is 40 pixels.
- RoIPool rounds the edges to 13 and 15. The left edge moves right by cells, which is 3 pixels. The right edge moves left by cells, which is 5 pixels.
- RoIPool's width is now cells, which is 32 pixels. That is 20 percent narrower than the pedestrian, and every bin is misplaced by the same kind of error.
- RoIAlign keeps the width at cells, so each of its 7 bins is cells wide, with its samples at the true positions.
- For the pixel-level mask, a 3 to 5 pixel shift at the edge is the difference between a mask that covers the person's arm and one that cuts it off.
Try it: a box runs from image pixel 50 to pixel 130 with stride 16. What feature-space left edge does RoIAlign use, and what does RoIPool use instead? Answer: RoIAlign uses . RoIPool rounds it to 3, which moves the edge 0.125 cells, or 2 pixels, to the left.
Common mistakes and tradeoffs
It is tempting to think that rounding is harmless, since a detector only needs a rough box. In fact the head that classifies a box is robust to small shifts, but the mask head is not, and the paper shows RoIPool's misalignment costs the most at strict overlap thresholds. Rounding that is fine for a label becomes a visible error on a mask.
It is tempting to think that RoIAlign only needs more samples. In fact the paper reports that the result is insensitive to the number of samples, as long as nothing is quantised. The gain comes from removing rounding, not from sampling more.
It is tempting to think that bilinear sampling alone is enough. In fact the paper compares against RoIWarp, which samples bilinearly but still quantises the RoI, and it performs on par with RoIPool. Alignment requires both steps, no rounding of the edges and bilinear sampling of the values.
When to use it: when the head predicts something that must match the image pixel by pixel, such as a mask, a keypoint or a precise box. When not to use it: when only a class label is needed and speed matters more, since RoIPool is simpler and close enough for classification. Alternatives include RoIWarp, which the paper shows does not solve the problem, and features taken at a single fixed size per object.
History
Detectors such as Fast R-CNN, from 2015, pooled features from each proposed region with RoIPool, which divided the region into a fixed grid and took the maximum in each cell after rounding the edges to whole cells. That made it cheap and simple, and it worked for classification.
In 2017 Kaiming He, Georgia Gkioxari, Piotr Dollar and Ross Girshick added a mask branch to Faster R-CNN in Mask R-CNN. They found that the mask predictions were poor because the rounding in RoIPool misaligned the features with the image, and they proposed RoIAlign to remove it. The change is small, and it caught on because one operation improved masks, boxes and keypoints alike.
Recap
- A proposed region is a box with fractional edges in feature units, and a head needs a fixed-size grid from it.
- RoIPool rounds the edges and the bins, which shifts the features away from the object.
- RoIAlign keeps the fractional edges, splits the region into bins, and samples each bin with bilinear interpolation.
- Averaging or taking the maximum over the samples gives one value per bin, and no rounding enters the computation.
- The gain is largest for masks and keypoints, where the output must line up with the image pixel by pixel.
Read next: Mask R-CNN, which uses RoIAlign in its mask branch, and Region Proposal Network, which supplies the boxes that RoIAlign pools from.
Check yourself
QuestionRecall: what is the difference between RoIPool and RoIAlign in how they handle the edges of a region?Show answer
RoIPool rounds the region's edges and bin boundaries to whole feature cells, so the features it reads can be shifted. RoIAlign keeps the fractional positions and samples the feature map there with bilinear interpolation.
QuestionComputation: a region's left edge is at image pixel 75 with stride 16. What feature-space value does RoIAlign use for that edge, and what does RoIPool round it to?Show answer
RoIAlign uses . RoIPool rounds this to 5, the nearest whole cell.
QuestionWhy: why does RoIAlign improve masks more than it improves class labels?Show answer
A class label depends on the overall appearance of the object, which is robust to a shift of a fraction of a cell. A mask must match the object's outline pixel by pixel, so the same shift that is harmless for a label puts the mask boundary in the wrong place.
QuestionApplied: a segmentation model's masks are consistently a few pixels too small on the left side of each object. Which step of the pipeline would you check first, and why?Show answer
Check how the region's edges are converted to feature units and whether they are rounded. A left edge that is rounded to the right would cut the mask on that side, which is the pattern described. Replacing the rounding with a fractional position, as RoIAlign does, would test the hypothesis.
Sources
- He, Gkioxari, Dollar and Girshick, "Mask R-CNN" (2017), which introduced RoIAlign: https://arxiv.org/abs/1703.06870
- Ren, He, Girshick and Sun, "Faster R-CNN" (2015): https://arxiv.org/abs/1506.01497
- Bilinear interpolation, Wikipedia: https://en.wikipedia.org/wiki/Bilinear_interpolation