From c5855d684ace2667314c5902f990d42265995908 Mon Sep 17 00:00:00 2001 From: vfdev-5 Date: Thu, 7 Apr 2022 09:17:30 +0000 Subject: [PATCH 1/3] [proto] Added crop_bounding_box op --- test/test_prototype_transforms_functional.py | 58 +++++++++++++++++++ .../transforms/functional/__init__.py | 3 +- .../transforms/functional/_geometry.py | 26 +++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/test/test_prototype_transforms_functional.py b/test/test_prototype_transforms_functional.py index 2c8540f093c..a9e250a22b9 100644 --- a/test/test_prototype_transforms_functional.py +++ b/test/test_prototype_transforms_functional.py @@ -321,6 +321,20 @@ def rotate_segmentation_mask(): ) +@register_kernel_info_from_sample_inputs_fn +def crop_bounding_box(): + for bounding_box, top, left in itertools.product(make_bounding_boxes(), [-8, 0, 9], [-8, 0, 9]): + yield SampleInput( + bounding_box, + format=bounding_box.format, + image_size=bounding_box.image_size, + top=top, + left=left, + height=top + 10, # this argument is unused + width=left + 10, # this argument is unused + ) + + @pytest.mark.parametrize( "kernel", [ @@ -808,3 +822,47 @@ def test_correctness_rotate_segmentation_mask_on_fixed_input(device): expected_mask = torch.rot90(mask, k=1, dims=(-2, -1)) out_mask = F.rotate_segmentation_mask(mask, 90, expand=False) torch.testing.assert_close(out_mask, expected_mask) + + +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize( + "top, left, height, width, expected_bboxes", + [ + [8, 12, 30, 40, [(-2.0, 7.0, 13.0, 27.0), (38.0, -3.0, 58.0, 14.0), (33.0, 38.0, 44.0, 54.0)]], + [-8, 12, 70, 40, [(-2.0, 23.0, 13.0, 43.0), (38.0, 13.0, 58.0, 30.0), (33.0, 54.0, 44.0, 70.0)]], + ], +) +def test_correctness_crop_bounding_box(device, top, left, height, width, expected_bboxes): + + # Expected bboxes computed using Albumentations: + # import numpy as np + # from albumentations.augmentations.crops.functional import crop_bbox_by_coords, normalize_bbox, denormalize_bbox + # expected_bboxes = [] + # for in_box in in_boxes: + # n_in_box = normalize_bbox(in_box, *size) + # n_out_box = crop_bbox_by_coords( + # n_in_box, (left, top, left + width, top + height), height, width, *size + # ) + # out_box = denormalize_bbox(n_out_box, height, width) + # expected_bboxes.append(out_box) + + size = (64, 76) + # xyxy format + in_boxes = [ + [10.0, 15.0, 25.0, 35.0], + [50.0, 5.0, 70.0, 22.0], + [45.0, 46.0, 56.0, 62.0], + ] + in_boxes = features.BoundingBox(in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=size, device=device) + + output_boxes = F.crop_bounding_box( + in_boxes, + in_boxes.format, + in_boxes.image_size, + top, + left, + height, + width, + ) + + torch.testing.assert_close(output_boxes.tolist(), expected_bboxes) diff --git a/torchvision/prototype/transforms/functional/__init__.py b/torchvision/prototype/transforms/functional/__init__.py index 64d47958b96..decf9e21020 100644 --- a/torchvision/prototype/transforms/functional/__init__.py +++ b/torchvision/prototype/transforms/functional/__init__.py @@ -57,9 +57,10 @@ rotate_image_tensor, rotate_image_pil, rotate_segmentation_mask, + pad_bounding_box, pad_image_tensor, pad_image_pil, - pad_bounding_box, + crop_bounding_box, crop_image_tensor, crop_image_pil, perspective_image_tensor, diff --git a/torchvision/prototype/transforms/functional/_geometry.py b/torchvision/prototype/transforms/functional/_geometry.py index 7629766c0e2..755e51ce81c 100644 --- a/torchvision/prototype/transforms/functional/_geometry.py +++ b/torchvision/prototype/transforms/functional/_geometry.py @@ -419,6 +419,32 @@ def pad_bounding_box( crop_image_pil = _FP.crop +def crop_bounding_box( + bounding_box: torch.Tensor, + format: features.BoundingBoxFormat, + image_size: Tuple[int, int], + top: int, + left: int, + height: int, + width: int, +) -> torch.Tensor: + pass + + shape = bounding_box.shape + + bounding_box = convert_bounding_box_format( + bounding_box, old_format=format, new_format=features.BoundingBoxFormat.XYXY + ).view(-1, 4) + + # Crop and optionally pad: + bounding_box[:, 0::2] -= left + bounding_box[:, 1::2] -= top + + return convert_bounding_box_format( + bounding_box, old_format=features.BoundingBoxFormat.XYXY, new_format=format, copy=False + ).view(shape) + + def perspective_image_tensor( img: torch.Tensor, perspective_coeffs: List[float], From 6306c068bf8bf3bc0ce8627bc5d1b9349204f57d Mon Sep 17 00:00:00 2001 From: vfdev-5 Date: Thu, 21 Apr 2022 15:24:38 +0000 Subject: [PATCH 2/3] Added tests for resized_crop_bounding_box --- test/test_prototype_transforms_functional.py | 75 +++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/test/test_prototype_transforms_functional.py b/test/test_prototype_transforms_functional.py index 91623854330..9adf623ebc7 100644 --- a/test/test_prototype_transforms_functional.py +++ b/test/test_prototype_transforms_functional.py @@ -332,6 +332,22 @@ def crop_bounding_box(): ) +@register_kernel_info_from_sample_inputs_fn +def resized_crop_bounding_box(): + for bounding_box, top, left, height, width, size in itertools.product( + make_bounding_boxes(), [-8, 9], [-8, 9], [32, 22], [34, 20], [(32, 32), (16, 18)] + ): + yield SampleInput( + bounding_box, + format=bounding_box.format, + top=top, + left=left, + height=height, + width=width, + size=size + ) + + @pytest.mark.parametrize( "kernel", [ @@ -822,6 +838,10 @@ def test_correctness_rotate_segmentation_mask_on_fixed_input(device): @pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize( + "format", + [features.BoundingBoxFormat.XYXY, features.BoundingBoxFormat.XYWH, features.BoundingBoxFormat.CXCYWH], +) @pytest.mark.parametrize( "top, left, height, width, expected_bboxes", [ @@ -829,7 +849,7 @@ def test_correctness_rotate_segmentation_mask_on_fixed_input(device): [-8, 12, 70, 40, [(-2.0, 23.0, 13.0, 43.0), (38.0, 13.0, 58.0, 30.0), (33.0, 54.0, 44.0, 70.0)]], ], ) -def test_correctness_crop_bounding_box(device, top, left, height, width, expected_bboxes): +def test_correctness_crop_bounding_box(device, format, top, left, height, width, expected_bboxes): # Expected bboxes computed using Albumentations: # import numpy as np @@ -851,12 +871,63 @@ def test_correctness_crop_bounding_box(device, top, left, height, width, expecte [45.0, 46.0, 56.0, 62.0], ] in_boxes = features.BoundingBox(in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=size, device=device) + if format != features.BoundingBoxFormat.XYXY: + in_boxes = convert_bounding_box_format(in_boxes, features.BoundingBoxFormat.XYXY, format) output_boxes = F.crop_bounding_box( in_boxes, - in_boxes.format, + format, top, left, ) + if format != features.BoundingBoxFormat.XYXY: + output_boxes = convert_bounding_box_format(output_boxes, format, features.BoundingBoxFormat.XYXY) + torch.testing.assert_close(output_boxes.tolist(), expected_bboxes) + + +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize( + "format", + [features.BoundingBoxFormat.XYXY, features.BoundingBoxFormat.XYWH, features.BoundingBoxFormat.CXCYWH], +) +@pytest.mark.parametrize( + "top, left, height, width, size", + [ + [0, 0, 30, 30, (60, 60)], + [-5, 5, 35, 45, (32, 34)], + ], +) +def test_correctness_resized_crop_bounding_box(device, format, top, left, height, width, size): + def _compute_expected(bbox, top_, left_, height_, width_, size_): + # bbox should be xyxy + bbox[0] = (bbox[0] - left_) * size_[1] / width_ + bbox[1] = (bbox[1] - top_) * size_[0] / height_ + bbox[2] = (bbox[2] - left_) * size_[1] / width_ + bbox[3] = (bbox[3] - top_) * size_[0] / height_ + return bbox + + image_size = (100, 100) + # xyxy format + in_boxes = [ + [10.0, 10.0, 20.0, 20.0], + [5.0, 10.0, 15.0, 20.0], + ] + expected_bboxes = [] + for in_box in in_boxes: + expected_bboxes.append(_compute_expected(list(in_box), top, left, height, width, size)) + expected_bboxes = torch.tensor(expected_bboxes, device=device) + + in_boxes = features.BoundingBox( + in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=image_size, device=device + ) + if format != features.BoundingBoxFormat.XYXY: + in_boxes = convert_bounding_box_format(in_boxes, features.BoundingBoxFormat.XYXY, format) + + output_boxes = F.resized_crop_bounding_box(in_boxes, format, top, left, height, width, size) + + if format != features.BoundingBoxFormat.XYXY: + output_boxes = convert_bounding_box_format(output_boxes, format, features.BoundingBoxFormat.XYXY) + + torch.testing.assert_close(output_boxes, expected_bboxes) From d712f1d1b3bea8500a42090c8b50964bfb41da65 Mon Sep 17 00:00:00 2001 From: vfdev-5 Date: Thu, 21 Apr 2022 15:36:18 +0000 Subject: [PATCH 3/3] Fixed code formatting --- test/test_prototype_transforms_functional.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/test/test_prototype_transforms_functional.py b/test/test_prototype_transforms_functional.py index 9adf623ebc7..78e419ca2c1 100644 --- a/test/test_prototype_transforms_functional.py +++ b/test/test_prototype_transforms_functional.py @@ -338,13 +338,7 @@ def resized_crop_bounding_box(): make_bounding_boxes(), [-8, 9], [-8, 9], [32, 22], [34, 20], [(32, 32), (16, 18)] ): yield SampleInput( - bounding_box, - format=bounding_box.format, - top=top, - left=left, - height=height, - width=width, - size=size + bounding_box, format=bounding_box.format, top=top, left=left, height=height, width=width, size=size )