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

Explicit cast to uint8 when bool inputs passed to argsort in MAP #983

Merged
merged 7 commits into from
Apr 24, 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 @@ -43,6 +43,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed "Sort currently does not support bool dtype on CUDA" error in MAP for empty preds ([#983](https://github.com/PyTorchLightning/metrics/pull/983))


- Fixed `BinnedPrecisionRecallCurve` when `thresholds` argument is not provided ([#968](https://github.com/PyTorchLightning/metrics/pull/968))


Expand Down
21 changes: 20 additions & 1 deletion tests/detection/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@
],
)

# Test empty preds case, to ensure bool inputs are properly casted to uint8
# From https://github.com/PyTorchLightning/metrics/issues/981
_inputs3 = Input(
krshrimali marked this conversation as resolved.
Show resolved Hide resolved
preds=[
[
dict(boxes=torch.tensor([]), scores=torch.tensor([]), labels=torch.tensor([])),
],
],
target=[
[
dict(
boxes=torch.tensor([[1.0, 2.0, 3.0, 4.0]]),
scores=torch.tensor([0.8]),
labels=torch.tensor([1]),
),
],
],
)


def _compare_fn(preds, target) -> dict:
"""Comparison function for map implementation.
Expand Down Expand Up @@ -283,7 +302,7 @@ def _move_to_gpu(input):

@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")
@pytest.mark.parametrize("inputs", [_inputs, _inputs2])
@pytest.mark.parametrize("inputs", [_inputs, _inputs2, _inputs3])
def test_map_gpu(inputs):
"""Test predictions on single gpu."""
metric = MeanAveragePrecision()
Expand Down
5 changes: 4 additions & 1 deletion torchmetrics/detection/mean_ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,10 @@ def __calculate_recall_precision_scores(

# different sorting method generates slightly different results.
# mergesort is used to be consistent as Matlab implementation.
inds = torch.argsort(det_scores, descending=True)
# Sort in PyTorch does not support bool types on CUDA (yet, 1.11.0)
dtype = torch.uint8 if det_scores.is_cuda and det_scores.dtype is torch.bool else det_scores.dtype
# Explicitly cast to uint8 to avoid error for bool inputs on CUDA to argsort
inds = torch.argsort(det_scores.to(dtype), descending=True)
det_scores_sorted = det_scores[inds]

det_matches = torch.cat([e["dtMatches"][:, :max_det] for e in img_eval_cls_bbox], axis=1)[:, inds]
Expand Down