Sub topic 4
Resizing: interpolation, antialiasing, and cost
This chapter assumes basic Python, NumPy arrays, image tensors, and PyTorch data loading. You should also know that an image tensor usually has height, width, and channel dimensions.
Resizing changes an image's width and height. A resize must estimate pixel values at new coordinates because the new pixel grid rarely matches the old one exactly. The estimation method is called interpolation.
Why resizing costs time
A typical input pipeline decodes a compressed image, resizes it, converts its data type, and places it in a batch. Resizing costs CPU time because it reads source pixels and calculates output pixels. The cost grows with the number of output pixels and with the number of source pixels considered for each output pixel.
Downsampling is especially important. For example, reducing a 4000 × 3000 image to 224 × 224 throws away most source pixels. A good downsampling method first limits high-frequency detail. Otherwise, fine lines can become false patterns called aliasing.
Choosing an interpolation method
Common methods make different trade-offs:
| Method | Neighboring samples | Typical use | Trade-off |
|---|---|---|---|
| Nearest | 1 | Masks, labels, pixel art | Fast, but blocky |
| Bilinear | 4 | General image input | Fast and smooth |
| Bicubic | 16 | Higher-quality photographic resize | Sharper, but slower |
| Lanczos | More samples | Offline high-quality resize | Sharp detail, possible ringing |
Nearest-neighbor interpolation must be used for class masks and other categorical data. Averaging label values would create invalid classes. For ordinary photographs, bilinear is often a practical training default. Bicubic or Lanczos can preserve more detail, but their extra work may not improve model accuracy after the image is reduced to a small input size.
Antialiasing applies a low-pass filter during downsampling. A low-pass filter removes detail that the smaller output grid cannot represent. Without it, thin lines and repeated textures can change unpredictably when their frequency exceeds the output resolution.
Keep the operation order consistent
Resize before expensive floating-point preprocessing when possible. Decode to RGB, resize while the image is still an 8-bit image, convert it to a tensor, and normalise later. This reduces memory traffic and avoids doing interpolation on four-byte floating-point values.
from PIL import Image
import numpy as np
def prepare_image(path, size=(224, 224)):
with Image.open(path) as image:
image = image.convert("RGB")
image = image.resize(size, Image.Resampling.BILINEAR)
return np.asarray(image, dtype=np.uint8)
pixels = prepare_image("photo.jpg")
print(pixels.shape, pixels.dtype)The order is not always interchangeable. Cropping before resizing can preserve a selected object, while resizing before cropping can change which details remain. Training and validation must use policies that match their intended meaning. Keep the same color conversion, aspect-ratio policy, interpolation, and antialiasing behavior when comparing pipelines.
Measure quality and speed
Benchmark representative images, not only one small file. Record images per second and CPU utilisation. Then compare model inputs from the old and new pipelines. Small numerical differences are expected, but large differences may indicate a changed color order, aspect ratio, crop, or interpolation method.
Exercise
Which interpolation method should you normally use for a segmentation mask, and why?
Show answer
Use nearest-neighbor interpolation because mask values are categorical labels. Other methods mix neighboring labels and can produce invalid class values.