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

Fixed MAP metric when using custom list of thresholds #995

Merged
merged 6 commits into from
Apr 29, 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.
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 @@ -76,6 +76,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `CalibrationError` to work on logit input ([#985](https://github.com/PyTorchLightning/metrics/pull/985))


- Fixed MAP metric when using custom list of thresholds ([#995](https://github.com/PyTorchLightning/metrics/issues/995))


## [0.8.0] - 2022-04-14

### Added
Expand Down
13 changes: 13 additions & 0 deletions tests/detection/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ def test_map_gpu(inputs):
metric.compute()


@pytest.mark.skipif(_pytest_condition, reason="test requires that torchvision=>0.8.0 is installed")
@pytest.mark.skipif(_gpu_test_condition, reason="test requires CUDA availability")
def test_map_with_custom_thresholds():
"""Test that map works with custom iou thresholds."""
metric = MeanAveragePrecision(iou_thresholds=[0.1, 0.2])
metric = metric.to("cuda")
for preds, targets in zip(_inputs.preds, _inputs.target):
metric.update(_move_to_gpu(preds), _move_to_gpu(targets))
res = metric.compute()
assert res["map_50"].item() == -1
assert res["map_75"].item() == -1


@pytest.mark.skipif(_pytest_condition, reason="test requires that pycocotools and torchvision=>0.8.0 is installed")
def test_empty_metric():
"""Test empty metric."""
Expand Down
14 changes: 10 additions & 4 deletions torchmetrics/detection/mean_ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,14 @@ def _summarize_results(self, precisions: Tensor, recalls: Tensor) -> Tuple[MAPMe
map_metrics = MAPMetricResults()
map_metrics.map = self._summarize(results, True)
last_max_det_thr = self.max_detection_thresholds[-1]
map_metrics.map_50 = self._summarize(results, True, iou_threshold=0.5, max_dets=last_max_det_thr)
map_metrics.map_75 = self._summarize(results, True, iou_threshold=0.75, max_dets=last_max_det_thr)
if 0.5 in self.iou_thresholds:
map_metrics.map_50 = self._summarize(results, True, iou_threshold=0.5, max_dets=last_max_det_thr)
else:
map_metrics.map_50 = torch.tensor([-1])
if 0.75 in self.iou_thresholds:
map_metrics.map_75 = self._summarize(results, True, iou_threshold=0.75, max_dets=last_max_det_thr)
else:
map_metrics.map_75 = torch.tensor([-1])
map_metrics.map_small = self._summarize(results, True, area_range="small", max_dets=last_max_det_thr)
map_metrics.map_medium = self._summarize(results, True, area_range="medium", max_dets=last_max_det_thr)
map_metrics.map_large = self._summarize(results, True, area_range="large", max_dets=last_max_det_thr)
Expand Down Expand Up @@ -750,8 +756,6 @@ def compute(self) -> dict:
dict containing

- map: ``torch.Tensor``
- map_50: ``torch.Tensor``
- map_75: ``torch.Tensor``
- map_small: ``torch.Tensor``
- map_medium: ``torch.Tensor``
- map_large: ``torch.Tensor``
Expand All @@ -761,6 +765,8 @@ def compute(self) -> dict:
- mar_small: ``torch.Tensor``
- mar_medium: ``torch.Tensor``
- mar_large: ``torch.Tensor``
- map_50: ``torch.Tensor`` (-1 if 0.5 not in the list of iou thresholds)
- map_75: ``torch.Tensor`` (-1 if 0.75 not in the list of iou thresholds)
- map_per_class: ``torch.Tensor`` (-1 if class metrics are disabled)
- mar_100_per_class: ``torch.Tensor`` (-1 if class metrics are disabled)
"""
Expand Down