Geometric augmentation
Training image classifiers with limited visual variety.
Keras · Class
RandomRotation is a Keras preprocessing layer that randomly rotates images during training. It supports configurable angle limits, boundary filling, interpolation, seeding, and channel layout.
RandomRotation augments each image by sampling an angle from the configured factor range and rotates it during training. Pixels falling outside the original image are filled using the selected mode; inference bypasses the transformation unless training is explicitly enabled.
keras.layers.RandomRotation(factor, fill_mode="reflect", interpolation="bilinear", seed=None, fill_value=0.0, data_format=None, **kwargs)| Name | Type | Description |
|---|---|---|
factor | float or tuple | Rotation amount as a fraction of 2π, or a two-element lower/upper bound. |
fill_mode | string | How pixels beyond image boundaries are filled: constant, reflect, wrap, or nearest. |
interpolation | string | Resampling method: nearest or bilinear. |
seed | integer or None | Integer used to create the random seed. |
fill_value | float | 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. |
A tensor with the same image structure, containing randomly rotated images during training.
import numpy as np
import keras
x = np.zeros((1, 32, 32, 3), dtype="float32")
layer = keras.layers.RandomRotation(0.2, seed=42)
y = layer(x, training=True)
print(y.shape)Creates a reproducible rotation augmenter, applies it explicitly in training mode, and preserves the batched image shape.
Training image classifiers with limited visual variety.
Building reusable input preprocessing inside a model.
Testing or visualizing augmentation behavior on demand.
factor controls the sampled rotation range. It can be one float or a two-value range, and the value is expressed as a fraction of 2π.
Follow-up: How would you permit only counter-clockwise rotations? Provide a range whose bounds are both nonnegative, such as (0.0, 0.1).
The layer acts by default only during training, so it performs augmentation while fitting but does nothing at inference. To force the transformation, call it with training=True.
Follow-up: Why is this useful in a model? It keeps evaluation deterministic unless augmentation is deliberately requested.
It accepts either an unbatched 3D or batched 4D image tensor. The documented channels-last shape is (..., height, width, channels).
Follow-up: What should be checked when using channels-first data? Configure data_format accordingly rather than passing the tensor unchanged.
RandomFlipRandomTranslationRandomZoomPipeline