π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[DOCS] doc updates and augmentation module clean (#524)
shijianjian6650355153e5
diff --git a/docs/source/augmentation.rst b/docs/source/augmentation.rstindex 3e5698959..f48668c95 100644--- a/docs/source/augmentation.rst+++ b/docs/source/augmentation.rst@@ -11,7 +11,7 @@ Kornia provides Torchvision-like augmentation APIs while may not reproduce Torch- AdjustBrightness- RandomRectangleErasing-For detailed comparision, please checkout the [Colab: Kornia vs. Torchvision](https://colab.research.google.com/drive/1T20UNAG4SdlE2n2wstuhiewve5Q81VpS).+For detailed comparision, please checkout the For detailed comparision, please checkout the `Colab: Kornia vs. Torchvision<https://colab.research.google.com/drive/1T20UNAG4SdlE2n2wstuhiewve5Q81VpS#revisionId=0B4unZG1uMc-WdzZqaStjVzZ1U0hHOHphQkgvcGFCZ1RlUzJvPQ>`... autoclass:: RandomHorizontalFlip.. autoclass:: RandomVerticalFlipdiff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.pyindex b20ce6ec2..b2fb11b77 100644--- a/kornia/augmentation/augmentation.py+++ b/kornia/augmentation/augmentation.py@@ -21,11 +21,10 @@ from .types import (class AugmentationBase(nn.Module):- def __init__(self, apply_fcn: Callable, return_transform: bool = False, same_on_batch: bool = False) -> None:+ def __init__(self, apply_fcn: Callable, return_transform: bool = False) -> None:super(AugmentationBase, self).__init__()self.return_transform = return_transformself._apply_fcn: Callable = apply_fcn- self.same_on_batch = same_on_batchself._params: Dict[str, torch.Tensor] = {}def infer_batch_shape(self, input: UnionType) -> torch.Size:@@ -97,8 +96,9 @@ class RandomHorizontalFlip(AugmentationBase):"""def __init__(self, p: float = 0.5, return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomHorizontalFlip, self).__init__(F.apply_hflip, return_transform, same_on_batch)+ super(RandomHorizontalFlip, self).__init__(F.apply_hflip, return_transform)self.p: float = p+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(p={self.p}, return_transform={self.return_transform}, same_on_batch={self.same_on_batch})"@@ -140,8 +140,9 @@ class RandomVerticalFlip(AugmentationBase):"""def __init__(self, p: float = 0.5, return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomVerticalFlip, self).__init__(F.apply_vflip, return_transform, same_on_batch)+ super(RandomVerticalFlip, self).__init__(F.apply_vflip, return_transform)self.p: float = p+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(p={self.p}, return_transform={self.return_transform}, same_on_batch={self.same_on_batch})"@@ -172,11 +173,12 @@ class ColorJitter(AugmentationBase):self, brightness: FloatUnionType = 0., contrast: FloatUnionType = 0., saturation: FloatUnionType = 0.,hue: FloatUnionType = 0., return_transform: bool = False, same_on_batch: bool = False) -> None:- super(ColorJitter, self).__init__(F.apply_color_jitter, return_transform, same_on_batch)+ super(ColorJitter, self).__init__(F.apply_color_jitter, return_transform)self.brightness: FloatUnionType = brightnessself.contrast: FloatUnionType = contrastself.saturation: FloatUnionType = saturationself.hue: FloatUnionType = hue+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(brightness={self.brightness}, contrast={self.contrast}, saturation={self.saturation},\@@ -200,8 +202,9 @@ class RandomGrayscale(AugmentationBase):"""def __init__(self, p: float = 0.1, return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomGrayscale, self).__init__(F.apply_grayscale, return_transform, same_on_batch)+ super(RandomGrayscale, self).__init__(F.apply_grayscale, return_transform)self.p = p+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(p={self.p}, return_transform={self.return_transform}, same_on_batch={self.same_on_batch})"@@ -237,11 +240,12 @@ class RandomErasing(AugmentationBase):self, p: float = 0.5, scale: Tuple[float, float] = (0.02, 0.33), ratio: Tuple[float, float] = (0.3, 3.3),value: float = 0., return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomErasing, self).__init__(F.apply_erase_rectangles, return_transform, same_on_batch)+ super(RandomErasing, self).__init__(F.apply_erase_rectangles, return_transform)self.p = pself.scale: Tuple[float, float] = scaleself.ratio: Tuple[float, float] = ratioself.value: float = value+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(scale={self.scale}, ratio={self.ratio}, value={self.value}, "@@ -268,13 +272,14 @@ class RandomPerspective(AugmentationBase):def __init__(self, distortion_scale: float = 0.5, p: float = 0.5,- interpolation: Union[str, int, Resample] = Resample.BILINEAR,+ interpolation: Union[str, int, Resample] = Resample.BILINEAR.name,return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomPerspective, self).__init__(F.apply_perspective, return_transform, same_on_batch)+ super(RandomPerspective, self).__init__(F.apply_perspective, return_transform)self.p: float = pself.distortion_scale: float = distortion_scaleself.interpolation: Resample = Resample.get(interpolation)+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(distortion_scale={self.distortion_scale}, p={self.p}, interpolation={self.interpolation.name}, "@@ -319,15 +324,16 @@ class RandomAffine(AugmentationBase):def __init__(self, degrees: UnionFloat, translate: Optional[TupleFloat] = None, scale: Optional[TupleFloat] = None,- shear: Optional[UnionFloat] = None, resample: Union[str, int, Resample] = Resample.BILINEAR,+ shear: Optional[UnionFloat] = None, resample: Union[str, int, Resample] = Resample.BILINEAR.name,return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomAffine, self).__init__(F.apply_affine, return_transform, same_on_batch)+ super(RandomAffine, self).__init__(F.apply_affine, return_transform)self.degrees = degreesself.translate = translateself.scale = scaleself.shear = shearself.resample: Resample = Resample.get(resample)+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(degrees={self.degrees}, translate={self.translate}, scale={self.scale}, shear={self.shear}, "@@ -353,7 +359,7 @@ class CenterCrop(AugmentationBase):def __init__(self, size: Union[int, Tuple[int, int]], return_transform: bool = False) -> None:# same_on_batch is always True for CenterCrop- super(CenterCrop, self).__init__(F.apply_crop, return_transform, True)+ super(CenterCrop, self).__init__(F.apply_crop, return_transform)self.size = sizedef __repr__(self) -> str:@@ -407,9 +413,10 @@ class RandomRotation(AugmentationBase):self, degrees: FloatUnionType, interpolation: Union[str, int, Resample] = Resample.BILINEAR.name,return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomRotation, self).__init__(F.apply_rotation, return_transform, same_on_batch)+ super(RandomRotation, self).__init__(F.apply_rotation, return_transform)self.degrees = degreesself.interpolation: Resample = Resample.get(interpolation)+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(degrees={self.degrees}, interpolation={self.interpolation.name}, "@@ -446,12 +453,13 @@ class RandomCrop(AugmentationBase):self, size: Tuple[int, int], padding: Optional[BoarderUnionType] = None, pad_if_needed: Optional[bool] = False,fill: int = 0, padding_mode: str = 'constant', return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomCrop, self).__init__(F.apply_crop, return_transform, same_on_batch)+ super(RandomCrop, self).__init__(F.apply_crop, return_transform)self.size = sizeself.padding = paddingself.pad_if_needed = pad_if_neededself.fill = fillself.padding_mode = padding_mode+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(crop_size={self.size}, padding={self.padding}, fill={self.fill}, "@@ -514,11 +522,12 @@ class RandomResizedCrop(AugmentationBase):interpolation: Union[str, int, Resample] = Resample.BILINEAR.name,return_transform: bool = False, same_on_batch: bool = False) -> None:- super(RandomResizedCrop, self).__init__(F.apply_crop, return_transform, same_on_batch)+ super(RandomResizedCrop, self).__init__(F.apply_crop, return_transform)self.size = sizeself.scale = scaleself.ratio = ratioself.interpolation: Resample = Resample.get(interpolation)+ self.same_on_batch = same_on_batchdef __repr__(self) -> str:repr = f"(size={self.size}, resize_to={self.scale}, resize_to={self.ratio}, "diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.pyindex 3ce2ae18e..cff3b3272 100644--- a/kornia/augmentation/functional.py+++ b/kornia/augmentation/functional.py@@ -119,7 +119,7 @@ def random_affine(input: torch.Tensor,translate: Optional[TupleFloat] = None,scale: Optional[TupleFloat] = None,shear: Optional[UnionFloat] = None,- resample: Resample = Resample.BILINEAR,+ resample: Union[str, int, Resample] = Resample.BILINEAR.name,return_transform: bool = False) -> UnionType:r"""Random affine transformation of the image keeping center invariantdiff --git a/kornia/augmentation/random_generator.py b/kornia/augmentation/random_generator.pyindex f98df5cef..f4427fa66 100644--- a/kornia/augmentation/random_generator.py+++ b/kornia/augmentation/random_generator.py@@ -191,7 +191,7 @@ def random_affine_generator(translate: Optional[TupleFloat] = None,scale: Optional[TupleFloat] = None,shear: Optional[UnionFloat] = None,- resample: Union[str, int, Resample] = Resample.BILINEAR,+ resample: Union[str, int, Resample] = Resample.BILINEAR.name,same_on_batch: bool = False) -> Dict[str, torch.Tensor]:r"""Get parameters for ``affine`` for a random affine transform.@@ -304,7 +304,7 @@ def _get_random_affine_params(batch_size: int, height: int, width: int,degrees: TupleFloat, translate: Optional[TupleFloat],scales: Optional[TupleFloat], shears: Optional[TupleFloat],- resample: Union[str, int, Resample] = Resample.BILINEAR, same_on_batch: bool = False+ resample: Union[str, int, Resample] = Resample.BILINEAR.name, same_on_batch: bool = False) -> Dict[str, torch.Tensor]:r"""Get parameters for ```affine``` transformation random affine transform.The returned matrix is Bx3x3.diff --git a/kornia/constants.py b/kornia/constants.pyindex 9cb51c611..7d612014b 100644--- a/kornia/constants.py+++ b/kornia/constants.py@@ -15,7 +15,7 @@ class Resample(Enum):@classmethoddef get(cls, value: Union[str, int, T]) -> T: # type: ignoreif type(value) == str:- return cls[value] # type: ignore+ return cls[value.upper()] # type: ignoreif type(value) == int:return cls(value) # type: ignoreif type(value) == cls: