# mixup

Python API: `luxonis_ml.data.augmentations.custom.mixup`

## Classes

### MixUp

Batch-based augmentation that blends two images together.

Blending is performed by a convex combination of the two images based on a mixing coefficient α sampled from a specified
distribution. The resulting image is computed as:

> x̃ = α**xi + (1 − α)xj

If the images have different sizes, the second image is resized to match the first one.

> **See Also**
> [mixup: Beyond Empirical Risk Minimization](https://arxiv.org/abs/1710.09412).

#### Methods

##### init

```python
def __init__(alpha: float | tuple[float, float] = 0.5, keep_aspect_ratio: bool = True, p: float = 0.5):
```

Create a MixUp augmentation.

Parameters

 * `alpha` (`float | tuple[float, float]`): Mixing coefficient or range to uniformly sample from. Must stay in [0, 1].
 * `keep_aspect_ratio` (`bool`): Whether to preserve the second image's aspect ratio when resizing.
 * `p` (`float`): Probability of applying the transform.

Raises

 * `ValueError`: If `alpha` falls outside [0, 1] or an `alpha` range is not in ascending order.

##### apply

```python
def apply(image_batch: list[np.ndarray], image_shapes: list[tuple[int, int]], alpha: float, **_) -> np.ndarray:
```

Apply MixUp to a batch of images.

Parameters

 * `image_batch` (`list[np.ndarray]`): Images to transform. Each image should be of shape (H, W, C) or (H, W).
 * `image_shapes` (`list[tuple[int, int]]`): Shapes of the original images.
 * `alpha` (`float`): Mixing coefficient.
 * `**_`

Returns

 * `np.ndarray`: A single image of shape (Hout, Wout, C) or (Hout, Wout) resulting from blending the input images.

##### apply_to_bboxes

```python
def apply_to_bboxes(bboxes_batch: list[np.ndarray], image_shapes: list[tuple[int, int]], **_) -> np.ndarray:
```

Apply MixUp to a batch of bounding boxes.

Parameters

 * `bboxes_batch` (`list[np.ndarray]`): Bounding boxes to transform.
 * `image_shapes` (`list[tuple[int, int]]`): Original image shapes.
 * `**_`

Returns

 * `np.ndarray`: Transformed bounding boxes.

##### apply_to_instance_mask

```python
def apply_to_instance_mask(masks_batch: list[np.ndarray], image_shapes: list[tuple[int, int]], **_) -> np.ndarray:
```

Apply MixUp to a batch of instance segmentation masks.

Parameters

 * `masks_batch` (`list[np.ndarray]`): Masks to transform. Each mask should be of shape (H, W, N), where N is the number of
   instances.
 * `image_shapes` (`list[tuple[int, int]]`): Shapes of the original images.
 * `**_`

Returns

 * `np.ndarray`: A single instance masks of shape (Hout, Wout, N).

##### apply_to_keypoints

```python
def apply_to_keypoints(keypoints_batch: list[np.ndarray], image_shapes: list[tuple[int, int]], **_) -> np.ndarray:
```

Apply MixUp to a batch of keypoints.

Parameters

 * `keypoints_batch` (`list[np.ndarray]`): Keypoints to transform.
 * `image_shapes` (`list[tuple[int, int]]`): Original image shapes.
 * `**_`

Returns

 * `np.ndarray`: Transformed keypoints.

##### apply_to_mask

```python
def apply_to_mask(masks_batch: list[np.ndarray], image_shapes: list[tuple[int, int]], alpha: float, **_) -> np.ndarray:
```

Apply MixUp to a batch of semantic segmentation masks.

Blends masks together. In case of a conflict, the class from the mask associated with the higher α is chosen.

Parameters

 * `masks_batch` (`list[np.ndarray]`): Masks to transform. Each mask should be of shape (H, W, C) or (H, W).
 * `image_shapes` (`list[tuple[int, int]]`): Shapes of the original images.
 * `alpha` (`float`): Mixing coefficient.
 * `**_`

Returns

 * `np.ndarray`: A single segmentation mask of shape (Hout, Wout, C) or (Hout, Wout).

##### get_params

```python
def get_params(self) -> dict[str, Any]:
```

Sample a mixing coefficient from the specified distribution.

Returns

 * `dict[str, Any]`: Dictionary containing `"alpha"` key with the sampled mixing coefficient.
