Mixup and CutMix Augmentation

easy
Unit 6 · Augmentation0/3 to milestone

Two simple augmentations that improve image classifiers by blending examples and their labels:

  • Mixup: x = lam * x_i + (1 - lam) * x_j, and the same for the one-hot labels
  • CutMix: paste a rectangle from image j onto image i, and mix the labels by area: lam = 1 - box_area / (H * W)

Implement for batches x: (N, C, H, W) and one-hot labels y: (N, K), where perm is a permutation that pairs each example i with example perm[i]:

  • mixup(x, y, lam, perm) returning (x_mixed, y_mixed)
  • cutmix(x, y, box, perm), where box = (r0, r1, c0, c1) is a half-open region of rows and columns. Return (x_mixed, y_mixed, lam). Don't modify the inputs.
  • sample_box(H, W, lam, rng): a box with area about (1 - lam) * H * W. Use cut sizes rh = int(H * sqrt(1 - lam)) and rw = int(W * sqrt(1 - lam)), a centre cy = rng.integers(H) and cx = rng.integers(W) (in that order), and clip the box to the image.
#augmentation#regularization#vision2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Mixup and CutMix Augmentation · TechGradify