torchcv icon indicating copy to clipboard operation
torchcv copied to clipboard

Corner case of box_nms

Open DHPO opened this issue 5 years ago • 1 comments

https://github.com/kuangliu/torchcv/blob/6291f3e1e4bbf6467fd6b1e79001d34a59481bb6/torchcv/utils/box.py#L128 When overlap contains only 1 element, ids will be a 0-dimensional tensor, which will cause dimension mismatch later. Examples:

>>> (torch.Tensor([0.1, 0.2, 0.6]) < 0.45).nonzero().squeeze()
tensor([0, 1])
>>> (torch.Tensor([0.1]) < 0.45).nonzero().squeeze()
tensor(0)
>>> (torch.Tensor([0.6]) < 0.45).nonzero().squeeze()
tensor([], dtype=torch.int64)

DHPO avatar Mar 15 '19 07:03 DHPO

It's when (overlap<=threshold).nonzero() contains only 1 element, more accurately.

And adding dim=1 will fix it: ids = (overlap<=threshold).nonzero().squeeze(dim=1)

Examples:

>>> (torch.Tensor([0.6, 0.1, 0.2]) < 0.45).nonzero().squeeze(dim=1)
tensor([1, 2])
>>> (torch.Tensor([0.1, 0.6]) < 0.45).nonzero().squeeze(dim=1)
tensor([0])
>>> (torch.Tensor([0.6, 0.1]) < 0.45).nonzero().squeeze(dim=1)
tensor([1])
>>> (torch.Tensor([0.1]) < 0.45).nonzero().squeeze(dim=1)
tensor([0])

DHPO avatar Mar 15 '19 08:03 DHPO