-
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
base: main
Are you sure you want to change the base?
Conversation
Test Plan: Run unit tests: `pytest test/test_ops.py -vvv -k TestBoxConvert`
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8841
Note: Links to docs will display an error until the docs builds have been completed. ❌ 15 New Failures, 1 Unrelated FailureAs of commit 3f437a6 with merge base 4249b61 (): NEW FAILURES - The following jobs have failed:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
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.
Thanks a lot for the PR @AntoineSimoulin ! I made some small comments below, I have higher-level points to discuss, let's sync! :)
""":class:`torch.Tensor` subclass for bounding boxes with shape ``[N, 4]``. | ||
""":class:`torch.Tensor` subclass for bounding boxes with shape ``[N, K]``. | ||
Where ``N`` is the number of bounding boxes | ||
and ``K`` is either 4 for unrotated boxes or 5 for rotated boxes. |
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.
and ``K`` is either 4 for unrotated boxes or 5 for rotated boxes. | |
and ``K`` is 4 for unrotated boxes, and 5 or 8 for rotated boxes. |
@@ -17,15 +17,25 @@ class BoundingBoxFormat(Enum): | |||
* ``XYXY`` | |||
* ``XYWH`` | |||
* ``CXCYWH`` | |||
* ``XYXYR``: rotated boxes represented via corners, x1, y1 being top left and x2, y2 being bottom right. r is rotation angle in degrees. |
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.
Just to make it more explicit
* ``XYXYR``: rotated boxes represented via corners, x1, y1 being top left and x2, y2 being bottom right. r is rotation angle in degrees. | |
* ``XYXYR``: rotated boxes represented via corners, x1, y1 being top left and x2, y2 being bottom right. r is rotation angle in degrees in [0, 360). |
@@ -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): |
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
@pytest.mark.parametrize("format", list(tv_tensors.BoundingBoxFormat)) | |
@pytest.mark.parametrize("inplace", [False, True]) | |
def test_kernel_noop(self, format, inplace): | |
input = make_bounding_boxes(format=format).as_subclass(torch.Tensor) | |
input_version = input._version | |
output = F.convert_bounding_box_format(input, old_format=format, new_format=format, inplace=inplace) | |
assert output is input | |
assert output.data_ptr() == input.data_ptr() | |
assert output._version == input_version |
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use torch.testing.assert_close()
instead of torch.allclose
, as it's more robust and provides better messages.
That being said, could we use assert_equal()
here, since we're expecting integer-valued tensors? Is it because of the r
column?
This PR is part of a series of contributions aiming to add rotated boxes to torchvision. This first contribution aims at modifying the definition of bounding boxes in torchvision. We operate the two following modifications:
Extend
BoundingBoxFormat
for rotated boxesWe add four multiple allowed formats in
BoundingBoxFormat
. The formats "xyxyr", "xywhr", "cxcywhr" simply extend the non-rotated counterparts by adding a 5th coordinate to the bounding box,r
, the rotation angle with respect to the box center by|r|
degrees counter clock wise in the image plan. The last format "xyxyxyxy" represents a box with 4 corners.Potential limitations:
BoundingBoxes
instead of creating a newRoratedBoundingBoxes
class. The reason is to simplify the possible input types for transforms and avoid having two different paths for transformations. For instance keeping a singlehorizontal_flip_bounding_boxes
and_horizontal_flip_bounding_boxes_dispatch
instead of creating a new functionhorizontal_flip_rotated_bounding_boxes
;generalized_box_iou_loss
. However, please note these functions do not expect aBoundingBox
as input, but atorch.Tensor[N, 4]
ortorch.Tensor[4]
. So there is no direct incompatibility.Add conversion functions for rotated boxes
We add 10 pairwise conversion functions in "_box_convert.py" to allow converting rotated bounding boxes between all four new formats. We also modified the logic in
box_convert
to support all possible conversion directions.Potential limitations:
Testing
Please run unit tests for the modifications with:
pytest test/test_ops.py -vvv -k TestBoxConvert
Next steps
Next modifications will aim at updating transforms functions (e.g.
horizontal_flip_bounding_boxes
), and adding utility functions specific to rotated boxes (e.g.rotated_box_area
,_rotated_box_inter_union
,rotated_box_iou
).