# mosaic

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

## Classes

| Name | Description |
| --- | --- |
|
[`Mosaic4`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mosaic.md)
| Batch-based augmentation that creates a mosaic of four images. |

### class `Mosaic4`

```python
class Mosaic4(BatchTransform)
```

Batch-based augmentation that creates a mosaic of four images.

The transform arranges four images in a deterministic 2×2 grid:

| Column 1 | Column 2 |
| --- | --- |
| 1 | 2 |
| 3 | 4 |

Images may have different sizes, but they must have the same number of channels. The result is cropped around the mosaic center to
`out_width` by `out_height` and padded when needed with the specified fill values.

![https://raw.githubusercontent.com/luxonis/luxonis-ml/58fb10adc38a393640b9dc889fdefd1db81f9900/luxonis_ml/data/augmentations/media/mosaic4.png](https://raw.githubusercontent.com/luxonis/luxonis-ml/58fb10adc38a393640b9dc889fdefd1db81f9900/luxonis_ml/data/augmentations/media/mosaic4.png)

An example of the Mosaic4 augmentation.

> **See Also**
> [Dynamic Scale Training for Object Detection](https://arxiv.org/abs/2004.12432)

Methods

| Name | Description |
| --- | --- |
|
[`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mosaic.md)
| Create a Mosaic4 augmentation. |
|
[`apply`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mosaic.md)
| Apply mosaic augmentation 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/mosaic.md)
| Apply mosaic augmentation 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/mosaic.md)
| Apply mosaic augmentation 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/mosaic.md)
| Apply mosaic augmentation 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/mosaic.md)
| Apply mosaic augmentation to a batch of semantic segmentation masks. |
|
[`generate_random_crop_center`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mosaic.md)
| Generate a random crop center within the bounds of the mosaic image size. |
|
[`get_params_dependent_on_data`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/augmentations/custom/mosaic.md)
| Get parameters dependent on the targets. |

#### method `__init__`

```python
def __init__(
    height: int | None = None,
    width: int | None = None,
    image_fill_value: float | list[int] | list[float] | None = None,
    mask_fill_value: float | list[int] | list[float] | None = None,
    p: float = 0.5,
    out_height: int | None = None,
    out_width: int | None = None,
    value: float | list[int] | list[float] | None = None,
    mask_value: float | list[int] | list[float] | None = None,
)
```

Create a Mosaic4 augmentation.

Args

 * `height` (`int | None`): Output image height.
 * `width` (`int | None`): Output image width.
 * `image_fill_value` (`float | list[int] | list[float] | None`): Padding value for images.
 * `mask_fill_value` (`float | list[int] | list[float] | None`): Padding value for masks.
 * `p` (`float`): Probability of applying the transform.
 * `out_height` (`int | None`):> **Deprecated**
   > Deprecated since version 0.9.0:Use `height` instead.
 * `out_width` (`int | None`):> **Deprecated**
   > Deprecated since version 0.9.0:Use `width` instead.
 * `value` (`float | list[int] | list[float] | None`):> **Deprecated**
   > Deprecated since version 0.9.0:Use `image_fill_value` instead.
 * `mask_value` (`float | list[int] | list[float] | None`):> **Deprecated**
   > Deprecated since version 0.9.0:Use `mask_fill_value` instead.

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the resolved output height or width is missing or not greater than 0. |

#### method `apply`

```python
def apply(image_batch: list[np.ndarray], x_crop: int, y_crop: int, **_) -> np.ndarray
```

Apply mosaic augmentation 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 |
| `x_crop` | `int` | X-coordinate of the crop start point. | required |
| `y_crop` | `int` | Y-coordinate of the crop start point. | required |
| `**_` |  |  |  |

Returns

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

#### method `apply_to_bboxes`

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

Apply mosaic augmentation 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 |
| `x_crop` | `int` | X-coordinate of the crop start point. | required |
| `y_crop` | `int` | Y-coordinate of the crop start point. | 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],
    x_crop: int,
    y_crop: int,
    **_,
) -> np.ndarray
```

Apply mosaic augmentation 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 |
| `x_crop` | `int` | X-coordinate of the crop start point. | required |
| `y_crop` | `int` | Y-coordinate of the crop start point. | 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]],
    x_crop: int,
    y_crop: int,
    **_,
) -> np.ndarray
```

Apply mosaic augmentation 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 |
| `x_crop` | `int` | X-coordinate of the crop start point. | required |
| `y_crop` | `int` | Y-coordinate of the crop start point. | required |
| `**_` |  |  |  |

Returns

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

#### method `apply_to_mask`

```python
def apply_to_mask(
    mask_batch: list[np.ndarray],
    x_crop: int,
    y_crop: int,
    out_height: int,
    out_width: int,
    **_,
) -> np.ndarray
```

Apply mosaic augmentation to a batch of semantic segmentation masks.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `mask_batch` | `list[np.ndarray]` | Masks to transform. Each mask should be of shape (H, W, C) or (H, W). | required |
| `x_crop` | `int` | X-coordinate of the crop start point. | required |
| `y_crop` | `int` | Y-coordinate of the crop start point. | required |
| `out_height` | `int` | The expected height of the output mask. | required |
| `out_width` | `int` | The expected width of the output mask. | required |
| `**_` |  |  |  |

Returns

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

#### method `generate_random_crop_center`

```python
def generate_random_crop_center() -> tuple[int, int]
```

Generate a random crop center within the bounds of the mosaic image size.

#### method `get_params_dependent_on_data`

```python
def get_params_dependent_on_data(
    params: dict[str, Any],
    data: dict[str, Any],
) -> dict[str, Any]
```

Get parameters dependent on the targets.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `params` | `dict[str, Any]` | Existing augmentation parameters. | required |
| `data` | `dict[str, Any]` | Input data. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, Any]` | Parameters derived from the input targets. |
