Scale augmentation
Training image classifiers with varied framing
Keras · Class
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.
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.
keras.layers.RandomZoom(height_factor, width_factor=None, fill_mode="reflect", interpolation="bilinear", seed=None, fill_value=0.0, data_format=None, **kwargs)| Name | Type | Description |
|---|---|---|
height_factor | float or tuple | A float or two-value range controlling vertical zoom; positive values zoom out and negative values zoom in. |
width_factor | float or tuple or None | A float or two-value range controlling horizontal zoom. |
fill_mode | string | How pixels outside the input boundaries are filled: constant, nearest, wrap, or reflect. |
interpolation | string | Interpolation mode; supported values are nearest and bilinear. |
seed | int or None | Integer used to create the random seed. |
fill_value | float | The value used outside boundaries when fill_mode is constant. |
data_format | string or None | Image layout, either channels_last or channels_first. |
**kwargs | keyword arguments | Additional Layer keyword arguments such as name. |
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.
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.
Training image classifiers with varied framing
Preventing reliance on fixed object size
Building preprocessing pipelines
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.
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.
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.
RandomRotationRandomTranslationRandomFlipRandomCropPipeline