Standardize image dimensions
Model input preparation
Keras · Class
Keras layers.Resizing converts 3D or 4D image tensors to a chosen height and width. It supports multiple interpolation methods, optional aspect-ratio-preserving cropping or padding, and both channel layouts.
Resizing converts each 3D unbatched or 4D batched image tensor to the requested height and width. It can use several interpolation methods, optionally crop or pad to preserve aspect ratio, and supports channels-last or channels-first layouts.
keras.layers.Resizing(height, width, interpolation="bilinear", crop_to_aspect_ratio=False, pad_to_aspect_ratio=False, fill_mode="constant", fill_value=0.0, antialias=False, data_format=None, **kwargs)| Name | Type | Description |
|---|---|---|
height | int | Integer output height. |
width | int | Integer output width. |
interpolation | str | Interpolation method: bilinear, nearest, bicubic, lanczos3, or lanczos5; defaults to bilinear. |
crop_to_aspect_ratio | bool | If true, crop images to the target aspect ratio before resizing. |
pad_to_aspect_ratio | bool | If true, pad images to the target aspect ratio without distortion. |
fill_mode | str | Padding mode; defaults to constant. |
fill_value | float | Value used for constant padding; defaults to 0.0. |
antialias | bool | Whether to apply antialiasing; defaults to false. |
data_format | str | Tensor layout, either channels_last or channels_first. |
**kwargs | object | Additional layer keyword arguments. |
A tensor with the same unbatched or batched rank and target spatial dimensions: (..., target_height, target_width, channels) for channels-last or (..., channels, target_height, target_width) for channels-first.
import keras
import numpy as np
resize = keras.layers.Resizing(128, 128, crop_to_aspect_ratio=True)
images = np.zeros((2, 256, 192, 3), dtype="float32")
output = resize(images)
print(output.shape) # (2, 128, 128, 3)Creates a resizing layer that crops each image to the target aspect ratio before producing a uniform 128-by-128 batch.
Model input preparation
Aspect-ratio-sensitive classification
Object detection or visualization
Input pipeline construction
It accepts a 3D unbatched or 4D batched image tensor and returns the same rank with the spatial dimensions changed to the configured target size. The default interpolation method is bilinear.
Follow-up: How can aspect ratio be preserved? Enable crop_to_aspect_ratio to crop to the target ratio, or pad_to_aspect_ratio to add padding without distortion.
height and width define the output dimensions. interpolation selects among bilinear, nearest, bicubic, lanczos3, and lanczos5, with bilinear as the default.
Follow-up: What layout options are supported? Set data_format to channels_last or channels_first so the channel axis is interpreted correctly.
The layer supports both channels-last and channels-first tensors. Channels-last uses (..., height, width, channels), while channels-first uses (..., channels, height, width).
Follow-up: What happens to pixel ranges? The layer accepts values in arbitrary ranges, so scaling to [0, 1] is a separate preprocessing concern.
keras.layers.Rescalingkeras.layers.RandomCropkeras.layers.RandomFlipkeras.Sequential