Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Merge branch 'master' into fix/augmentation/private_functions

Edgar Ribaaad9851fba5c
diff --cc kornia/augmentation/augmentation.py
index acb884e7d,48cb482d4..34422595a
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@@ -89,12 -89,12 +89,12 @@@ class RandomHorizontalFlip(Augmentation
"""
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomHorizontalFlip, self).__init__(F.apply_hflip, return_transform)
+ super(RandomHorizontalFlip, self).__init__(F._apply_hflip, return_transform)
self.p: float = p
+ self._params: Dict[str, torch.Tensor] = {}
- @staticmethod
- def get_params(batch_size: int, p: float) -> Dict[str, torch.Tensor]:
- return pg._random_prob_gen(batch_size, p)
+ def set_params(self, batch_size: int, p: float):
+ self._params = pg._random_prob_gen(batch_size, p)
def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
if params is None:
@@@ -134,12 -134,12 +134,12 @@@ class RandomVerticalFlip(AugmentationBa
"""
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomVerticalFlip, self).__init__(F.apply_vflip, return_transform)
+ super(RandomVerticalFlip, self).__init__(F._apply_vflip, return_transform)
self.p: float = p
+ self._params: Dict[str, torch.Tensor] = {}
- @staticmethod
- def get_params(batch_size: int, p: float) -> Dict[str, torch.Tensor]:
- return pg._random_prob_gen(batch_size, p)
+ def set_params(self, batch_size: int, p: float):
+ self._params = pg._random_prob_gen(batch_size, p)
def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
if params is None:
@@@ -201,8 -201,9 +201,9 @@@ class RandomGrayscale(AugmentationBase)
"""
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomGrayscale, self).__init__(F.apply_grayscale, return_transform)
+ super(RandomGrayscale, self).__init__(F._apply_grayscale, return_transform)
self.p = p
+ self._params: Dict[str, torch.Tensor] = {}
def __repr__(self) -> str:
repr = f"(p={self.p}, return_transform={self.return_transform})"
diff --cc kornia/augmentation/functional.py
index b2903a5f3,d4bbb5db6..3345660c8
--- a/kornia/augmentation/functional.py
+++ b/kornia/augmentation/functional.py
@@@ -111,10 -113,65 +113,65 @@@ def random_affine(input: torch.Tensor
batch_size, _, height, width = input.shape
params: Dict[str, torch.Tensor] = pg._random_affine_gen(
batch_size, height, width, degrees, translate, scale, shear)
- return apply_affine(input, params, return_transform)
+ return _apply_affine(input, params, return_transform)
+ def random_rectangle_erase(
+ images: torch.Tensor,
+ erase_scale_range: Tuple[float, float],
+ aspect_ratio_range: Tuple[float, float]
+ ) -> torch.Tensor:
+ r"""
+ Function that erases a random selected rectangle for each image in the batch, putting
+ the value to zero.
+ The rectangle will have an area equal to the original image area multiplied by a value uniformly
+ sampled between the range [erase_scale_range[0], erase_scale_range[1]) and an aspect ratio sampled
+ between [aspect_ratio_range[0], aspect_ratio_range[1])
+
+ Args:
+ images (torch.Tensor): input images.
+ erase_scale_range (Tuple[float, float]): range of proportion of erased area against input image.
+ aspect_ratio_range (Tuple[float, float]): range of aspect ratio of erased area.
+ """
+
+ if not (isinstance(erase_scale_range[0], float) and
+ isinstance(erase_scale_range[1], float) and
+ erase_scale_range[0] > 0. and erase_scale_range[1] > 0.):
+ raise TypeError(
+ f"'erase_scale_range' must be a Tuple[float, float] with positive values"
+ )
+ if not (isinstance(aspect_ratio_range[0], float) and
+ isinstance(aspect_ratio_range[1], float) and
+ aspect_ratio_range[0] > 0. and aspect_ratio_range[1] > 0.):
+ raise TypeError(
+ f"'aspect_ratio_range' must be a Tuple[float, float] with positive values"
+ )
+
+ images_size = images.size()
+ b, _, h, w = images_size
+ rect_params = get_random_rectangles_params(
+ (b, ), h, w, erase_scale_range, aspect_ratio_range
+ )
+ images = erase_rectangles(images, rect_params)
+ return images
+
+
+ def random_rotation(input: torch.Tensor, degrees: FloatUnionType, return_transform: bool = False) -> UnionType:
+ r"""Generate params and apply operation on input tensor.
+
+ See :func:`~kornia.augmentation.param_gen._random_rotation_gen` for details.
+ See :func:`~kornia.augmentation.functional.apply_rotation` for details.
+ """
+ input_tmp: torch.Tensor = input.unsqueeze(0)
+ input_tmp = input_tmp.view(-1, *input_tmp.shape[-3:])
+ batch_size = input_tmp.shape[0]
+
+ params = pg._random_rotation_gen(batch_size, degrees=degrees)
+
+ return apply_rotation(input, params, return_transform)
+
+
-def apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
+def _apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
r"""Apply Horizontally flip on a tensor image or a batch of tensor images with given random parameters.
Input should be a tensor of shape (H, W), (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
@@@ -331,49 -388,9 +388,9 @@@ def _apply_grayscale(input: torch.Tenso
return grayscale
- def random_rectangle_erase(
- images: torch.Tensor,
- erase_scale_range: Tuple[float, float],
- aspect_ratio_range: Tuple[float, float]
- ) -> torch.Tensor:
- r"""
- Function that erases a random selected rectangle for each image in the batch, putting
- the value to zero.
- The rectangle will have an area equal to the original image area multiplied by a value uniformly
- sampled between the range [erase_scale_range[0], erase_scale_range[1]) and an aspect ratio sampled
- between [aspect_ratio_range[0], aspect_ratio_range[1])
-
- Args:
- images (torch.Tensor): input images.
- erase_scale_range (Tuple[float, float]): range of proportion of erased area against input image.
- aspect_ratio_range (Tuple[float, float]): range of aspect ratio of erased area.
- """
-
- if not (isinstance(erase_scale_range[0], float) and
- isinstance(erase_scale_range[1], float) and
- erase_scale_range[0] > 0. and erase_scale_range[1] > 0.):
- raise TypeError(
- f"'erase_scale_range' must be a Tuple[float, float] with positive values"
- )
- if not (isinstance(aspect_ratio_range[0], float) and
- isinstance(aspect_ratio_range[1], float) and
- aspect_ratio_range[0] > 0. and aspect_ratio_range[1] > 0.):
- raise TypeError(
- f"'aspect_ratio_range' must be a Tuple[float, float] with positive values"
- )
-
- images_size = images.size()
- b, _, h, w = images_size
- rect_params = get_random_rectangles_params(
- (b, ), h, w, erase_scale_range, aspect_ratio_range
- )
- images = erase_rectangles(images, rect_params)
- return images
-
-
-def apply_perspective(input: torch.Tensor,
- params: Dict[str, torch.Tensor],
- return_transform: bool = False) -> UnionType:
+def _apply_perspective(input: torch.Tensor,
+ params: Dict[str, torch.Tensor],
+ return_transform: bool = False) -> UnionType:
r"""Perform perspective transform of the given torch.Tensor or batch of tensors.
Args: