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

Keras · Class

layers.RandomZoom

RandomZoom randomly zooms images during training, independently sampling the vertical and horizontal axes. It supports zoom-in or zoom-out factors and configurable filling for regions outside the source image.

How it works

RandomZoom is an image-augmentation layer that samples vertical and horizontal zoom amounts independently when training. Positive factors zoom out and negative factors zoom in; areas exposed by the transformation are filled using the selected fill mode.

Syntax

keras.layers.RandomZoom(height_factor, width_factor=None, fill_mode="reflect", interpolation="bilinear", seed=None, fill_value=0.0, data_format=None, **kwargs)

Parameters

NameTypeDescription
height_factorfloat or tupleA float or two-value range controlling vertical zoom; positive values zoom out and negative values zoom in.
width_factorfloat or tuple or NoneA float or two-value range controlling horizontal zoom.
fill_modestringHow pixels outside the input boundaries are filled: constant, nearest, wrap, or reflect.
interpolationstringInterpolation mode; supported values are nearest and bilinear.
seedint or NoneInteger used to create the random seed.
fill_valuefloatThe value used outside boundaries when fill_mode is constant.
data_formatstring or NoneImage layout, either channels_last or channels_first.
**kwargskeyword argumentsAdditional Layer keyword arguments such as name.

Returns

A tensor with the same spatial layout and target height and width as the input; the output shape is (..., target_height, target_width, channels) for channels_last or (..., channels, target_height, target_width) for channels_first.

Key points

  • Use it as an augmentation layer to vary image scale during model training.
  • Height and width transformations are sampled independently rather than sharing one zoom value.
  • The constructor requires height_factor; width_factor defaults to None and padding defaults to reflection.
  • A two-element height_factor defines the lower and upper vertical zoom bounds.
  • The layer does not require normalized pixels; common ranges such as 0–1 and 0–255 are supported.
  • Inputs may be one image or a batch of images.

Common mistakes

  • Integer inputs are converted to floating-point outputs by default, so account for the output dtype in downstream code.
  • The sign may feel counterintuitive: use positive factors to zoom out and negative factors to zoom in.
  • fill_mode must use one of the documented modes; constant padding uses fill_value.

Example: Randomly zoom a batch of images

import keras
import numpy as np

images = np.random.random((2, 32, 32, 3)).astype("float32")
zoom = keras.layers.RandomZoom(height_factor=0.2, width_factor=0.2, seed=7)
augmented = zoom(images, training=True)
print(augmented.shape)

Creates a reproducible augmentation layer and applies independent random zooming to a batch of RGB images during training.

When to use it

Scale augmentation

Training image classifiers with varied framing

Robustness to zoom

Preventing reliance on fixed object size

Dataset augmentation

Building preprocessing pipelines

Interview questions

QWhat does RandomZoom do?

It is a preprocessing layer that randomly zooms images during training. Vertical and horizontal zooming are sampled independently, and exposed regions are filled according to fill_mode.

Follow-up: How would you preserve reproducibility? Pass an integer seed when constructing the layer, while remembering that reproducibility also depends on the surrounding random pipeline.

QHow are zoom ranges configured?

height_factor controls vertical zooming and width_factor controls horizontal zooming. Each can be a scalar or a two-value range; a scalar is used as both bounds for that axis.

Follow-up: What happens when width_factor is omitted? The layer uses the height factor for both dimensions, so specify it explicitly when anisotropic augmentation is required.

QWhat input and transformation options does it support?

The layer accepts either unbatched 3D or batched 4D image tensors. It supports channels-last and channels-first layouts through data_format, and supports nearest or bilinear interpolation.

Follow-up: How do you handle padding outside the image? Select fill_mode and, for constant mode, configure fill_value.

  • RandomRotation
  • RandomTranslation
  • RandomFlip
  • RandomCrop
  • Pipeline

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