Normalize varying image sizes to a fixed resolution
Model input preprocessing
TensorFlow · Class
A Keras preprocessing layer that resizes individual images or batches to a chosen height and width. It supports several interpolation methods and optional aspect-ratio-preserving cropping or padding.
Resizing transforms each 3D image or 4D image batch to the requested height and width. By default it uses bilinear interpolation and channels-last ordering; optional cropping or padding can preserve the original aspect ratio instead of distorting the image.
tf.keras.layers.Resizing(height, width, interpolation='bilinear', crop_to_aspect_ratio=False, pad_to_aspect_ratio=False, fill_mode='constant', fill_value=0.0, data_format=None, **kwargs)| Name | Type | Description |
|---|---|---|
height | int | Integer output height. |
width | int | Integer output width. |
interpolation | str | Interpolation method; defaults to bilinear. |
crop_to_aspect_ratio | bool | Whether to crop while preserving aspect ratio. |
pad_to_aspect_ratio | bool | Whether to pad while preserving aspect ratio. |
fill_mode | str | Padding fill mode. |
fill_value | float | Padding value. |
data_format | str | Input dimension ordering: channels_last or channels_first. |
**kwargs | dict | Additional layer keyword arguments. |
A 3D unbatched or 4D batched tensor resized to the target dimensions, with channel placement determined by data_format.
import tensorflow as tf
images = tf.random.uniform((2, 32, 48, 3))
resize = tf.keras.layers.Resizing(64, 64)
output = resize(images)
print(output.shape) # (2, 64, 64, 3)Creates a resizing layer and applies it to a batch of RGB images.
Model input preprocessing
Aspect-ratio-sensitive vision tasks
Segmentation and other label-image pipelines
The layer accepts a 3D unbatched image or a 4D batch and produces the requested spatial dimensions. It supports channels_last by default and can also use channels_first.
Follow-up: How would you avoid geometric distortion? Enable crop_to_aspect_ratio or pad_to_aspect_ratio, choosing the option that matches the preprocessing requirement.
Bilinear interpolation is the default. The documented alternatives are nearest, bicubic, lanczos3, and lanczos5.
Follow-up: Which method is often preferable for discrete masks? Nearest-neighbor interpolation avoids blending class IDs.
crop_to_aspect_ratio preserves proportions by cropping the largest matching window before producing the target size. pad_to_aspect_ratio preserves proportions by adding padding instead.
Follow-up: What constraint applies when selecting these options? They cannot both be enabled simultaneously.
tf.keras.layers.Rescalingtf.image.resize_with_padtf.keras.layers.RandomZoom