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

TensorFlow · Class

layers.RandomFlip

A Keras preprocessing layer that randomly flips channels-last images horizontally, vertically, or both during training. It leaves images unchanged during inference and supports integer or floating-point pixel inputs.

How it works

RandomFlip conditionally augments image tensors by reversing pixels horizontally, vertically, or in both directions according to mode. Random behavior is enabled when called with training=True; during inference, the layer returns the input unchanged. It accepts unbatched or batched channels-last images and can process integer or floating-point pixel values.

Syntax

tf.keras.layers.RandomFlip(mode=HORIZONTAL_AND_VERTICAL, seed=None, **kwargs)

Parameters

NameTypeDescription
modestringString selecting the flip direction: horizontal, vertical, or horizontal_and_vertical; defaults to horizontal_and_vertical.
seedintegerInteger used to create a random seed.
**kwargskeyword argumentsAdditional base-layer keyword arguments, including name and dtype.

Returns

A tensor with the same image structure after the configured random flip; during inference it is identical to the input.

Key points

  • Choose the augmentation direction with mode: horizontal, vertical, or horizontal_and_vertical.
  • The layer does not require a particular pixel-value scale; common normalized and byte-style ranges are supported.
  • Both integer and floating-point image tensors are accepted as inputs.
  • Inputs may contain one image or a batch, using three or four dimensions respectively.
  • The documented image layout places height and width before the channel dimension.
  • RandomFlip can be inserted directly into a tf.data input pipeline.

Common mistakes

  • Do not expect augmentation during inference; call the layer with training=True when testing its random behavior.
  • Account for the default floating-point output when downstream code expects an integer image dtype.

Example: Randomly flip training images

import tensorflow as tf

layer = tf.keras.layers.RandomFlip(mode="horizontal", seed=7)
images = tf.random.uniform((2, 32, 32, 3))
flipped = layer(images, training=True)
print(flipped.shape)

Creates a layer that randomly applies left-right flips to a batch of images during training.

When to use it

Horizontal augmentation

Training image classifiers with orientation-invariant objects.

tf.data preprocessing

Building an input pipeline that performs augmentation before the model.

Synthetic image diversity

Increasing training-set variation to help reduce overfitting.

Interview questions

QWhat does RandomFlip do, and when is it active?

RandomFlip is a Keras preprocessing layer for image augmentation. It randomly flips images according to mode, and it leaves outputs identical to inputs during inference.

Follow-up: How would you activate augmentation in a custom training loop? Call the layer with training=True when transforming the input batch.

QWhat values can mode take?

The mode selects the transform: horizontal means left-to-right, vertical means top-to-bottom, and horizontal_and_vertical enables both directions. The default is horizontal_and_vertical.

Follow-up: Which mode should be used for an image task where vertical orientation carries meaning? Use horizontal, so top-bottom structure is preserved.

QWhat input tensors and pixel values are supported?

The layer accepts 3D unbatched or 4D batched channels-last image tensors shaped like (..., height, width, channels). Pixel values may be integer or floating point and may use ranges such as 0 to 1 or 0 to 255.

Follow-up: What input layout is documented? Use channels_last; a channels-first tensor does not match the documented input shape.

  • tf.keras.layers.Layer
  • tf.keras.layers.RandomCrop
  • tf.keras.layers.RandomRotation
  • tf.keras.layers.RandomTranslation
  • tf.keras.layers.RandomZoom
  • tf.data

Source: tf.keras.layers.RandomFlip | TensorFlow v2.16.1. Checked 2026-10-15.