Machine Learning Academy · 课时

数据增强:提升鲁棒性的变换

您将通过 torchvision.transforms 应用随机水平翻转、裁剪和色彩抖动,并衡量数据增强带来的准确率提升。

第 4 / 4 课13 个步骤

数据增强:提升鲁棒性的变换 是 CoddyKit 上的免费 Machine Learning Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Machine Learning Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Machine Learning Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Is Data Augmentation?

Data augmentation artificially increases the size and diversity of the training dataset by applying random transformations to existing images. Instead of collecting new data (expensive), augmentation creates new training examples on the fly. The key insight is that for many tasks, the label should remain the same under the transformation — a horizontally flipped image of a cat is still a cat. Augmentation reduces overfitting and teaches the model to be invariant to irrelevant variations.

import torchvision.transforms as transforms
from PIL import Image
import torch

# A simple augmentation pipeline
transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomRotation(degrees=15),
    transforms.ColorJitter(brightness=0.3),
    transforms.ToTensor()
])

# Apply to same image -> different result each time
# img = Image.open('cat.jpg')
# aug1 = transform(img)  # one augmented version
# aug2 = transform(img)  # different augmented version
print('Augmentation pipeline defined')

torchvision.transforms: The Augmentation Toolkit

torchvision.transforms provides a rich library of image transforms. Key categories: geometric (flip, rotate, crop, resize, perspective); color (jitter, grayscale, solarize, equalize); pixel-level (Gaussian blur, random erasing); and tensor operations (ToTensor, Normalize). They chain together with transforms.Compose. From PyTorch 2.0, transforms.v2 provides improved speed and additional augmentation primitives.

import torchvision.transforms as transforms

# Catalog of key transforms
train_tf = transforms.Compose([
    transforms.Resize(36),                    # scale
    transforms.RandomCrop(32, padding=4),     # spatial
    transforms.RandomHorizontalFlip(),        # mirror
    transforms.RandomRotation(10),            # rotate
    transforms.RandomGrayscale(p=0.1),        # de-color
    transforms.ColorJitter(
        brightness=0.3, contrast=0.3,
        saturation=0.3, hue=0.1),             # color
    transforms.GaussianBlur(kernel_size=3,    # blur
                            sigma=(0.1, 1.0)),
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3)
])
print('Full augmentation pipeline ready')

RandomHorizontalFlip and RandomVerticalFlip

RandomHorizontalFlip(p=0.5) mirrors the image left-to-right with probability p. This is one of the most effective augmentations for natural images — most objects look equally valid when mirrored. RandomVerticalFlip flips top-to-bottom; use it only when vertical flipping makes semantic sense (e.g., satellite imagery, microscopy). Never use vertical flip for upright objects like faces or digits — it would create unrealistic examples that confuse the model.

import torchvision.transforms as transforms
import torch

hflip = transforms.RandomHorizontalFlip(p=0.5)
vflip = transforms.RandomVerticalFlip(p=0.5)

# Both input and output are PIL images or tensors
# On a tensor: shape (C, H, W)

t = torch.arange(12.).reshape(1, 3, 4)  # 1ch, 3x4
flipped = transforms.functional.hflip(t)
print('Original:', t)
print('H-flipped:', flipped)  # columns reversed

RandomCrop for Spatial Invariance

RandomCrop(size, padding) pads the image (typically with zeros) then crops a random sub-region. For CIFAR-10 (32x32), padding=4 and crop size 32 is standard — it shifts the image content by up to 4 pixels in any direction. This teaches the model that the object label is independent of its exact position, providing translation invariance. CenterCrop is used at test time for deterministic central cropping from a slightly larger image.

import torchvision.transforms as transforms
import torch

# Training: random crop -> trains on shifted versions
train_crop = transforms.Compose([
    transforms.Pad(4, padding_mode='reflect'),  # pad 4 pixels
    transforms.RandomCrop(32)                  # random 32x32 region
])

# Test: deterministic center crop (no padding needed if same size)
test_crop = transforms.CenterCrop(32)

# RandomResizedCrop: zoom + crop (ImageNet-style)
rrc = transforms.RandomResizedCrop(
    224, scale=(0.08, 1.0), ratio=(0.75, 1.33)
)
print('Random crop transforms ready')

ColorJitter: Colour Augmentation

ColorJitter randomly perturbs brightness (how light/dark), contrast (difference between darks and lights), saturation (colour intensity), and hue (colour shift). These simulate variations in lighting, camera settings, and environmental conditions. Strong colour augmentation (jitter with 0.4 on each parameter) is part of the SimCLR self-supervised learning recipe. For standard supervised training, moderate values (0.2-0.3) are recommended.

import torchvision.transforms as transforms

# Moderate color augmentation for supervised learning
color_aug = transforms.ColorJitter(
    brightness=0.2,   # multiply by U[0.8, 1.2]
    contrast=0.2,     # multiply by U[0.8, 1.2]
    saturation=0.2,   # multiply by U[0.8, 1.2]
    hue=0.05          # shift hue by U[-0.05, 0.05]
)

# Strong color augmentation for self-supervised learning
strong_color = transforms.ColorJitter(
    brightness=0.8, contrast=0.8,
    saturation=0.8, hue=0.2
)

print('Color augmentation configured')

RandomRotation and RandomPerspective

RandomRotation(degrees) rotates the image by a random angle within [-degrees, +degrees]. Use small angles (10-15 degrees) for natural images; larger for medical imaging or scientific data where arbitrary orientations are valid. RandomPerspective applies a random projective transformation that simulates viewing the object from a different angle — as if the camera had tilted. Both add robustness to viewpoint changes at the cost of distorting rectangular features.

import torchvision.transforms as transforms

# Mild rotation for natural images
rotate = transforms.RandomRotation(
    degrees=15,
    fill=0            # fill empty corners with black
)

# Perspective distortion
perspective = transforms.RandomPerspective(
    distortion_scale=0.3,
    p=0.3             # apply 30% of the time
)

# Affine: combines rotation, scale, shear, translate
affine = transforms.RandomAffine(
    degrees=10,
    translate=(0.1, 0.1),
    scale=(0.9, 1.1)
)
print('Geometric augmentations defined')

Random Erasing: Occlusion Robustness

RandomErasing randomly masks out a rectangular region of the image by replacing it with zeros, random noise, or the mean pixel value. This simulates occlusion — when part of the object is blocked by another object. Models trained with random erasing learn to recognise objects from partial views, making them more robust in real-world scenes where occlusion is common. It is typically applied to the tensor (after ToTensor), not the PIL image.

import torchvision.transforms as transforms
import torch

# Apply to tensor after normalization
erasing = transforms.RandomErasing(
    p=0.5,           # probability of applying
    scale=(0.02, 0.2),  # fraction of image area to erase
    ratio=(0.3, 3.3),   # aspect ratio of erased region
    value=0           # fill with zeros
)

tf = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3),
    erasing           # applied last on tensor
])
print('Random erasing added to pipeline')

AutoAugment and RandAugment

AutoAugment learns the best augmentation policy for a dataset using reinforcement learning — it was discovered that CIFAR-10 benefits from specific policies (shear X, rotate, equalize). RandAugment is simpler: randomly select N of K available operations and apply them at a magnitude M. Both consistently outperform hand-designed augmentation pipelines. PyTorch provides pre-trained policies via transforms.AutoAugment and transforms.RandAugment.

import torchvision.transforms as transforms

# AutoAugment with CIFAR10 policy
auto_aug = transforms.AutoAugment(
    policy=transforms.AutoAugmentPolicy.CIFAR10
)

# RandAugment: N ops, magnitude M
rand_aug = transforms.RandAugment(
    num_ops=2,         # apply 2 random operations
    magnitude=9        # strength of each operation (0-30)
)

train_tf = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    rand_aug,          # or auto_aug
    transforms.ToTensor(),
    transforms.Normalize([0.4914, 0.4822, 0.4465],
                        [0.2023, 0.1994, 0.2010])
])

Online vs Offline Augmentation

There are two approaches to augmentation. Online augmentation applies transforms during training on-the-fly using the DataLoader — new augmented versions are created each epoch, providing effectively unlimited diversity. Offline augmentation pre-generates augmented images and saves them to disk — faster data loading but fixed diversity. Online augmentation (via transforms) is almost always preferred for deep learning because it creates new variety every epoch and requires no additional storage.

# Online augmentation: new random transform every epoch
# -> effectively infinite training data from finite images

train_dataset = torchvision.datasets.CIFAR10(
    root='./data', train=True,
    transform=train_transform,    # applied at load time
    download=True
)

# The SAME image gets a DIFFERENT augmented version each epoch!
# epoch 1: image 0 -> flipped, dark
# epoch 2: image 0 -> not flipped, brighter
# epoch 3: image 0 -> rotated, saturated
# This diversity is the core benefit of online augmentation
print('Online: new augmentation every epoch automatically')

Augmentation for Non-Image Data

Augmentation is not limited to images. For time-series: add Gaussian noise, scale amplitude, time-warp, or random crop sequences. For audio: pitch shift, time stretch, add background noise, random masking (SpecAugment for speech). For text: synonym replacement, back-translation, random insertion/deletion. For tabular data: add Gaussian noise to features, randomly permute rows in a batch. The principle is always the same: label-preserving variation that increases diversity.

import torch

# Tabular augmentation: Gaussian noise
def tabular_augment(X, noise_std=0.05):
    noise = torch.randn_like(X) * noise_std
    return X + noise  # label unchanged

# Time-series augmentation: amplitude scale
def ts_augment(series, scale_range=(0.8, 1.2)):
    scale = torch.empty(1).uniform_(*scale_range)
    return series * scale  # label unchanged

X = torch.randn(32, 10)   # 32 tabular samples, 10 features
X_aug = tabular_augment(X)
print('Augmented tabular shape:', X_aug.shape)

Measuring Augmentation Impact

Always measure the impact of augmentation quantitatively. Train the same model with and without augmentation for the same number of epochs and compare final validation accuracy. Also compare the training vs validation accuracy gap (generalisation gap) — good augmentation closes this gap. Too aggressive augmentation can actually hurt performance by making training too hard. Ablation studies (testing one augmentation type at a time) reveal which transforms contribute most to your specific task.

# Typical CIFAR-10 accuracy improvements from augmentation
results = {
    'No augmentation':               '73% val acc',
    '+ Random crop':                 '79% val acc (+6%)',
    '+ Horizontal flip':             '82% val acc (+3%)',
    '+ Color jitter':                '84% val acc (+2%)',
    '+ Random erasing':              '85% val acc (+1%)',
    '+ RandAugment':                 '87% val acc (+2%)',
    '+ Cutmix/Mixup':                '89% val acc (+2%)'
}

for aug, result in results.items():
    print(f'{aug}: {result}')

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: data augmentation applies label-preserving random transforms during training to increase dataset diversity and reduce overfitting, key transforms include RandomCrop, RandomHorizontalFlip, ColorJitter, and RandomErasing, and AutoAugment and RandAugment automatically find effective augmentation policies, adding 2-6% accuracy over hand-designed pipelines. Next up we explore vanilla RNNs for sequential data processing.

免费开始

用 AI 导师学习 Python — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「数据增强:提升鲁棒性的变换」课时是免费的吗?

是的 — 「数据增强:提升鲁棒性的变换」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Machine Learning Academy 课程的其余内容,请升级到 CoddyKit PRO。 Machine Learning Academy 课程共包含 4 节课。

「数据增强:提升鲁棒性的变换」这节课中我会学到什么?

您将通过 torchvision.transforms 应用随机水平翻转、裁剪和色彩抖动,并衡量数据增强带来的准确率提升。 你通过在浏览器中直接运行的动手代码来练习 Machine Learning Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Machine Learning Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Machine Learning Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「数据增强:提升鲁棒性的变换」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Machine Learning Academy 课中编写并运行代码吗?

能。每节 Machine Learning Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 卷积与滤波器:检测边缘与模式
  2. 池化层:空间下采样与不变性
  3. 在 CIFAR-10 上构建并训练 CNN
  4. 数据增强:提升鲁棒性的变换
← 返回 Machine Learning Academy