Advanced Data Augmentation
Module: Computer Vision | Difficulty: Intermediate
Mixup
where .
CutMix
where is a binary mask and .
RandAugment
Apply random operations from a pool with magnitude :
AutoAugment
Learn augmentation policy via reinforcement learning.
import torch
import numpy as np
def mixup_data(x, y, alpha=0.2):
lam = np.random.beta(alpha, alpha)
batch_size = x.size(0)
index = torch.randperm(batch_size)
mixed_x = lam * x + (1 - lam) * x[index]
return mixed_x, y, y[index], lam
def cutmix_data(x, y, alpha=1.0):
lam = np.random.beta(alpha, alpha)
batch_size = x.size(0)
index = torch.randperm(batch_size)
_, _, H, W = x.shape
cut_ratio = np.sqrt(1 - lam)
rw = int(W * cut_ratio)
rh = int(H * cut_ratio)
cx = np.random.randint(W)
cy = np.random.randint(H)
x1, y1 = max(0, cx - rw // 2), max(0, cy - rh // 2)
x2, y2 = min(W, cx + rw // 2), min(H, cy + rh // 2)
mask = torch.ones_like(x)
mask[:, :, y1:y2, x1:x2] = 0
mixed_x = mask * x + (1 - mask) * x[index]
lam = 1 - (x2 - x1) * (y2 - y1) / (W * H)
return mixed_x, y, y[index], lam
Key Takeaways
- Mixup and CutMix improve calibration and generalization
- RandAugment is simpler and competitive with AutoAugment
- Augmentation should match the domain and task