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=None)[source]

Compute the transformation matrix according to the provided parameters.

Parameters:
  • input (Tensor) – the input torch.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

  • extra_args (Optional[Dict[str, Any]], optional) – Optional dictionary of extra arguments with specific options for different input types. Default: None

Return type:

Optional[Tensor]

forward_parameters(batch_shape)[source]

Generate per-module parameters for one policy forward pass.

Parameters:

batch_shape (Size) – Input shape used to sample operation parameters.

Return type:

List[ParamItem]

Returns:

Parameters for each selected child module, in execution order.

forward(input, params=None, extra_args=None)[source]

Apply the policy to input and cache the parameters used.

Parameters:
  • input (Tensor) – Input tensor.

  • params (Optional[List[ParamItem]], optional) – Optional precomputed parameters. If omitted, parameters are sampled on-the-fly. Default: None

  • extra_args (Optional[Dict[str, Any]], optional) – Optional overrides forwarded to child transforms. Default: None

Return type:

Tensor

Returns:

Augmented tensor.

inverse(input, params=None, extra_args=None)[source]

Inverse transformation.

Used to inverse a torch.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=None)[source]

Compute the transformation matrix according to the provided parameters.

Parameters:
  • input (Tensor) – the input torch.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

  • extra_args (Optional[Dict[str, Any]], optional) – Optional dictionary of extra arguments with specific options for different input types. Default: None

Return type:

Optional[Tensor]

forward_parameters(batch_shape)[source]

Sample parameters for the selected RandAugment operations.

Parameters:

batch_shape (Size) – Input shape used for parameter sampling.

Return type:

List[ParamItem]

Returns:

Parameters for the sampled operations, including magnitude scaled from the global m value.

forward(input, params=None, extra_args=None)[source]

Apply the policy to input and cache the parameters used.

Parameters:
  • input (Tensor) – Input tensor.

  • params (Optional[List[ParamItem]], optional) – Optional precomputed parameters. If omitted, parameters are sampled on-the-fly. Default: None

  • extra_args (Optional[Dict[str, Any]], optional) – Optional overrides forwarded to child transforms. Default: None

Return type:

Tensor

Returns:

Augmented tensor.

inverse(input, params=None, extra_args=None)[source]

Inverse transformation.

Used to inverse a torch.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=None)[source]

Compute the transformation matrix according to the provided parameters.

Parameters:
  • input (Tensor) – the input torch.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

  • extra_args (Optional[Dict[str, Any]], optional) – Optional dictionary of extra arguments with specific options for different input types. Default: None

Return type:

Optional[Tensor]

forward_parameters(batch_shape)[source]

Generate per-module parameters for one policy forward pass.

Parameters:

batch_shape (Size) – Input shape used to sample operation parameters.

Return type:

List[ParamItem]

Returns:

Parameters for each selected child module, in execution order.

forward(input, params=None, extra_args=None)[source]

Apply the policy to input and cache the parameters used.

Parameters:
  • input (Tensor) – Input tensor.

  • params (Optional[List[ParamItem]], optional) – Optional precomputed parameters. If omitted, parameters are sampled on-the-fly. Default: None

  • extra_args (Optional[Dict[str, Any]], optional) – Optional overrides forwarded to child transforms. Default: None

Return type:

Tensor

Returns:

Augmented tensor.

inverse(input, params=None, extra_args=None)[source]

Inverse transformation.

Used to inverse a torch.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.