Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect IntersectionOverUnion calculations when predictions or targets contain empty boxes #2805

Closed
yurithefury opened this issue Oct 28, 2024 · 1 comment · Fixed by #2806
Labels
bug / fix Something isn't working help wanted Extra attention is needed

Comments

@yurithefury
Copy link

🐛 Bug

IntersectionOverUnion appears to mishandle cases where targets or predictions contain empty bounding boxes

To Reproduce

targets = [
    {
       "boxes": torch.FloatTensor([[0.4, 0.4, 0.5, 0.5], [0.6, 0.6, 0.7, 0.7]]),
        "labels": torch.LongTensor([1, 2]),
    },
    {
        "boxes": torch.FloatTensor([[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.4, 0.4]]),
        "labels": torch.LongTensor([1, 2]),
    },
]

preds = [
    {
        "boxes": torch.FloatTensor([[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.4, 0.4]]),
        "labels": torch.LongTensor([1, 2]),
        "scores": torch.FloatTensor([0.9, 0.8]),
    },
    {
        "boxes": torch.FloatTensor([[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.4, 0.4]]),
        "labels": torch.LongTensor([1, 2]),
        "scores": torch.FloatTensor([0.9, 0.8]),
    },
]
iou = IntersectionOverUnion()
iou.update(preds, targets)
result = iou.compute()

The code above correctly returns IoU of 0.5, but if the first targets item contains empty bounding boxes, the result unexpectedly changes to 1:

targets = [
    {
        "boxes": torch.empty(size=(0, 4), dtype=torch.float32),
        "labels": torch.tensor([], dtype=torch.long),
    },
    {
        "boxes": torch.FloatTensor([[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.4, 0.4]]),
        "labels": torch.LongTensor([1, 2]),
    },
]

preds = [
    {
        "boxes": torch.FloatTensor([[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.4, 0.4]]),
        "labels": torch.LongTensor([1, 2]),
        "scores": torch.FloatTensor([0.9, 0.8]),
    },
    {
        "boxes": torch.FloatTensor([[0.1, 0.1, 0.2, 0.2], [0.3, 0.3, 0.4, 0.4]]),
        "labels": torch.LongTensor([1, 2]),
        "scores": torch.FloatTensor([0.9, 0.8]),
    },
]
iou = IntersectionOverUnion()
iou.update(preds, targets)
result = iou.compute()

This seems incorrect, as the model made inaccurate predictions in both cases. The same issue arises when targets and predictions are swapped.

Expected behavior

The IoU value should be 0.5 when predictions or targets contain empty boxes.

Environment

  • torchmetrics=1.5.1
  • python=3.12, pytorch=2.5.0
  • Windows 11
@yurithefury yurithefury added bug / fix Something isn't working help wanted Extra attention is needed labels Oct 28, 2024
Copy link

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

@yurithefury yurithefury changed the title Incorrect IntersectionOverUnion calculations when predictions or targets are empty Incorrect IntersectionOverUnion calculations when predictions or targets contain empty boxes Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant