Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.Resizing

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
heightintInteger output height.
widthintInteger output width.
interpolationstrInterpolation method: bilinear, nearest, bicubic, lanczos3, or lanczos5; defaults to bilinear.
crop_to_aspect_ratioboolIf true, crop images to the target aspect ratio before resizing.
pad_to_aspect_ratioboolIf true, pad images to the target aspect ratio without distortion.
fill_modestrPadding mode; defaults to constant.
fill_valuefloatValue used for constant padding; defaults to 0.0.
antialiasboolWhether to apply antialiasing; defaults to false.
data_formatstrTensor layout, either channels_last or channels_first.
**kwargsobjectAdditional layer keyword arguments.

Returns

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.

Key points

  • Resizing is a preprocessing layer for converting images to a fixed spatial size.
  • The constructor's height and width establish the output's target spatial dimensions.
  • Interpolation can be bilinear, nearest, bicubic, lanczos3, or lanczos5.
  • Inputs may be individual images or batches of images.
  • Resizing does not require a particular numeric pixel range.
  • The data_format argument determines whether channels appear after or before the spatial axes.
  • Padding can preserve aspect ratio while fitting images into the requested output size.

Common mistakes

  • Setting crop_to_aspect_ratio=True changes the operation from simple stretching to cropping, so image content near the borders can be removed.
  • Use pad_to_aspect_ratio when preserving the entire image matters; padding changes the surrounding pixels rather than cropping content.

Example: Resize a batch of images

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.

When to use it

Standardize image dimensions

Model input preparation

Crop images to a fixed ratio

Aspect-ratio-sensitive classification

Pad without distorting content

Object detection or visualization

Resize batched or individual tensors

Input pipeline construction

Interview questions

QWhat input does Resizing accept, and what does it produce?

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.

QWhich constructor arguments control spatial resizing?

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.

QHow does Resizing handle tensor layout and pixel values?

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.Rescaling
  • keras.layers.RandomCrop
  • keras.layers.RandomFlip
  • keras.Sequential

Source: Resizing layer - Keras. Checked 2026-10-15.