Closed
Description
Strange behaviour of pytorch3d.ops.box3d_overlap
First case - zero overlap expected, but non-zero value is returned (and also different values depending on the order of the arguments):
import torch
from pytorch3d.ops import box3d_overlap
a = torch.tensor([[-1.0000, -1.0000, -0.5000],
[ 1.0000, -1.0000, -0.5000],
[ 1.0000, 1.0000, -0.5000],
[-1.0000, 1.0000, -0.5000],
[-1.0000, -1.0000, 0.5000],
[ 1.0000, -1.0000, 0.5000],
[ 1.0000, 1.0000, 0.5000],
[-1.0000, 1.0000, 0.5000]])
b = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
print(box3d_overlap(a[None], b[None]))
print(box3d_overlap(b[None], a[None]))
Returns:
(tensor([[4.0000]]), tensor([[inf]]))
(tensor([[0.]]), tensor([[0.]]))
Second case:
import torch
from pytorch3d.ops import box3d_overlap
# function from https://github.com/facebookresearch/pytorch3d/blob/main/tests/test_iou_box3d.py
def create_box(xyz, whl):
x, y, z = xyz
w, h, le = whl
verts = torch.tensor(
[
[x - w / 2.0, y - h / 2.0, z - le / 2.0],
[x + w / 2.0, y - h / 2.0, z - le / 2.0],
[x + w / 2.0, y + h / 2.0, z - le / 2.0],
[x - w / 2.0, y + h / 2.0, z - le / 2.0],
[x - w / 2.0, y - h / 2.0, z + le / 2.0],
[x + w / 2.0, y - h / 2.0, z + le / 2.0],
[x + w / 2.0, y + h / 2.0, z + le / 2.0],
[x - w / 2.0, y + h / 2.0, z + le / 2.0],
],
device=xyz.device,
dtype=torch.float32,
)
return verts
ctrs = torch.tensor([[0., 0., 0.], [-1., 1., 0.]])
whl = torch.tensor([[2., 2., 2.], [2., 2, 2]])
box8a = create_box(ctrs[0], whl[0])
box8b = create_box(ctrs[1], whl[1])
print(box3d_overlap(box8a[None], box8b[None]))
Returns:
(tensor([[1.3704]]), tensor([[0.0937]]))
if you calculate the overlap area by hand, you get exactly 2, but the function gives a different result.
Moreover, if you slightly change the values, then the answer will be correct:
ctrs = torch.tensor([[0., 0., 0.], [-0.999999, 1., 0.]])
whl = torch.tensor([[2., 2., 2.], [2., 2, 2]])
box8a = create_box(ctrs[0], whl[0])
box8b = create_box(ctrs[1], whl[1])
print(box3d_overlap(box8a[None], box8b[None]))
ctrs = torch.tensor([[0., 0., 0.], [-1., 1., 0.]])
whl = torch.tensor([[2., 2., 2.], [2., 2, 2]])
box8a = create_box(ctrs[0], whl[0])
box8b = create_box(ctrs[1], whl[1])
print(box3d_overlap(box8a[None], box8b[None]))
ctrs = torch.tensor([[0., 0., 0.], [-1.000001, 1., 0.]])
whl = torch.tensor([[2., 2., 2.], [2., 2, 2]])
box8a = create_box(ctrs[0], whl[0])
box8b = create_box(ctrs[1], whl[1])
print(box3d_overlap(box8a[None], box8b[None]))
Returns:
(tensor([[2.0002]]), tensor([[0.1429]]))
(tensor([[1.3704]]), tensor([[0.0937]]))
(tensor([[1.9998]]), tensor([[0.1428]]))
Was checked with
torch == 1.6.0+cu101
pytorch3d==0.6.1