CutMixUp & Friends

Callbacks to add a MixUp, CutMix, CutMix & MixUp, and CutMix, MixUp, & Augmentation

fastxtend replaces fastai’s MixUp and CutMix with backwards compatible versions that support optional training with MultiLoss via MixHandlerX.

CutMixUp and CutMixUpAugment allow applying MixUp, CutMix, and Augmentations using one callback. Optionally element-wise on the same batch.


source

MixUp

 MixUp (alpha:float=0.4, interp_label:bool|None=None)

Implementation of https://arxiv.org/abs/1710.09412. Supports MultiLoss

Type Default Details
alpha float 0.4 Alpha & beta parametrization for Beta distribution
interp_label bool | None None Blend or stack labels. Defaults to loss’ y_int if None

source

CutMix

 CutMix (alpha:float=1.0, uniform:bool=True, interp_label:bool|None=None)

Implementation of https://arxiv.org/abs/1905.04899. Supports MultiLoss

Type Default Details
alpha float 1.0 Alpha & beta parametrization for Beta distribution
uniform bool True Uniform patches across batch. True matches fastai CutMix
interp_label bool | None None Blend or stack labels. Defaults to loss’ y_int if None

source

CutMixUp

 CutMixUp (mix_alpha:float=0.4, cut_alpha:float=1.0,
           mixup_ratio:Union[int,float]=1,
           cutmix_ratio:Union[int,float]=1, cutmix_uniform:bool=True,
           element:bool=True, interp_label:bool|None=None)

Combo implementation of https://arxiv.org/abs/1710.09412 and https://arxiv.org/abs/1905.04899

Supports element-wise application of MixUp and CutMix on a batch.

Type Default Details
mix_alpha float 0.4 MixUp alpha & beta parametrization for Beta distribution
cut_alpha float 1.0 CutMix alpha & beta parametrization for Beta distribution
mixup_ratio Number 1 Ratio to apply MixUp relative to CutMix
cutmix_ratio Number 1 Ratio to apply CutMix relative to MixUp
cutmix_uniform bool True Uniform patches across batch. True matches fastai CutMix
element bool True Apply element-wise MixUp and CutMix on a batch
interp_label bool | None None Blend or stack labels. Defaults to loss’ y_int if None

source

CutMixUpAugment

 CutMixUpAugment (mix_alpha:float=0.4, cut_alpha:float=1.0,
                  mixup_ratio:Union[int,float]=1,
                  cutmix_ratio:Union[int,float]=1,
                  augment_ratio:Union[int,float]=1,
                  augment_finetune:Union[int,float,NoneType]=None,
                  cutmix_uniform:bool=True, cutmixup_augs:Union[fastcore.t
                  ransform.Transform,Callable[...,fastcore.transform.Trans
                  form],Iterable[Union[fastcore.transform.Transform,Callab
                  le[...,fastcore.transform.Transform]]],fastcore.foundati
                  on.L,fastcore.basics.fastuple,NoneType]=None,
                  element:bool=True, interp_label:bool|None=None)

Combo implementation of https://arxiv.org/abs/1710.09412 and https://arxiv.org/abs/1905.04899 plus Augmentation.

Supports element-wise application of MixUp, CutMix, and Augmentation on a batch.

Pulls augmentations from Dataloaders.train.after_batch. These augmentations are not applied when performing MixUp & CutMix, the frequency is controlled by augment_ratio.

Use augment_finetune to only apply dataloader augmentations at the end of training.

cutmixup_augs are an optional separate set of augmentations to apply with MixUp and CutMix. Usually less intensive then the dataloader augmentations.

Type Default Details
mix_alpha float 0.4 MixUp alpha & beta parametrization for Beta distribution
cut_alpha float 1.0 CutMix alpha & beta parametrization for Beta distribution
mixup_ratio Number 1 Ratio to apply MixUp relative to CutMix & augmentations
cutmix_ratio Number 1 Ratio to apply CutMix relative to MixUp & augmentations
augment_ratio Number 1 Ratio to apply augmentations relative to MixUp & CutMix
augment_finetune Number | None None Number of epochs or pct of training to only apply augmentations
cutmix_uniform bool True Uniform patches across batch. True matches fastai CutMix
cutmixup_augs listified[Transform | Callable[…, Transform]] | None None Augmentations to apply before MixUp & CutMix. Should not have Normalize
element bool True Apply element-wise MixUp, CutMix, and Augment on a batch
interp_label bool | None None Blend or stack labels. Defaults to loss’ y_int if None

CutMixUp Examples

CutMix with uniform cuts

with less_random():
    cutmix = CutMixUp(cutmix_ratio=1, mixup_ratio=0, element=False)
    test_cutmixup(cutmix)

CutMix with random cuts

with less_random():
    cutmix = CutMixUp(cutmix_ratio=1, mixup_ratio=0, cutmix_uniform=False, element=False)
    test_cutmixup(cutmix)

MixUp

with less_random():
    mixup = CutMixUp(mix_alpha=1., cutmix_ratio=0, mixup_ratio=1, element=False)
    test_cutmixup(mixup)

CutMix and MixUp on the same batch

with less_random():
    mixup = CutMixUp(mix_alpha=1., cutmix_ratio=1, mixup_ratio=1)
    test_cutmixup(mixup)

CutMixAugment Examples

CutMix with weak augmentations

with less_random():
    cutmix = CutMixUpAugment(cutmix_ratio=1, mixup_ratio=0, augment_ratio=0, cutmix_uniform=False,
                             cutmixup_augs=aug_transforms(size=112, max_warp=0.1, max_lighting=0.1),
                             element=False)
    test_cutmixup(cutmix, True, size=112)

MixUp with weak augmentations

with less_random():
    mixup = CutMixUpAugment(mix_alpha=1., cutmix_ratio=0, mixup_ratio=1, augment_ratio=0,
                            cutmixup_augs=aug_transforms(max_rotate=20),
                            element=False)
    test_cutmixup(mixup, True)

Just the strong Augmentations

with less_random():
    augment = CutMixUpAugment(cutmix_ratio=0, mixup_ratio=0, augment_ratio=1, element=False)
    test_cutmixup(augment, True)

Mixup, CutMix, and strong Augmentations on the same batch

with less_random():
    batch = CutMixUpAugment(mix_alpha=1., cutmix_ratio=1, mixup_ratio=1, augment_ratio=1, cutmix_uniform=False)
    test_cutmixup(batch, True)

Mixup and CutMix with weak augmentations and strong Augmentations on the same batch

with less_random():
    batch = CutMixUpAugment(mix_alpha=1., cutmix_ratio=1, mixup_ratio=1, augment_ratio=1,
                            cutmix_uniform=False, cutmixup_augs=aug_transforms(max_rotate=20))
    test_cutmixup(batch, True)