albumentations.batch.flip
Horizontal and vertical flips of individual samples in a collated raster batch.
Members
- classFlipPlan
- functionsample_flip
- functionapply_flip_
FlipPlanclass
FlipPlan()Share one flip direction and one editable boolean decision per sample across images and masks.
sample_flipfunction
sample_flip(
batch_size: int,
direction: FlipDirection,
device: torch.device | str,
generator: torch.Generator,
p: float = 0.5
)Draw independent flip decisions for a collated batch so that each image and its paired targets can share a flip. Args: batch_size (int): Number of samples in the batch. direction (FlipDirection): Horizontal flips reverse width and vertical flips reverse height. device (torch.device | str): CPU or CUDA device of the batch and generator. generator (torch.Generator): Draws one uniform value per sample, including at zero and unit probability. p (float): Probability of flipping each sample, in [0, 1]. Returns: FlipPlan: One boolean flag per sample on the batch device.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| batch_size | int | - | - |
| direction | FlipDirection | - | - |
| device | One of:
| - | - |
| generator | torch.Generator | - | - |
| p | float | 0.5 | - |
apply_flip_function
apply_flip_(
values: torch.Tensor,
plan: FlipPlan
)Flip selected images or volumes in place after collation and reuse their decisions for masks at any resolution. In eager mode the CUDA kernel swaps selected pixel pairs in place. The PyTorch fallback uses at most one sample of temporary pixels and keeps CUDA flags on the device. If an in-place operation fails after writing, retry with a fresh batch. Args: values (torch.Tensor): Writable CPU or CUDA raster with non-overlapping elements and shape `[B, ..., H, W]`, used outside autograd before the model reads the batch. Supports bool, uint8, signed integers and float16, bfloat16, float32, float64. plan (FlipPlan): One flag per sample on the same device. Construction and assignment use ordinary tensor references. If a raster is a view of the flags' storage, the plan takes a copy of the flags before flipping so that paired targets see the same decisions. Returns: torch.Tensor: The original tensor. Channels and depth slices move together. Examples: >>> import torch >>> from albumentations.batch import apply_flip_, sample_flip >>> images = torch.arange(120).reshape(4, 2, 3, 5) >>> masks = images[:, 0].clone() >>> plan = sample_flip(batch_size=4, direction="horizontal", device="cpu", generator=torch.Generator()) >>> _ = apply_flip_(images, plan) >>> _ = apply_flip_(masks, plan) >>> torch.equal(images[:, 0], masks) True
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| values | torch.Tensor | - | - |
| plan | FlipPlan | - | - |