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

fix: Properly checks if ground truths are empty #684

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions tests/detection/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ def test_empty_preds():
metric.compute()


@pytest.mark.skipif(_pytest_condition, reason="test requires that torchvision=>0.8.0 is installed")
def test_empty_ground_truths():
"""Test empty ground truths."""
metric = MAP()

metric.update(
[
dict(
boxes=torch.Tensor([[214.1500, 41.2900, 562.4100, 285.0700]]),
scores=torch.Tensor([0.5]),
labels=torch.IntTensor([4]),
),
],
[
dict(boxes=torch.Tensor([]), labels=torch.IntTensor([])),
],
)
metric.compute()


_gpu_test_condition = not torch.cuda.is_available()


Expand Down
4 changes: 2 additions & 2 deletions torchmetrics/detection/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def _compute_iou(self, id: int, class_id: int, max_det: int) -> Tensor:
det = self.detection_boxes[id]
gt_label_mask = self.groundtruth_labels[id] == class_id
det_label_mask = self.detection_labels[id] == class_id
if len(det_label_mask) == 0 or len(det_label_mask) == 0:
if len(gt_label_mask) == 0 or len(det_label_mask) == 0:
return Tensor([])
gt = gt[gt_label_mask]
det = det[det_label_mask]
Expand Down Expand Up @@ -396,7 +396,7 @@ def _evaluate_image(
det = self.detection_boxes[id]
gt_label_mask = self.groundtruth_labels[id] == class_id
det_label_mask = self.detection_labels[id] == class_id
if len(det_label_mask) == 0 or len(det_label_mask) == 0:
if len(gt_label_mask) == 0 or len(det_label_mask) == 0:
return None
gt = gt[gt_label_mask]
det = det[det_label_mask]
Expand Down