Sub topic 4
Geometric transforms: flips, crops, rotations, and scaling
Assume you know Python, image arrays, supervised classification, and basic PyTorch training loops. This chapter focuses on geometric transforms: operations that change pixel positions while trying to preserve the image label.
The working model
A geometric transform maps an output pixel to a position in the input image. A horizontal flip reverses the column index. A crop selects a smaller region. A rotation changes the coordinate system around a center point. Scaling changes the image size.
Use a transform only when the changed image should keep the same target label. If a classifier recognizes digits, a small rotation may preserve the label. A large rotation might turn a “6” into something visually different. For traffic signs, horizontal flips may change the sign’s meaning, so they are unsafe.
| Transform | Typical benefit | Main risk |
|---|---|---|
| Horizontal flip | Handles left-right variation | Reverses text, faces, or directional signs |
| Random crop | Handles framing and object position | Removes the object or important context |
| Rotation | Handles camera tilt | Creates unrealistic orientations |
| Scaling | Handles distance and object size | Distorts detail or changes apparent shape |
Flips, crops, rotations, and scaling
A flip is usually the simplest geometric operation. RandomHorizontalFlip samples a random decision for each image. Use vertical flips only when upside-down images are realistic for the task.
A crop changes both the visible content and its position. RandomResizedCrop chooses a random area, crops it, and resizes the result to a fixed output size. Its scale argument controls the fraction of the original area that may remain. Smaller values create stronger crops.
Rotation is useful when images can arrive with small camera tilts. Keep the angle range modest unless the real data contains large rotations. Rotation exposes empty regions, called borders. A transform must fill those regions with a chosen padding mode, such as reflection, constant color, or edge values.
Scaling normally requires interpolation, which estimates new pixel values between existing pixels. Bilinear interpolation is common for natural images. Masks and class-index label maps need nearest-neighbor interpolation so their discrete values do not become fractional.
from PIL import Image
from torchvision import transforms
train_tf = transforms.Compose([
transforms.RandomResizedCrop(
size=224,
scale=(0.7, 1.0),
ratio=(0.8, 1.25),
),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomRotation(
degrees=10,
interpolation=transforms.InterpolationMode.BILINEAR,
fill=0,
),
transforms.ToTensor(),
])
image = Image.open("data/train/cat/example.jpg").convert("RGB")
tensor = train_tf(image)
print(tensor.shape)Composition and common errors
Composition means applying several transforms in sequence. The order matters. Cropping before rotation gives a different result from rotating before cropping. A common image-classification sequence is crop, flip, rotate, convert to a tensor, and normalize.
Do not apply random geometric transforms to validation or test images. Their metrics should measure a fixed preprocessing rule. Also avoid transformations that remove the class-defining object. A crop that cuts away a small bird may produce an image whose label is no longer reliable.
For segmentation or object detection, transform annotations with the image. A flipped image needs a flipped mask. A crop must shift or remove bounding boxes according to the same crop. Independent random draws break this alignment.
Exercise
Why should a segmentation mask use nearest-neighbor interpolation during a rotation?
Show answer
A mask contains discrete class IDs. Nearest-neighbor interpolation preserves those IDs, while bilinear interpolation could create invalid fractional classes.