# CutMix

> Cut a random box out of one training image, paste in the same region of another, and mix the labels by the pasted area. Every pixel stays real, no pixels are wasted as in Cutout, and the label stays honest about what is visible.

Source: https://www.mlfoundry.in/learn/cutmix  
Section: Deep Learning › Regularization & Normalization  
Level: core · 10 min read · updated 2026-10-11

**CutMix:** is a data augmentation for images that replaces a rectangular region of image $x_a$ with the same region of image $x_b$ and sets the target to $\lambda y_a+(1-\lambda)y_b$, where $1-\lambda$ is the **fraction of the image area** taken from $x_b$.

Two earlier ideas each fixed half of a problem. **Cutout** (DeVries and Taylor, 2017) erases a random square, forcing a classifier to use the whole object instead of one discriminative part. But the erased pixels carry no information, and the label still claims 100% of a class whose evidence may have been cut away. **Mixup** (Zhang et al., 2018) uses every pixel and mixes the labels, but a pixel-wise blend of two photos is not a locally plausible image, which confuses tasks that depend on local detail, such as localization.

**Yun, Han, Oh, Chun, Choe and Yoo (2019)**, *CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features* (ICCV), combined them: cut like Cutout, but fill the hole with a patch of another training image, and mix the labels in proportion to area. On ImageNet with ResNet-50 they reported top-1 error 21.40%, against 23.68% for the baseline, 22.93% for Cutout and 22.58% for Mixup, and better weakly supervised localization and detection transfer. CutMix and Mixup are now used together in most image-classification recipes, from DeiT (Touvron et al., 2021) to the torchvision reference models.

## Formula

$$
\tilde x=M\odot x_a+(\mathbf 1-M)\odot x_b,\qquad \tilde y=\lambda y_a+(1-\lambda)\,y_b,\qquad \lambda=1-\frac{r_w\,r_h}{W H}
$$

- $M\in\{0,1\}^{W\times H}$: binary mask: 0 inside the box, 1 elsewhere (shared across channels)
- $\odot$: element-wise product
- $r_w,r_h$: the box size after clipping to the image
- $\lambda$: the area fraction kept from $x_a$; also its label weight

**Sampling the box (Yun et al., 2019; torchvision v2.CutMix)**

1. Draw $\lambda\sim\mathrm{Beta}(\alpha,\alpha)$; the paper and torchvision default to $\alpha=1$, i.e. $\lambda\sim U(0,1)$.
2. Box size $r_w=W\sqrt{1-\lambda}$, $r_h=H\sqrt{1-\lambda}$: same aspect ratio as the image and area $(1-\lambda)WH$.
3. Box centre $(c_x,c_y)$ uniform over the image. Clip the box to the image borders.
4. Clipping shrinks the box, so **recompute** $\lambda=1-r_wr_h/(WH)$ from the actual box. The label follows the pixels, not the sampled number.
5. Paste the box from the partner image (a shuffled or rolled copy of the batch) and mix the labels with the corrected $\lambda$.

**At a glance**

- **Kind:** label-aware, region-level data augmentation for images (also used on spectrograms)
- **Hyperparameter:** $\alpha$ (default 1), plus the probability of applying it
- **Label:** area ratio $\lambda$ after clipping; soft-target cross-entropy
- **Pixels:** all real, from one of the two images; only the box edges are artificial
- **Cost:** one slice copy per batch
- **Compared with:** Cutout (box erased, label unchanged), Mixup (all pixels blended, label blended)

> Figure: Two procedural 20×20 images, a sun scene (A) and stripes (B), combined with the same λ and box. At λ = 0.75 and centre (10, 10) the box is [5, 15) × [5, 15), 100 pixels, so λ′ = 1 − 100/400 = 0.75. Mixup has 0% untouched pixels: every one is a blend. Cutout keeps 75% of the pixels but still labels the image (1, 0). CutMix keeps 100% real pixels, 75% from A and 25% from B, exactly matching its label (0.75, 0.25). Drag the box to the corner at (18, 18): it is clipped to 7 × 7 = 49 pixels and the label becomes (0.8775, 0.1225). The sample button draws λ and the centre as the paper does; integer box sizes also move λ′ slightly.

## Why it works: local realism and honest labels

Each method changes the input and the label. The difference is whether the two stay consistent and whether the input still looks like natural images locally.

Same λ = 0.75 and box as the figure's default (20×20 images), values from the figure's code

| method | input | untouched real pixels | pixels showing A | label (A, B) |
| --- | --- | --- | --- | --- |
| Cutout | box filled with the mean colour | 75% | 75% | (1, 0) |
| Mixup | $0.75A+0.25B$ everywhere | 0% | blended | (0.75, 0.25) |
| CutMix | box replaced by B's pixels | 100% | 75% | (0.75, 0.25) |

**Local realism.** Apart from the edges of the box, every 3×3 neighbourhood in a CutMix image comes from a real image, so convolutional filters see the statistics they will see at test time. **Honest labels.** The label weight equals the share of visible evidence. **No wasted pixels.** The region Cutout spends on a grey square now teaches the second class. And because the network must recognize a class from a partial view and from a pasted patch in an unusual place, it learns to spread its attention over the whole object, which is why CutMix improved class-activation-map localization in the paper.

## Worked example: a clipped box on a 32×32 image

**λ = 0.75 drawn, CIFAR-sized image, checked in Node with the torchvision box rule**

1. Half sizes $\lfloor\tfrac12\sqrt{1-0.75}\cdot32\rfloor=\lfloor8\rfloor=8$, so the box is $16\times16$.
2. Centre $(16,16)$: box $[8,24)\times[8,24)$, area 256, $\lambda'=1-256/1024=0.75$. Nothing changes.
3. Centre $(28,28)$: the box $[20,36)$ is clipped to $[20,32)$ in both axes, area $12\times12=144$, so $\lambda'=1-144/1024=0.859$.
4. Label $(0.859,\,0.141)$. If the model predicts $p(a)=0.6$, $p(b)=0.3$, the loss is $-0.859\ln0.6-0.141\ln0.3=0.6083$.
5. Using the uncorrected $\lambda=0.75$ would claim 25% of class $b$ when only 14% of the pixels show it.

```python
import math, torch, torch.nn.functional as F
from torchvision.transforms import v2

def cutmix(x, y, num_classes, alpha=1.0):
    """x: (N, C, H, W), y: (N,) int labels -> mixed images, soft labels."""
    N, _, H, W = x.shape
    lam = torch.distributions.Beta(alpha, alpha).sample().item()
    cx, cy = torch.randint(W, ()).item(), torch.randint(H, ()).item()
    rw, rh = int(0.5 * math.sqrt(1 - lam) * W), int(0.5 * math.sqrt(1 - lam) * H)
    x1, x2 = max(cx - rw, 0), min(cx + rw, W)
    y1, y2 = max(cy - rh, 0), min(cy + rh, H)
    idx = torch.randperm(N)
    x = x.clone()
    x[:, :, y1:y2, x1:x2] = x[idx, :, y1:y2, x1:x2]
    lam = 1 - (x2 - x1) * (y2 - y1) / (W * H)        # area actually pasted
    y1h = F.one_hot(y, num_classes).float()
    return x, lam * y1h + (1 - lam) * y1h[idx]

# torchvision: batch-level transforms (apply after the DataLoader or in collate_fn)
NUM_CLASSES = 1000
cutmix_t = v2.CutMix(alpha=1.0, num_classes=NUM_CLASSES)
mixup_t = v2.MixUp(alpha=0.2, num_classes=NUM_CLASSES)
cutmix_or_mixup = v2.RandomChoice([cutmix_t, mixup_t])     # one of the two per batch

images = torch.rand(16, 3, 224, 224)
labels = torch.randint(0, NUM_CLASSES, (16,))
images, soft = cutmix_or_mixup(images, labels)             # soft: (16, 1000)
# loss = F.cross_entropy(model(images), soft)

# timm's equivalent (DeiT-style recipe): one object, switch between the two per batch
# from timm.data import Mixup
# mix = Mixup(mixup_alpha=0.8, cutmix_alpha=1.0, switch_prob=0.5, label_smoothing=0.1, num_classes=1000)
# loss with timm.loss.SoftTargetCrossEntropy()
```

CutMix by hand, and the torchvision v2 transforms with a random choice between CutMix and MixUp

> **Recompute λ after clipping:** Boxes near the border are clipped, and the pasted area is then smaller than $(1-\lambda)WH$. Using the sampled $\lambda$ for the label over-states the partner class: by 25% versus 14% in the worked example. torchvision's `CutMix` returns the corrected value; hand-written versions often forget it. Also apply CutMix to whole batches, not inside a per-sample dataset transform: it needs a partner image and integer labels to turn into soft ones.

> **CutMix and Mixup are complementary:** Mixup regularizes globally, smoothing outputs between examples. CutMix regularizes locally, teaching the network that any region can belong to a different object. Modern recipes alternate them: torchvision's `RandomChoice([CutMix, MixUp])` picks one per batch, and timm's `Mixup` object with `switch_prob=0.5` does the same, typically with Mixup $\alpha=0.8$, CutMix $\alpha=1.0$ and label smoothing 0.1 in the DeiT recipe.

> **When not to use it:** If the label depends on something a box can remove or create, CutMix can produce wrong targets: counting tasks, fine-grained classes decided by one small part, or detection and segmentation without mixing the boxes and masks too. Its value is also smaller for short training schedules, since the task is harder and takes longer to fit, like Mixup.

**Strengths**

- Every pixel is real and informative; no grey squares as in Cutout
- Label weights match the visible area, so targets stay honest
- Improves classification and localization; locally realistic inputs suit CNNs
- One hyperparameter and almost no compute

**Limitations**

- Box edges create artificial boundaries the model may learn to detect
- The pasted patch may contain only background, so the label can still over-state what is visible
- Image-specific: needs a spatial layout to cut
- Needs soft-label losses and longer schedules to pay off

**Question:** On a 224×224 image, CutMix pastes a 112×112 box from the partner image that lies fully inside the image. What is the label weight λ of the original image?

- A. 0.75
- B. 0.5
- C. 0.25
- D. It depends on α only

**Answer:** A. 0.75 — The box covers $112^2/224^2=0.25$ of the area, so $\lambda=1-0.25=0.75$ for the original and 0.25 for the partner. The sampled $\lambda$ sets the box size, but the label is always read off the actual area.

**Question:** Which statement contrasts Cutout, Mixup and CutMix correctly?

- A. Cutout erases a region but keeps the label; Mixup blends every pixel and the label; CutMix pastes a real region and mixes the label by area
- B. All three mix labels in proportion to λ
- C. CutMix blends pixels like Mixup but only inside a box
- D. Cutout and CutMix are identical when α = 1

**Answer:** A. Cutout erases a region but keeps the label; Mixup blends every pixel and the label; CutMix pastes a real region and mixes the label by area — Cutout's label stays one-hot even though evidence was removed. Mixup's inputs are blends everywhere, which is not locally realistic. CutMix keeps each pixel from a real image and sets the label weight to the area fraction.

**Key points**

- $\tilde x=M\odot x_a+(1-M)\odot x_b$: paste a box from $x_b$ into $x_a$; $\tilde y=\lambda y_a+(1-\lambda)y_b$ with $\lambda$ the area kept (Yun et al., 2019).
- Box side $\sqrt{1-\lambda}$ times the image side, centre uniform, then clip and **recompute** $\lambda$ from the actual area.
- Fixes Cutout's wasted pixels and dishonest label, and Mixup's unrealistic blended pixels.
- ResNet-50 on ImageNet: 21.40% top-1 error versus 23.68% baseline, 22.93% Cutout and 22.58% Mixup in the paper; better localization too.
- torchvision.transforms.v2.CutMix on batches; v2.RandomChoice([CutMix, MixUp]) or timm's Mixup(switch_prob=0.5) alternate the two.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
