Sub topic 5
Interpolation and Border Handling
This chapter assumes you understand images as arrays and have used basic geometric transforms such as resizing, cropping, or rotating. The important question here is what happens to pixel values when a transform moves pixels to positions that did not exist in the original image.
Why interpolation matters
A geometric transform maps output coordinates to input coordinates. After a resize or rotation, an output pixel often falls between input pixels. Interpolation is the rule used to estimate its value.
Common choices are:
| Method | How it estimates values | Typical use |
|---|---|---|
| Nearest neighbor | Copies the closest pixel | Class masks, labels, pixel art |
| Bilinear | Uses a weighted average of four nearby pixels | Most natural-image resizing |
| Bicubic | Uses a larger neighborhood and a smoother curve | High-quality image resizing |
For a photograph, bilinear or bicubic interpolation usually produces smooth results. Nearest neighbor can create blocky edges. For a segmentation mask, however, nearest neighbor is usually required. A mask contains class IDs such as 0 for background and 2 for road. Averaging those IDs could create meaningless values such as 1.3.
import torch
import torchvision.transforms.functional as TF
from torchvision.transforms import InterpolationMode
image = torch.rand(3, 64, 96) # C, H, W; continuous pixel values
mask = torch.randint(0, 4, (64, 96), dtype=torch.long) # discrete class IDs
resized_image = TF.resize(
image,
[128, 128],
interpolation=InterpolationMode.BILINEAR,
antialias=True,
)
resized_mask = TF.resize(
mask.unsqueeze(0),
[128, 128],
interpolation=InterpolationMode.NEAREST,
).squeeze(0)
print(resized_image.shape) # torch.Size([3, 128, 128])
print(resized_mask.unique()) # Still contains only class IDsThe antialias option reduces high-frequency artifacts when shrinking an image. It is useful for natural images, but it does not replace nearest-neighbor interpolation for discrete targets.
Border handling
Rotation, translation, and some crops can ask for pixels outside the original image. The transform must fill those locations. This choice is called border handling or padding mode.
- Constant fills the outside area with one value, often black.
- Replicate extends the nearest edge pixel.
- Reflect mirrors the image at its edge.
- Wrap continues from the opposite edge.
The choice changes the training signal. Constant black borders can create artificial dark corners. Reflect padding often produces a more natural continuation for photographs. Replicate padding avoids new colors but can create bands along the edge.
Geometric targets need special treatment
Images and annotations do not use the same interpolation rule.
- A natural image uses bilinear or bicubic interpolation.
- A segmentation mask uses nearest neighbor.
- A bounding box uses transformed corner coordinates. It is not resized as if it were an image.
- Keypoints use transformed coordinates and may need visibility checks.
A common bug is applying a random crop to the image but not to its mask or boxes. Another is rotating a mask with bilinear interpolation, which silently invents class IDs. Use a transform library that supports paired targets, or make one random decision and apply the same geometric operation to every target.
>callout warning > Never choose interpolation based only on visual smoothness. Choose it based on the data type. Continuous image values can be averaged; discrete labels usually cannot.
Practical decisions
For ordinary image classification, bilinear interpolation with antialiasing is a safe default for resizing. For masks, use nearest neighbor. For borders, inspect examples from the actual dataset. A mode that works for centered objects may fail when objects commonly touch the frame.
Resize and rotate a small batch before training. Look for jagged edges, halos, unexpected colors, broken masks, and objects cut off in implausible ways. These artifacts can teach the model properties of the augmentation pipeline instead of properties of the task.
Exercise
You resize an RGB image and a four-class segmentation mask from 128 × 128 to 256 × 256. Which interpolation method should each use, and why?
Show answer
Use bilinear or bicubic interpolation for the RGB image because its values are continuous. Use nearest-neighbor interpolation for the mask because class IDs are discrete and must not be averaged.