-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Rotated bounding box NMS implementation for CPU #9450
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
Open
zy1git
wants to merge
17
commits into
pytorch:main
Choose a base branch
from
zy1git:rotated-NMS
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−40
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bfb8321
NMS implementation for CPU
8875875
add tests
ae5fb41
Merge remote-tracking branch 'origin/main' into rotated-NMS
0e924ed
fuse two implementations
667e1b4
Remove Detectron2 license header from nms_kernel.cpp since the NMS al…
f01ca8b
revert the unnecessary linting
40adc7f
Preserve original IoU computation in fused NMS implementation
eb4aefe
Add box caching in fused NMS to match original memory access pattern
5499015
fix the test failure
c940345
remove the torchscript test and err_msg
17a54cc
Remove the fmt parameter from nms function
54aee4c
fix the test failures
c5cf1c4
Reuse TestNMS._reference_nms in TestNMSRotated
9c96e26
address the comment
4a993c8
change the variable name
84e960c
address more comments on the file torchvision/csrc/ops/cpu/nms_kernel…
9ef0c0b
add batched_nms and the test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -770,7 +770,8 @@ def test_is_leaf_node(self, device): | |
|
|
||
|
|
||
| class TestNMS: | ||
| def _reference_nms(self, boxes, scores, iou_threshold): | ||
| @classmethod | ||
| def _reference_nms(cls, boxes, scores, iou_threshold): | ||
| """ | ||
| Args: | ||
| boxes: boxes in corner-form | ||
|
|
@@ -825,8 +826,8 @@ def test_nms_ref(self, iou, seed): | |
| def test_nms_input_errors(self): | ||
| with pytest.raises(RuntimeError): | ||
| ops.nms(torch.rand(4), torch.rand(3), 0.5) | ||
| with pytest.raises(RuntimeError): | ||
| ops.nms(torch.rand(3, 5), torch.rand(3), 0.5) | ||
| with pytest.raises((RuntimeError, ValueError)): | ||
| ops.nms(torch.rand(3, 6), torch.rand(3), 0.5) | ||
| with pytest.raises(RuntimeError): | ||
| ops.nms(torch.rand(3, 4), torch.rand(3, 2), 0.5) | ||
| with pytest.raises(RuntimeError): | ||
|
|
@@ -2007,6 +2008,87 @@ def test_cuda_cpu_consistency(self): | |
| torch.testing.assert_close(iou_cpu, iou_cuda.cpu(), atol=1e-5, rtol=1e-5) | ||
|
|
||
|
|
||
| class TestNMSRotated: | ||
| @staticmethod | ||
| def _create_tensors(N, device="cpu"): | ||
| boxes = torch.rand(N, 4, device=device) * 200 | ||
| boxes[:, 2:] += boxes[:, :2] | ||
| scores = torch.rand(N, device=device) | ||
| return boxes, scores | ||
|
|
||
| @pytest.mark.parametrize("iou", (0.2, 0.5, 0.8)) | ||
| def test_nms_rotated_0_degree(self, iou): | ||
| torch.manual_seed(0) | ||
| N = 1000 | ||
| boxes, scores = self._create_tensors(N) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and in other tests, call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in the new commit. |
||
| rotated_boxes = torch.zeros(N, 5) | ||
| rotated_boxes[:, 0] = (boxes[:, 0] + boxes[:, 2]) / 2.0 | ||
| rotated_boxes[:, 1] = (boxes[:, 1] + boxes[:, 3]) / 2.0 | ||
| rotated_boxes[:, 2] = boxes[:, 2] - boxes[:, 0] | ||
| rotated_boxes[:, 3] = boxes[:, 3] - boxes[:, 1] | ||
|
|
||
| keep_ref = TestNMS._reference_nms(boxes, scores, iou) | ||
| keep = ops.nms(rotated_boxes, scores, iou) | ||
| torch.testing.assert_close(keep, keep_ref, atol=0, rtol=0) | ||
| keep_non_rotated = ops.nms(boxes, scores, iou) | ||
| torch.testing.assert_close(keep, keep_non_rotated, atol=0, rtol=0) | ||
|
|
||
| @pytest.mark.parametrize("iou", (0.2, 0.5, 0.8)) | ||
| def test_nms_rotated_90_degrees(self, iou): | ||
| torch.manual_seed(0) | ||
| N = 1000 | ||
| boxes, scores = self._create_tensors(N) | ||
| rotated_boxes = torch.zeros(N, 5) | ||
| rotated_boxes[:, 0] = (boxes[:, 0] + boxes[:, 2]) / 2.0 | ||
| rotated_boxes[:, 1] = (boxes[:, 1] + boxes[:, 3]) / 2.0 | ||
| # Swap width and height for 90 degrees so reference horizontal NMS can be used | ||
| rotated_boxes[:, 2] = boxes[:, 3] - boxes[:, 1] | ||
| rotated_boxes[:, 3] = boxes[:, 2] - boxes[:, 0] | ||
| rotated_boxes[:, 4] = 90 | ||
|
|
||
| keep_ref = TestNMS._reference_nms(boxes, scores, iou) | ||
| keep = ops.nms(rotated_boxes, scores, iou) | ||
| torch.testing.assert_close(keep, keep_ref, atol=0, rtol=0) | ||
| keep_non_rotated = ops.nms(boxes, scores, iou) | ||
| torch.testing.assert_close(keep, keep_non_rotated, atol=0, rtol=0) | ||
|
|
||
| @pytest.mark.parametrize("iou", (0.2, 0.5, 0.8)) | ||
| def test_nms_rotated_180_degrees(self, iou): | ||
| torch.manual_seed(0) | ||
| N = 1000 | ||
| boxes, scores = self._create_tensors(N) | ||
| rotated_boxes = torch.zeros(N, 5) | ||
| rotated_boxes[:, 0] = (boxes[:, 0] + boxes[:, 2]) / 2.0 | ||
| rotated_boxes[:, 1] = (boxes[:, 1] + boxes[:, 3]) / 2.0 | ||
| rotated_boxes[:, 2] = boxes[:, 2] - boxes[:, 0] | ||
| rotated_boxes[:, 3] = boxes[:, 3] - boxes[:, 1] | ||
| rotated_boxes[:, 4] = 180 | ||
|
|
||
| keep_ref = TestNMS._reference_nms(boxes, scores, iou) | ||
| keep = ops.nms(rotated_boxes, scores, iou) | ||
| torch.testing.assert_close(keep, keep_ref, atol=0, rtol=0) | ||
| keep_non_rotated = ops.nms(boxes, scores, iou) | ||
| torch.testing.assert_close(keep, keep_non_rotated, atol=0, rtol=0) | ||
|
|
||
| @pytest.mark.parametrize("iou", (0.2, 0.5, 0.8)) | ||
| def test_batched_nms_rotated_0_degree(self, iou): | ||
| torch.manual_seed(0) | ||
| N = 2000 | ||
| num_classes = 50 | ||
| boxes, scores = self._create_tensors(N) | ||
| idxs = torch.randint(0, num_classes, (N,)) | ||
| rotated_boxes = torch.zeros(N, 5) | ||
| rotated_boxes[:, 0] = (boxes[:, 0] + boxes[:, 2]) / 2.0 | ||
| rotated_boxes[:, 1] = (boxes[:, 1] + boxes[:, 3]) / 2.0 | ||
| rotated_boxes[:, 2] = boxes[:, 2] - boxes[:, 0] | ||
| rotated_boxes[:, 3] = boxes[:, 3] - boxes[:, 1] | ||
| backup = rotated_boxes.clone() | ||
| keep_non_rotated = ops.batched_nms(boxes, scores, idxs, iou) | ||
| keep = ops.batched_nms(rotated_boxes, scores, idxs, iou) | ||
| assert torch.allclose(rotated_boxes, backup) | ||
| torch.testing.assert_close(keep, keep_non_rotated, atol=0, rtol=0) | ||
|
|
||
|
|
||
| def get_boxes(dtype, device): | ||
| box1 = torch.tensor([-1, -1, 1, 1], dtype=dtype, device=device) | ||
| box2 = torch.tensor([0, 0, 1, 1], dtype=dtype, device=device) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| // All rights reserved. | ||
| // | ||
| // This source code is licensed under the BSD-style license found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| #include "nms_rotated.h" | ||
|
|
||
| #include <ATen/core/dispatch/Dispatcher.h> | ||
| #include <torch/library.h> | ||
| #include <torch/types.h> | ||
|
|
||
| namespace vision { | ||
| namespace ops { | ||
|
|
||
| at::Tensor nms_rotated( | ||
| const at::Tensor& dets, | ||
| const at::Tensor& scores, | ||
| double iou_threshold) { | ||
| C10_LOG_API_USAGE_ONCE("torchvision.csrc.ops.nms_rotated.nms_rotated"); | ||
| static auto op = c10::Dispatcher::singleton() | ||
| .findSchemaOrThrow("torchvision::nms_rotated", "") | ||
| .typed<decltype(nms_rotated)>(); | ||
| return op.call(dets, scores, iou_threshold); | ||
| } | ||
|
|
||
| TORCH_LIBRARY_FRAGMENT(torchvision, m) { | ||
| m.def(TORCH_SELECTIVE_SCHEMA( | ||
| "torchvision::nms_rotated(Tensor dets, Tensor scores, float iou_threshold) -> Tensor")); | ||
| } | ||
|
|
||
| } // namespace ops | ||
| } // namespace vision |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is comparing our rotated implementation against
_reference_horizontal_nms. We should also have a test that uses our non-rotated nms implementation as the reference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Fixed in the new commit.