votenet
votenet copied to clipboard
3D bounding box IoU error - buggy Convex Hull Intersection
I am running into a potential bug in the box3d_iou computation.
Steps to reproduce
import sys
import numpy as np
sys.path.append('./utils/')
from box_util import box3d_iou
box = [[12.05 , 3.358, -1.927],
[12.05 , 3.358, -2.309],
[11.744, 3.358, -2.309],
[11.744, 3.358, -1.927],
[12.05 , 3.168, -1.927],
[12.05 , 3.168, -2.309],
[11.744, 3.168, -2.309],
[11.744, 3.168, -1.927],]
box = np.array(box)
iou, iou_2d = box3d_iou(box,box)
print(iou, iou_2d)
>> 1.4911757425289889 1.4911757425290015
Expected behavior
The 3D IoU of a box with itself should be 1.0
Additional info
The issue is coming from the convex_hull_intersection() function.
For the two rectangles defined here, that function returns an area greater than the area of each rectangle.
Fix
Switching to shapely fixed the issue.
box_utils.py - L92
def box3d_iou(corners1, corners2):
''' Compute 3D bounding box IoU.
Input:
corners1: numpy array (8,3), assume up direction is negative Y
corners2: numpy array (8,3), assume up direction is negative Y
Output:
iou: 3D bounding box IoU
iou_2d: bird's eye view 2D bounding box IoU
todo (rqi): add more description on corner points' orders.
'''
from shapely.geometry import Polygon
# corner points are in counter clockwise order
rect1 = [[corners1[i,0], corners1[i,2]] for i in range(3,-1,-1)]
rect2 = [[corners2[i,0], corners2[i,2]] for i in range(3,-1,-1)]
poly1 = Polygon(rect1)
poly2 = Polygon(rect2)
area1 = poly1.area
area2 = poly2.area
inter_area = poly1.intersection(poly2).area
iou_2d = inter_area/(area1+area2-inter_area)
ymax = min(corners1[0,1], corners2[0,1])
ymin = max(corners1[4,1], corners2[4,1])
inter_vol = inter_area * max(0.0, ymax-ymin)
vol1 = box3d_vol(corners1)
vol2 = box3d_vol(corners2)
iou = inter_vol / (vol1 + vol2 - inter_vol)
return iou, iou_2d