Sub topic 3
Offline versus Online Augmentation
Prerequisites and the core difference
This chapter assumes you know basic Python, image arrays, supervised learning, dataset splits, and PyTorch training loops.
Data augmentation creates altered versions of training images. The two main choices are when to create those versions:
- Offline augmentation creates files before training and stores them on disk.
- Online augmentation creates a random version when the dataset returns an image.
Both methods can improve generalization. They differ in storage, speed, reproducibility, and flexibility.
Offline augmentation
With offline augmentation, a preprocessing program reads each original image, applies one or more random transforms, and saves the result. Training then treats the saved files like ordinary data.
For example, one original image might produce five rotated, cropped, or color-adjusted files. The dataset becomes larger, but each generated image remains unchanged across epochs.
Offline augmentation is useful when:
- Training hardware has limited CPU capacity.
- Transforms are expensive and can be computed once.
- You need a fixed dataset for an experiment or deployment pipeline.
- You need to inspect every generated image before training.
Its main cost is storage. It also limits variety. If you save five versions, the model sees those same five versions repeatedly. Generating more versions requires running preprocessing again.
Online augmentation
With online augmentation, the original files stay unchanged. A dataset applies transforms while loading each sample. A random crop or color change can therefore be different every time the same image is requested.
from pathlib import Path
from PIL import Image
from torchvision import transforms
from torchvision.datasets import ImageFolder
offline_tf = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.7, 1.0)),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
])
source = ImageFolder("data/train")
output_root = Path("data/train_offline")
for image_index, (path, label) in enumerate(source.samples):
image = Image.open(path).convert("RGB")
class_dir = output_root / source.classes[label]
class_dir.mkdir(parents=True, exist_ok=True)
for view in range(3):
augmented = offline_tf(image)
filename = f"{Path(path).stem}_{image_index}_{view}.jpg"
augmented.save(class_dir / filename, quality=95)
online_tf = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.7, 1.0)),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
transforms.ToTensor(),
])
online_dataset = ImageFolder("data/train", transform=online_tf)The first part materializes three offline views per image. The final lines define an online dataset. In a real training loop, add normalization after ToTensor and use a deterministic transform for validation.
Online augmentation is usually the default because it produces many possible views without multiplying stored files. Its cost is runtime work. Increase the number of data-loader workers if the CPU cannot prepare images fast enough.
Choosing between the methods
| Criterion | Offline augmentation | Online augmentation |
|---|---|---|
| Storage | Requires extra files | Keeps the original dataset |
| Variety across epochs | Fixed saved views | New random views are possible |
| Runtime cost | Lower during training | Higher during loading |
| Reproducibility | Easy to archive exactly | Requires controlled random seeds |
| Experiment changes | Requires regeneration | Change the transform configuration |
A hybrid approach is also common. Use offline processing for expensive, deterministic steps such as decoding, resizing, or format conversion. Apply cheap random transforms online.
Practical decision
Start with online augmentation when the dataset fits normal storage and the transforms are inexpensive. Choose offline augmentation when preprocessing is expensive, training must use an immutable artifact, or the data-loading CPU is a clear bottleneck. Measure data-loader time before making this decision.
Exercise
An experiment uses three saved augmented versions of every training image. Will the model see a new random crop of an image on every epoch?
Show answer
No. It will see the same three saved versions. Online augmentation would sample new crops during access, assuming the transform is random.