From 07b3058aa9a340d6e6f52e29510da12ac00d1b59 Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Thu, 3 Nov 2022 18:20:24 +0000 Subject: [PATCH 1/6] Restore BC on perspective --- test/prototype_transforms_kernel_infos.py | 14 ++--- test/test_prototype_transforms.py | 4 +- test/test_prototype_transforms_functional.py | 4 +- .../prototype/features/_bounding_box.py | 8 ++- torchvision/prototype/features/_feature.py | 4 +- torchvision/prototype/features/_image.py | 6 +- torchvision/prototype/features/_mask.py | 8 ++- torchvision/prototype/features/_video.py | 6 +- torchvision/prototype/transforms/_geometry.py | 6 +- .../transforms/functional/_geometry.py | 58 ++++++++++++++----- 10 files changed, 83 insertions(+), 35 deletions(-) diff --git a/test/prototype_transforms_kernel_infos.py b/test/prototype_transforms_kernel_infos.py index a106aea65ba..1fd6581f528 100644 --- a/test/prototype_transforms_kernel_infos.py +++ b/test/prototype_transforms_kernel_infos.py @@ -1184,38 +1184,38 @@ def reference_inputs_pad_bounding_box(): def sample_inputs_perspective_image_tensor(): for image_loader in make_image_loaders(sizes=["random"]): for fill in [None, 128.0, 128, [12.0], [12.0 + c for c in range(image_loader.num_channels)]]: - yield ArgsKwargs(image_loader, fill=fill, perspective_coeffs=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(image_loader, None, None, fill=fill, coefficients=_PERSPECTIVE_COEFFS[0]) def reference_inputs_perspective_image_tensor(): - for image_loader, perspective_coeffs in itertools.product(make_image_loaders(extra_dims=[()]), _PERSPECTIVE_COEFFS): + for image_loader, coefficients in itertools.product(make_image_loaders(extra_dims=[()]), _PERSPECTIVE_COEFFS): # FIXME: PIL kernel doesn't support sequences of length 1 if the number of channels is larger. Shouldn't it? for fill in [None, 128.0, 128, [12.0 + c for c in range(image_loader.num_channels)]]: - yield ArgsKwargs(image_loader, fill=fill, perspective_coeffs=perspective_coeffs) + yield ArgsKwargs(image_loader, None, None, fill=fill, coefficients=coefficients) def sample_inputs_perspective_bounding_box(): for bounding_box_loader in make_bounding_box_loaders(): yield ArgsKwargs( - bounding_box_loader, format=bounding_box_loader.format, perspective_coeffs=_PERSPECTIVE_COEFFS[0] + bounding_box_loader, bounding_box_loader.format, None, None, coefficients=_PERSPECTIVE_COEFFS[0] ) def sample_inputs_perspective_mask(): for mask_loader in make_mask_loaders(sizes=["random"]): - yield ArgsKwargs(mask_loader, perspective_coeffs=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(mask_loader, None, None, coefficients=_PERSPECTIVE_COEFFS[0]) def reference_inputs_perspective_mask(): for mask_loader, perspective_coeffs in itertools.product( make_mask_loaders(extra_dims=[()], num_objects=[1]), _PERSPECTIVE_COEFFS ): - yield ArgsKwargs(mask_loader, perspective_coeffs=perspective_coeffs) + yield ArgsKwargs(mask_loader, None, None, coefficients=perspective_coeffs) def sample_inputs_perspective_video(): for video_loader in make_video_loaders(sizes=["random"], num_frames=["random"]): - yield ArgsKwargs(video_loader, perspective_coeffs=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(video_loader, None, None, coefficients=_PERSPECTIVE_COEFFS[0]) KERNEL_INFOS.extend( diff --git a/test/test_prototype_transforms.py b/test/test_prototype_transforms.py index fab4cc0ddd6..2793171df84 100644 --- a/test/test_prototype_transforms.py +++ b/test/test_prototype_transforms.py @@ -917,8 +917,8 @@ def test__get_params(self, mocker): params = transform._get_params([image]) h, w = image.spatial_size - assert "perspective_coeffs" in params - assert len(params["perspective_coeffs"]) == 8 + assert "coefficients" in params + assert len(params["coefficients"]) == 8 @pytest.mark.parametrize("distortion_scale", [0.1, 0.7]) def test__transform(self, distortion_scale, mocker): diff --git a/test/test_prototype_transforms_functional.py b/test/test_prototype_transforms_functional.py index 20f5e5330ff..9917fea8218 100644 --- a/test/test_prototype_transforms_functional.py +++ b/test/test_prototype_transforms_functional.py @@ -874,7 +874,9 @@ def _compute_expected_bbox(bbox, pcoeffs_): output_bboxes = F.perspective_bounding_box( bboxes, bboxes_format, - perspective_coeffs=pcoeffs, + None, + None, + coefficients=pcoeffs, ) if bboxes.ndim < 2: diff --git a/torchvision/prototype/features/_bounding_box.py b/torchvision/prototype/features/_bounding_box.py index 638759ae850..13dcdb4b742 100644 --- a/torchvision/prototype/features/_bounding_box.py +++ b/torchvision/prototype/features/_bounding_box.py @@ -169,11 +169,15 @@ def affine( def perspective( self, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> BoundingBox: - output = self._F.perspective_bounding_box(self.as_subclass(torch.Tensor), self.format, perspective_coeffs) + output = self._F.perspective_bounding_box( + self.as_subclass(torch.Tensor), startpoints, endpoints, self.format, coefficients=coefficients + ) return BoundingBox.wrap_like(self, output) def elastic( diff --git a/torchvision/prototype/features/_feature.py b/torchvision/prototype/features/_feature.py index 1cc2d8d4bb7..9893e24d751 100644 --- a/torchvision/prototype/features/_feature.py +++ b/torchvision/prototype/features/_feature.py @@ -218,9 +218,11 @@ def affine( def perspective( self, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> _Feature: return self diff --git a/torchvision/prototype/features/_image.py b/torchvision/prototype/features/_image.py index 74904294f59..eb9253a1ecd 100644 --- a/torchvision/prototype/features/_image.py +++ b/torchvision/prototype/features/_image.py @@ -206,12 +206,14 @@ def affine( def perspective( self, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> Image: output = self._F.perspective_image_tensor( - self.as_subclass(torch.Tensor), perspective_coeffs, interpolation=interpolation, fill=fill + self.as_subclass(torch.Tensor), startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients ) return Image.wrap_like(self, output) diff --git a/torchvision/prototype/features/_mask.py b/torchvision/prototype/features/_mask.py index a297c43c20f..1962a8d64eb 100644 --- a/torchvision/prototype/features/_mask.py +++ b/torchvision/prototype/features/_mask.py @@ -118,11 +118,15 @@ def affine( def perspective( self, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> Mask: - output = self._F.perspective_mask(self.as_subclass(torch.Tensor), perspective_coeffs, fill=fill) + output = self._F.perspective_mask( + self.as_subclass(torch.Tensor), startpoints, endpoints, fill=fill, coefficients=coefficients + ) return Mask.wrap_like(self, output) def elastic( diff --git a/torchvision/prototype/features/_video.py b/torchvision/prototype/features/_video.py index a2311678304..9079b2109bd 100644 --- a/torchvision/prototype/features/_video.py +++ b/torchvision/prototype/features/_video.py @@ -166,12 +166,14 @@ def affine( def perspective( self, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> Video: output = self._F.perspective_video( - self.as_subclass(torch.Tensor), perspective_coeffs, interpolation=interpolation, fill=fill + self.as_subclass(torch.Tensor), startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients ) return Video.wrap_like(self, output) diff --git a/torchvision/prototype/transforms/_geometry.py b/torchvision/prototype/transforms/_geometry.py index 214296d0350..bda25048500 100644 --- a/torchvision/prototype/transforms/_geometry.py +++ b/torchvision/prototype/transforms/_geometry.py @@ -524,15 +524,17 @@ def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]: startpoints = [[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]] endpoints = [topleft, topright, botright, botleft] perspective_coeffs = _get_perspective_coeffs(startpoints, endpoints) - return dict(perspective_coeffs=perspective_coeffs) + return dict(coefficients=perspective_coeffs) def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any: fill = self.fill[type(inpt)] return F.perspective( inpt, - **params, + None, + None, fill=fill, interpolation=self.interpolation, + **params, ) diff --git a/torchvision/prototype/transforms/functional/_geometry.py b/torchvision/prototype/transforms/functional/_geometry.py index f2a12d6f609..b6e5ead8c91 100644 --- a/torchvision/prototype/transforms/functional/_geometry.py +++ b/torchvision/prototype/transforms/functional/_geometry.py @@ -11,6 +11,7 @@ from torchvision.transforms.functional import ( _compute_resized_output_size as __compute_resized_output_size, _get_inverse_affine_matrix, + _get_perspective_coeffs, InterpolationMode, pil_modes_mapping, pil_to_tensor, @@ -906,12 +907,30 @@ def crop(inpt: features.InputTypeJIT, top: int, left: int, height: int, width: i return crop_image_pil(inpt, top, left, height, width) +def _perspective_coefficients( + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], + coefficients: Optional[List[float]], +) -> List[float]: + if coefficients is not None: + if len(coefficients) != 8: + raise ValueError("Argument coefficients should have 8 float values") + return coefficients + elif startpoints is not None and endpoints is not None: + return _get_perspective_coeffs(startpoints, endpoints) + else: + raise ValueError("Either the startpoints/endpoints or the coefficients must have non `None` values.") + + def perspective_image_tensor( image: torch.Tensor, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: features.FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> torch.Tensor: + perspective_coeffs = _perspective_coefficients(startpoints, endpoints, coefficients) if image.numel() == 0: return image @@ -934,21 +953,24 @@ def perspective_image_tensor( @torch.jit.unused def perspective_image_pil( image: PIL.Image.Image, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BICUBIC, fill: features.FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> PIL.Image.Image: + perspective_coeffs = _perspective_coefficients(startpoints, endpoints, coefficients) return _FP.perspective(image, perspective_coeffs, interpolation=pil_modes_mapping[interpolation], fill=fill) def perspective_bounding_box( bounding_box: torch.Tensor, format: features.BoundingBoxFormat, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], + coefficients: Optional[List[float]] = None, ) -> torch.Tensor: - - if len(perspective_coeffs) != 8: - raise ValueError("Argument perspective_coeffs should have 8 float values") + perspective_coeffs = _perspective_coefficients(startpoints, endpoints, coefficients) original_shape = bounding_box.shape bounding_box = ( @@ -1029,8 +1051,10 @@ def perspective_bounding_box( def perspective_mask( mask: torch.Tensor, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], fill: features.FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> torch.Tensor: if mask.ndim < 3: mask = mask.unsqueeze(0) @@ -1039,7 +1063,7 @@ def perspective_mask( needs_squeeze = False output = perspective_image_tensor( - mask, perspective_coeffs=perspective_coeffs, interpolation=InterpolationMode.NEAREST, fill=fill + mask, startpoints, endpoints, interpolation=InterpolationMode.NEAREST, fill=fill, coefficients=coefficients ) if needs_squeeze: @@ -1050,25 +1074,31 @@ def perspective_mask( def perspective_video( video: torch.Tensor, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: features.FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> torch.Tensor: - return perspective_image_tensor(video, perspective_coeffs, interpolation=interpolation, fill=fill) + return perspective_image_tensor( + video, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients + ) def perspective( inpt: features.InputTypeJIT, - perspective_coeffs: List[float], + startpoints: Optional[List[List[int]]], + endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.BILINEAR, fill: features.FillTypeJIT = None, + coefficients: Optional[List[float]] = None, ) -> features.InputTypeJIT: if isinstance(inpt, torch.Tensor) and (torch.jit.is_scripting() or not isinstance(inpt, features._Feature)): - return perspective_image_tensor(inpt, perspective_coeffs, interpolation=interpolation, fill=fill) + return perspective_image_tensor(inpt, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients) elif isinstance(inpt, features._Feature): - return inpt.perspective(perspective_coeffs, interpolation=interpolation, fill=fill) + return inpt.perspective(startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients) else: - return perspective_image_pil(inpt, perspective_coeffs, interpolation=interpolation, fill=fill) + return perspective_image_pil(inpt, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients) def elastic_image_tensor( From a8bed02a70420b33b072ede8fc052db1de87b129 Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Thu, 3 Nov 2022 18:34:10 +0000 Subject: [PATCH 2/6] Fixes linter --- torchvision/prototype/features/_image.py | 7 ++++++- torchvision/prototype/features/_video.py | 7 ++++++- .../prototype/transforms/functional/_geometry.py | 12 +++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/torchvision/prototype/features/_image.py b/torchvision/prototype/features/_image.py index eb9253a1ecd..1e9c4623d93 100644 --- a/torchvision/prototype/features/_image.py +++ b/torchvision/prototype/features/_image.py @@ -213,7 +213,12 @@ def perspective( coefficients: Optional[List[float]] = None, ) -> Image: output = self._F.perspective_image_tensor( - self.as_subclass(torch.Tensor), startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients + self.as_subclass(torch.Tensor), + startpoints, + endpoints, + interpolation=interpolation, + fill=fill, + coefficients=coefficients, ) return Image.wrap_like(self, output) diff --git a/torchvision/prototype/features/_video.py b/torchvision/prototype/features/_video.py index 9079b2109bd..0d0961d77be 100644 --- a/torchvision/prototype/features/_video.py +++ b/torchvision/prototype/features/_video.py @@ -173,7 +173,12 @@ def perspective( coefficients: Optional[List[float]] = None, ) -> Video: output = self._F.perspective_video( - self.as_subclass(torch.Tensor), startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients + self.as_subclass(torch.Tensor), + startpoints, + endpoints, + interpolation=interpolation, + fill=fill, + coefficients=coefficients, ) return Video.wrap_like(self, output) diff --git a/torchvision/prototype/transforms/functional/_geometry.py b/torchvision/prototype/transforms/functional/_geometry.py index b6e5ead8c91..cdb7bab1ad3 100644 --- a/torchvision/prototype/transforms/functional/_geometry.py +++ b/torchvision/prototype/transforms/functional/_geometry.py @@ -1094,11 +1094,17 @@ def perspective( coefficients: Optional[List[float]] = None, ) -> features.InputTypeJIT: if isinstance(inpt, torch.Tensor) and (torch.jit.is_scripting() or not isinstance(inpt, features._Feature)): - return perspective_image_tensor(inpt, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients) + return perspective_image_tensor( + inpt, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients + ) elif isinstance(inpt, features._Feature): - return inpt.perspective(startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients) + return inpt.perspective( + startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients + ) else: - return perspective_image_pil(inpt, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients) + return perspective_image_pil( + inpt, startpoints, endpoints, interpolation=interpolation, fill=fill, coefficients=coefficients + ) def elastic_image_tensor( From 030f7d3eb5c353ecbf922cd9c48ebadbefcc16be Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Thu, 3 Nov 2022 18:41:53 +0000 Subject: [PATCH 3/6] Fixing tests. --- test/test_prototype_transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_prototype_transforms.py b/test/test_prototype_transforms.py index 2793171df84..9ba5c856421 100644 --- a/test/test_prototype_transforms.py +++ b/test/test_prototype_transforms.py @@ -940,7 +940,7 @@ def test__transform(self, distortion_scale, mocker): params = transform._get_params([inpt]) fill = transforms._utils._convert_fill_arg(fill) - fn.assert_called_once_with(inpt, **params, fill=fill, interpolation=interpolation) + fn.assert_called_once_with(inpt, None, None, **params, fill=fill, interpolation=interpolation) class TestElasticTransform: From 217ef2ace9b2ff1494c521d7c26778842496a69c Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Fri, 4 Nov 2022 09:32:28 +0000 Subject: [PATCH 4/6] Apply code-review changes. --- test/prototype_transforms_kernel_infos.py | 18 ++++++++++++------ test/test_prototype_transforms_functional.py | 4 ++-- .../transforms/functional/_geometry.py | 7 +++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/test/prototype_transforms_kernel_infos.py b/test/prototype_transforms_kernel_infos.py index 1fd6581f528..6ce26412659 100644 --- a/test/prototype_transforms_kernel_infos.py +++ b/test/prototype_transforms_kernel_infos.py @@ -1184,38 +1184,44 @@ def reference_inputs_pad_bounding_box(): def sample_inputs_perspective_image_tensor(): for image_loader in make_image_loaders(sizes=["random"]): for fill in [None, 128.0, 128, [12.0], [12.0 + c for c in range(image_loader.num_channels)]]: - yield ArgsKwargs(image_loader, None, None, fill=fill, coefficients=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs( + image_loader, startpoints=None, endpoints=None, fill=fill, coefficients=_PERSPECTIVE_COEFFS[0] + ) def reference_inputs_perspective_image_tensor(): for image_loader, coefficients in itertools.product(make_image_loaders(extra_dims=[()]), _PERSPECTIVE_COEFFS): # FIXME: PIL kernel doesn't support sequences of length 1 if the number of channels is larger. Shouldn't it? for fill in [None, 128.0, 128, [12.0 + c for c in range(image_loader.num_channels)]]: - yield ArgsKwargs(image_loader, None, None, fill=fill, coefficients=coefficients) + yield ArgsKwargs(image_loader, startpoints=None, endpoints=None, fill=fill, coefficients=coefficients) def sample_inputs_perspective_bounding_box(): for bounding_box_loader in make_bounding_box_loaders(): yield ArgsKwargs( - bounding_box_loader, bounding_box_loader.format, None, None, coefficients=_PERSPECTIVE_COEFFS[0] + bounding_box_loader, + bounding_box_loader.format, + startpoints=None, + endpoints=None, + coefficients=_PERSPECTIVE_COEFFS[0], ) def sample_inputs_perspective_mask(): for mask_loader in make_mask_loaders(sizes=["random"]): - yield ArgsKwargs(mask_loader, None, None, coefficients=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(mask_loader, startpoints=None, endpoints=None, coefficients=_PERSPECTIVE_COEFFS[0]) def reference_inputs_perspective_mask(): for mask_loader, perspective_coeffs in itertools.product( make_mask_loaders(extra_dims=[()], num_objects=[1]), _PERSPECTIVE_COEFFS ): - yield ArgsKwargs(mask_loader, None, None, coefficients=perspective_coeffs) + yield ArgsKwargs(mask_loader, startpoints=None, endpoints=None, coefficients=perspective_coeffs) def sample_inputs_perspective_video(): for video_loader in make_video_loaders(sizes=["random"], num_frames=["random"]): - yield ArgsKwargs(video_loader, None, None, coefficients=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(video_loader, startpoints=None, endpoints=None, coefficients=_PERSPECTIVE_COEFFS[0]) KERNEL_INFOS.extend( diff --git a/test/test_prototype_transforms_functional.py b/test/test_prototype_transforms_functional.py index 9917fea8218..54228b2bf07 100644 --- a/test/test_prototype_transforms_functional.py +++ b/test/test_prototype_transforms_functional.py @@ -874,8 +874,8 @@ def _compute_expected_bbox(bbox, pcoeffs_): output_bboxes = F.perspective_bounding_box( bboxes, bboxes_format, - None, - None, + startpoints=None, + endpoints=None, coefficients=pcoeffs, ) diff --git a/torchvision/prototype/transforms/functional/_geometry.py b/torchvision/prototype/transforms/functional/_geometry.py index cdb7bab1ad3..82e05248606 100644 --- a/torchvision/prototype/transforms/functional/_geometry.py +++ b/torchvision/prototype/transforms/functional/_geometry.py @@ -912,11 +912,14 @@ def _perspective_coefficients( endpoints: Optional[List[List[int]]], coefficients: Optional[List[float]], ) -> List[float]: + has_points = startpoints is not None and endpoints is not None if coefficients is not None: - if len(coefficients) != 8: + if has_points: + raise ValueError("The startpoints/endpoints and the coefficients shouldn't be defined concurrently.") + elif len(coefficients) != 8: raise ValueError("Argument coefficients should have 8 float values") return coefficients - elif startpoints is not None and endpoints is not None: + elif has_points: return _get_perspective_coeffs(startpoints, endpoints) else: raise ValueError("Either the startpoints/endpoints or the coefficients must have non `None` values.") From 8fd80c56a4e7af28d18bddc26058c8ab45557d8a Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Fri, 4 Nov 2022 09:46:42 +0000 Subject: [PATCH 5/6] Pleasing mypy. --- torchvision/prototype/transforms/functional/_geometry.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/torchvision/prototype/transforms/functional/_geometry.py b/torchvision/prototype/transforms/functional/_geometry.py index 82e05248606..24d4303e4ce 100644 --- a/torchvision/prototype/transforms/functional/_geometry.py +++ b/torchvision/prototype/transforms/functional/_geometry.py @@ -912,14 +912,13 @@ def _perspective_coefficients( endpoints: Optional[List[List[int]]], coefficients: Optional[List[float]], ) -> List[float]: - has_points = startpoints is not None and endpoints is not None if coefficients is not None: - if has_points: + if startpoints is not None and endpoints is not None: raise ValueError("The startpoints/endpoints and the coefficients shouldn't be defined concurrently.") elif len(coefficients) != 8: raise ValueError("Argument coefficients should have 8 float values") return coefficients - elif has_points: + elif startpoints is not None and endpoints is not None: return _get_perspective_coeffs(startpoints, endpoints) else: raise ValueError("Either the startpoints/endpoints or the coefficients must have non `None` values.") From dbc3d0964085d6ee656ae5ac3ea98674cc0af97a Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Fri, 4 Nov 2022 10:13:51 +0000 Subject: [PATCH 6/6] Revert named parameters. --- test/prototype_transforms_kernel_infos.py | 18 ++++++------------ test/test_prototype_transforms_functional.py | 4 ++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/test/prototype_transforms_kernel_infos.py b/test/prototype_transforms_kernel_infos.py index 6ce26412659..1fd6581f528 100644 --- a/test/prototype_transforms_kernel_infos.py +++ b/test/prototype_transforms_kernel_infos.py @@ -1184,44 +1184,38 @@ def reference_inputs_pad_bounding_box(): def sample_inputs_perspective_image_tensor(): for image_loader in make_image_loaders(sizes=["random"]): for fill in [None, 128.0, 128, [12.0], [12.0 + c for c in range(image_loader.num_channels)]]: - yield ArgsKwargs( - image_loader, startpoints=None, endpoints=None, fill=fill, coefficients=_PERSPECTIVE_COEFFS[0] - ) + yield ArgsKwargs(image_loader, None, None, fill=fill, coefficients=_PERSPECTIVE_COEFFS[0]) def reference_inputs_perspective_image_tensor(): for image_loader, coefficients in itertools.product(make_image_loaders(extra_dims=[()]), _PERSPECTIVE_COEFFS): # FIXME: PIL kernel doesn't support sequences of length 1 if the number of channels is larger. Shouldn't it? for fill in [None, 128.0, 128, [12.0 + c for c in range(image_loader.num_channels)]]: - yield ArgsKwargs(image_loader, startpoints=None, endpoints=None, fill=fill, coefficients=coefficients) + yield ArgsKwargs(image_loader, None, None, fill=fill, coefficients=coefficients) def sample_inputs_perspective_bounding_box(): for bounding_box_loader in make_bounding_box_loaders(): yield ArgsKwargs( - bounding_box_loader, - bounding_box_loader.format, - startpoints=None, - endpoints=None, - coefficients=_PERSPECTIVE_COEFFS[0], + bounding_box_loader, bounding_box_loader.format, None, None, coefficients=_PERSPECTIVE_COEFFS[0] ) def sample_inputs_perspective_mask(): for mask_loader in make_mask_loaders(sizes=["random"]): - yield ArgsKwargs(mask_loader, startpoints=None, endpoints=None, coefficients=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(mask_loader, None, None, coefficients=_PERSPECTIVE_COEFFS[0]) def reference_inputs_perspective_mask(): for mask_loader, perspective_coeffs in itertools.product( make_mask_loaders(extra_dims=[()], num_objects=[1]), _PERSPECTIVE_COEFFS ): - yield ArgsKwargs(mask_loader, startpoints=None, endpoints=None, coefficients=perspective_coeffs) + yield ArgsKwargs(mask_loader, None, None, coefficients=perspective_coeffs) def sample_inputs_perspective_video(): for video_loader in make_video_loaders(sizes=["random"], num_frames=["random"]): - yield ArgsKwargs(video_loader, startpoints=None, endpoints=None, coefficients=_PERSPECTIVE_COEFFS[0]) + yield ArgsKwargs(video_loader, None, None, coefficients=_PERSPECTIVE_COEFFS[0]) KERNEL_INFOS.extend( diff --git a/test/test_prototype_transforms_functional.py b/test/test_prototype_transforms_functional.py index 54228b2bf07..9917fea8218 100644 --- a/test/test_prototype_transforms_functional.py +++ b/test/test_prototype_transforms_functional.py @@ -874,8 +874,8 @@ def _compute_expected_bbox(bbox, pcoeffs_): output_bboxes = F.perspective_bounding_box( bboxes, bboxes_format, - startpoints=None, - endpoints=None, + None, + None, coefficients=pcoeffs, )