MixUp and Friends

Open In Colab

Callbacks that can apply the MixUp (and variants) data augmentation to your training

  1. /usr/local/lib/python3.8/dist-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
  2. return torch._C._cuda_getDeviceCount() > 0
  1. from fastai.vision.all import *

reduce_loss[source]

reduce_loss(loss, reduction='mean')

Reduce the loss based on reduction

class MixHandler[source]

MixHandler(alpha=0.5) :: Callback

A handler class for implementing MixUp style scheduling

Most Mix variants will perform the data augmentation on the batch, so to implement your Mix you should adjust the before_batch event with however your training regiment requires. Also if a different loss function is needed, you should adjust the lf as well.

class MixUp[source]

MixUp(alpha=0.4) :: MixHandler

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

First we’ll look at a very minimalistic example to show how our data is being generated with the PETS dataset:

  1. path = untar_data(URLs.PETS)
  2. pat = r'([^/]+)_d+.*$'
  3. fnames = get_image_files(path/'images')
  4. item_tfms = [Resize(256, method='crop')]
  5. batch_tfms = [*aug_transforms(size=224), Normalize.from_stats(*imagenet_stats)]
  6. dls = ImageDataLoaders.from_name_re(path, fnames, pat, bs=64, item_tfms=item_tfms,
  7. batch_tfms=batch_tfms)

We can examine the results of our Callback by grabbing our data during fit at before_batch like so:

  1. mixup = MixUp(1.)
  2. with Learner(dls, nn.Linear(3,4), loss_func=CrossEntropyLossFlat(), cbs=mixup) as learn:
  3. learn.epoch,learn.training = 0,True
  4. learn.dl = dls.train
  5. b = dls.one_batch()
  6. learn._split(b)
  7. learn('before_train')
  8. learn('before_batch')
  9. _,axs = plt.subplots(3,3, figsize=(9,9))
  10. dls.show_batch(b=(mixup.x,mixup.y), ctxs=axs.flatten())
epochtrain_lossvalid_losstime
000:00

MixUp and Friends - 图2

We can see that every so often an image gets “mixed” with another.

How do we train? You can pass the Callback either to Learner directly or to cbs in your fit function:

  1. learn = cnn_learner(dls, resnet18, loss_func=CrossEntropyLossFlat(), metrics=[error_rate])
  2. learn.fit_one_cycle(1, cbs=mixup)
epochtrain_lossvalid_losserror_ratetime
02.0419600.4954920.16238200:12

class CutMix[source]

CutMix(alpha=1.0) :: MixHandler

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

Similar to MixUp, CutMix will cut a random box out of two images and swap them together. We can look at a few examples below:

  1. cutmix = CutMix(1.)
  2. with Learner(dls, nn.Linear(3,4), loss_func=CrossEntropyLossFlat(), cbs=cutmix) as learn:
  3. learn.epoch,learn.training = 0,True
  4. learn.dl = dls.train
  5. b = dls.one_batch()
  6. learn._split(b)
  7. learn('before_train')
  8. learn('before_batch')
  9. _,axs = plt.subplots(3,3, figsize=(9,9))
  10. dls.show_batch(b=(cutmix.x,cutmix.y), ctxs=axs.flatten())
epochtrain_lossvalid_losstime
000:00

MixUp and Friends - 图3

We train with it in the exact same way as well

  1. learn = cnn_learner(dls, resnet18, loss_func=CrossEntropyLossFlat(), metrics=[accuracy, error_rate])
  2. learn.fit_one_cycle(1, cbs=cutmix)
epochtrain_lossvalid_lossaccuracyerror_ratetime
03.4408830.7930590.7699590.23004100:12

Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021