flowvision.data

Advanced data augmentations

class flowvision.data.AugMixAugment(ops, alpha=1.0, width=3, depth=- 1, blended=False)[source]

AugMix Transform Adapted and improved from impl here: https://github.com/google-research/augmix/blob/master/imagenet.py

From paper AugMix: A Simple Data Processing Method to Improve Robustness and Uncertainty

class flowvision.data.AutoAugment(policy)[source]

Auto Augmentation

From paper AutoAugment: Learning Augmentation Policies from Data

class flowvision.data.Mixup(mixup_alpha=1.0, cutmix_alpha=0.0, cutmix_minmax=None, prob=1.0, switch_prob=0.5, mode='batch', correct_lam=True, label_smoothing=0.1, num_classes=1000)[source]

Mixup/Cutmix that applies different params to each element or whole batch

Parameters
  • mixup_alpha (float) – Mixup alpha value, mixup is active if > 0

  • cutmix_alpha (float) – Cutmix alpha value, cutmix is active if > 0

  • cutmix_minmax (List[float]) – Cutmix min/max image ratio, cutmix is active and uses this vs alpha if not None

  • prob (float) – Probability of applying mixup or cutmix per batch or element

  • switch_prob (float) – Probability of switching to cutmix instead of mixup when both are active

  • mode (str) – How to apply mixup/cutmix params (per ‘batch’, ‘pair’ (pair of elements), ‘elem’ (element)

  • correct_lam (bool) – Apply lambda correction when cutmix bbox clipped by image borders

  • label_smoothing (float) – Apply label smoothing to the mixed target tensor

  • num_classes (int) – Number of classes for target

class flowvision.data.RandAugment(ops, num_layers=2, choice_weights=None)[source]

Random Augmentation

From paper RandAugment: Practical automated data augmentation

class flowvision.data.RandomErasing(probability=0.5, min_area=0.02, max_area=0.3333333333333333, min_aspect=0.3, max_aspect=None, mode='const', min_count=1, max_count=None, num_splits=0, device='cuda')[source]

Randomly selects a rectangle region in an image and erases its pixels. ‘Random Erasing Data Augmentation’ by Zhong et al. See https://arxiv.org/pdf/1708.04896.pdf

This variant of RandomErasing is intended to be applied to either a batch or single image tensor after it has been normalized by dataset mean and std.

Parameters
  • probability – Probability that the Random Erasing operation will be performed

  • min_area – Minimum percentage of erased area wrt input image area

  • max_area – Maximum percentage of erased area wrt input image area

  • min_aspect – Minimum aspect ratio of erased area

  • mode

    Pixel color mode, one of ‘const’, ‘rand’, or ‘pixel’

    • ’const’ - erase block is constant color of 0 for all channels

    • ’rand’ - erase block is same per-channel random (normal) color

    • ’pixel’ - erase block is per-pixel random (normal) color

  • max_count – Maximum number of erasing blocks per image, area per box is scaled by count. per-image count is randomly chosen between 1 and this value

flowvision.data.augment_and_mix_transform(config_str, hparams)[source]

Create AugMix OneFlow transform

Parameters
  • config_str

    String defining configuration of random augmentation. Consists of multiple sections separated by dashes (‘-‘). The first section defines the specific variant of rand augment (currently only ‘rand’). The remaining sections, not order sepecific determine

    • ’m’ - integer magnitude (severity) of augmentation mix (default: 3)

    • ’w’ - integer width of augmentation chain (default: 3)

    • ’d’ - integer depth of augmentation chain (-1 is random [1, 3], default: -1)

    • ’b’ - integer (bool), blend each branch of chain into end result without a final blend, less CPU (default: 0)

    • ’mstd’ - float std deviation of magnitude noise applied (default: 0)

    Example: ‘augmix-m5-w4-d2’ results in AugMix with severity 5, chain width 4, chain depth 2

  • hparams – Other hparams (kwargs) for the Augmentation transforms

Returns

An OneFlow compatible Transform

flowvision.data.auto_augment_transform(config_str, hparams)[source]

Create a AutoAugment transform

Parameters
  • config_str

    String defining configuration of auto augmentation. Consists of multiple sections separated by dashes (‘-‘). The first section defines the AutoAugment policy (one of ‘v0’, ‘v0r’, ‘original’, ‘originalr’). The remaining sections, not order sepecific determine

    • ’mstd’ - float std deviation of magnitude noise applied

    Example: ‘original-mstd0.5’ results in AutoAugment with original policy, magnitude_std 0.5

  • hparams – Other hparams (kwargs) for the AutoAugmentation scheme

Returns

An OneFlow compatible Transform

flowvision.data.cutmix_bbox_and_lam(img_shape, lam, ratio_minmax=None, correct_lam=True, count=None)[source]

Generate bbox and apply lambda correction.

flowvision.data.mixup_target(target, num_classes, lam=1.0, smoothing=0.0, device='cuda')[source]

Mixup the targets with label-smoothing

flowvision.data.rand_augment_transform(config_str, hparams)[source]

Create a RandAugment transform

Parameters
  • config_str

    String defining configuration of random augmentation. Consists of multiple sections separated by dashes (‘-‘). The first section defines the specific variant of rand augment (currently only ‘rand’). The remaining sections, not order sepecific determine

    • ’m’ - integer magnitude of rand augment

    • ’n’ - integer num layers (number of transform ops selected per image)

    • ’w’ - integer probabiliy weight index (index of a set of weights to influence choice of op)

    • ’mstd’ - float std deviation of magnitude noise applied, or uniform sampling if infinity (or > 100)

    • ’mmax’ - set upper bound for magnitude to something other than default of _LEVEL_DENOM (10)

    • ’inc’ - integer (bool), use augmentations that increase in severity with magnitude (default: 0)

    Example: ‘rand-m9-n3-mstd0.5’ results in RandAugment with magnitude 9, num_layers 3, magnitude_std 0.5 ‘rand-mstd1-w0’ results in magnitude_std 1.0, weights 0, default magnitude of 10 and num_layers 2

  • hparams – Other hparams (kwargs) for the RandAugmentation scheme

Returns

An OneFlow compatible Transform

flowvision.data.rand_bbox(img_shape, lam, margin=0.0, count=None)[source]

Standard CutMix bounding-box Generates a random square bbox based on lambda value. This impl includes support for enforcing a border margin as percent of bbox dimensions.

Parameters
  • img_shape (tuple) – Image shape as tuple

  • lam (float) – Cutmix lambda value

  • margin (float) – Percentage of bbox dimension to enforce as margin (reduce amount of box outside image)

  • count (int) – Number of bbox to generate

flowvision.data.rand_bbox_minmax(img_shape, minmax, count=None)[source]

Min-Max CutMix bounding-box Inspired by Darknet cutmix impl, generates a random rectangular bbox based on min/max percent values applied to each dimension of the input image. Typical defaults for minmax are usually in the .2-.3 for min and .8-.9 range for max.

Parameters
  • img_shape (tuple) – Image shape as tuple

  • minmax (tuple or list) – Min and max bbox ratios (as percent of image size)

  • count (int) – Number of bbox to generate