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

Keras · Class

layers.RandomRotation

RandomRotation is a Keras preprocessing layer that randomly rotates images during training. It supports configurable angle limits, boundary filling, interpolation, seeding, and channel layout.

How it works

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.

Syntax

keras.layers.RandomRotation(factor, fill_mode="reflect", interpolation="bilinear", seed=None, fill_value=0.0, data_format=None, **kwargs)

Parameters

NameTypeDescription
factorfloat or tupleRotation amount as a fraction of 2π, or a two-element lower/upper bound.
fill_modestringHow pixels beyond image boundaries are filled: constant, reflect, wrap, or nearest.
interpolationstringResampling method: nearest or bilinear.
seedinteger or NoneInteger used to create the random seed.
fill_valuefloatValue used outside boundaries when fill_mode is constant.
data_formatstring or NoneImage layout, either channels_last or channels_first.
**kwargskeyword argumentsAdditional layer keyword arguments.

Returns

A tensor with the same image structure, containing randomly rotated images during training.

Key points

  • Use it as an image-augmentation preprocessing layer to introduce random geometric variation during training.
  • factor accepts either a scalar fraction of a full turn or lower and upper bounds.
  • A scalar factor creates symmetric magnitude limits around zero.
  • fill_mode determines how areas exposed by rotation are populated.
  • interpolation supports nearest-neighbor and bilinear resampling.
  • Set seed to control the layer's random-number initialization.
  • Inputs may be individual images or batches of images.

Common mistakes

  • Calling the layer during inference normally leaves images unchanged; pass training=True when rotation is needed there.
  • The sign convention may differ from an intuitive clockwise-positive assumption.
  • Plan for a floating-point output even when the input image tensor uses an integer dtype.

Example: Apply random image rotation

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.

When to use it

Geometric augmentation

Training image classifiers with limited visual variety.

Model-integrated preprocessing

Building reusable input preprocessing inside a model.

Explicit augmentation at inference

Testing or visualizing augmentation behavior on demand.

Interview questions

QWhat does factor represent?

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).

QWhen does RandomRotation apply its transformation?

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.

QWhat input shapes does the layer support?

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.

  • RandomFlip
  • RandomTranslation
  • RandomZoom
  • Pipeline

Source: RandomRotation layer. Checked 2026-10-15.