|
| 1 | +--- |
| 2 | +hide_title: true |
| 3 | +sidebar_label: IoU3D |
| 4 | +--- |
| 5 | + |
| 6 | +# Intersection Over Union of Oriented 3D Boxes: A New Algorithm |
| 7 | + |
| 8 | +Author: Georgia Gkioxari |
| 9 | + |
| 10 | +Implementation: Georgia Gkioxari and Nikhila Ravi |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +Intersection over union (IoU) of boxes is widely used as an evaluation metric in object detection ([1][pascalvoc], [2][coco]). |
| 15 | +In 2D, IoU is commonly applied to axis-aligned boxes, namely boxes with edges parallel to the image axis. |
| 16 | +In 3D, boxes are usually not axis aligned and can be oriented in any way in the world. |
| 17 | +We introduce a new algorithm which computes the *exact* IoU of two *oriented 3D boxes*. |
| 18 | + |
| 19 | +Our algorithm is based on the simple observation that the intersection of two oriented boxes, `box1` and `box2`, is a convex n-gon with `n > 2` comprised of connected *planar units*. |
| 20 | +In 3D, these planar units are 3D triangular faces. |
| 21 | +In 2D, they are 2D edges. |
| 22 | +Each planar unit belongs strictly to either `box1` or `box2`. |
| 23 | +Our algorithm finds these units by iterating through the sides of each box. |
| 24 | + |
| 25 | +1. For each 3D triangular face `e` in `box1` we check wether `e` is *inside* `box2`. |
| 26 | +2. If `e` is not *inside*, then we discard it. |
| 27 | +3. If `e` is *inside* or *partially inside*, then the part of `e` *inside* `box2` is added to the units that comprise the final intersection shape. |
| 28 | +4. We repeat for `box2`. |
| 29 | + |
| 30 | +Below, we show a visualization of our algorithm for the case of 2D oriented boxes. |
| 31 | + |
| 32 | +<p align="center"> |
| 33 | +<img src="assets/iou3d.gif" alt="drawing" width="400"/> |
| 34 | +</p> |
| 35 | + |
| 36 | +Note that when a box's unit `e` is *partially inside* a `box` then `e` breaks into smaller units. In 2D, `e` is an edge and breaks into smaller edges. In 3D, `e` is a 3D triangular face and is clipped to more and smaller faces by the plane of the `box` it intersects with. |
| 37 | +This is the sole fundamental difference between the algorithms for 2D and 3D. |
| 38 | + |
| 39 | +## Comparison With Other Algorithms |
| 40 | + |
| 41 | +Current algorithms for 3D box IoU rely on crude approximations or make box assumptions, for example they restrict the orientation of the 3D boxes. |
| 42 | +[Objectron][objectron] provides a nice discussion on the limitations of prior works. |
| 43 | +[Objectron][objectron] introduces a great algorithm for exact IoU computation of oriented 3D boxes. |
| 44 | +Objectron's algorithm computes the intersection points of two boxes using the [Sutherland-Hodgman algorithm][clipalgo]. |
| 45 | +The intersection shape is formed by the convex hull from the intersection points, using the [Qhull library][qhull]. |
| 46 | + |
| 47 | +Our algorithm has several advantages over Objectron's: |
| 48 | + |
| 49 | +1. Our algorithm also computes the points of intersection, similar to Objectron, but in addition stores the *planar units* the points belong to. This eliminates the need for convex hull computation which is `O(nlogn)` and relies on a third party library which often crashes with nondescript error messages. |
| 50 | +2. Objectron's implementation assumes that boxes are a rotation away from axis aligned. Our algorithm and implementation makes no such assumption and works for any 3D boxes. |
| 51 | +3. Our implementation supports batching, unlike Objectron which assumes single element inputs for `box1` and `box2`. |
| 52 | +4. Our implementation is easily parallelizable and in fact we provide a custom C++/CUDA implementation which is **450 times faster than Objectron**. |
| 53 | + |
| 54 | +Below we compare the performance for Objectron (in C++) and our algorithm, in C++ and CUDA. We benchmark for a common use case in object detection where `boxes1` hold M predictions and `boxes2` hold N ground truth 3D boxes in an image. We compute the `MxN` IoU matrix and report the time in ms for `M=N=16`. |
| 55 | + |
| 56 | +<p align="center"> |
| 57 | +<img src="assets/iou3d_comp.png" alt="drawing" width="400"/> |
| 58 | +</p> |
| 59 | + |
| 60 | +## Usage and Code |
| 61 | + |
| 62 | +```python |
| 63 | +from pytorch3d.ops import box3d_overlap |
| 64 | +# Assume inputs: boxes1 (M, 8, 3) and boxes2 (N, 8, 3) |
| 65 | +intersection_vol, iou_3d = box3d_overal(boxes1, boxes2) |
| 66 | +``` |
| 67 | + |
| 68 | +For more details, read [iou_box3d.py](https://github.com/facebookresearch/pytorch3d/blob/main/pytorch3d/ops/iou_box3d.py). |
| 69 | + |
| 70 | +Note that our implementation is not differentiable as of now. We plan to add gradient support soon. |
| 71 | + |
| 72 | +We also include have extensive [tests](https://github.com/facebookresearch/pytorch3d/blob/main/tests/test_iou_box3d.py) comparing our implementation with Objectron and MeshLab. |
| 73 | + |
| 74 | + |
| 75 | +## Cite |
| 76 | + |
| 77 | +If you use our 3D IoU algorithm, please cite PyTorch3D |
| 78 | + |
| 79 | +```bibtex |
| 80 | +@article{ravi2020pytorch3d, |
| 81 | + author = {Nikhila Ravi and Jeremy Reizenstein and David Novotny and Taylor Gordon |
| 82 | + and Wan-Yen Lo and Justin Johnson and Georgia Gkioxari}, |
| 83 | + title = {Accelerating 3D Deep Learning with PyTorch3D}, |
| 84 | + journal = {arXiv:2007.08501}, |
| 85 | + year = {2020}, |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +[pascalvoc]: http://host.robots.ox.ac.uk/pascal/VOC/ |
| 90 | +[coco]: https://cocodataset.org/ |
| 91 | +[objectron]: https://arxiv.org/abs/2012.09988 |
| 92 | +[qhull]: http://www.qhull.org/ |
| 93 | +[clipalgo]: https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm |
0 commit comments