# mixup

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

## Classes

| Name | Description |
| --- | --- |
|
[`MixUp`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Batch-based augmentation that blends two images together. |

### class `MixUp`

```python
class MixUp(BatchTransform)
```

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

| Name | Description |
| --- | --- |
|
[`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Create a MixUp augmentation. |
|
[`apply`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Apply MixUp to a batch of images. |
|
[`apply_to_bboxes`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Apply MixUp to a batch of bounding boxes. |
|
[`apply_to_instance_mask`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Apply MixUp to a batch of instance segmentation masks. |
|
[`apply_to_keypoints`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Apply MixUp to a batch of keypoints. |
|
[`apply_to_mask`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Apply MixUp to a batch of semantic segmentation masks. |
|
[`get_params`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mixup.md)
| Sample a mixing coefficient from the specified distribution. |

#### method `__init__`

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

Create a MixUp augmentation.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `alpha` | `float | tuple[float, float]` | Mixing coefficient or range to uniformly sample from. Must stay in [0, 1]. | `0.5` |
| `keep_aspect_ratio` | `bool` | Whether to preserve the second image's aspect ratio when resizing. | `True` |
| `p` | `float` | Probability of applying the transform. | `0.5` |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If `alpha` falls outside [0, 1] or an `alpha` range is not in ascending order. |

#### method `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.

Args

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

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | A single image of shape (Hout, Wout, C) or (Hout, Wout) resulting from blending the input images. |

#### method `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.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `bboxes_batch` | `list[np.ndarray]` | Bounding boxes to transform. | required |
| `image_shapes` | `list[tuple[int, int]]` | Original image shapes. | required |
| `**_` |  |  |  |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Transformed bounding boxes. |

#### method `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.

Args

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

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | A single instance masks of shape (Hout, Wout, N). |

#### method `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.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `keypoints_batch` | `list[np.ndarray]` | Keypoints to transform. | required |
| `image_shapes` | `list[tuple[int, int]]` | Original image shapes. | required |
| `**_` |  |  |  |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Transformed keypoints. |

#### method `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.

Args

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

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | A single segmentation mask of shape (Hout, Wout, C) or (Hout, Wout). |

#### method `get_params`

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

Sample a mixing coefficient from the specified distribution.

Returns

| Type | Description |
| --- | --- |
| `dict[str, Any]` | Dictionary containing `"alpha"` key with the sampled mixing coefficient. |
