Computer Vision · Intermediate level

Intermediate level · Module 4: Data Augmentation · Lesson 1 of 3

Augmentation with torchvision Transforms

Learners apply flips, crops, rotations and colour changes with torchvision.transforms.v2 and measure the effect on validation accuracy.

In this lesson you will

  • Build augmentation pipelines
  • Measure the effect of augmentation

Key concepts

transforms.v2.ComposeRandomResizedCropRandomHorizontalFlipColorJittertrain-only augmentation

The lesson

Augmentation creates new training examples by changing images in ways that keep their label: flipping, cropping, rotating slightly or changing brightness. It is one of the most effective ways to improve a model trained on little data.

In torchvision, transforms.v2.Compose([v2.RandomResizedCrop(64, scale=(0.7, 1.0)), v2.RandomHorizontalFlip(), v2.ColorJitter(0.2, 0.2), ...]) defines a pipeline. Apply augmentation only to the training set; validation and test images get only resizing and normalisation.

Measure, do not assume: train with and without augmentation and compare validation accuracy and the gap between training and validation.

Step by step

  1. Display a grid of augmented versions of one image.
  2. Train the CNN with and without augmentation.
  3. Compare validation accuracy and overfitting.
  4. Try one stronger augmentation and observe its effect.

Practice activity

Run an augmentation experiment and report its effect.

Expected result: A visual grid and a measured comparison showing reduced overfitting.

Check your understanding

  1. Why apply augmentation only to training data?

    Show answer

    Validation and test data must reflect real, unmodified images.

  2. What makes an augmentation valid?

    Show answer

    It must not change the correct label.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.