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

Add better errors when wrong input is received in detection intersection metrics #2577

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `input_format` argument to segmentation metrics ([#2572](https://github.com/Lightning-AI/torchmetrics/pull/2572))


- 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
Loading