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

Keras · Class

layers.RandomFlip

RandomFlip randomly mirrors images horizontally, vertically, or in both directions during training. It preserves image dimensions and returns the original input during inference.

How it works

RandomFlip is an image-preprocessing layer that applies randomized left-right, top-bottom, or combined flips when training is enabled. It accepts either one image or a batch, preserves the image dimensions, and leaves inputs unchanged during inference.

Syntax

keras.layers.RandomFlip(mode="horizontal_and_vertical", seed=None, data_format=None, **kwargs)

Parameters

NameTypeDescription
modestringFlip direction: horizontal, vertical, or horizontal_and_vertical. Defaults to horizontal_and_vertical.
seedintegerInteger used to create a random seed.
**kwargskeyword argumentsBase-layer keyword arguments, such as name and dtype.

Returns

A 3D unbatched or 4D batched tensor with the same (..., height, width, channels) shape convention as the input.

Key points

  • Use RandomFlip as an in-model augmentation layer rather than implementing flip logic in the input loop.
  • The selected mode determines whether the transformation mirrors width, height, or both axes.
  • The mode argument accepts exactly three documented string choices.
  • Provide an integer seed when you want to control the layer's random-number initialization.
  • The layer handles either a single image tensor or a batch of images.

Common mistakes

  • Augmentation is disabled during inference; pass training=True when explicitly calling the layer if you need a flip.
  • Expect floating-point output unless you configure an appropriate dtype.
  • The layer does not require normalized pixels, so scaling to [0, 1] is optional.

Example: Apply random horizontal flips

import keras

augment = keras.layers.RandomFlip(
    mode="horizontal",
    seed=42,
)

images = keras.random.uniform((8, 128, 128, 3))
flipped = augment(images, training=True)
unchanged = augment(images, training=False)
print(flipped.shape)
print(unchanged.shape)

Creates a horizontal-flip augmenter, applies it during training, and shows that inference preserves the input shape and values.

When to use it

Add stochastic mirror augmentation

Training image classifiers

Augment images within a data pipeline

tf.data or grain input pipelines

Increase variation without changing annotations

Tasks with mirror-invariant labels

Interview questions

QWhat does RandomFlip do during training and inference?

It is a preprocessing layer for randomly flipping images during training. The mode selects horizontal, vertical, or both directions, while inference returns the input unchanged.

Follow-up: What happens if the layer is called directly outside a training context? Pass training=True to request augmentation; otherwise it follows inference behavior.

QWhich flip modes are supported?

The mode can be horizontal, vertical, or horizontal_and_vertical. Horizontal means left-to-right mirroring, and vertical means top-to-bottom mirroring.

Follow-up: How would you restrict augmentation to one direction? Construct the layer with mode="horizontal" or mode="vertical".

QWhat input and output shapes does the layer use?

It accepts a 3D unbatched or 4D batched image tensor in channels_last layout, with shape (..., height, width, channels). The output has the same rank and shape convention.

Follow-up: Can integer images be supplied? Yes; pixel values may be integer or floating point and may use ranges such as [0, 255] or [0, 1).

  • keras.layers.RandomRotation
  • keras.layers.RandomCrop
  • keras.layers.RandomTranslation
  • keras.layers.Rescaling

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