Horizontal augmentation
Training image classifiers with orientation-invariant objects.
TensorFlow · Class
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.
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.
tf.keras.layers.RandomFlip(mode=HORIZONTAL_AND_VERTICAL, seed=None, **kwargs)| Name | Type | Description |
|---|---|---|
mode | string | String selecting the flip direction: horizontal, vertical, or horizontal_and_vertical; defaults to horizontal_and_vertical. |
seed | integer | Integer used to create a random seed. |
**kwargs | keyword arguments | Additional base-layer keyword arguments, including name and dtype. |
A tensor with the same image structure after the configured random flip; during inference it is identical to the input.
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.
Training image classifiers with orientation-invariant objects.
Building an input pipeline that performs augmentation before the model.
Increasing training-set variation to help reduce overfitting.
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.
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.
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.Layertf.keras.layers.RandomCroptf.keras.layers.RandomRotationtf.keras.layers.RandomTranslationtf.keras.layers.RandomZoomtf.data