# mixup

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

## 类

### MixUp

基于批次的增强，将两张图像混合在一起。

混合是通过基于从指定分布中采样的混合系数 α 对两张图像进行凸组合来执行的。结果图像计算如下：

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

如果图像大小不同，则第二张图像将调整大小以匹配第一张图像。

> **参阅**
> [mixup: Beyond Empirical Risk Minimization](https://arxiv.org/abs/1710.09412)。

#### 方法

##### init

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

创建 MixUp 增强。

参数

 * `alpha` (`float | tuple[float, float]`): 混合系数或要从中均匀采样的范围。必须保持在 [0, 1] 之间。
 * `keep_aspect_ratio` (`bool`): 调整大小时是否保留第二张图像的纵横比。
 * `p` (`float`): 应用变换的概率。

引发

 * `ValueError`: 如果 `alpha` 超出 [0, 1] 范围或 `alpha` 范围不是升序。

##### apply

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

将 MixUp 应用于一批图像。

参数

 * `image_batch` (`list[np.ndarray]`): 要变换的图像。每张图像的形状应为 (H, W, C) 或 (H, W)。
 * `image_shapes` (`list[tuple[int, int]]`): 原始图像的形状。
 * `alpha` (`float`): 混合系数。
 * `**_`

返回

 * `np.ndarray`: 形状为 (Hout, Wout, C) 或 (Hout, Wout) 的单张图像，由输入图像混合而成。

##### apply_to_bboxes

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

将 MixUp 应用于一批边界框。

参数

 * `bboxes_batch` (`list[np.ndarray]`): 要变换的边界框。
 * `image_shapes` (`list[tuple[int, int]]`): 原始图像形状。
 * `**_`

返回

 * `np.ndarray`: 变换后的边界框。

##### apply_to_instance_mask

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

将 MixUp 应用于一批实例分割掩码。

参数

 * `masks_batch` (`list[np.ndarray]`): 要变换的掩码。每个掩码的形状应为 (H, W, N)，其中 N 是实例数。
 * `image_shapes` (`list[tuple[int, int]]`): 原始图像的形状。
 * `**_`

返回

 * `np.ndarray`: 形状为 (Hout, Wout, N) 的单个实例掩码。

##### apply_to_keypoints

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

将 MixUp 应用于一批关键点。

参数

 * `keypoints_batch` (`list[np.ndarray]`): 要变换的关键点。
 * `image_shapes` (`list[tuple[int, int]]`): 原始图像形状。
 * `**_`

返回

 * `np.ndarray`: 变换后的关键点。

##### apply_to_mask

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

将 MixUp 应用于一批语义分割掩码。

将掩码混合在一起。如果发生冲突，则选择与较高 α 关联的掩码中的类。

参数

 * `masks_batch` (`list[np.ndarray]`): 要变换的掩码。每个掩码的形状应为 (H, W, C) 或 (H, W)。
 * `image_shapes` (`list[tuple[int, int]]`): 原始图像的形状。
 * `alpha` (`float`): 混合系数。
 * `**_`

返回

 * `np.ndarray`: 形状为 (Hout, Wout, C) 或 (Hout, Wout) 的单个分割掩码。

##### get_params

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

从指定分布中采样混合系数。

返回

 * `dict[str, Any]`: 包含带有采样混合系数的 `"alpha"` 键的字典。
