-
Notifications
You must be signed in to change notification settings - Fork 7.1k
add tests for F.pad_bounding_box #6038
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
Changes from all commits
ca7d822
ee388be
d3ae52a
5945b41
8355db2
a7fd05c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -382,6 +382,15 @@ def pad_segmentation_mask(): | |||||||||||||||||||||||
yield SampleInput(mask, padding=padding, padding_mode=padding_mode) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
@register_kernel_info_from_sample_inputs_fn | ||||||||||||||||||||||||
def pad_bounding_box(): | ||||||||||||||||||||||||
for bounding_box, padding in itertools.product( | ||||||||||||||||||||||||
make_bounding_boxes(), | ||||||||||||||||||||||||
[[1], [1, 1], [1, 1, 2, 2]], | ||||||||||||||||||||||||
): | ||||||||||||||||||||||||
yield SampleInput(bounding_box, padding=padding, format=bounding_box.format) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
@register_kernel_info_from_sample_inputs_fn | ||||||||||||||||||||||||
def perspective_bounding_box(): | ||||||||||||||||||||||||
for bounding_box, perspective_coeffs in itertools.product( | ||||||||||||||||||||||||
|
@@ -1103,22 +1112,67 @@ def test_correctness_pad_segmentation_mask_on_fixed_input(device): | |||||||||||||||||||||||
torch.testing.assert_close(out_mask, expected_mask) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def _parse_padding(padding): | ||||||||||||||||||||||||
if isinstance(padding, int): | ||||||||||||||||||||||||
return [padding] * 4 | ||||||||||||||||||||||||
if isinstance(padding, list): | ||||||||||||||||||||||||
if len(padding) == 1: | ||||||||||||||||||||||||
return padding * 4 | ||||||||||||||||||||||||
if len(padding) == 2: | ||||||||||||||||||||||||
return padding * 2 # [left, up, right, down] | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return padding | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
@pytest.mark.parametrize("device", cpu_and_gpu()) | ||||||||||||||||||||||||
@pytest.mark.parametrize("padding", [[1], [1, 1], [1, 1, 2, 2]]) | ||||||||||||||||||||||||
def test_correctness_pad_bounding_box(device, padding): | ||||||||||||||||||||||||
def _compute_expected_bbox(bbox, padding_): | ||||||||||||||||||||||||
pad_left, pad_up, _, _ = _parse_padding(padding_) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
bbox_format = bbox.format | ||||||||||||||||||||||||
bbox_dtype = bbox.dtype | ||||||||||||||||||||||||
bbox = convert_bounding_box_format(bbox, old_format=bbox_format, new_format=features.BoundingBoxFormat.XYXY) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
bbox[0::2] += pad_left | ||||||||||||||||||||||||
bbox[1::2] += pad_up | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
bbox = convert_bounding_box_format( | ||||||||||||||||||||||||
bbox, old_format=features.BoundingBoxFormat.XYXY, new_format=bbox_format, copy=False | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
if bbox.dtype != bbox_dtype: | ||||||||||||||||||||||||
# Temporary cast to original dtype | ||||||||||||||||||||||||
# e.g. float32 -> int | ||||||||||||||||||||||||
bbox = bbox.to(bbox_dtype) | ||||||||||||||||||||||||
return bbox | ||||||||||||||||||||||||
Comment on lines
+1140
to
+1147
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.
Suggested change
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. @pmeier actually it does not work with >>> t = torch.tensor([[1.0, 2.0, 3.0, 4.0]])
>>> bbox = features.BoundingBox([[0, 1, 2, 3]], format=features.BoundingBoxFormat.XYXY, image_size=(32, 32))
>>> t.to(bbox)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/vision/torchvision/prototype/features/_feature.py", line 83, in __torch_function__
return cls.new_like(args[0], output, dtype=output.dtype, device=output.device)
File "/vision/torchvision/prototype/features/_bounding_box.py", line 54, in new_like
format=format if format is not None else other.format,
AttributeError: 'Tensor' object has no attribute 'format' I'll keep my code, especially it is a temporary cast once we fixed 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. I'll added #6094 to track this. |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
for bboxes in make_bounding_boxes(): | ||||||||||||||||||||||||
bboxes = bboxes.to(device) | ||||||||||||||||||||||||
bboxes_format = bboxes.format | ||||||||||||||||||||||||
bboxes_image_size = bboxes.image_size | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
output_boxes = F.pad_bounding_box(bboxes, padding, format=bboxes_format) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if bboxes.ndim < 2: | ||||||||||||||||||||||||
bboxes = [bboxes] | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
expected_bboxes = [] | ||||||||||||||||||||||||
for bbox in bboxes: | ||||||||||||||||||||||||
bbox = features.BoundingBox(bbox, format=bboxes_format, image_size=bboxes_image_size) | ||||||||||||||||||||||||
expected_bboxes.append(_compute_expected_bbox(bbox, padding)) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if len(expected_bboxes) > 1: | ||||||||||||||||||||||||
expected_bboxes = torch.stack(expected_bboxes) | ||||||||||||||||||||||||
else: | ||||||||||||||||||||||||
expected_bboxes = expected_bboxes[0] | ||||||||||||||||||||||||
torch.testing.assert_close(output_boxes, expected_bboxes) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
@pytest.mark.parametrize("padding", [[1, 2, 3, 4], [1], 1, [1, 2]]) | ||||||||||||||||||||||||
def test_correctness_pad_segmentation_mask(padding): | ||||||||||||||||||||||||
def _compute_expected_mask(): | ||||||||||||||||||||||||
def parse_padding(): | ||||||||||||||||||||||||
if isinstance(padding, int): | ||||||||||||||||||||||||
return [padding] * 4 | ||||||||||||||||||||||||
if isinstance(padding, list): | ||||||||||||||||||||||||
if len(padding) == 1: | ||||||||||||||||||||||||
return padding * 4 | ||||||||||||||||||||||||
if len(padding) == 2: | ||||||||||||||||||||||||
return padding * 2 # [left, up, right, down] | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return padding | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def _compute_expected_mask(mask, padding_): | ||||||||||||||||||||||||
h, w = mask.shape[-2], mask.shape[-1] | ||||||||||||||||||||||||
pad_left, pad_up, pad_right, pad_down = parse_padding() | ||||||||||||||||||||||||
pad_left, pad_up, pad_right, pad_down = _parse_padding(padding_) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
new_h = h + pad_up + pad_down | ||||||||||||||||||||||||
new_w = w + pad_left + pad_right | ||||||||||||||||||||||||
|
@@ -1132,7 +1186,7 @@ def parse_padding(): | |||||||||||||||||||||||
for mask in make_segmentation_masks(): | ||||||||||||||||||||||||
out_mask = F.pad_segmentation_mask(mask, padding, "constant") | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
expected_mask = _compute_expected_mask() | ||||||||||||||||||||||||
expected_mask = _compute_expected_mask(mask, padding) | ||||||||||||||||||||||||
torch.testing.assert_close(out_mask, expected_mask) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.