-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Add rotated bounding box formats #8841
Open
AntoineSimoulin
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
AntoineSimoulin:add-rotated-bboxes
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.
Open
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -1288,6 +1288,38 @@ def test_bbox_same(self): | |
assert_equal(ops.box_convert(box_tensor, in_fmt="xywh", out_fmt="xywh"), exp_xyxy) | ||
assert_equal(ops.box_convert(box_tensor, in_fmt="cxcywh", out_fmt="cxcywh"), exp_xyxy) | ||
|
||
def test_rotated_bbox_same(self): | ||
box_tensor = torch.tensor( | ||
[ | ||
[0, 0, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[10, 15, 30, 35, 0], | ||
[23, 35, 93, 95, 0], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
exp_xyxyr = torch.tensor( | ||
[ | ||
[0, 0, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[10, 15, 30, 35, 0], | ||
[23, 35, 93, 95, 0], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
assert exp_xyxyr.size() == torch.Size([4, 5]) | ||
assert_equal( | ||
ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="xyxyr"), exp_xyxyr | ||
) | ||
assert_equal( | ||
ops.box_convert(box_tensor, in_fmt="xywhr", out_fmt="xywhr"), exp_xyxyr | ||
) | ||
assert_equal( | ||
ops.box_convert(box_tensor, in_fmt="cxcywhr", out_fmt="cxcywhr"), exp_xyxyr | ||
) | ||
|
||
def test_bbox_xyxy_xywh(self): | ||
# Simple test convert boxes to xywh and back. Make sure they are same. | ||
# box_tensor is in x1 y1 x2 y2 format. | ||
|
@@ -1339,8 +1371,154 @@ def test_bbox_xywh_cxcywh(self): | |
box_xywh = ops.box_convert(box_cxcywh, in_fmt="cxcywh", out_fmt="xywh") | ||
assert_equal(box_xywh, box_tensor) | ||
|
||
@pytest.mark.parametrize("inv_infmt", ["xwyh", "cxwyh"]) | ||
@pytest.mark.parametrize("inv_outfmt", ["xwcx", "xhwcy"]) | ||
def test_bbox_xyxy_to_cxcywhr(self): | ||
box_tensor = torch.tensor( | ||
[[0, 0, 100, 100], [0, 0, 0, 0], [10, 15, 30, 35], [23, 35, 93, 95]], | ||
dtype=torch.float, | ||
) | ||
exp_cxcywhr = torch.tensor( | ||
[ | ||
[50, 50, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[20, 25, 20, 20, 0], | ||
[58, 65, 70, 60, 0], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
assert exp_cxcywhr.size() == torch.Size([4, 5]) | ||
box_cxcywhr = ops.box_convert(box_tensor, in_fmt="xyxy", out_fmt="cxcywhr") | ||
assert_equal(box_cxcywhr, exp_cxcywhr) | ||
|
||
def test_bbox_xyxyr_xywhr(self): | ||
# Simple test convert boxes to xywh and back. Make sure they are same. | ||
# box_tensor is in x1 y1 x2 y2 format. | ||
box_tensor = torch.tensor( | ||
[ | ||
[0, 0, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[10, 15, 30, 35, 0], | ||
[23, 35, 93, 95, 0], | ||
[3, 2, 7, 4, 0], | ||
[3, 2, 5, -2, 90], | ||
], | ||
dtype=torch.float, | ||
) | ||
exp_xywhr = torch.tensor( | ||
[ | ||
[0, 0, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[10, 15, 20, 20, 0], | ||
[23, 35, 70, 60, 0], | ||
[3, 2, 4, 2, 0], | ||
[3, 2, 4, 2, 90], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
assert exp_xywhr.size() == torch.Size([6, 5]) | ||
box_xywhr = ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="xywhr") | ||
assert torch.allclose(box_xywhr, exp_xywhr) | ||
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. Let's use That being said, could we use |
||
|
||
# Reverse conversion | ||
box_xyxyr = ops.box_convert(box_xywhr, in_fmt="xywhr", out_fmt="xyxyr") | ||
assert torch.allclose(box_xyxyr, box_tensor) | ||
|
||
def test_bbox_xyxyr_cxcywhr(self): | ||
# Simple test convert boxes to cxcywh and back. Make sure they are same. | ||
# box_tensor is in x1 y1 x2 y2 format. | ||
box_tensor = torch.tensor( | ||
[ | ||
[0, 0, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[10, 15, 30, 35, 0], | ||
[23, 35, 93, 95, 0], | ||
[3, 2, 7, 4, 0], | ||
], | ||
dtype=torch.float, | ||
) | ||
exp_cxcywhr = torch.tensor( | ||
[ | ||
[50, 50, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[20, 25, 20, 20, 0], | ||
[58, 65, 70, 60, 0], | ||
[5, 3, 4, 2, 0], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
assert exp_cxcywhr.size() == torch.Size([5, 5]) | ||
box_cxcywhr = ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="cxcywhr") | ||
assert torch.allclose(box_cxcywhr, exp_cxcywhr) | ||
|
||
# Reverse conversion | ||
box_xyxyr = ops.box_convert(box_cxcywhr, in_fmt="cxcywhr", out_fmt="xyxyr") | ||
assert torch.allclose(box_xyxyr, box_tensor) | ||
|
||
def test_bbox_xywhr_cxcywhr(self): | ||
box_tensor = torch.tensor( | ||
[ | ||
[0, 0, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[10, 15, 20, 20, 0], | ||
[23, 35, 70, 60, 0], | ||
[4.0, 2.0, 4.0, 2.0, 0.0], | ||
[5.0, 5.0, 4.0, 2.0, 90.0], | ||
[8.0, 4.0, 4.0, 2.0, 180.0], | ||
[7.0, 1.0, 4.0, 2.0, -90.0], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
exp_cxcywhr = torch.tensor( | ||
[ | ||
[50, 50, 100, 100, 0], | ||
[0, 0, 0, 0, 0], | ||
[20, 25, 20, 20, 0], | ||
[58, 65, 70, 60, 0], | ||
[6, 3, 4, 2, 0], | ||
[6, 3, 4, 2, 90], | ||
[6, 3, 4, 2, 180], | ||
[6, 3, 4, 2, -90], | ||
], | ||
dtype=torch.float, | ||
) | ||
|
||
assert exp_cxcywhr.size() == torch.Size([8, 5]) | ||
box_cxcywhr = ops.box_convert(box_tensor, in_fmt="xywhr", out_fmt="cxcywhr") | ||
assert torch.allclose(box_cxcywhr, exp_cxcywhr) | ||
|
||
# Reverse conversion | ||
box_xywhr = ops.box_convert(box_cxcywhr, in_fmt="cxcywhr", out_fmt="xywhr") | ||
assert torch.allclose(box_xywhr, box_tensor) | ||
|
||
def test_bbox_xyxyr_to_xyxyxyxy(self): | ||
box_tensor = torch.tensor([[4, 5, 6, 1, 90]], dtype=torch.float) | ||
exp_xyxyxyxy = torch.tensor([[4, 5, 4, 1, 6, 1, 6, 5]], dtype=torch.float) | ||
|
||
assert exp_xyxyxyxy.size() == torch.Size([1, 8]) | ||
box_xyxyxyxy = ops.box_convert(box_tensor, in_fmt="xyxyr", out_fmt="xyxyxyxy") | ||
assert_equal(box_xyxyxyxy, exp_xyxyxyxy) | ||
|
||
def test_bbox_cxcywhr_to_xyxyxyxy(self): | ||
box_tensor = torch.tensor([[5, 3, 4, 2, 90]], dtype=torch.float) | ||
exp_xyxyxyxy = torch.tensor([[4, 5, 4, 1, 6, 1, 6, 5]], dtype=torch.float) | ||
|
||
assert exp_xyxyxyxy.size() == torch.Size([1, 8]) | ||
box_xyxyxyxy = ops.box_convert(box_tensor, in_fmt="cxcywhr", out_fmt="xyxyxyxy") | ||
assert_equal(box_xyxyxyxy, exp_xyxyxyxy) | ||
|
||
def test_bbox_xywhr_to_xyxyxyxy(self): | ||
box_tensor = torch.tensor([[4, 5, 4, 2, 90]], dtype=torch.float) | ||
exp_xyxyxyxy = torch.tensor([[4, 5, 4, 1, 6, 1, 6, 5]], dtype=torch.float) | ||
|
||
assert exp_xyxyxyxy.size() == torch.Size([1, 8]) | ||
box_xyxyxyxy = ops.box_convert(box_tensor, in_fmt="xywhr", out_fmt="xyxyxyxy") | ||
assert_equal(box_xyxyxyxy, exp_xyxyxyxy) | ||
|
||
@pytest.mark.parametrize("inv_infmt", ["xwyh", "cxwyh", "xwyhr", "cxwyhr", "xxxxyyyy"]) | ||
@pytest.mark.parametrize("inv_outfmt", ["xwcx", "xhwcy", "xwcxr", "xhwcyr", "xyxyxxyy"]) | ||
def test_bbox_invalid(self, inv_infmt, inv_outfmt): | ||
box_tensor = torch.tensor( | ||
[[0, 0, 100, 100], [0, 0, 0, 0], [10, 15, 20, 20], [23, 35, 70, 60]], dtype=torch.float | ||
|
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.
I think this kind of check is already taken care of in
vision/test/test_transforms_v2.py
Lines 3521 to 3531 in 06a925c