Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Updated doc strings.

Jian S87fe5b550622
diff --git a/kornia/augmentation/augmentations.py b/kornia/augmentation/augmentations.py
index a0c5610b5..6b3780bc2 100644
--- a/kornia/augmentation/augmentations.py
+++ b/kornia/augmentation/augmentations.py
@@ -23,7 +23,7 @@ class AugmentationBase(nn.Module):
raise NotImplementedError("forward method is not implemented for this class.")
@staticmethod
- def get_params():
+ def get_params() -> Dict[str, torch.Tensor]:
raise NotImplementedError("forward method is not implemented for this class.")
@@ -39,7 +39,7 @@ class RandomFlip(AugmentationBase):
return self.__class__.__name__ + repr
@staticmethod
- def get_params(batch_size: int, p: float = 0.5) -> Optional[Dict[str, torch.Tensor]]:
+ def get_params(batch_size: int, p: float = 0.5) -> Dict[str, torch.Tensor]:
return pg._random_prob_gen(batch_size, p)
def forward_flip(self, input: UnionType, flip_func: Callable,
diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.py
index e2ceb4230..17de76229 100644
--- a/kornia/augmentation/functional.py
+++ b/kornia/augmentation/functional.py
@@ -14,6 +14,11 @@ FloatUnionType = Union[torch.Tensor, float, Tuple[float, float], List[float]]
def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = False) -> UnionType:
+ """Generate params and apply operation on input tensor.
+
+ See :class:`~kornia.augmentation.param_gen._random_prob_gen` for details.
+ See :class:`~kornia.augmentation.functional.apply_hflip` for details.
+ """
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
@@ -23,6 +28,11 @@ def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = F
def random_vflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = False) -> UnionType:
+ """Generate params and apply operation on input tensor.
+
+ See :class:`~kornia.augmentation.param_gen._random_prob_gen` for details.
+ See :class:`~kornia.augmentation.functional.apply_vflip` for details.
+ """
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
@@ -34,6 +44,11 @@ def random_vflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = F
def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,
contrast: FloatUnionType = 0., saturation: FloatUnionType = 0.,
hue: FloatUnionType = 0., return_transform: bool = False) -> UnionType:
+ """Generate params and apply operation on input tensor.
+
+ See :class:`~kornia.augmentation.param_gen._random_color_jitter_gen` for details.
+ See :class:`~kornia.augmentation.functional.apply_color_jitter` for details.
+ """
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
@@ -43,6 +58,11 @@ def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,
def random_grayscale(input: torch.Tensor, p: float = 0.5, return_transform: bool = False):
+ """Generate params and apply operation on input tensor.
+
+ See :class:`~kornia.augmentation.param_gen._random_prob_gen` for details.
+ See :class:`~kornia.augmentation.functional.apply_grayscale` for details.
+ """
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
@@ -52,11 +72,12 @@ def random_grayscale(input: torch.Tensor, p: float = 0.5, return_transform: bool
def apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
- r"""Horizontally flip a tensor image or a batch of tensor images randomly with a given probability.
- Input should be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
+ 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)`.
Args:
- p (float): probability of the image being flipped. Default value is 0.5
+ params (dict): A dict that must have {'batch_prob': torch.Tensor}. Can be generated from
+ kornia.augmentation.param_gen._random_prob_gen
return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
input tensor.
@@ -110,11 +131,12 @@ def apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_tra
def apply_vflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
- r"""Vertically flip a tensor image or a batch of tensor images randomly with a given probability.
- Input should be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
+ r"""Apply vertically 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)`.
Args:
- p (float): probability of the image being flipped. Default value is 0.5
+ params (dict): A dict that must have {'batch_prob': torch.Tensor}. Can be generated from
+ kornia.augmentation.param_gen._random_prob_gen
return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
input tensor.
@@ -164,6 +186,22 @@ def apply_vflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_tra
def apply_color_jitter(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
+ """Apply Color Jitter 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)`.
+ Args:
+ params (dict): A dict that must have {
+ 'brightness_factor': torch.Tensor,
+ 'contrast_factor': torch.Tensor,
+ 'hue_factor': torch.Tensor,
+ 'saturation_factor': torch.Tensor,
+ }. Can be generated from kornia.augmentation.param_gen._random_color_jitter_gen
+ return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
+ input tensor.
+ Returns:
+ torch.Tensor: The color jitterred input
+ torch.Tensor: The applied transformation matrix :math: `(*, 3, 3)` if return_transform flag
+ is set to ``True``
+ """
# TODO: params validation
if not torch.is_tensor(input):
@@ -205,8 +243,17 @@ def apply_color_jitter(input: torch.Tensor, params: Dict[str, torch.Tensor], ret
def apply_grayscale(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
- """
- params: {'bool_p': torch.Tensor}
+ """Apply Gray Scale on a tensor image or a batch of tensor images with given random parameters.
+ Input should be a tensor of shape (3, H, W) or a batch of tensors :math:`(*, 3, H, W)`.
+ Args:
+ params (dict): A dict that must have {'batch_prob': torch.Tensor}. Can be generated from
+ kornia.augmentation.param_gen._random_prob_gen
+ return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
+ input tensor.
+ Returns:
+ torch.Tensor: The grayscaled input
+ torch.Tensor: The applied transformation matrix :math: `(*, 3, 3)` if return_transform flag
+ is set to ``True``
"""
# TODO: params validation
diff --git a/kornia/augmentation/param_gen.py b/kornia/augmentation/param_gen.py
index e337f7f0f..a512a9e7b 100644
--- a/kornia/augmentation/param_gen.py
+++ b/kornia/augmentation/param_gen.py
@@ -13,7 +13,14 @@ FloatUnionType = Union[torch.Tensor, float, Tuple[float, float], List[float]]
def _random_color_jitter_gen(batch_size: int, brightness: FloatUnionType = 0.,
contrast: FloatUnionType = 0., saturation: FloatUnionType = 0.,
hue: FloatUnionType = 0.) -> Dict[str, torch.Tensor]:
- r"""Random color jiter of an image or batch of images.
+ r"""Generator random color jiter parameters for a batch of images.
+
+ Args:
+ batch_size (int): the number of images.
+ brightness (float or tuple): Default value is 0
+ contrast (float or tuple): Default value is 0
+ saturation (float or tuple): Default value is 0
+ hue (float or tuple): Default value is 0
See :class:`~kornia.augmentation.ColorJitter` for details.
"""
@@ -90,9 +97,15 @@ def _random_color_jitter_gen(batch_size: int, brightness: FloatUnionType = 0.,
def _random_prob_gen(batch_size: int, p: float = .5) -> Dict[str, torch.Tensor]:
- r"""Random grayscale of an image or batch of images.
+ r"""Generator random probabilities for a batch of inputs.
+
+ Args:
+ batch_size (int): the number of images.
+ p (float): probability of the image being flipped or grayscaled. Default value is 0.5
See :class:`~kornia.augmentation.RandomGrayscale` for details.
+ See :class:`~kornia.augmentation.RandomHorizontalFlip` for details.
+ See :class:`~kornia.augmentation.RandomVerticalFlip` for details.
"""
if not isinstance(p, float):