Skip to main content

Image Augmentation

Augmentation encodes an invariance hypothesis: after a transformation, the target should still be valid. It is not free data and it must not make validation or test examples stochastic.

Prerequisites​

Complete Images as Tensors and understand the training, validation, and test split roles from the MLP project.

Learning goals​

  • separate stochastic training transforms from deterministic evaluation transforms.
  • preserve image/target alignment for classification, boxes, and masks.
  • select interpolation by data semantics rather than convenience.

Worked example​

An asymmetric one-channel shape makes the horizontal flip observable. The operation preserves range, dtype, and shape. A direction label such as “points left” would not survive this transform unchanged.

import torch
from torchvision.transforms import v2

image = torch.tensor(
[[[1., 0., 0., 0.],
[1., 1., 0., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 1.]]]
)
train_transform = v2.RandomHorizontalFlip(p=1.0)
evaluation_transform = v2.Identity()
flipped = train_transform(image)
unchanged = evaluation_transform(image)

print(f"shape={tuple(flipped.shape)} dtype={flipped.dtype} range=({flipped.min():.1f}, {flipped.max():.1f})")
print(f"original_top={unchanged[0, 0].tolist()}")
print(f"flipped_top={flipped[0, 0].tolist()}")

Output:

shape=(1, 4, 4) dtype=torch.float32 range=(0.0, 1.0)
original_top=[1.0, 0.0, 0.0, 0.0]
flipped_top=[0.0, 0.0, 0.0, 1.0]

TorchVision v2 transformations can jointly transform images and typed targets such as bounding boxes and masks. For segmentation masks, use nearest-neighbor interpolation so class IDs remain discrete. Fix random generators only when reproducibility is the goal; training can intentionally sample different valid transformations across epochs while validation remains fixed.

Common mistakes​

  • Applying random crops or flips to validation and test data, making model selection and reporting unstable.
  • Transforming an image but not its boxes, keypoints, or mask.
  • Using a flip when orientation defines the label, such as left-versus-right anatomy or traffic direction.
  • Applying color jitter to a task where absolute color is the measurement of interest.
  • Augmenting before the split, allowing variants of one source image to cross boundaries.

Practice​

List two tasks where horizontal flips preserve labels and two where they do not. For a segmentation task, write the separate interpolation choices for the photograph and its integer mask.

Next steps​

Continue with Transfer Learning to reuse a representation while keeping preprocessing and data provenance aligned.

Primary sources​

Checked 2026-10-16: TorchVision transforms v2 guide, TorchVision RandomHorizontalFlip, TorchVision Resize, and TorchVision TVTensors FAQ.