Two simple augmentations that improve image classifiers by blending examples and their labels:
x = lam * x_i + (1 - lam) * x_j, and the same for the one-hot labelslam = 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.