Add stochastic mirror augmentation
Training image classifiers
Keras · Class
RandomFlip randomly mirrors images horizontally, vertically, or in both directions during training. It preserves image dimensions and returns the original input during inference.
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.
keras.layers.RandomFlip(mode="horizontal_and_vertical", seed=None, data_format=None, **kwargs)| Name | Type | Description |
|---|---|---|
mode | string | 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 | Base-layer keyword arguments, such as name and dtype. |
A 3D unbatched or 4D batched tensor with the same (..., height, width, channels) shape convention as the input.
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.
Training image classifiers
tf.data or grain input pipelines
Tasks with mirror-invariant labels
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.
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".
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.RandomRotationkeras.layers.RandomCropkeras.layers.RandomTranslationkeras.layers.Rescaling