import torch
import torch.nn as nn
from torchvision.transforms.v2 import MixUp, CutMix, RandomChoice
mixup = MixUp(alpha=0.2, num_classes=10)
cutmix = CutMix(alpha=1.0, num_classes=10)
# Randomly pick one each batch
batch_aug = RandomChoice([mixup, cutmix])
# Loss: with mixed labels, use CE with soft targets
criterion = nn.CrossEntropyLoss()
for x, y in train_loader:
x, y = batch_aug(x, y) # y is now soft (probability over classes)
out = model(x)
loss = criterion(out, y)
loss.backward()
변환한 이미지 한 장 살펴보기·python
import torch
import torchvision.transforms.v2 as T
from torchvision import tv_tensors
# Build a synthetic image
img = torch.randint(0, 255, (3, 224, 224), dtype=torch.uint8)
aug = T.Compose([
T.ToImage(),
T.ToDtype(torch.float32, scale=True),
T.RandomResizedCrop(224, scale=(0.5, 1.0)),
T.RandomHorizontalFlip(p=0.5),
])
# Apply twice — different random outcomes
out1 = aug(img)
out2 = aug(img)
print(torch.equal(out1, out2)) # False — random transforms differ each call
CIFAR-10용 학습 변환과 검증 변환 파이프라인을 만들어 봐. 같은 이미지에 각각 다섯 번 적용하면 학습 변환은 서로 다른 출력 다섯 개를, 검증 변환은 같은 출력 다섯 개를 만들어야 해. matplotlib로 2x5 격자에 저장해 두면 두 파이프라인을 분리해야 하는 이유를 눈으로 기억할 수 있어.
Progress
Progress is local-only — sign in to sync across devices.