Skip to content

Commit

Permalink
More mypy fixes/ignores (#8412)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHug authored May 7, 2024
1 parent f766d7a commit d23a6e1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions torchvision/transforms/v2/_auto_augment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Type, Union

import PIL.Image
import torch
Expand Down Expand Up @@ -94,6 +94,8 @@ def _apply_image_or_video_transform(
interpolation: Union[InterpolationMode, int],
fill: Dict[Union[Type, str], _FillTypeJIT],
) -> ImageOrVideo:
# Note: this cast is wrong and is only here to make mypy happy (it disagrees with torchscript)
image = cast(torch.Tensor, image)
fill_ = _get_fill(fill, type(image))

if transform_id == "Identity":
Expand Down Expand Up @@ -322,7 +324,7 @@ def _get_policies(

def forward(self, *inputs: Any) -> Any:
flat_inputs_with_spec, image_or_video = self._flatten_and_extract_image_or_video(inputs)
height, width = get_size(image_or_video)
height, width = get_size(image_or_video) # type: ignore[arg-type]

policy = self._policies[int(torch.randint(len(self._policies), ()))]

Expand Down Expand Up @@ -411,7 +413,7 @@ def __init__(

def forward(self, *inputs: Any) -> Any:
flat_inputs_with_spec, image_or_video = self._flatten_and_extract_image_or_video(inputs)
height, width = get_size(image_or_video)
height, width = get_size(image_or_video) # type: ignore[arg-type]

for _ in range(self.num_ops):
transform_id, (magnitudes_fn, signed) = self._get_random_item(self._AUGMENTATION_SPACE)
Expand Down Expand Up @@ -480,7 +482,7 @@ def __init__(

def forward(self, *inputs: Any) -> Any:
flat_inputs_with_spec, image_or_video = self._flatten_and_extract_image_or_video(inputs)
height, width = get_size(image_or_video)
height, width = get_size(image_or_video) # type: ignore[arg-type]

transform_id, (magnitudes_fn, signed) = self._get_random_item(self._AUGMENTATION_SPACE)

Expand Down Expand Up @@ -572,7 +574,7 @@ def _sample_dirichlet(self, params: torch.Tensor) -> torch.Tensor:

def forward(self, *inputs: Any) -> Any:
flat_inputs_with_spec, orig_image_or_video = self._flatten_and_extract_image_or_video(inputs)
height, width = get_size(orig_image_or_video)
height, width = get_size(orig_image_or_video) # type: ignore[arg-type]

if isinstance(orig_image_or_video, torch.Tensor):
image_or_video = orig_image_or_video
Expand Down Expand Up @@ -613,9 +615,7 @@ def forward(self, *inputs: Any) -> Any:
else:
magnitude = 0.0

aug = self._apply_image_or_video_transform(
aug, transform_id, magnitude, interpolation=self.interpolation, fill=self._fill
)
aug = self._apply_image_or_video_transform(aug, transform_id, magnitude, interpolation=self.interpolation, fill=self._fill) # type: ignore[assignment]
mix.add_(combined_weights[:, i].reshape(batch_dims) * aug)
mix = mix.reshape(orig_dims).to(dtype=image_or_video.dtype)

Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/v2/functional/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def permute_channels_image(image: torch.Tensor, permutation: List[int]) -> torch


@_register_kernel_internal(permute_channels, PIL.Image.Image)
def _permute_channels_image_pil(image: PIL.Image.Image, permutation: List[int]) -> PIL.Image:
def _permute_channels_image_pil(image: PIL.Image.Image, permutation: List[int]) -> PIL.Image.Image:
return to_pil_image(permute_channels_image(pil_to_tensor(image), permutation=permutation))


Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/v2/functional/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def vertical_flip_image(image: torch.Tensor) -> torch.Tensor:


@_register_kernel_internal(vertical_flip, PIL.Image.Image)
def _vertical_flip_image_pil(image: PIL.Image) -> PIL.Image:
def _vertical_flip_image_pil(image: PIL.Image.Image) -> PIL.Image.Image:
return _FP.vflip(image)


Expand Down

0 comments on commit d23a6e1

Please sign in to comment.