From 38b589e7222ca6d0f39ceb84107c097aa7e9f684 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Wed, 5 Jul 2023 13:09:44 +0200 Subject: [PATCH] spatial_size -> size --- test/common_utils.py | 24 ++++++++---------- test/test_prototype_transforms.py | 16 ++++++------ test/test_transforms_v2.py | 18 ++++++------- test/test_transforms_v2_consistency.py | 22 ++++++++-------- test/test_transforms_v2_refactored.py | 35 +++++++++++++------------- test/test_transforms_v2_utils.py | 4 +-- 6 files changed, 59 insertions(+), 60 deletions(-) diff --git a/test/common_utils.py b/test/common_utils.py index ca4dc78811b..6c813f47c03 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -400,7 +400,7 @@ def load(self, device="cpu"): # new v2 default -DEFAULT_SPATIAL_SIZE = (17, 11) +DEFAULT_SIZE = (17, 11) # old v2 defaults DEFAULT_SQUARE_SPATIAL_SIZE = 15 DEFAULT_LANDSCAPE_SPATIAL_SIZE = (7, 33) @@ -495,7 +495,7 @@ def get_num_channels(color_space): def make_image( - spatial_size=DEFAULT_SPATIAL_SIZE, + size=DEFAULT_SIZE, *, color_space="RGB", batch_dims=(), @@ -505,7 +505,7 @@ def make_image( ): max_value = get_max_value(dtype) data = torch.testing.make_tensor( - (*batch_dims, get_num_channels(color_space), *spatial_size), + (*batch_dims, get_num_channels(color_space), *size), low=0, high=max_value, dtype=dtype or torch.uint8, @@ -635,7 +635,7 @@ def sample_position(values, max_value): if spatial_size is None: if size is None: - spatial_size = DEFAULT_SPATIAL_SIZE + spatial_size = DEFAULT_SIZE else: height, width = size height_margin, width_margin = torch.randint(10, (2,)).tolist() @@ -713,11 +713,11 @@ class MaskLoader(TensorLoader): pass -def make_detection_mask(spatial_size=DEFAULT_SPATIAL_SIZE, *, num_objects=5, batch_dims=(), dtype=None, device="cpu"): +def make_detection_mask(size=DEFAULT_SIZE, *, num_objects=5, batch_dims=(), dtype=None, device="cpu"): """Make a "detection" mask, i.e. (*, N, H, W), where each object is encoded as one of N boolean masks""" return datapoints.Mask( torch.testing.make_tensor( - (*batch_dims, num_objects, *spatial_size), + (*batch_dims, num_objects, *size), low=0, high=2, dtype=dtype or torch.bool, @@ -752,13 +752,11 @@ def make_detection_mask_loaders( make_detection_masks = from_loaders(make_detection_mask_loaders) -def make_segmentation_mask( - spatial_size=DEFAULT_SPATIAL_SIZE, *, num_categories=10, batch_dims=(), dtype=None, device="cpu" -): +def make_segmentation_mask(size=DEFAULT_SIZE, *, num_categories=10, batch_dims=(), dtype=None, device="cpu"): """Make a "segmentation" mask, i.e. (*, H, W), where the category is encoded as pixel value""" return datapoints.Mask( torch.testing.make_tensor( - (*batch_dims, *spatial_size), + (*batch_dims, *size), low=0, high=num_categories, dtype=dtype or torch.uint8, @@ -817,8 +815,8 @@ class VideoLoader(ImageLoader): pass -def make_video(spatial_size=DEFAULT_SPATIAL_SIZE, *, num_frames=3, batch_dims=(), **kwargs): - return datapoints.Video(make_image(spatial_size, batch_dims=(*batch_dims, num_frames), **kwargs)) +def make_video(size=DEFAULT_SIZE, *, num_frames=3, batch_dims=(), **kwargs): + return datapoints.Video(make_image(size, batch_dims=(*batch_dims, num_frames), **kwargs)) def make_video_loader( @@ -836,8 +834,8 @@ def fn(shape, dtype, device, memory_format): return make_video( (height, width), num_frames=num_frames, - color_space=color_space, batch_dims=batch_dims, + color_space=color_space, dtype=dtype, device=device, memory_format=memory_format, diff --git a/test/test_prototype_transforms.py b/test/test_prototype_transforms.py index 80e5162ed30..cfbcc7c0557 100644 --- a/test/test_prototype_transforms.py +++ b/test/test_prototype_transforms.py @@ -216,7 +216,7 @@ def test__get_params(self, mocker): transform = transforms.FixedSizeCrop(size=crop_size) flat_inputs = [ - make_image(spatial_size=spatial_size, color_space="RGB"), + make_image(size=spatial_size, color_space="RGB"), make_bounding_box(format=BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=batch_shape), ] params = transform._get_params(flat_inputs) @@ -315,7 +315,7 @@ def test__transform_culling(self, mocker): bounding_boxes = make_bounding_box( format=BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=(batch_size,) ) - masks = make_detection_mask(spatial_size=spatial_size, batch_dims=(batch_size,)) + masks = make_detection_mask(size=spatial_size, batch_dims=(batch_size,)) labels = make_label(extra_dims=(batch_size,)) transform = transforms.FixedSizeCrop((-1, -1)) @@ -495,29 +495,29 @@ def make_datapoints(): size = (600, 800) num_objects = 22 - pil_image = to_image_pil(make_image(spatial_size=size, color_space="RGB")) + pil_image = to_image_pil(make_image(size=size, color_space="RGB")) target = { "boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float), "labels": make_label(extra_dims=(num_objects,), categories=80), - "masks": make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long), + "masks": make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long), } yield (pil_image, target) - tensor_image = torch.Tensor(make_image(spatial_size=size, color_space="RGB")) + tensor_image = torch.Tensor(make_image(size=size, color_space="RGB")) target = { "boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float), "labels": make_label(extra_dims=(num_objects,), categories=80), - "masks": make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long), + "masks": make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long), } yield (tensor_image, target) - datapoint_image = make_image(spatial_size=size, color_space="RGB") + datapoint_image = make_image(size=size, color_space="RGB") target = { "boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float), "labels": make_label(extra_dims=(num_objects,), categories=80), - "masks": make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long), + "masks": make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long), } yield (datapoint_image, target) diff --git a/test/test_transforms_v2.py b/test/test_transforms_v2.py index 1c6ee23c95f..0f6c4cc3b2e 100644 --- a/test/test_transforms_v2.py +++ b/test/test_transforms_v2.py @@ -168,8 +168,8 @@ class TestSmoke: @pytest.mark.parametrize( "image_or_video", [ - make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), - make_video(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), + make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), + make_video(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), next(make_pil_images(color_spaces=["RGB"])), next(make_vanilla_tensor_images()), ], @@ -179,8 +179,8 @@ def test_common(self, transform, adapter, container_type, image_or_video, device spatial_size = F.get_spatial_size(image_or_video) input = dict( image_or_video=image_or_video, - image_datapoint=make_image(spatial_size=spatial_size), - video_datapoint=make_video(spatial_size=spatial_size), + image_datapoint=make_image(size=spatial_size), + video_datapoint=make_video(size=spatial_size), image_pil=next(make_pil_images(sizes=[spatial_size], color_spaces=["RGB"])), bounding_box_xyxy=make_bounding_box( format=datapoints.BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=(3,) @@ -227,8 +227,8 @@ def test_common(self, transform, adapter, container_type, image_or_video, device format=datapoints.BoundingBoxFormat.CXCYWH, spatial_size=spatial_size, ), - detection_mask=make_detection_mask(spatial_size=spatial_size), - segmentation_mask=make_segmentation_mask(spatial_size=spatial_size), + detection_mask=make_detection_mask(size=spatial_size), + segmentation_mask=make_segmentation_mask(size=spatial_size), int=0, float=0.0, bool=True, @@ -353,7 +353,7 @@ def test_random_resized_crop(self, transform, input): next(make_vanilla_tensor_images()), next(make_vanilla_tensor_images()), next(make_pil_images()), - make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), + make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), next(make_videos()), ], 3, @@ -1347,8 +1347,8 @@ class TestToDtype: ) def test_call(self, dtype, expected_dtypes): sample = dict( - video=make_video(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.int64), - image=make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.uint8), + video=make_video(size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.int64), + image=make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE, dtype=torch.uint8), bounding_box=make_bounding_box(format=datapoints.BoundingBoxFormat.XYXY, dtype=torch.float32), str="str", int=0, diff --git a/test/test_transforms_v2_consistency.py b/test/test_transforms_v2_consistency.py index f65a3407b7c..3f631d7ac94 100644 --- a/test/test_transforms_v2_consistency.py +++ b/test/test_transforms_v2_consistency.py @@ -711,11 +711,11 @@ def test_call_consistency(config, args_kwargs): for transform_cls, get_params_args_kwargs in [ ( v2_transforms.RandomResizedCrop, - ArgsKwargs(make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=[0.3, 0.7], ratio=[0.5, 1.5]), + ArgsKwargs(make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=[0.3, 0.7], ratio=[0.5, 1.5]), ), ( v2_transforms.RandomErasing, - ArgsKwargs(make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=(0.3, 0.7), ratio=(0.5, 1.5)), + ArgsKwargs(make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE), scale=(0.3, 0.7), ratio=(0.5, 1.5)), ), (v2_transforms.ColorJitter, ArgsKwargs(brightness=None, contrast=None, saturation=None, hue=None)), (v2_transforms.ElasticTransform, ArgsKwargs(alpha=[15.3, 27.2], sigma=[2.5, 3.9], size=[17, 31])), @@ -724,7 +724,7 @@ def test_call_consistency(config, args_kwargs): v2_transforms.RandomAffine, ArgsKwargs(degrees=[-20.0, 10.0], translate=None, scale_ranges=None, shears=None, img_size=[15, 29]), ), - (v2_transforms.RandomCrop, ArgsKwargs(make_image(spatial_size=(61, 47)), output_size=(19, 25))), + (v2_transforms.RandomCrop, ArgsKwargs(make_image(size=(61, 47)), output_size=(19, 25))), (v2_transforms.RandomPerspective, ArgsKwargs(23, 17, 0.5)), (v2_transforms.RandomRotation, ArgsKwargs(degrees=[-20.0, 10.0])), (v2_transforms.AutoAugment, ArgsKwargs(5)), @@ -1095,33 +1095,33 @@ def make_datapoints(self, with_mask=True): def make_label(extra_dims, categories): return torch.randint(categories, extra_dims, dtype=torch.int64) - pil_image = to_image_pil(make_image(spatial_size=size, color_space="RGB")) + pil_image = to_image_pil(make_image(size=size, color_space="RGB")) target = { "boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float), "labels": make_label(extra_dims=(num_objects,), categories=80), } if with_mask: - target["masks"] = make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long) + target["masks"] = make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long) yield (pil_image, target) - tensor_image = torch.Tensor(make_image(spatial_size=size, color_space="RGB", dtype=torch.float32)) + tensor_image = torch.Tensor(make_image(size=size, color_space="RGB", dtype=torch.float32)) target = { "boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float), "labels": make_label(extra_dims=(num_objects,), categories=80), } if with_mask: - target["masks"] = make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long) + target["masks"] = make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long) yield (tensor_image, target) - datapoint_image = make_image(spatial_size=size, color_space="RGB", dtype=torch.float32) + datapoint_image = make_image(size=size, color_space="RGB", dtype=torch.float32) target = { "boxes": make_bounding_box(spatial_size=size, format="XYXY", batch_dims=(num_objects,), dtype=torch.float), "labels": make_label(extra_dims=(num_objects,), categories=80), } if with_mask: - target["masks"] = make_detection_mask(spatial_size=size, num_objects=num_objects, dtype=torch.long) + target["masks"] = make_detection_mask(size=size, num_objects=num_objects, dtype=torch.long) yield (datapoint_image, target) @@ -1203,8 +1203,8 @@ def make_datapoints(self, supports_pil=True, image_dtype=torch.uint8): conv_fns.extend([torch.Tensor, lambda x: x]) for conv_fn in conv_fns: - datapoint_image = make_image(spatial_size=size, color_space="RGB", dtype=image_dtype) - datapoint_mask = make_segmentation_mask(spatial_size=size, num_categories=num_categories, dtype=torch.uint8) + datapoint_image = make_image(size=size, color_space="RGB", dtype=image_dtype) + datapoint_mask = make_segmentation_mask(size=size, num_categories=num_categories, dtype=torch.uint8) dp = (conv_fn(datapoint_image), datapoint_mask) dp_ref = ( diff --git a/test/test_transforms_v2_refactored.py b/test/test_transforms_v2_refactored.py index 57fce82f254..e44cd5e9264 100644 --- a/test/test_transforms_v2_refactored.py +++ b/test/test_transforms_v2_refactored.py @@ -316,7 +316,7 @@ def make_input(input_type, *, mask_type="segmentation", **kwargs): elif input_type is PIL.Image.Image: input = F.to_image_pil(input) elif input_type is datapoints.BoundingBox: - input = make_bounding_box() + input = make_bounding_box(**kwargs) elif input_type is datapoints.Mask: make_mask = { "segmentation": make_segmentation_mask, @@ -506,7 +506,7 @@ def test_kernel_image_tensor(self, size, interpolation, use_max_size, antialias, check_kernel( F.resize_image_tensor, - make_input(datapoints.Image, dtype=dtype, device=device, spatial_size=self.INPUT_SIZE), + make_input(datapoints.Image, dtype=dtype, device=device, size=self.INPUT_SIZE), size=size, interpolation=interpolation, **max_size_kwarg, @@ -540,14 +540,14 @@ def test_kernel_bounding_box(self, format, size, use_max_size, dtype, device): def test_kernel_mask(self, mask_type): check_kernel( F.resize_mask, - make_input(datapoints.Mask, spatial_size=self.INPUT_SIZE, mask_type=mask_type), + make_input(datapoints.Mask, size=self.INPUT_SIZE, mask_type=mask_type), size=self.OUTPUT_SIZES[-1], ) def test_kernel_video(self): check_kernel( F.resize_video, - make_input(datapoints.Video, spatial_size=self.INPUT_SIZE), + make_input(datapoints.Video, size=self.INPUT_SIZE), size=self.OUTPUT_SIZES[-1], antialias=True, ) @@ -568,7 +568,7 @@ def test_dispatcher(self, size, input_type, kernel): check_dispatcher( F.resize, kernel, - make_input(input_type, spatial_size=self.INPUT_SIZE), + make_input(input_type, size=self.INPUT_SIZE), size=size, antialias=True, check_scripted_smoke=not isinstance(size, int), @@ -595,7 +595,7 @@ def test_dispatcher_signature(self, kernel, input_type): [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video], ) def test_transform(self, size, device, input_type): - input = make_input(input_type, device=device, spatial_size=self.INPUT_SIZE) + input = make_input(input_type, device=device, size=self.INPUT_SIZE) check_transform( transforms.Resize, @@ -619,7 +619,7 @@ def test_image_correctness(self, size, interpolation, use_max_size, fn): if not (max_size_kwarg := self._make_max_size_kwarg(use_max_size=use_max_size, size=size)): return - image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu", spatial_size=self.INPUT_SIZE) + image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu", size=self.INPUT_SIZE) actual = fn(image, size=size, interpolation=interpolation, **max_size_kwarg, antialias=True) expected = F.to_image_tensor( @@ -662,7 +662,7 @@ def test_bounding_box_correctness(self, format, size, use_max_size, fn): if not (max_size_kwarg := self._make_max_size_kwarg(use_max_size=use_max_size, size=size)): return - bounding_box = make_input(datapoints.BoundingBox, spatial_size=self.INPUT_SIZE) + bounding_box = make_input(datapoints.BoundingBox, size=self.INPUT_SIZE) actual = fn(bounding_box, size=size, **max_size_kwarg) expected = self._reference_resize_bounding_box(bounding_box, size=size, **max_size_kwarg) @@ -676,7 +676,7 @@ def test_bounding_box_correctness(self, format, size, use_max_size, fn): [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.Video], ) def test_pil_interpolation_compat_smoke(self, interpolation, input_type): - input = make_input(input_type, spatial_size=self.INPUT_SIZE) + input = make_input(input_type, size=self.INPUT_SIZE) with ( contextlib.nullcontext() @@ -692,9 +692,7 @@ def test_pil_interpolation_compat_smoke(self, interpolation, input_type): def test_dispatcher_pil_antialias_warning(self): with pytest.warns(UserWarning, match="Anti-alias option is always applied for PIL Image input"): - F.resize( - make_input(PIL.Image.Image, spatial_size=self.INPUT_SIZE), size=self.OUTPUT_SIZES[0], antialias=False - ) + F.resize(make_input(PIL.Image.Image, size=self.INPUT_SIZE), size=self.OUTPUT_SIZES[0], antialias=False) @pytest.mark.parametrize("size", OUTPUT_SIZES) @pytest.mark.parametrize( @@ -711,7 +709,7 @@ def test_max_size_error(self, size, input_type): match = "size should be an int or a sequence of length 1" with pytest.raises(ValueError, match=match): - F.resize(make_input(input_type, spatial_size=self.INPUT_SIZE), size=size, max_size=max_size, antialias=True) + F.resize(make_input(input_type, size=self.INPUT_SIZE), size=size, max_size=max_size, antialias=True) @pytest.mark.parametrize("interpolation", INTERPOLATION_MODES) @pytest.mark.parametrize( @@ -725,7 +723,7 @@ def test_antialias_warning(self, interpolation, input_type): else assert_no_warnings() ): F.resize( - make_input(input_type, spatial_size=self.INPUT_SIZE), + make_input(input_type, size=self.INPUT_SIZE), size=self.OUTPUT_SIZES[0], interpolation=interpolation, ) @@ -742,7 +740,7 @@ def test_interpolation_int(self, interpolation, input_type): if issubclass(input_type, torch.Tensor) and interpolation is transforms.InterpolationMode.NEAREST_EXACT: return - input = make_input(input_type, spatial_size=self.INPUT_SIZE) + input = make_input(input_type, size=self.INPUT_SIZE) expected = F.resize(input, size=self.OUTPUT_SIZES[0], interpolation=interpolation, antialias=True) actual = F.resize( @@ -763,7 +761,10 @@ def test_transform_unknown_size_error(self): [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video], ) def test_noop(self, size, input_type): - input = make_input(input_type, spatial_size=self.INPUT_SIZE) + input = make_input( + input_type, + **{"spatial_size" if issubclass(input_type, datapoints.BoundingBox) else "size": self.INPUT_SIZE}, + ) output = F.resize(input, size=size, antialias=True) @@ -785,7 +786,7 @@ def test_no_regression_5405(self, input_type): # Checks that `max_size` is not ignored if `size == small_edge_size` # See https://github.com/pytorch/vision/issues/5405 - input = make_input(input_type, spatial_size=self.INPUT_SIZE) + input = make_input(input_type, size=self.INPUT_SIZE) size = min(F.get_spatial_size(input)) max_size = size + 1 diff --git a/test/test_transforms_v2_utils.py b/test/test_transforms_v2_utils.py index ad30c223530..98271b893d6 100644 --- a/test/test_transforms_v2_utils.py +++ b/test/test_transforms_v2_utils.py @@ -11,9 +11,9 @@ from torchvision.transforms.v2.utils import has_all, has_any -IMAGE = make_image(spatial_size=DEFAULT_PORTRAIT_SPATIAL_SIZE, color_space="RGB") +IMAGE = make_image(size=DEFAULT_PORTRAIT_SPATIAL_SIZE, color_space="RGB") BOUNDING_BOX = make_bounding_box(format=datapoints.BoundingBoxFormat.XYXY, spatial_size=IMAGE.spatial_size) -MASK = make_detection_mask(spatial_size=IMAGE.spatial_size) +MASK = make_detection_mask(size=IMAGE.spatial_size) @pytest.mark.parametrize(