Deep LearningDeep Learning Comparison Series
Mixup vs CutMix
Question 312 of 314 · Updated
On this page
- 1. How the methods create a training example
- Mixup: blend the whole images
- CutMix: replace a rectangular patch
- 2. A simple visual and numerical example
- 3. Side-by-side comparison
- 4. What do they encourage the model to learn?
- 5. When should you choose each?
- 6. Important implementation details
- 7. PyTorch-style training snippets
- 8. Can you use Mixup and CutMix together?
- 9. How should you evaluate them?
- Key takeaway
Mixup blends two complete training examples pixel by pixel; CutMix replaces a rectangular region of one image with a patch from another. Both mix the corresponding class labels, but they create different visual evidence and encourage different kinds of generalization.
1. How the methods create a training example#
Mixup: blend the whole images#
Choose two images and a mixing coefficient between 0 and 1:
Every pixel is interpolated. The target label is interpolated using the same coefficient, producing a soft target.
For example, if , the new input is a 70% pixel-wise blend of image A and a 30% blend of image B. Its training target gives the same 70:30 weighting to their labels.
CutMix: replace a rectangular patch#
CutMix keeps most of one image and pastes a rectangular patch from another image:
Here, is a binary spatial mask. The target is weighted by the actual area contributed by each image.
If the pasted patch occupies 25% of the image, the target gives 75% weight to the base image and 25% to the donor image:
The label weight must be calculated from the actual patch area after clipping and rounding, not blindly copied from the initially sampled coefficient.
2. A simple visual and numerical example#
Imagine two 100 × 100 images: a cat and a dog.
With Mixup:
- Each pixel becomes a weighted blend of the corresponding cat and dog pixels.
- With , the entire image contains a 70:30 blend.
- The target is 70% cat and 30% dog.
With CutMix:
- Keep 75% of the cat image.
- Replace a 25% rectangular region with pixels from the dog image.
- The target is 75% cat and 25% dog, based on image area.
The key difference is not the label formula: both use weighted labels. It is how the input pixels are combined.
3. Side-by-side comparison#
| Aspect | Mixup | CutMix |
|---|---|---|
| Mixing operation | Pixel-wise interpolation | Spatial cut-and-paste |
| Image appearance | Both images blended throughout | Original pixels retained in separate regions |
| Spatial structure | Blurs local boundaries and details | Preserves local pixels within each region |
| Label weighting | Sampled interpolation coefficient | Actual area contributed by each image |
| Main regularization effect | Encourages smoother predictions between examples | Encourages recognition from partial and mixed visual evidence |
| Useful signal | Global appearance and label interpolation | Local features, partial visibility and occlusion |
| Main caveat | Can create unrealistic blended images | Patch area may not reflect semantic object area |
| Implementation | Simple tensor interpolation | Patch mask, replacement and area correction |
Neither method is universally better. Their results depend on the dataset, architecture, mixing strength, training recipe and evaluation task.
4. What do they encourage the model to learn?#
Mixup encourages smoother behavior between training examples. Because both the input and target are interpolated, the model is trained to produce intermediate predictions for interpolated samples. The original Mixup work studied generalization, memorization of corrupted labels and adversarial robustness.
CutMix encourages the model to use evidence from different image regions. A classifier cannot rely only on the most discriminative patch if that region may be replaced. The original CutMix work reported improvements in classification and weakly supervised localization, as well as transfer to detection and image captioning benchmarks.
These are research findings, not guarantees for every dataset or model.
5. When should you choose each?#
Choose Mixup when:
- You want a simple, low-overhead augmentation baseline.
- Smoother behavior between examples is useful.
- You want to test regularization against memorization or noisy labels.
- Global interpolation is reasonable for the input representation.
Choose CutMix when:
- Recognizing objects from partial visual evidence matters.
- Occlusion is common or important.
- Preserving local image texture and structure is useful.
- Localization-related behavior is important to evaluate.
For fine-grained recognition, medical images, or datasets where a small region determines the class, do not assume either method will help. A randomly selected patch can remove the important feature or produce a misleading label. Validate the augmentation policy on representative data.
6. Important implementation details#
Both methods commonly sample a mixing strength using a Beta distribution:
The parameter controls the distribution of mixing strengths. It is a hyperparameter to tune, not a guarantee of a particular accuracy improvement.
For Mixup, use the same sampled coefficient for the input and soft target. For CutMix, derive the target weight from the final pasted rectangle:
where is the actual pasted area.
In both cases, calculate the loss against the mixed target. For soft-label cross-entropy, this is equivalent to weighting the two original class losses by their respective contributions.
A common implementation mistake is to use the sampled CutMix coefficient after the rectangle has been rounded or clipped. That makes the target inconsistent with the pixels actually present.
7. PyTorch-style training snippets#
Mixup
PYTHONimport torch def mixup(x, y, alpha=0.2): lam = torch.distributions.Beta(alpha, alpha).sample().item() permutation = torch.randperm(x.size(0), device=x.device) mixed_x = lam * x + (1 - lam) * x[permutation] y_a = y y_b = y[permutation] return mixed_x, y_a, y_b, lam # logits = model(mixed_x) # loss = lam * criterion(logits, y_a) \ # + (1 - lam) * criterion(logits, y_b)
CutMix
PYTHONdef cutmix(x, y, alpha=1.0): lam = torch.distributions.Beta(alpha, alpha).sample().item() permutation = torch.randperm(x.size(0), device=x.device) height, width = x.shape[-2:] cut_ratio = (1.0 - lam) ** 0.5 cut_w = int(width * cut_ratio) cut_h = int(height * cut_ratio) cx = torch.randint(width, (1,)).item() cy = torch.randint(height, (1,)).item() x1 = max(cx - cut_w // 2, 0) x2 = min(cx + cut_w // 2, width) y1 = max(cy - cut_h // 2, 0) y2 = min(cy + cut_h // 2, height) mixed_x = x.clone() mixed_x[:, :, y1:y2, x1:x2] = \ x[permutation, :, y1:y2, x1:x2] # Recalculate from the actual clipped rectangle. lam_actual = 1.0 - ( (x2 - x1) * (y2 - y1) / (width * height) ) y_a = y y_b = y[permutation] return mixed_x, y_a, y_b, lam_actual # logits = model(mixed_x) # loss = lam_actual * criterion(logits, y_a) \ # + (1 - lam_actual) * criterion(logits, y_b)
These snippets illustrate the core operations for a classification batch. Production implementations should also handle odd patch dimensions, batch pairing, device and random-number-generator behavior, and the intended target format.
8. Can you use Mixup and CutMix together?#
Yes. They are distinct augmentation policies and can be evaluated in the same training pipeline, for example by randomly choosing one policy for a batch or by applying them at different stages. But combining them is not automatically better: stronger or excessive mixing can make training examples too ambiguous. Compare each method and the combined policy under the same training budget and validation protocol.
For object detection or segmentation, adapt the targets to the task. A simple two-class soft target based on image area is not a substitute for updating bounding boxes, masks or per-pixel labels correctly.
9. How should you evaluate them?#
Run a controlled comparison:
- Train a baseline without either method.
- Add Mixup while keeping the model, optimizer, schedule and data split unchanged.
- Repeat with CutMix.
- If useful, test a combined policy.
- Compare validation accuracy, calibration, robustness to corruption or occlusion, localization quality when relevant, and training cost.
Tune the mixing probability and on validation data. Do not compare results from different model sizes, training durations or optimization settings and attribute every difference to the augmentation method.
Key takeaway#
Mixup blends whole images and their labels, encouraging smoother predictions between examples. CutMix pastes image regions and weights labels by actual area, encouraging recognition from partial visual evidence. Start with the method that matches the failure mode you want to address, then verify its value on your own validation set.
References
- 1.Mixup: Beyond Empirical Risk Minimization — ICLR 2018openreview.net
- 2.
- 3.
- 4.Torchvision MixUp documentationdocs.pytorch.org
- 5.Torchvision CutMix documentationdocs.pytorch.org
- 6.Mixup augmentationmachinoai.in
- 7.CutMix augmentationmachinoai.in
Related questions
Explore other topics:Machine LearningLarge Language Models