Back to paper library
arXiv 2020ECCV 2020Retail visionPaper notes

PIoU Loss

Every Pixel Counts

Original paper

PIoU Loss: Towards Accurate Oriented Object Detection in Complex Environments

Paper authors

Zhiming Chen · Kean Chen · Weiyao Lin …

Show all 7 authors

Zhiming Chen · Kean Chen · Weiyao Lin · John See · Hui Yu · Yan Ke · Cong Yang

Publication
ECCV 2020Conference
First submitted to arXiv
arXiv:2007.09584
Cite this paper · BibTeX

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

@misc{retailproductpiou2020,
  title = {{PIoU Loss: Towards Accurate Oriented Object Detection in Complex Environments}},
  author = {Zhiming Chen and Kean Chen and Weiyao Lin and John See and Hui Yu and Yan Ke and Cong Yang},
  year = {2020},
  eprint = {2007.09584},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2007.09584}
}
On this page

Every level of a store shelf has a long, narrow shelf edge at the front, lined with price tags.

If you want to know how many levels a shelf photo contains and where each one starts and ends, the most direct approach is to box the edge of every shelf. These boxes are long and thin, with aspect ratios of 1 to 20 being nothing unusual; tilt the photo a little and the boxes tilt with it.

This paper comes from Clobotics, a retail AI company, and Shanghai Jiao Tong University. The authors found that rotated-box detectors trained the usual way at the time were especially inaccurate on long, thin objects like these, so they proposed a new loss function, PIoU Loss, and along the way released Retail50K, a dataset that targets shelf edges specifically.

Problem Definition

An oriented bounding box (OBB) is described by five numbers: the center (cx,cy)(c_x, c_y), the width and height (w,h)(w, h), and the angle θ\theta. Most rotated-box detectors at the time added an angle dimension to a horizontal-box detector and then regressed each of the five numbers separately with a distance loss such as L1 or SmoothL1.

The problem is that distance losses are only loosely tied to IoU.

Original Fig. 1: two pairs of predicted boxes (green) and annotated boxes (red). The pair at the top left has long, thin boxes slightly off in angle, with an IoU of only 0.35; the pair at the bottom right has shorter, stubbier boxes, with an IoU of 0.72. (a) With the SmoothL1 loss, both pairs have a loss of 0.57. (b) With the PIoU loss, the two pairs get 0.65 and 0.28, in line with their IoUs.
Original Fig. 1, Chen et al., from PDF page 2, arXiv license, scholarly quotation.

This figure makes the problem very clear. The two pairs of boxes have exactly the same SmoothL1 loss, 0.57, yet one has an IoU of 0.35 and the other 0.72.

The cause is the aspect ratio. For a very long box, a slight angle error swings the two ends farthest from the center way off, and IoU falls fast; but to SmoothL1, an angle error is just a number, and it doesn't care how long the box is. The thinner the box, the worse this mismatch gets.

So why not just use IoU as the loss? IoU losses for horizontal boxes (like GIoU) already existed, but the intersection of two rotated boxes can be an arbitrary polygon with up to eight sides, and writing a differentiable formula for it is a pain.

Solution

The authors' idea is down to earth. If the shape of the intersection is hard to compute, don't compute the shape; count it pixel by pixel instead.

First, Is the Pixel Inside the Box?

Original Fig. 3: (a) the geometry for deciding whether a pixel is inside a rotated box. The red rectangle is the rotated box, the red dot c is its center, the green dot p is the pixel being tested, and the purple dot t is the foot of the perpendicular dropped from p to the box's center line; c, p and t form a right triangle whose legs are d^w along the box's width and d^h along its height, with hypotenuse d, and β is the angle between d and the width direction. (b) Curves of the kernel function F(p|b) against the pixel's normalized distance from the center, with s at 0.5 or 0.7 and k at 10, 50 or 100; the larger k is, the closer the curve gets to a step that drops suddenly from 1 to 0.
Original Fig. 3, Chen et al., from PDF page 5, arXiv license, scholarly quotation.

Given a rotated box bb and a pixel pp, deciding whether pp is in the box only takes projecting the vector from pp to the box center onto the box's width and height directions.

Let the distance from pp to the center be dd and the angle between this vector and the box's width direction be β\beta; then the distance along the width is dw=∣dcos⁡β∣d^w = |d \cos\beta| and the distance along the height is dh=∣dsin⁡β∣d^h = |d \sin\beta|. β\beta is determined by the box angle θ\theta together with the pixel's bearing.

A pixel is inside the box when it doesn't go past half the side length in either direction:

δ(p∣b)={1,dw≤w2 ∧ dh≤h20,dw>w2 ∨ dh>h2\delta(p \mid b) = \begin{cases} 1, & d^w \le \dfrac{w}{2} \ \land\ d^h \le \dfrac{h}{2} \\ 0, & d^w > \dfrac{w}{2} \ \lor\ d^h > \dfrac{h}{2} \end{cases}

With this test, the intersection is the number of pixels both boxes call "inside", the union is the number of pixels at least one box calls "inside", and dividing one by the other gives IoU. The authors count pixels only within the smallest horizontal rectangle that covers both boxes, so there's no need to scan the whole image.

Making It Differentiable

The δ\delta above is a step function that jumps straight from 1 to 0 at the box boundary, with no gradient to pass back. The authors replace it with a sigmoid-shaped kernel:

K(d,s)=1−11+e−k(d−s)K(d, s) = 1 - \frac{1}{1 + e^{-k(d - s)}}

ss is the position of the boundary and kk controls how steep the curve is. Each pixel's contribution is the product of the kernels in the width and height directions, F(p∣b)=K(dw,⋅)⋅K(dh,⋅)F(p \mid b) = K(d^w, \cdot) \cdot K(d^h, \cdot).

The paper's formula writes ss as the full side lengths ww and hh; the official code actually uses half the side length, factor = -k * (dis_w - w / 2.), which matches the inside-the-box condition above.

Let's plug in a box. Say it's 40 pixels wide, so the half-width is 20, and k=10k = 10:

  • A pixel 19 pixels from the center along the width, still inside the box, gets K=1−1/(1+e10)≈0.99995K = 1 - 1/(1 + e^{10}) \approx 0.99995.
  • Exactly on the boundary, at 20 pixels, K=0.5K = 0.5.
  • 0.3 pixels outside, at 20.3 pixels, K=1−1/(1+e−3)≈0.047K = 1 - 1/(1 + e^{-3}) \approx 0.047.

So with k=10k = 10, apart from a band less than a pixel wide around the boundary, the kernel is essentially 0 or 1 everywhere. Its job is to give the pixels on the boundary a gradient, telling the network "push the box a little this way and you'll cover a few more pixels".

Most pixels inside the box have values close to 1, with fixed contributions and tiny gradients; the ring of pixels near the boundary is where the gradient comes from, and the positions of these pixels depend on all five parameters at once (center, width/height and angle). That's why PIoU can optimize the five parameters jointly.

The intersection and the union are both replaced by sums of kernels:

Sb∩b′≈∑pF(p∣b) F(p∣b′)Sb∪b′≈∑p[F(p∣b)+F(p∣b′)−F(p∣b) F(p∣b′)]\begin{aligned} S_{b \cap b'} &\approx \sum_{p} F(p \mid b)\, F(p \mid b') \\ S_{b \cup b'} &\approx \sum_{p} \big[F(p \mid b) + F(p \mid b') \\ &\qquad - F(p \mid b)\, F(p \mid b')\big] \end{aligned}

The union can be simplified one step further, as the areas of the two boxes minus the intersection, Sb∪b′=wh+w′h′−Sb∩b′S_{b \cup b'} = wh + w'h' - S_{b \cap b'}. The authors call this simplified version HPIoU.

The Loss

PIoU=Sb∩b′/Sb∪b′\text{PIoU} = S_{b \cap b'} / S_{b \cup b'}, and the loss takes its negative log, averaged over all positive pairs:

Lpiou=−1∣M∣∑(b,b′)∈Mln⁡PIoU(b,b′)L_{piou} = -\frac{1}{|M|} \sum_{(b, b') \in M} \ln \text{PIoU}(b, b')

MM is the set of positive pairs, which require an IoU above 0.5 between the anchor box and the annotated box. Since the kernel isn't truly 0 outside the box, PIoU stays above 0 even when two boxes don't intersect at all, so the gradient is still there. Set θ\theta to 0 and the loss works for horizontal boxes too.

The authors plug PIoU into two detectors, the anchor-based RefineDet and the anchor-free CenterNet; in both, the original box regression loss is replaced with PIoU and the classification loss is left alone. CenterNet's center points are decided by the heatmap, so PIoU's gradient isn't passed back to the center point.

Retail50K

Original Fig. 4: examples from three datasets. (a) DOTA is aerial imagery, a row of helicopters each with a blue rotated box; (b) HRSC2016 is also aerial, a few ships docked along a pier, boxed with long, thin yellow rotated boxes; (c) Retail50K is a supermarket shelf photo taken from an oblique side angle, with the front edge of every shelf level boxed as a long, thin red rotated box.
Original Fig. 4, Chen et al., from PDF page 8, arXiv license, scholarly quotation.

With Retail in the name, it's easy to assume it's a product dataset. In fact, Retail50K doesn't annotate products; it annotates the edge of every level of shelves, freezers and display racks, and the whole dataset has just this one class.

The authors give three reasons. Shelf edges are blocked by price tags and promotional labels, with an average occlusion of about 37.5%, against a background of brightly colored products; the box aspect ratios are extreme, with a median of 1 to 20, while 90% of the boxes in the aerial datasets commonly used at the time are within 1 to 4; and in practical applications, finding the shelf edges lets you split a shelf into levels, detect price tags, and estimate the camera's skew angle.

The data was shot by dozens of volunteers on their own phones in 20 supermarkets in China and the US, 47,000 images in total, all resized to 600×800. Annotation used arbitrary quadrilaterals first, which were then converted into rotated boxes. The training, validation and test split is 1/2, 1/6 and 1/3.

Discussion

Results on Retail50K

DetectorBackboneBase AP+ PIoUAP50 changeAP75 change
RefineDet-OBBResNet-5053.9661.7874.15 → 80.1733.77 → 43.39
RefineDet-OBBResNet-10155.4663.0077.05 → 79.0833.87 → 46.01
CenterNet-OBBResNet-1854.4461.0276.58 → 87.1932.29 → 34.85
CenterNet-OBBDLA-3456.1361.6478.29 → 88.4733.97 → 34.80

Source: Table 5 of the paper; AP is averaged over IoU from 0.5 to 0.95.

All four settings improve by 5.5 to 7.8 points. The loss is only used in training, so inference speed is exactly the same; CenterNet with ResNet-18 runs at 140 images per second.

Look closely at the last two columns and the two detectors improve in different ways. RefineDet gains mainly in AP75, meaning its boxes fit more tightly; CenterNet gains mainly in AP50, with AP75 up only 0.8 to 2.6 points. On the CenterNet side, PIoU's gradient isn't passed back to the center point, so the box position is set by the heatmap and PIoU can only adjust width, height and angle, which may be why its improvement at the strict threshold is limited. The paper doesn't discuss this difference.

Against Other Losses

On aerial DOTA, the authors keep RefineDet fixed and swap only the loss:

LossAPAP50AP75Train (h)
L150.6664.1437.1820
L249.7062.7436.6520
SmoothL151.4665.6837.2521.5
PIoU54.2467.8940.5925.7
HPIoU53.3766.3840.3624.8

Source: Table 3 of the paper.

PIoU beats SmoothL1 by 2.8 points, at the cost of about 20% more training time. The simplified HPIoU saves some computation and scores only 0.9 lower.

The choice of kk matters quite a bit too. With k=5k = 5, AP is only 46.88; k=10k = 10 gives 54.24, and k=15k = 15 gives 53.41. A reasonable explanation is that when kk is too small, the kernel blurs the line between inside and outside the box, and the computed IoU drifts from the true value; when it's too large, the kernel approaches a step function, and the gradient concentrates on too narrow a ring of pixels. The paper only reports the numbers and doesn't analyze the cause.

On the DOTA test set, PIoU improves each of three settings by 1.4 to 1.6 points, much less than the 5.5 to 7.8 points on Retail50K. The authors see this as evidence that PIoU is especially effective on high-aspect-ratio objects; the DOTA classes that benefit more are also long, thin targets such as harbors, ground track fields and soccer fields.

It works for horizontal boxes too. With SSD on PASCAL VOC, SmoothL1 gets 48.8, GIoU 49.9 and PIoU 50.3.

Can You Get Retail50K?

The paper says the dataset will be made public on GitHub. The repo does have Retail50K, in the form of a few CSV files where each row is an image URL plus annotation coordinates, and you have to download the images one by one yourself.

The rest of the story is in the issues. In August 2020, someone parsed the CSVs and found only 40,063 images across training and test, not the 47,000 the paper states; the paper mentions a validation set, but the CSVs have none; the two training CSVs share 2,987 duplicate images; and plotting the boxes revealed quite a few missing and wrong labels (issue #6). In 2023 someone else reported missing labels, such as an entire bottom row of price tags left unannotated in one image (issue #20); the same year, someone said the server reset the connection halfway through the download (issue #19).

None of these issues got a single reply from a maintainer. The repo's last update was on July 21, 2020, two days after the paper went up on arXiv (paper's out, shift's over). We tried the URL of the image posted in issue #6; connecting in November 2026, it redirects to a 404.

Somewhat ironically, in the issues of the previous paper, DRN, someone complained that DRN was only half open-sourced, and the suggestion underneath was "use PIoU, I've already reproduced it". PIoU's loss function code is indeed complete, with both CPU and CUDA versions; the dataset wasn't so lucky.

Conclusion

The problem PIoU solves is well defined. Distance losses don't care how long a box is; IoU does. With per-pixel accumulation and a sigmoid kernel, the authors sidestep the hassle of rotated-box intersections being arbitrary polygons, so IoU can be used directly as a loss and pass back gradients. The experiments also back the claim that "the thinner the box, the more it helps": 5.5 to 7.8 points of improvement on Retail50K, with its median aspect ratio of 1 to 20, versus only about 1.5 points on DOTA.

Long, thin targets that rotate, such as shelf edges, price-tag strips and text lines, are all good candidates for trying PIoU. It adds no cost at inference and about 20% more training time. If you implement it yourself, go by the official code and use half the side length for the kernel boundary; if you use HPIoU's simplified union but compute the intersection with the full side length as in the paper's formula, the areas on the two sides won't match.

As for Retail50K, before using it as a benchmark, you'd do well to count how many images you can actually download.