Upload src/augmentations.py with huggingface_hub
Browse files- src/augmentations.py +55 -0
src/augmentations.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Advanced augmentations: MixUp, CutMix at batch level.
|
| 3 |
+
|
| 4 |
+
These are applied after the dataloader returns a batch,
|
| 5 |
+
not as part of the per-image transform pipeline.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def mixup_data(x: torch.Tensor, y: torch.Tensor, alpha: float = 0.2) -> tuple:
|
| 13 |
+
"""MixUp: convex combination of pairs of examples."""
|
| 14 |
+
if alpha <= 0:
|
| 15 |
+
return x, y, y, 1.0
|
| 16 |
+
lam = np.random.beta(alpha, alpha)
|
| 17 |
+
batch_size = x.size(0)
|
| 18 |
+
index = torch.randperm(batch_size, device=x.device)
|
| 19 |
+
mixed_x = lam * x + (1 - lam) * x[index]
|
| 20 |
+
return mixed_x, y, y[index], lam
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def cutmix_data(x: torch.Tensor, y: torch.Tensor, alpha: float = 1.0) -> tuple:
|
| 24 |
+
"""CutMix: cut and paste patches between training images."""
|
| 25 |
+
if alpha <= 0:
|
| 26 |
+
return x, y, y, 1.0
|
| 27 |
+
lam = np.random.beta(alpha, alpha)
|
| 28 |
+
batch_size = x.size(0)
|
| 29 |
+
index = torch.randperm(batch_size, device=x.device)
|
| 30 |
+
|
| 31 |
+
_, _, h, w = x.shape
|
| 32 |
+
cut_ratio = np.sqrt(1.0 - lam)
|
| 33 |
+
cut_h = int(h * cut_ratio)
|
| 34 |
+
cut_w = int(w * cut_ratio)
|
| 35 |
+
|
| 36 |
+
cy = np.random.randint(h)
|
| 37 |
+
cx = np.random.randint(w)
|
| 38 |
+
|
| 39 |
+
y1 = np.clip(cy - cut_h // 2, 0, h)
|
| 40 |
+
y2 = np.clip(cy + cut_h // 2, 0, h)
|
| 41 |
+
x1 = np.clip(cx - cut_w // 2, 0, w)
|
| 42 |
+
x2 = np.clip(cx + cut_w // 2, 0, w)
|
| 43 |
+
|
| 44 |
+
x_cut = x.clone()
|
| 45 |
+
x_cut[:, :, y1:y2, x1:x2] = x[index, :, y1:y2, x1:x2]
|
| 46 |
+
|
| 47 |
+
# Adjust lambda to the actual area ratio
|
| 48 |
+
lam = 1 - (y2 - y1) * (x2 - x1) / (h * w)
|
| 49 |
+
|
| 50 |
+
return x_cut, y, y[index], lam
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def mixup_criterion(criterion, pred, y_a, y_b, lam):
|
| 54 |
+
"""Loss for MixUp/CutMix: weighted sum of losses."""
|
| 55 |
+
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
|