Skip to content

Commit aafb356

Browse files
committed
Replacing Sequencies with Lists.
1 parent 1e301b1 commit aafb356

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

torchvision/prototype/transforms/functional/_geometry.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numbers
22
import warnings
3-
from typing import List, Optional, Sequence, Tuple, Union
3+
from typing import List, Optional, Tuple, Union
44

55
import PIL.Image
66
import torch
@@ -117,14 +117,12 @@ def resize_image_tensor(
117117
@torch.jit.unused
118118
def resize_image_pil(
119119
img: PIL.Image.Image,
120-
size: Union[Sequence[int], int],
120+
size: Union[List[int], int],
121121
interpolation: InterpolationMode = InterpolationMode.BILINEAR,
122122
max_size: Optional[int] = None,
123123
) -> PIL.Image.Image:
124124
if isinstance(size, int):
125125
size = [size, size]
126-
# Explicitly cast size to list otherwise mypy issue: incompatible type "Sequence[int]"; expected "List[int]"
127-
size: List[int] = list(size)
128126
size = _compute_resized_output_size(img.size[::-1], size=size, max_size=max_size)
129127
return _FP.resize(img, size, interpolation=pil_modes_mapping[interpolation])
130128

@@ -261,7 +259,7 @@ def affine_image_pil(
261259
scale: float,
262260
shear: List[float],
263261
interpolation: InterpolationMode = InterpolationMode.NEAREST,
264-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
262+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
265263
center: Optional[List[float]] = None,
266264
) -> PIL.Image.Image:
267265
angle, translate, shear, center = _affine_parse_args(angle, translate, scale, shear, interpolation, center)
@@ -403,15 +401,15 @@ def affine_mask(
403401
return output
404402

405403

406-
def _convert_fill_arg(fill: Optional[Union[int, float, Sequence[int], Sequence[float]]]) -> Optional[List[float]]:
404+
def _convert_fill_arg(fill: Optional[Union[int, float, List[int], List[float]]]) -> Optional[List[float]]:
407405
# Fill = 0 is not equivalent to None, https://github.com/pytorch/vision/issues/6517
408406
# So, we can't reassign fill to 0
409407
# if fill is None:
410408
# fill = 0
411409
if fill is None:
412410
return fill
413411

414-
# This cast does Sequence -> List[float] to please mypy and torch.jit.script
412+
# This cast does List -> List[float] to please mypy and torch.jit.script
415413
if not isinstance(fill, (int, float)):
416414
fill = [float(v) for v in list(fill)]
417415
else:
@@ -427,7 +425,7 @@ def affine(
427425
scale: float,
428426
shear: List[float],
429427
interpolation: InterpolationMode = InterpolationMode.NEAREST,
430-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
428+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
431429
center: Optional[List[float]] = None,
432430
) -> features.DType:
433431
if isinstance(inpt, torch.Tensor) and (torch.jit.is_scripting() or not isinstance(inpt, features._Feature)):
@@ -503,7 +501,7 @@ def rotate_image_pil(
503501
angle: float,
504502
interpolation: InterpolationMode = InterpolationMode.NEAREST,
505503
expand: bool = False,
506-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
504+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
507505
center: Optional[List[float]] = None,
508506
) -> PIL.Image.Image:
509507
if center is not None and expand:
@@ -570,7 +568,7 @@ def rotate(
570568
angle: float,
571569
interpolation: InterpolationMode = InterpolationMode.NEAREST,
572570
expand: bool = False,
573-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
571+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
574572
center: Optional[List[float]] = None,
575573
) -> features.DType:
576574
if isinstance(inpt, torch.Tensor) and (torch.jit.is_scripting() or not isinstance(inpt, features._Feature)):
@@ -611,7 +609,7 @@ def pad_image_tensor(
611609
def _pad_with_vector_fill(
612610
img: torch.Tensor,
613611
padding: Union[int, List[int]],
614-
fill: Sequence[float] = [0.0],
612+
fill: List[float],
615613
padding_mode: str = "constant",
616614
) -> torch.Tensor:
617615
if padding_mode != "constant":
@@ -677,15 +675,11 @@ def pad_bounding_box(
677675

678676
def pad(
679677
inpt: features.DType,
680-
padding: Union[int, Sequence[int]],
681-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
678+
padding: Union[int, List[int]],
679+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
682680
padding_mode: str = "constant",
683681
) -> features.DType:
684682
if isinstance(inpt, torch.Tensor) and (torch.jit.is_scripting() or not isinstance(inpt, features._Feature)):
685-
# This cast does Sequence[int] -> List[int] and is required to make mypy happy
686-
if not isinstance(padding, int):
687-
padding = list(padding)
688-
689683
# TODO: PyTorch's pad supports only scalars on fill. So we need to overwrite the colour
690684
if isinstance(fill, (int, float)) or fill is None:
691685
return pad_image_tensor(inpt, padding, fill=fill, padding_mode=padding_mode)
@@ -746,7 +740,7 @@ def perspective_image_pil(
746740
img: PIL.Image.Image,
747741
perspective_coeffs: List[float],
748742
interpolation: InterpolationMode = InterpolationMode.BICUBIC,
749-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
743+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
750744
) -> PIL.Image.Image:
751745
return _FP.perspective(img, perspective_coeffs, interpolation=pil_modes_mapping[interpolation], fill=fill)
752746

@@ -858,7 +852,7 @@ def perspective(
858852
startpoints: List[List[int]],
859853
endpoints: List[List[int]],
860854
interpolation: InterpolationMode = InterpolationMode.BILINEAR,
861-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
855+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
862856
) -> features.DType:
863857
perspective_coeffs = _get_perspective_coeffs(startpoints, endpoints)
864858

@@ -885,7 +879,7 @@ def elastic_image_pil(
885879
img: PIL.Image.Image,
886880
displacement: torch.Tensor,
887881
interpolation: InterpolationMode = InterpolationMode.BILINEAR,
888-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
882+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
889883
) -> PIL.Image.Image:
890884
t_img = pil_to_tensor(img)
891885
fill = _convert_fill_arg(fill)
@@ -953,7 +947,7 @@ def elastic(
953947
inpt: features.DType,
954948
displacement: torch.Tensor,
955949
interpolation: InterpolationMode = InterpolationMode.BILINEAR,
956-
fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
950+
fill: Optional[Union[int, float, List[int], List[float]]] = None,
957951
) -> features.DType:
958952
if isinstance(inpt, torch.Tensor) and (torch.jit.is_scripting() or not isinstance(inpt, features._Feature)):
959953
fill = _convert_fill_arg(fill)

0 commit comments

Comments
 (0)