데이터 증강(Data Augmentation)은 기존 학습 데이터에 다양한 변환을 적용해 데이터셋을 인위적으로 확대하는 기법이다. 과적합을 방지하고 모델의 일반화 성능을 높이는 데 효과적이다.
이미지 증강
python
import torchvision.transforms as T
import torchvision.transforms.v2 as T2
# 기본 증강
basic_transform = T.Compose([
T.RandomHorizontalFlip(p=0.5),
T.RandomVerticalFlip(p=0.1),
T.RandomRotation(degrees=15),
T.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
T.RandomResizedCrop(224, scale=(0.8, 1.0)),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 고급 증강 (AutoAugment)
auto_transform = T.Compose([
T.AutoAugment(T.AutoAugmentPolicy.IMAGENET),
T.ToTensor()
])
# RandAugment: 랜덤하게 N개 변환 선택 후 magnitude M으로 적용
rand_transform = T.Compose([
T.RandAugment(num_ops=2, magnitude=9),
T.ToTensor()
])
MixUp & CutMix
python
import torch
import numpy as np
def mixup(x, y, alpha=0.2):
"""두 샘플을 선형 보간"""
lam = np.random.beta(alpha, alpha)
idx = torch.randperm(x.size(0))
x_mix = lam * x + (1 - lam) * x[idx]
y_a, y_b = y, y[idx]
return x_mix, y_a, y_b, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
def cutmix(x, y, alpha=1.0):
"""이미지 일부를 다른 이미지로 교체"""
lam = np.random.beta(alpha, alpha)
idx = torch.randperm(x.size(0))
_, _, H, W = x.shape
cut_ratio = np.sqrt(1 - lam)
cut_h, cut_w = int(H * cut_ratio), int(W * cut_ratio)
cx, cy = np.random.randint(W), np.random.randint(H)
x1 = max(cx - cut_w // 2, 0); x2 = min(cx + cut_w // 2, W)
y1 = max(cy - cut_h // 2, 0); y2 = min(cy + cut_h // 2, H)
x_mix = x.clone()
x_mix[:, :, y1:y2, x1:x2] = x[idx, :, y1:y2, x1:x2]
lam_real = 1 - (x2-x1)*(y2-y1)/(W*H)
return x_mix, y, y[idx], lam_real
텍스트 증강
python
import random
def text_augment(text, method='swap'):
words = text.split()
if method == 'swap' and len(words) > 1:
i, j = random.sample(range(len(words)), 2)
words[i], words[j] = words[j], words[i]
elif method == 'delete':
idx = random.randint(0, len(words) - 1)
words.pop(idx)
elif method == 'duplicate':
idx = random.randint(0, len(words) - 1)
words.insert(idx, words[idx])
return ' '.join(words)
# Back-translation: 한→영→한으로 번역 후 원문 대체
증강 기법 비교
| 기법 | 대상 | 효과 | 비용 |
|---|
| 기하 변환 | 이미지 | 위치 불변성 | 낮음 |
| 색상 지터 | 이미지 | 색상 불변성 | 낮음 |
| MixUp | 이미지 | 일반화 | 낮음 |
| CutMix | 이미지 | 강력한 정규화 | 낮음 |
| AutoAugment | 이미지 | 최적 증강 탐색 | 높음 |
| Back-translation | 텍스트 | 표현 다양성 | 중간 |
관련 개념