Automatic Augmentation Methods#

Augmentation Policy#

This module contains common data augmentation policies that can improve the accuracy of image classification models.

class kornia.augmentation.auto.AutoAugment(policy='imagenet', transformation_matrix_mode='silent')[source]#

Apply AutoAugment [CZM+18] searched strategies.

Parameters:
  • policy (Union[str, List[List[Tuple[str, Union[float, int], Union[float, int, None]]]]], optional) – a customized policy config or presets of “imagenet”, “cifar10”, and “svhn”. Default: "imagenet"

  • transformation_matrix_mode (str, optional) – computation mode for the chained transformation matrix, via .transform_matrix attribute. If silent, transformation matrix will be computed silently and the non-rigid modules will be ignored as identity transformations. If rigid, transformation matrix will be computed silently and the non-rigid modules will trigger errors. If skip, transformation matrix will be totally ignored. Default: "silent"

Examples

>>> import torch
>>> import kornia.augmentation as K
>>> in_tensor = torch.rand(5, 3, 30, 30)
>>> aug = K.AugmentationSequential(AutoAugment())
>>> aug(in_tensor).shape
torch.Size([5, 3, 30, 30])
get_transformation_matrix(input, params=None, recompute=False, extra_args={})#

Compute the transformation matrix according to the provided parameters.

Parameters:
  • input (Tensor) – the input tensor.

  • params (Optional[List[ParamItem]], optional) – params for the sequence. Default: None

  • recompute (bool, optional) – if to recompute the transformation matrix according to the params. default: False. Default: False

Return type:

Optional[Tensor]

forward_parameters(batch_shape)#
Return type:

List[ParamItem]

forward(input, params=None, extra_args={})#

Defines the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

inverse(input, params=None, extra_args={})#

Inverse transformation.

Used to inverse a tensor according to the performed transformation by a forward pass, or with respect to provided parameters.

Return type:

Tensor

class kornia.augmentation.auto.RandAugment(n, m, policy=None, transformation_matrix_mode='silent')[source]#

Apply RandAugment [CZSL20] augmentation strategies.

Parameters:
  • n (int) – the number of augmentations to apply sequentially.

  • m (int) – magnitude for all the augmentations, ranged from [0, 30].

  • policy (Optional[List[List[Tuple[str, Union[float, int], Union[float, int, None]]]]], optional) – candidate transformations. If None, a default candidate list will be used. Default: None

  • transformation_matrix_mode (str, optional) – computation mode for the chained transformation matrix, via .transform_matrix attribute. If silent, transformation matrix will be computed silently and the non-rigid modules will be ignored as identity transformations. If rigid, transformation matrix will be computed silently and the non-rigid modules will trigger errors. If skip, transformation matrix will be totally ignored. Default: "silent"

Examples

>>> import kornia.augmentation as K
>>> in_tensor = torch.rand(5, 3, 30, 30)
>>> aug = K.AugmentationSequential(RandAugment(n=2, m=10))
>>> aug(in_tensor).shape
torch.Size([5, 3, 30, 30])
get_transformation_matrix(input, params=None, recompute=False, extra_args={})#

Compute the transformation matrix according to the provided parameters.

Parameters:
  • input (Tensor) – the input tensor.

  • params (Optional[List[ParamItem]], optional) – params for the sequence. Default: None

  • recompute (bool, optional) – if to recompute the transformation matrix according to the params. default: False. Default: False

Return type:

Optional[Tensor]

forward_parameters(batch_shape)[source]#
Return type:

List[ParamItem]

forward(input, params=None, extra_args={})#

Defines the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

inverse(input, params=None, extra_args={})#

Inverse transformation.

Used to inverse a tensor according to the performed transformation by a forward pass, or with respect to provided parameters.

Return type:

Tensor

class kornia.augmentation.auto.TrivialAugment(policy=None, transformation_matrix_mode='silent')[source]#

Apply TrivialAugment [MullerH21] augmentation strategies.

Parameters:
  • policy (Optional[List[List[Tuple[str, Union[float, int], Union[float, int, None]]]]], optional) – candidate transformations. If None, a default candidate list will be used. Default: None

  • transformation_matrix_mode (str, optional) – computation mode for the chained transformation matrix, via .transform_matrix attribute. If silent, transformation matrix will be computed silently and the non-rigid modules will be ignored as identity transformations. If rigid, transformation matrix will be computed silently and the non-rigid modules will trigger errors. If skip, transformation matrix will be totally ignored. Default: "silent"

Examples

>>> import kornia.augmentation as K
>>> in_tensor = torch.rand(5, 3, 30, 30)
>>> aug = K.AugmentationSequential(TrivialAugment())
>>> aug(in_tensor).shape
torch.Size([5, 3, 30, 30])
get_transformation_matrix(input, params=None, recompute=False, extra_args={})#

Compute the transformation matrix according to the provided parameters.

Parameters:
  • input (Tensor) – the input tensor.

  • params (Optional[List[ParamItem]], optional) – params for the sequence. Default: None

  • recompute (bool, optional) – if to recompute the transformation matrix according to the params. default: False. Default: False

Return type:

Optional[Tensor]

forward_parameters(batch_shape)#
Return type:

List[ParamItem]

forward(input, params=None, extra_args={})#

Defines the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

inverse(input, params=None, extra_args={})#

Inverse transformation.

Used to inverse a tensor according to the performed transformation by a forward pass, or with respect to provided parameters.

Return type:

Tensor

Augmentation Search Methods#

WIP. This module contains common data augmentation search methods.