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 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
23 changes: 22 additions & 1 deletion tests/detection/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@
],
)

_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., 2., 3., 4.]]),
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 +304,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
7 changes: 6 additions & 1 deletion torchmetrics/detection/mean_ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,12 @@ 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)
if det_scores.is_cuda and det_scores.dtype is torch.bool:
# Explicitly cast to uint8 to avoid error for bool inputs on CUDA to argsort
inds = torch.argsort(det_scores.to(torch.uint8), descending=True)
else:
inds = torch.argsort(det_scores, 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