Lightning-AI/torchmetrics

`intersection_over_union` error

bennymi opened this issue ยท 2 comments

๐Ÿ› Bug

Trying to copy over the Jaccard Index example to use with the functional IoU, results in the following error:

RuntimeError: The size of tensor a (23) must match the size of tensor b (2) at non-singleton dimension 2

To Reproduce

Steps to reproduce the behavior...

from torchmetrics.functional.detection import intersection_over_union
import torch

target = torch.randint(0, 2, (10, 25, 25))
preds = torch.tensor(target)
preds[2:5, 7:13, 9:15] = 1 - preds[2:5, 7:13, 9:15]

intersection_over_union(preds=preds[2], target=target[2])

Expected behavior

Expecting to get the same prediction as in the Jaccard Index example.

Environment

  • TorchMetrics version (and how you installed TM, e.g. conda, pip, build from source):
    • installed with pip: torchmetrics 1.2.1
  • Python & PyTorch Version (e.g., 1.0):
    • Python 3.11.6 + PyTorch 2.1.0
  • Any other relevant information such as OS (e.g., Linux):
    • Windows 10

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

Hi @bennymi, thanks for raising this issue.
If you look at the documentation for the functional interface of intersection_over_union you will see that the input for this metric is expected to be bounding box coordinates and not raw predicted labels. The difference in expected input is one reason why this metric is not in the classification domain but instead in the detection domain.
PR #2577 will add better error messages when trying to use the metric.