Skip to content

Commit

Permalink
Add better errors when wrong input is received in detection intersect…
Browse files Browse the repository at this point in the history
…ion metrics (#2577)

* checks
* tests
* changelog

(cherry picked from commit f1a2be7)
  • Loading branch information
SkafteNicki authored and Borda committed Aug 2, 2024
1 parent 4e8290c commit 2792da5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

-

- Added better error messages for intersection detection metrics for wrong user input ([#2577](https://github.com/Lightning-AI/torchmetrics/pull/2577))


### Changed

-
Expand Down
5 changes: 5 additions & 0 deletions src/torchmetrics/functional/detection/ciou.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
def _ciou_update(
preds: torch.Tensor, target: torch.Tensor, iou_threshold: Optional[float], replacement_val: float = 0
) -> torch.Tensor:
if preds.ndim != 2 or preds.shape[-1] != 4:
raise ValueError(f"Expected preds to be of shape (N, 4) but got {preds.shape}")
if target.ndim != 2 or target.shape[-1] != 4:
raise ValueError(f"Expected target to be of shape (N, 4) but got {target.shape}")

from torchvision.ops import complete_box_iou

iou = complete_box_iou(preds, target)
Expand Down
5 changes: 5 additions & 0 deletions src/torchmetrics/functional/detection/diou.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
def _diou_update(
preds: torch.Tensor, target: torch.Tensor, iou_threshold: Optional[float], replacement_val: float = 0
) -> torch.Tensor:
if preds.ndim != 2 or preds.shape[-1] != 4:
raise ValueError(f"Expected preds to be of shape (N, 4) but got {preds.shape}")
if target.ndim != 2 or target.shape[-1] != 4:
raise ValueError(f"Expected target to be of shape (N, 4) but got {target.shape}")

from torchvision.ops import distance_box_iou

iou = distance_box_iou(preds, target)
Expand Down
5 changes: 5 additions & 0 deletions src/torchmetrics/functional/detection/giou.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
def _giou_update(
preds: torch.Tensor, target: torch.Tensor, iou_threshold: Optional[float], replacement_val: float = 0
) -> torch.Tensor:
if preds.ndim != 2 or preds.shape[-1] != 4:
raise ValueError(f"Expected preds to be of shape (N, 4) but got {preds.shape}")
if target.ndim != 2 or target.shape[-1] != 4:
raise ValueError(f"Expected target to be of shape (N, 4) but got {target.shape}")

from torchvision.ops import generalized_box_iou

iou = generalized_box_iou(preds, target)
Expand Down
6 changes: 6 additions & 0 deletions src/torchmetrics/functional/detection/iou.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
def _iou_update(
preds: torch.Tensor, target: torch.Tensor, iou_threshold: Optional[float], replacement_val: float = 0
) -> torch.Tensor:
"""Compute the IoU matrix between two sets of boxes."""
if preds.ndim != 2 or preds.shape[-1] != 4:
raise ValueError(f"Expected preds to be of shape (N, 4) but got {preds.shape}")
if target.ndim != 2 or target.shape[-1] != 4:
raise ValueError(f"Expected target to be of shape (N, 4) but got {target.shape}")

from torchvision.ops import box_iou

iou = box_iou(preds, target)
Expand Down
14 changes: 14 additions & 0 deletions tests/unittests/detection/test_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,20 @@ def test_error_on_wrong_input(self, class_metric, functional_metric, reference_m
[{"boxes": Tensor(), "labels": []}],
)

def test_functional_error_on_wrong_input_shape(self, class_metric, functional_metric, reference_metric):
"""Test functional input validation."""
with pytest.raises(ValueError, match="Expected preds to be of shape.*"):
functional_metric(torch.randn(25), torch.randn(25, 4))

with pytest.raises(ValueError, match="Expected target to be of shape.*"):
functional_metric(torch.randn(25, 4), torch.randn(25))

with pytest.raises(ValueError, match="Expected preds to be of shape.*"):
functional_metric(torch.randn(25, 25), torch.randn(25, 4))

with pytest.raises(ValueError, match="Expected target to be of shape.*"):
functional_metric(torch.randn(25, 4), torch.randn(25, 25))


def test_corner_case():
"""See issue: https://github.com/Lightning-AI/torchmetrics/issues/1921."""
Expand Down

0 comments on commit 2792da5

Please sign in to comment.