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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mAP calculation for areas with 0 predictions #1080

Merged
merged 3 commits into from
Jun 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -40,7 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

-
- Fixed mAP calculation for areas with 0 predictions ([#1080](https://github.com/PyTorchLightning/metrics/pull/1080))


-
Expand Down
23 changes: 22 additions & 1 deletion tests/detection/test_map.py
Expand Up @@ -462,7 +462,7 @@ def test_missing_gt():


@pytest.mark.skipif(_pytest_condition, reason="test requires that torchvision=>0.8.0 is installed")
def test_segm_iou_empty_mask():
def test_segm_iou_empty_gt_mask():
"""Test empty ground truths."""
metric = MeanAveragePrecision(iou_type="segm")

Expand All @@ -482,6 +482,27 @@ def test_segm_iou_empty_mask():
metric.compute()


@pytest.mark.skipif(_pytest_condition, reason="test requires that torchvision=>0.8.0 is installed")
def test_segm_iou_empty_pred_mask():
"""Test empty predictions."""
metric = MeanAveragePrecision(iou_type="segm")

metric.update(
[
dict(
masks=torch.BoolTensor([]),
scores=Tensor([]),
labels=IntTensor([]),
),
],
[
dict(masks=torch.randint(0, 1, (1, 10, 10)).bool(), labels=IntTensor([4])),
],
)

metric.compute()


@pytest.mark.skipif(_pytest_condition, reason="test requires that torchvision=>0.8.0 is installed")
def test_error_on_wrong_input():
"""Test class input validation."""
Expand Down
4 changes: 2 additions & 2 deletions torchmetrics/detection/mean_ap.py
Expand Up @@ -482,9 +482,9 @@ def __evaluate_image_gt_no_preds(
) -> Dict[str, Any]:
"""Some GT but no predictions."""
# GTs
gt = gt[gt_label_mask]
gt = [gt[i] for i in gt_label_mask]
nb_gt = len(gt)
areas = box_area(gt)
areas = compute_area(gt, iou_type=self.iou_type).to(self.device)
ignore_area = (areas < area_range[0]) | (areas > area_range[1])
gt_ignore, _ = torch.sort(ignore_area.to(torch.uint8))
gt_ignore = gt_ignore.to(torch.bool)
Expand Down