albumentations.batch.mixup
Sample and apply MixUp plans for tensors with a leading batch axis.
Members
- classMixUpPlan
- functionsample_mixup
- functionapply_mixup
MixUpPlanclass
MixUpPlan()Donor indices and original-sample weights for a batch.
sample_mixupfunction
sample_mixup(
batch_size: int,
alpha: float,
device: torch.device,
generator: torch.Generator,
mode: MixingMode = pair
)Draw a donor index and a Beta(alpha, alpha) weight for each sample. Args: batch_size (int): Number of samples in the batch. alpha (float): Finite positive concentration for the symmetric Beta distribution. device (torch.device): Device of the input batch and generator. generator (torch.Generator): Random generator used for both pairing and weights. mode (MixingMode): "pair" swaps randomly paired samples with independent weights, "element" permutes samples with independent weights, and "batch" swaps randomly paired samples with one shared weight. An unpaired sample keeps itself with weight 1. Returns: MixUpPlan: Donor indices and weights on the specified device. Raises: ValueError: If the batch size is nonpositive, alpha is invalid, or the mode is unsupported.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| batch_size | int | - | - |
| alpha | float | - | - |
| device | torch.device | - | - |
| generator | torch.Generator | - | - |
| mode | MixingMode | pair | - |
apply_mixupfunction
apply_mixup(
values: torch.Tensor,
plan: MixUpPlan,
flip_donor: bool = False
)Interpolate each sample with its donor along the batch axis. MixUp runs outside autograd. Args: values (torch.Tensor): Tensor with the batch axis first, such as BCHW or BCDHW. plan (MixUpPlan): Donor indices and weights for every sample on the input device. flip_donor (bool): Flip the donor along the last (width) axis before blending. Use for spatial tensors such as BHW, BCHW or BCDHW. For BHWC, first move channels with `values.movedim(-1, 1)`. Defaults to False. Returns: torch.Tensor: A tensor with the input shape and dtype. Interpolation uses FP32. Uint8 results are rounded to the nearest integer (ties to even) and clipped to [0, 255]. Raises: ValueError: If the input has no batch axis, the plan lengths differ from the batch size, or a donor flip is requested for a tensor with fewer than three dimensions. Examples: >>> import torch >>> from albumentations.batch import MixUpPlan, apply_mixup >>> images = torch.tensor([[[0., 4., 8.]], [[12., 20., 28.]]]) >>> plan = MixUpPlan(torch.tensor([1, 0]), torch.tensor([0.75, 0.5])) >>> mixed = apply_mixup(images, plan, flip_donor=True) >>> assert mixed.tolist() == [[[7, 8, 9]], [[10, 12, 14]]] >>> targets = apply_mixup(torch.eye(2), plan) >>> assert targets.tolist() == [[0.75, 0.25], [0.5, 0.5]]
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| values | torch.Tensor | - | - |
| plan | MixUpPlan | - | - |
| flip_donor | bool | False | - |