Skip to content

Added typing annotations to transforms/autoaugment #4226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ ignore_errors = True

ignore_errors = True

[mypy-torchvision.transforms.autoaugment.*]

ignore_errors = True

[mypy-PIL.*]

ignore_missing_imports = True
Expand Down
22 changes: 14 additions & 8 deletions torchvision/transforms/autoaugment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from enum import Enum
from torch import Tensor
from typing import List, Tuple, Optional
from typing import List, Tuple, Optional, Dict

from . import functional as F, InterpolationMode

Expand All @@ -19,7 +19,9 @@ class AutoAugmentPolicy(Enum):
SVHN = "svhn"


def _get_transforms(policy: AutoAugmentPolicy):
def _get_transforms( # type: ignore[return]
policy: AutoAugmentPolicy
) -> List[Tuple[Tuple[str, float, Optional[int]], Tuple[str, float, Optional[int]]]]:
if policy == AutoAugmentPolicy.IMAGENET:
return [
(("Posterize", 0.4, 8), ("Rotate", 0.6, 9)),
Expand Down Expand Up @@ -106,7 +108,7 @@ def _get_transforms(policy: AutoAugmentPolicy):
]


def _get_magnitudes():
def _get_magnitudes() -> Dict[str, Tuple[Optional[Tensor], Optional[bool]]]:
_BINS = 10
return {
# name: (magnitudes, signed)
Expand Down Expand Up @@ -144,8 +146,12 @@ class AutoAugment(torch.nn.Module):
image. If given a number, the value is used for all bands respectively.
"""

def __init__(self, policy: AutoAugmentPolicy = AutoAugmentPolicy.IMAGENET,
interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: Optional[List[float]] = None):
def __init__(
self,
policy: AutoAugmentPolicy = AutoAugmentPolicy.IMAGENET,
interpolation: InterpolationMode = InterpolationMode.NEAREST,
fill: Optional[List[float]] = None
) -> None:
super().__init__()
self.policy = policy
self.interpolation = interpolation
Expand All @@ -163,7 +169,7 @@ def get_params(transform_num: int) -> Tuple[int, Tensor, Tensor]:
Returns:
params required by the autoaugment transformation
"""
policy_id = torch.randint(transform_num, (1,)).item()
policy_id = int(torch.randint(transform_num, (1,)).item())
probs = torch.rand((2,))
signs = torch.randint(2, (2,))

Expand All @@ -172,7 +178,7 @@ def get_params(transform_num: int) -> Tuple[int, Tensor, Tensor]:
def _get_op_meta(self, name: str) -> Tuple[Optional[Tensor], Optional[bool]]:
return self._op_meta[name]

def forward(self, img: Tensor):
def forward(self, img: Tensor) -> Tensor:
"""
img (PIL Image or Tensor): Image to be transformed.

Expand Down Expand Up @@ -233,5 +239,5 @@ def forward(self, img: Tensor):

return img

def __repr__(self):
def __repr__(self) -> str:
return self.__class__.__name__ + '(policy={}, fill={})'.format(self.policy, self.fill)