yamle.data.augmentations module#

class yamle.data.augmentations.Augmentation(batch_proportion)[source]#

Bases: ABC

This is a base class for all augmentations. It is an abstract class and should not be used directly. Instead, use one of the concrete subclasses.

Parameters:

batch_proportion (float) – The proportion of the batch to augment.

abstract augment(inputs, targets, model)[source]#
Return type:

Tuple[Tensor, Tensor]

class yamle.data.augmentations.MixUp(alpha, *args, **kwargs)[source]#

Bases: Augmentation

This is an implementation of the MixUp augmentation, as described in https://arxiv.org/abs/1710.09412.

Parameters:

alpha (float) – The alpha parameter for the beta distribution used to sample the mixup weights.

augment(inputs, targets, model)[source]#

Applies the MixUp augmentation to the inputs and returns the augmented inputs.

The targets are assumed to be one-hot encoded.

It is applied such that:

l ~ Beta(alpha, alpha) x’ = l * x + (1 - l) * x’ y’ = l * y + (1 - l) * y’ where x’ and y’ are the randomly shuffled inputs and targets, respectively.

Return type:

Tuple[Tensor, Tensor]

class yamle.data.augmentations.CutOut(batch_proportion, size)[source]#

Bases: Augmentation

This is an implementation of the CutOut augmentation, as described in https://arxiv.org/abs/1708.04552.

Parameters:
  • batch_proportion (float) – The proportion of the batch to augment.

  • size (int) – The size of the square region to cut out.

augment(inputs, targets, model)[source]#

Applies the CutOut augmentation to the inputs and returns the augmented inputs.

The targets are not modified.

It is applied such that a square region of size size is randomly cut out from each input image.

Parameters:
  • inputs (torch.Tensor) – The inputs to augment.

  • targets (torch.Tensor) – The targets (labels) associated with the inputs. Not modified.

  • model (nn.Module) – The model to use for the augmentation (not used here). Not modified.

Return type:

Tuple[Tensor, Tensor]

Returns:

Tuple[torch.Tensor, torch.Tensor] – The augmented inputs and unchanged targets.

class yamle.data.augmentations.CutMix(batch_proportion, alpha)[source]#

Bases: Augmentation

This is an implementation of CutMix augmentation, as described in the paper: https://arxiv.org/abs/1905.04899.

Parameters:
  • batch_proportion (float) – The proportion of the batch to augment.

  • alpha (float) – The alpha parameter for the distribution used to sample the combination ratio.

augment(inputs, targets, model)[source]#

Applies the CutMix augmentation to the inputs and returns the augmented inputs and targets.

The targets are assumed to be one-hot encoded.

It is applied such that: - Two random inputs are selected for combining. - A random box is cut out from one input and replaced with the corresponding box from another input. - The targets are adjusted to match the pixel ratio.

Parameters:
  • inputs (torch.Tensor) – The inputs to augment.

  • targets (torch.Tensor) – The targets to augment.

  • model (nn.Module) – The model to use for the augmentation.

Return type:

Tuple[Tensor, Tensor]

Returns:

Tuple[torch.Tensor, torch.Tensor] – The augmented inputs and targets.

class yamle.data.augmentations.RandomErasing(batch_proportion, scale, ratio, value=0)[source]#

Bases: Augmentation

This is a wrapper class for the RandomErasing augmentation provided by torchvision.

Parameters:
  • batch_proportion (float) – The proportion of the batch to augment.

  • scale (Tuple[float, float]) – The range of the proportion of erased area against the input image.

  • ratio (Tuple[float, float]) – The range of the aspect ratio of the erased area.

  • value (int or tuple or str) – The erasing value. Default is 0. - If an integer, it is used to erase all pixels. - If a tuple of length 3, it is used to erase R, G, B channels respectively. - If a string ‘random’, each pixel is erased with random values.

augment(inputs, targets, model)[source]#

Applies the Random Erasing augmentation to the inputs and returns the augmented inputs and targets.

The targets are unchanged.

Parameters:
  • inputs (torch.Tensor) – The inputs to augment.

  • targets (torch.Tensor) – The targets to augment.

  • model (nn.Module) – The model to use for the augmentation.

Return type:

Tuple[Tensor, Tensor]

Returns:

Tuple[torch.Tensor, torch.Tensor] – The augmented inputs and unchanged targets.

yamle.data.augmentations.rand_bbox(size, lam)[source]#

Generates a random bounding box coordinates for CutMix.

Parameters:
  • size (Tuple[int, int, int, int]) – The shape of the input tensor.

  • lam (torch.Tensor) – The combination ratio.

Return type:

Tuple[int, int, int, int]

Returns:

Tuple[int, int, int, int] – The bounding box coordinates (x1, y1, x2, y2).