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')[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'

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)[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

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)[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

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.