Sub topic 9
Training Versus Evaluation Transforms
Assume you know Python, image tensors, supervised classification, dataset splits, and a PyTorch training loop. This chapter focuses on how the transform pipeline should differ between training and validation or test data.
Why the Pipelines Differ
A training transform creates useful variation. Each time an image is loaded, random operations can produce a different view. This helps the model learn features that remain useful when lighting, position, or scale changes. It also acts as a regularizer, which reduces overfitting to the training images.
An evaluation transform should measure the model under stable conditions. It normally contains only deterministic preprocessing, such as resizing, cropping, conversion to a tensor, and normalization. If validation images receive random crops or random color changes, the metric can change between runs even when the model has not changed.
Use the same evaluation transform for validation and test data unless the task requires a documented difference.
| Stage | Random augmentation | Typical purpose |
|---|---|---|
| Training | Yes | Improve generalization |
| Validation | Usually no | Compare experiments fairly |
| Test | No | Report final performance |
Build the Two Pipelines
The training pipeline below applies spatial and color changes before converting the image to a tensor. The evaluation pipeline uses a fixed resize and center crop. Both pipelines use the same normalization values, so the model receives inputs on the same numerical scale.
from torchvision import transforms
from torchvision.datasets import ImageFolder
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
train_tf = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.5, 1.0)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(
brightness=0.2,
contrast=0.2,
saturation=0.2,
),
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std),
])
eval_tf = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std),
])
train_ds = ImageFolder("data/train", transform=train_tf)
val_ds = ImageFolder("data/val", transform=eval_tf)The training transform is random, so inspecting the same file repeatedly can show different images. The evaluation transform is deterministic. This makes validation loss and accuracy easier to compare across epochs and experiments.
Transform order matters. Operations such as RandomResizedCrop, ColorJitter, and RandAugment generally work on PIL images or unsigned integer image tensors. Normalize expects a floating-point tensor produced by ToTensor. Tensor-only operations, such as RandomErasing, should be placed after tensor conversion.
Avoid Evaluation Leakage
Evaluation leakage occurs when information from validation or test data influences training or model selection improperly. Do not fit normalization statistics, augmentation policies, or preprocessing choices using test images. Choose settings with the training set and, when appropriate, the validation set. Use the test set once for the final report.
Do not apply training-only randomness to validation data to average away an unstable result. If you intentionally use test-time augmentation, describe it as a separate evaluation method. Run several transformed views, obtain a prediction for each, and combine the predictions using a fixed rule such as averaging probabilities.
Check the Difference Visually
A useful debugging check compares several outputs from the training transform with outputs from the evaluation transform. Confirm that training images remain recognizable and that the evaluation crop does not remove the object or its class-defining features.
Exercise
Why should a validation pipeline normally omit RandomHorizontalFlip and ColorJitter?
Show answer
They make the validation input and metric vary between runs, which makes model comparisons less reliable. The evaluation pipeline should usually be deterministic.