Data Augmentation Strategies Advanced Techniques Mixup Cutmix
# Data Augmentation: Strategies & Advanced Techniques (Mixup, CutMix)
## Introduction & Motivation
Data augmentation: artificially expand dataset. Geometric: rotation, flipping, cropping. Color: brightness, contrast, saturation. Mixup: linear interpolation of samples. CutMix: mix via region replacement. RandAugment: automatic random selection. Applications: improve generalization, reduce overfitting, enable limited data.
Motivation: Limited data; overfitting risk. Augmentation increases effective dataset; improves robustness.
Applications: All supervised learning; critical for small datasets.
---
## Core Concepts & Theory
### Geometric Transformations
Rotation, translation, scaling; preserve labels.
### Mixing Strategies
Mixup: y = λy_a + (1-λ)y_b. CutMix: spatial mixing.
### Automatic Augmentation
Learn augmentation policy; empirical or AutoML.
---
## Mathematical Formulation
Mixup:
$$ ilde{x} = \lambda x_i + (1-\lambda) x_j, \quad ilde{y} = \lambda y_i + (1-\lambda) y_j$$
$$\lambda \sim ext{Beta}(\alpha, \alpha)$$
CutMix:
$$ ilde{x} = x_i \odot M + x_j \odot (1-M)$$
where M = random binary mask.
---
## Advanced Theory & Extensions
### AutoAugment
Reinforcement learning; search optimal policy.
### RandAugment
Random magnitude + operation; simple effective.
### Cutout
Mask random region; simple regularization.
---
## Computational Considerations
Geometric: O(1) per sample; fast.
Mixup/CutMix: O(1) linear/masking; efficient.
AutoAugment: O(policy_search) offline.
---
## Practical Implementation Strategies
### Augmentation Strength
Weak for stable; strong for regularization.
### Augmentation Probability
Apply with probability p; typically 0.5-1.0.
### Domain-Specific
Task-specific; medical vs natural images.
---
## Benchmark Datasets & Evaluation
CIFAR-10: Standard; augmentation improves ~5%.
ImageNet: Essential; strong aug standard practice.
Small Datasets: Critical; enables training.
---
## Key Challenges & Limitations
### Label Preservation
Augmentations should preserve; semantic-level.
### Distribution Shift
Too strong → different distribution.
### Computational Overhead
Augmentation during training; adds cost.
---
## Hyperparameter Tuning
Mixup α: 0.1-1.0; smaller = weaker.
CutMix probability: 0.5-1.0.
Augmentation strength: dataset dependent.
---
## Real-World Applications & Case Studies
ImageNet: AutoAugment improves top-1 by ~0.5%.
Medical: Careful augmentation; preserve pathology.
Low-Data: Strong augmentation; critical.
---
## Integration with Other Methods
Augmentation + Regularization → combined overfitting prevention.
Augmentation + Contrastive Learning → pair generation.
---
## Summary & Key Takeaways
Data augmentation via geometric, color, and mixing strategies artificially expands datasets, improving generalization and robustness through diverse sample creation.
Principles:
1. Geometric: rotation, flipping, cropping; preserve.
2. Mixing: Mixup/CutMix linear interpolation.
3. AutoAugment: learned policy; automatic.
4. RandAugment: simple, effective random selection.
5. Domain-specific: task-dependent strategies.
---
---
## Appendix: Practical Labs
### Lab 1: Geometric Augmentation
import numpy as np
import cv2
def geometric_augment(image, angle=15, scale=0.1, h_shift=0.1):
"""Apply geometric transformations"""
h, w = image.shape[:2]
# Random parameters
rotation_angle = np.random.uniform(-angle, angle)
scale_factor = np.random.uniform(1 - scale, 1 + scale)
h_shift_px = int(h * np.random.uniform(-h_shift, h_shift))
# Rotation matrix
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, rotation_angle, scale_factor)
# Apply transformation
augmented = cv2.warpAffine(image, M, (w, h))
return augmented
# Test
np.random.seed(42)
image = np.random.rand(32, 32, 3)
augmented = geometric_augment(image)
assert augmented.shape == image.shape, "Shape preserved"
print("✓ Geometric augmentation working")
if __name__ == "__main__":
print("Lab 1: Geometric - PASSED")### Lab 2: Mixup
import numpy as np
def mixup(x1, y1, x2, y2, alpha=1.0):
"""Mixup augmentation"""
lam = np.random.beta(alpha, alpha)
x_mix = lam * x1 + (1 - lam) * x2
y_mix = lam * y1 + (1 - lam) * y2
return x_mix, y_mix
# Test
np.random.seed(42)
x1 = np.random.randn(10)
y1 = np.array([1.0])
x2 = np.random.randn(10)
y2 = np.array([0.0])
x_mix, y_mix = mixup(x1, y1, x2, y2)
assert x_mix.shape == x1.shape, "Shape correct"
assert 0 <= y_mix[0] <= 1, "Label in [0,1]"
print("✓ Mixup working")
if __name__ == "__main__":
print("Lab 2: Mixup - PASSED")### Lab 3: CutMix
import numpy as np
def cutmix(x1, y1, x2, y2, alpha=1.0):
"""CutMix augmentation"""
lam = np.random.beta(alpha, alpha)
h, w = x1.shape[:2]
# Random cut region
cut_h = int(h * np.sqrt(1 - lam))
cut_w = int(w * np.sqrt(1 - lam))
cx = np.random.randint(0, w)
cy = np.random.randint(0, h)
x1_min = max(0, cx - cut_w // 2)
x2_min = max(0, cy - cut_h // 2)
x1_max = min(w, cx + cut_w // 2)
x2_max = min(h, cy + cut_h // 2)
x_mix = x1.copy()
x_mix[x2_min:x2_max, x1_min:x1_max] = x2[x2_min:x2_max, x1_min:x1_max]
# Adjust label
box_area = (x1_max - x1_min) * (x2_max - x2_min)
total_area = h * w
lam_adj = 1 - box_area / total_area
y_mix = lam_adj * y1 + (1 - lam_adj) * y2
return x_mix, y_mix
# Test
np.random.seed(42)
x1 = np.random.rand(32, 32, 3)
y1 = np.array([1.0])
x2 = np.random.rand(32, 32, 3)
y2 = np.array([0.0])
x_mix, y_mix = cutmix(x1, y1, x2, y2)
assert x_mix.shape == x1.shape, "Shape correct"
print("✓ CutMix working")
if __name__ == "__main__":
print("Lab 3: CutMix - PASSED")### Lab 4: Augmentation Pipeline
import numpy as np
class AugmentationPipeline:
def __init__(self, augmentations):
self.augmentations = augmentations
def __call__(self, image):
for aug in self.augmentations:
if np.random.rand() < aug['prob']:
image = aug['func'](image, **aug['params'])
return image
def random_brightness(image, delta=0.1):
return image + np.random.uniform(-delta, delta)
def random_contrast(image, delta=0.1):
return image * np.random.uniform(1 - delta, 1 + delta)
# Test
np.random.seed(42)
augmentations = [
{'func': random_brightness, 'prob': 0.5, 'params': {'delta': 0.1}},
{'func': random_contrast, 'prob': 0.5, 'params': {'delta': 0.1}},
]
pipeline = AugmentationPipeline(augmentations)
image = np.random.rand(32, 32, 3)
augmented = pipeline(image)
assert augmented.shape == image.shape, "Shape preserved"
print("✓ Augmentation pipeline working")
if __name__ == "__main__":
print("Lab 4: Pipeline - PASSED")