← All Posts
Deep Learning · Regularization in Deep LearningChapter 05 / 9

Data Augmentation, Mixup, and CutMix: Change the Training Distribution

Separate invariance from equivariance, derive Mixup’s linear cross-entropy identity, and calculate exact CutMix targets after boundary clipping.

Instead of adding a penalty to the weights, data augmentation regularizes the model by expanding the empirical training distribution with domain-valid transformations and convex combinations of examples.

1. Invariance vs. equivariance under transformations

Given a distribution of transformations $T\sim\mathcal T$, augmented training minimizes the expected loss across transformed views:

$$J(\theta)=\frac1N\sum_{i=1}^N\mathbb E_{T\sim\mathcal T}\bigl[\ell\bigl(f_\theta(Tx_i),\,T_y y_i\bigr)\bigr].$$

2. Mixup: convex combinations of inputs and targets

Rather than transforming a single example, Mixup draws $\lambda\sim\operatorname{Beta}(\alpha,\alpha)$ on $[0,1]$ and forms a linear interpolation between two training pairs $(x_A,y_A)$ and $(x_B,y_B)$:

$$\tilde x=\lambda x_A+(1-\lambda)x_B,\qquad\tilde y=\lambda y_A+(1-\lambda)y_B.$$

Because cross-entropy $\operatorname{CE}(y,p)=-\sum_k y_k\log p_k$ is linear in the target vector $y$, the loss on the mixed prediction $p=f_\theta(\tilde x)$ decomposes without constructing a dense one-hot tensor:

$$\operatorname{CE}(\tilde y,p)=-\sum_{k=1}^K\bigl(\lambda y_{A,k}+(1-\lambda)y_{B,k}\bigr)\log p_k=\lambda\operatorname{CE}(y_A,p)+(1-\lambda)\operatorname{CE}(y_B,p).$$

For $\lambda=0.7$ and predicted probabilities $(p_A,p_B)=(0.8,0.2)$ on $\tilde x$, the loss is $-0.7\log 0.8-0.3\log 0.2\approx 0.639$. Notice that $p=f_\theta(\tilde x)$ is evaluated once on the blended input $\tilde x$—forcing the model’s logits to transition linearly between classes rather than jumping across a sharp boundary.

3. CutMix: spatial patch replacement and clipped area weights

Blending two images at every pixel can wash out local textures. CutMix instead pastes a rectangular patch from $x_B$ into $x_A$ using a binary mask $M\in\{0,1\}^{H\times W}$ ($M_{uv}=1$ where $x_A$ is kept, $0$ inside the pasted box):

$$\tilde x=M\odot x_A+(1-M)\odot x_B,\qquad\lambda_{\rm actual}=\frac{1}{HW}\sum_{u,v}M_{uv}=1-\frac{(x_1-x_0)(y_1-y_0)}{HW}.$$

Given a target ratio $\lambda$, CutMix proposes box dimensions $r_w=\lfloor W\sqrt{1-\lambda}\rfloor$ and $r_h=\lfloor H\sqrt{1-\lambda}\rfloor$ around a uniform center $(c_x,c_y)$, then clips the box to $[0,W]\times[0,H]$. Because clipping near an image border shrinks the pasted rectangle, the target $\tilde y=\lambda_{\rm actual}y_A+(1-\lambda_{\rm actual})y_B$ must use the actual post-clipping pixel fraction $\lambda_{\rm actual}$:

Source images with vertical and horizontal patterns. Mixup blends both images at every pixel; CutMix replaces one contiguous patch.
Scroll the figure horizontally on small screens.
Mixup blends pixel intensities globally; CutMix preserves sharp local pixels from both images and weights their labels by actual pixel area.

4. Lab: compare Mixup and CutMix with boundary clipping

Pixels, clipped area, and soft targets

Choose a requested A fraction. For CutMix, the square side is rounded from $8\sqrt{1-\lambda}$; its vertical center stays at 4. Move the horizontal center to clip the patch and observe the corrected label weight.

5. Implementation

# Batch Mixup / CutMix loss using cross-entropy linearity in the target
perm = torch.randperm(x.size(0), device=x.device)
mixed_x = lam * x + (1.0 - lam) * x[perm]  # or patch replacement with lam = lam_actual
logits = model(mixed_x)
loss = lam * F.cross_entropy(logits, y) + (1.0 - lam) * F.cross_entropy(logits, y[perm])

6. Questions worth answering

How does CutMix differ from Cutout (Random Erasing)?

Cutout replaces the rectangular patch with zeros or noise while keeping the single hard label $y_A$. CutMix fills the patch with real pixels from another training image $x_B$ and interpolates the target $\lambda_{\rm actual}y_A+(1-\lambda_{\rm actual})y_B$.

Why must CutMix recompute $\lambda_{\rm actual}$ after clipping the bounding box?

When the sampled center $(c_x,c_y)$ lies near an image edge, clipping removes part of the $r_w\times r_h$ box. Using the unclipped $\lambda$ would supervise the network with a larger weight on class B than the fraction of B pixels actually present.

Sources

Zhang et al., mixup: Beyond Empirical Risk Minimization; Yun et al., CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features.