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.
See the Convention block on
PolicyAugmentBase.- Convention:
selects one sub-policy uniformly for each forward call. A sub-policy is an ordered sequence of
(name, probability, magnitude_bin)entries, so every listed operation is considered in order.a magnitude bin selects the adjacent interval of that operation’s eleven-point scale. Bins
0through9are valid for operations with a magnitude; operations that ignore magnitude accept any value. The wrapped operation samples a value inside its selected interval for every batch row;posterizethen rounds to the nearest integer number of bits (so bin1, spanning4.4to4.8, produces4or5),translate_x/translate_ymultiply it by the image size in pixels, andshear_x/shear_y, whose scale is a shear fraction from-0.3to0.3, multiply it by180to give degrees.
- 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, the result is the same for a policy: every operation wrapper contributes a matrix (an intensity operation the identity), so none is rejected. If skip, transformation matrix will be totally ignored. Default:"silent"
Examples
>>> import torch >>> import kornia.augmentation as K >>> from kornia.augmentation.auto import AutoAugment >>> 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:Nonerecompute (
bool, optional) – if to recompute the transformation matrix according to the params. default: False. Default:Falseextra_args (
Optional[Dict[str,Any]], optional) – Optional dictionary of extra arguments with specific options for different input types. Default:None
- Return type:
- forward_parameters(batch_shape)[source]#
Generate per-module parameters for one policy forward pass.
- forward(input, params=None, extra_args=None)[source]#
Apply the policy to
inputand cache the parameters used.- Parameters:
- Return type:
- Returns:
Augmented tensor.
- inverse(input, params=None, extra_args=None)[source]#
Undo the drawn sub-policy, or refuse when it cannot be undone.
Only geometric operations are invertible. A policy draw that contains an intensity operation is not round-trippable, and inverting it would return a tensor that still carries that operation – for a draw with no geometry at all, the input unchanged. This raises instead, as
inverseonMixAugmentationBaseV2already does for the mix classes.- Parameters:
- Return type:
- Returns:
The inverse-transformed tensor.
- Raises:
RuntimeError – The drawn sub-policy contains a non-invertible operation.
- class kornia.augmentation.auto.RandAugment(n, m, policy=None, transformation_matrix_mode='silent')[source]#
Apply RandAugment [CZSL20] augmentation strategies.
See the Convention block on
PolicyAugmentBase.- Convention:
selects
ndistinct one-operation candidate sub-policies uniformly without replacement, then applies them in the order drawn.nmust be in[1, len(policy)].mmust be strictly between0and30and sets the magnitude of every selected operation that has one tolow + (high - low) * m / 30over its magnitude range. A symmetric operation’s range is(0, max)with a random sign per row:m=15on("rotate", -30, 30)gives+15or-15. Atranslate_x/translate_yvalue is a fraction of the image width / height, applied in pixels. The operation’s magnitude mapping is the identity for every default entry exceptshear_x/shear_y, which multiply by180(27degrees atm=15for the default range), andposterize, which truncates to integer bits.posterizealso runs its range backwards,high - (high - low) * m / 30, so a largermkeeps fewer bits (7,6and4atm=7,15and29).
- Parameters:
n (
int) – the number of augmentations to apply sequentially. Must be at least1and at most the number of sub-policies inpolicy, since they are sampled without replacement.m (
int) – magnitude for all the augmentations, ranged from (0, 30) exclusive.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:Nonetransformation_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, the result is the same for a policy: every operation wrapper contributes a matrix (an intensity operation the identity), so none is rejected. If skip, transformation matrix will be totally ignored. Default:"silent"
Examples
>>> import torch >>> import kornia.augmentation as K >>> from kornia.augmentation.auto import RandAugment >>> 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:Nonerecompute (
bool, optional) – if to recompute the transformation matrix according to the params. default: False. Default:Falseextra_args (
Optional[Dict[str,Any]], optional) – Optional dictionary of extra arguments with specific options for different input types. Default:None
- Return type:
- forward(input, params=None, extra_args=None)[source]#
Apply the policy to
inputand cache the parameters used.- Parameters:
- Return type:
- Returns:
Augmented tensor.
- inverse(input, params=None, extra_args=None)[source]#
Undo the drawn sub-policy, or refuse when it cannot be undone.
Only geometric operations are invertible. A policy draw that contains an intensity operation is not round-trippable, and inverting it would return a tensor that still carries that operation – for a draw with no geometry at all, the input unchanged. This raises instead, as
inverseonMixAugmentationBaseV2already does for the mix classes.- Parameters:
- Return type:
- Returns:
The inverse-transformed tensor.
- Raises:
RuntimeError – The drawn sub-policy contains a non-invertible operation.
- class kornia.augmentation.auto.TrivialAugment(policy=None, transformation_matrix_mode='silent')[source]#
Apply TrivialAugment [MullerH21] augmentation strategies.
See the Convention block on
PolicyAugmentBase.- Convention:
selects one candidate sub-policy uniformly for each forward call. Each candidate holds one operation whose probability is fixed at
1, so the selected operation is applied to every row and no probability is sampled; a candidate’s second and third entries are its magnitude range, not a probability, unlikeAutoAugment. Only the magnitude is drawn independently for every batch row.the operation wrapper’s magnitude mapping applies, as under
RandAugment. A("rotate", -30, 30)candidate draws angles in[-30, 30]with a random sign, and("shear_x", -0.3, 0.3)shears by up to0.3 * 180degrees.
- 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:Nonetransformation_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, the result is the same for a policy: every operation wrapper contributes a matrix (an intensity operation the identity), so none is rejected. If skip, transformation matrix will be totally ignored. Default:"silent"
Examples
>>> import torch >>> import kornia.augmentation as K >>> from kornia.augmentation.auto import TrivialAugment >>> 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:Nonerecompute (
bool, optional) – if to recompute the transformation matrix according to the params. default: False. Default:Falseextra_args (
Optional[Dict[str,Any]], optional) – Optional dictionary of extra arguments with specific options for different input types. Default:None
- Return type:
- forward_parameters(batch_shape)[source]#
Generate per-module parameters for one policy forward pass.
- forward(input, params=None, extra_args=None)[source]#
Apply the policy to
inputand cache the parameters used.- Parameters:
- Return type:
- Returns:
Augmented tensor.
- inverse(input, params=None, extra_args=None)[source]#
Undo the drawn sub-policy, or refuse when it cannot be undone.
Only geometric operations are invertible. A policy draw that contains an intensity operation is not round-trippable, and inverting it would return a tensor that still carries that operation – for a draw with no geometry at all, the input unchanged. This raises instead, as
inverseonMixAugmentationBaseV2already does for the mix classes.- Parameters:
- Return type:
- Returns:
The inverse-transformed tensor.
- Raises:
RuntimeError – The drawn sub-policy contains a non-invertible operation.
- class kornia.augmentation.auto.PolicyAugmentBase(policy, transformation_matrix_mode='silence')[source]#
Base class for policy-based image augmentations.
- Convention:
a concrete policy selects one or more
PolicySequentialchildren for each forward call and records that selected path, including every operation parameter dictionary, in_params. Passing that list toforward(input, params=...)selects the recorded children rather than drawing a new path and reproduces their output.the selected operations run in the order recorded in
_params: the listed order inside a sub-policy, and forRandAugmentthe order in which its sub-policies were drawn. When matrix computation is enabled, their geometric transformation matrices compose in that same execution order; a nonempty policy containing only intensity operations has the identity matrix. An empty selected sub-policy has no matrix.inversereverses a geometry-only selected path and raisesRuntimeErrorwhen an applied intensity operation cannot be undone.input normalization, random-number generation, parameter placement, replay, and serialization follow the canonical augmentation contract in Conventions & Pitfalls. This base does not expose a per-instance generator.
Concrete policies define how they select children and interpret policy magnitudes.
- class kornia.augmentation.auto.PolicySequential(*operations)[source]#
Policy tuple for applying multiple operations.
- Convention:
accepts only
OperationBasechildren. Generated parameters apply every child in construction order. Suppliedparamsdefine the execution path instead: their order and membership select the referenced children, so they may replay a reordered or partial path.forwardstores theParamItemobjects for the path it executed in_params; those parameters can replay the same operations throughforward(input, params=...).its transformation matrix is the ordered product of its geometric wrapped operations. Intensity operations do not contribute to that matrix.
forward_parameterssamples each child throughforward_parameters(), so the operation’s magnitude, magnitude mapping and symmetric sign apply; the gate comes from the wrapped augmentation’sp.
- Parameters:
operations (
OperationBase) – the operations to perform, passed as positional arguments rather than as one list.
- class kornia.augmentation.auto.operations.OperationBase(operation, initial_magnitude=None, temperature=0.1, is_batch_operation=False, magnitude_fn=None, symmetric_megnitude=False)[source]#
Base class of differentiable augmentation operations.
- Parameters:
operation (
_AugmentationBase) – Kornia augmentation module.initial_magnitude (
Optional[List[Tuple[str,Optional[float]]]], optional) – targeted magnitude parameter name and its initial magnitude value. The magnitude parameter name shall align with the attribute inside the random_generator in each augmentation. If None, the augmentation will be randomly applied according to the augmentation sampling range. Default:Nonetemperature (
float, optional) – retained for API compatibility. Default:0.1is_batch_operation (
bool, optional) – determine if to obtain the probability from p or p_batch. Set to True for most non-shape-persistent operations (e.g. cropping). Default:False
- Convention:
probabilityis initialized from the wrapped augmentation’sp(p_batchfor a batch operation), clamped to[1e-7, 1 - 1e-7]and kept instate_dict()for checkpoint compatibility, but it takes no part in sampling and receives no gradient: the gate is drawn from the wrapped augmentation’s ownpandp_batchas a hard0or1.magnitude, where the operation has one, is clamped to the wrapped generator’s range and receives a gradient where the wrapped augmentation is differentiable in it;forward_parameterssubstitutes it into the wrapped augmentation’s draw.forwardlinearly blends the wrapped output with the input usingbatch_prob. The wrapped augmentation transforms every row whose gate is above0, so the same supplied fractionalbatch_probproduces the same soft blend whatever the wrappedporp_batch. Belowp = 1the wrapped augmentation skips a row with gate0, so that row’s matrix stays the identity.a symmetric magnitude applies the magnitude mapping first and then a random sign per row, so a mapping that quantizes to zero stays zero (
Posterizemaps0.5to0bits withmagnitude_range=(0, 8)).the concrete classes in
kornia.augmentation.auto.operations.opswrap public 2D augmentations and inherit their input, dtype, RNG and replay contracts. A wrapper pickles, with either sign setting, when its magnitude mapping is picklable.
- forward_parameters(batch_shape, mag=None)[source]#
Sample parameters for the wrapped augmentation op.
- Parameters:
- Return type:
- Returns:
Parameter dictionary consumed by
forward().
Augmentation Search Methods#
WIP. This module contains common data augmentation search methods.