Sub topic 1
Why Augment Images: Overfitting and Limited Data
Prerequisites
This chapter assumes Basic Python, familiarity with images as arrays of pixel values, supervised image classification, train/validation/test splits, and a PyTorch-style training loop. You should also understand that overfitting happens when a model performs well on training examples but poorly on unseen examples.
The problem: limited data and overfitting
A model can memorize details that happen to occur in the training set. For example, a classifier trained to recognize dogs might learn that most training dog images contain a particular background, camera angle, or lighting pattern. These details are not the concept of “dog.” They are accidental properties of the collected images.
Data augmentation creates a changed version of a training image while keeping its intended label. A horizontal flip, small crop, or brightness change can produce a new training example. The underlying image file is not necessarily stored. Many libraries create the changed version each time the sample is loaded. This is called online augmentation.
Augmentation does not add new real-world facts to the dataset. Instead, it encourages the model to treat harmless changes as irrelevant. This usually improves generalization, which is performance on examples the model did not see during training.
| Without augmentation | With augmentation |
|---|---|
| The model can memorize repeated visual details. | The model sees varied versions of each example. |
| Training accuracy may rise quickly. | Training is made more difficult. |
| Validation performance may stop improving. | Validation performance may improve. |
Augmentation as regularization
Regularization is a method that limits a model's ability to memorize training data. Weight decay and dropout are common examples. Augmentation acts as a data-level regularizer: it changes the training inputs so the model must learn stable visual patterns.
Suppose an image classifier receives a cat image. If random crops remove some background, the model is pushed to use the cat rather than the background. If brightness changes do not change the label, the model learns that exact lighting is not essential.
A useful test is:
> Would a human assign the same label after this change?
If the answer is usually yes, the transform may be suitable. If the answer is no, the transform can inject incorrect supervision. Rotating a traffic-sign image by 10 degrees may be reasonable, while rotating it by 180 degrees may change its meaning.
from torchvision import transforms
train_tf = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.7, 1.0)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(brightness=0.2, contrast=0.2),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
),
])Each training access can produce a different view of the same source image. RandomResizedCrop changes position and scale. RandomHorizontalFlip changes orientation. ColorJitter changes appearance without changing the object category, assuming color is not the label-defining feature.
When augmentation helps, and when it does not
Augmentation is most useful when the training set is small, repetitive, or visually narrower than the deployment data. It cannot fix incorrect labels, severe class imbalance, or a validation set that does not represent real use.
Strong augmentation can also hurt. It may erase the object, create unrealistic images, or remove a feature required by the task. Start with changes that reflect real variation, then compare validation results with a controlled experiment.
Keep validation and test images unchanged except for deterministic resizing, cropping, and normalization. Otherwise, evaluation becomes harder to interpret.
Exercise
A model classifies images of handwritten digits. Which is safer: a small rotation or a vertical flip, and why?
Show answer
A small rotation is usually safer because handwriting can be slightly tilted, while a vertical flip creates an image that is unlike a normal digit and may change its meaning.