From f3f88f20a8938e742bf5c67f4658748667c45b00 Mon Sep 17 00:00:00 2001 From: twsl <45483159+twsl@users.noreply.github.com> Date: Tue, 7 Dec 2021 20:03:48 +0000 Subject: [PATCH 1/4] Fix bool sort on CUDA --- torchmetrics/detection/map.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/torchmetrics/detection/map.py b/torchmetrics/detection/map.py index 4d18f5be2cd..d40370857d7 100644 --- a/torchmetrics/detection/map.py +++ b/torchmetrics/detection/map.py @@ -407,7 +407,9 @@ def _evaluate_image( ignore_area = (areas < area_range[0]) | (areas > area_range[1]) # sort dt highest score first, sort gt ignore last - ignore_area_sorted, gtind = torch.sort(ignore_area) + ignore_area_sorted, gtind = torch.sort(ignore_area.to(torch.uint8)) + # Convert to uint8 temporarily and back to bool, because "Sort currently does not support bool dtype on CUDA" + ignore_area_sorted = ignore_area_sorted.to(torch.bool) gt = gt[gtind] scores = self.detection_scores[id] scores_filtered = scores[det_label_mask] @@ -429,7 +431,7 @@ def _evaluate_image( for idx_iou, t in enumerate(self.iou_thresholds): for idx_det in range(nb_det): m = MAP._find_best_gt_match(t, nb_gt, gt_matches, idx_iou, gt_ignore, ious, idx_det) - if m is not -1: + if m != -1: det_ignore[idx_iou, idx_det] = gt_ignore[m] det_matches[idx_iou, idx_det] = True gt_matches[idx_iou, m] = True From 8f8542bcc749b9c35f68a7579431ab4e5b0db101 Mon Sep 17 00:00:00 2001 From: twsl <45483159+twsl@users.noreply.github.com> Date: Tue, 7 Dec 2021 21:18:11 +0000 Subject: [PATCH 2/4] Add dedicated gpu test --- tests/detection/test_map.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/detection/test_map.py b/tests/detection/test_map.py index 43c586cee28..0232195bdbe 100644 --- a/tests/detection/test_map.py +++ b/tests/detection/test_map.py @@ -215,6 +215,19 @@ def test_empty_preds(): metric.compute() +_gpu_test_condition = not torch.cuda.is_available() + + +@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_gpu(): + """Test predictions on single gpu.""" + metric = MAP() + metric = metric.to("cuda") + metric.update(_inputs.preds[0], _inputs.target[0]) + metric.compute() + + @pytest.mark.skipif(_pytest_condition, reason="test requires that pycocotools and torchvision=>0.8.0 is installed") def test_empty_metric(): """Test empty metric.""" From 025156cc144a520f70777d737163b04ff42f0437 Mon Sep 17 00:00:00 2001 From: twsl <45483159+twsl@users.noreply.github.com> Date: Tue, 7 Dec 2021 21:25:54 +0000 Subject: [PATCH 3/4] Updated CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bc7c284d3..13f09f6dcf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed `torch.sort` currently does not support bool dtype on CUDA ([#665](https://github.com/PyTorchLightning/metrics/pull/665)) + + ## [0.6.1] - 2021-12-06 From 8a468bae9f20dba164e28b70e576eab08c27a998 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Wed, 8 Dec 2021 19:45:43 +0100 Subject: [PATCH 4/4] Apply suggestions from code review --- tests/detection/test_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/detection/test_map.py b/tests/detection/test_map.py index 0232195bdbe..83d850c8d4b 100644 --- a/tests/detection/test_map.py +++ b/tests/detection/test_map.py @@ -220,7 +220,7 @@ def test_empty_preds(): @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_gpu(): +def test_map_gpu(): """Test predictions on single gpu.""" metric = MAP() metric = metric.to("cuda")