transforms.RandomHorizontalFlip

class lucid.transforms.RandomHorizontalFlip(p: float = 0.5)

The RandomHorizontalFlip class is a transformation that randomly flips an input tensor horizontally with a given probability. This transformation is commonly used in data augmentation to increase the diversity of training samples for image-based neural networks.

Class Signature

class lucid.transforms.RandomHorizontalFlip(p: float = 0.5)

Parameters

  • p (float, optional): The probability of flipping the input tensor horizontally. Must be a value between 0 and 1. Default is 0.5.

Attributes

  • p (float): The probability with which the input tensor will be flipped horizontally, stored as an internal attribute.

Forward Calculation

The horizontal flip of an input tensor \(\mathbf{x}\) is applied with a probability \(p\) as follows:

  1. Generate a random number \(r\) from a uniform distribution over [0, 1].

  2. If \(r < p\), flip the tensor along its horizontal axis.

This can be mathematically represented as:

\[\mathbf{x}_{\text{flipped}} = \mathbf{x}[..., :, ::-1]\]

where the last axis of the tensor is reversed to achieve the horizontal flip.

Examples

Example 1: Basic Usage

>>> import lucid.transforms as T
>>> input_tensor = lucid.Tensor([[[1.0, 2.0], [3.0, 4.0]]])  # Shape: (1, 2, 2)
>>> flip = T.RandomHorizontalFlip(p=1.0)  # Always flip
>>> output = flip(input_tensor)
>>> print(output)
Tensor([[[2.0, 1.0],
         [4.0, 3.0]]], grad=None)

Example 2: Randomized Flip

>>> import lucid.transforms as T
>>> input_tensor = lucid.Tensor([[[1.0, 2.0], [3.0, 4.0]]])  # Shape: (1, 2, 2)
>>> flip = T.RandomHorizontalFlip(p=0.5)  # 50% chance to flip
>>> output = flip(input_tensor)  # Random outcome