Lightning-AI / torchmetrics

Machine learning metrics for distributed, scalable PyTorch applications.
https://lightning.ai/docs/torchmetrics/
Apache License 2.0
2.11k stars 401 forks source link

Result of IoU is Nan when there is empty item in preds. #2778

Open joshuasv opened 2 hours ago

joshuasv commented 2 hours ago

🐛 Bug

To Reproduce

The target is:

[
 {'boxes': tensor([[  8.0000,  70.0000,  76.0000, 110.0000],
                   [247.0000, 131.0000, 315.0000, 175.0000],
                   [361.0000, 177.0000, 395.0000, 203.0000]]), 'labels': tensor([0, 0, 0])}
]

The preds is:

[
{'boxes': empty(size=(0, 4)), 'labels': tensor([], dtype=torch.int64), 'scores': tensor([])}
]

The last item of preds is empty, when using torchmetrics.detection.iou.IntersectionOverUnion to calculate IoU, the result is nan.

Code sample ```python import torch from torch import tensor from torch import empty from torchmetrics.detection import IntersectionOverUnion def calc_iou(preds: list, target: list): metric = IntersectionOverUnion() metric.update(preds, target) result = metric.compute() return result target = [ {'boxes': tensor([[ 8.0000, 70.0000, 76.0000, 110.0000], [247.0000, 131.0000, 315.0000, 175.0000], [361.0000, 177.0000, 395.0000, 203.0000]]), 'labels': tensor([0, 0, 0])} ] preds = [ {'boxes': empty(size=(0, 4)), 'labels': tensor([], dtype=torch.int64), 'scores': tensor([])} ] calc_iou(preds, target) ``` ```bash {'iou': tensor(nan)} ```

Expected behavior

A correct iou value, i.e: 0.0

Environment

Additional context

github-actions[bot] commented 2 hours ago

Hi! thanks for your contribution!, great first issue!