<br />

> [!NOTE]
> **Note:** This API is new and only available via `pip install tf-nightly`.

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/mosaic.py#L16-L416) |

Mosaic implements the Mosaic data augmentation technique.

Inherits From: [`Layer`](https://www.tensorflow.org/api_docs/python/tf/keras/Layer.md.txt), [`Operation`](https://www.tensorflow.org/api_docs/python/tf/keras/Operation.md.txt)

```python
tf.keras.layers.Mosaic(
    offset=(0.25, 0.75),
    seed=None,
    bounding_box_format=None,
    data_format=None,
    **kwargs
)
```

For every image in the batch, Mosaic picks 3 other images at random from
the same batch and arranges the 4 images in a 2x2 grid. A crop the size of
a single image is then taken from the grid around a randomly sampled mosaic
center, so that each output image contains a piece of each of the 4 images.

Labels are mixed in proportion to the area that each of the 4 images covers
in the output. Bounding boxes are moved along with their images and clipped
to the output image; boxes that end up entirely outside of it get a label
of `-1`. Segmentation masks are cropped exactly like the images.

A single (unbatched) image is processed as a batch of one image. All 4
images of its grid are then that same image, so the output is the image
shifted with wrap-around. To combine different images, call the layer on
batches, for example after [`tf.data.Dataset.batch()`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset.md.txt#batch).

> [!NOTE]
> **Note:** This layer is safe to use inside a [`tf.data`](https://www.tensorflow.org/api_docs/python/tf/data.md.txt) or `grain` pipeline (independently of which backend you're using).

<br />

<br />

<br />

| ## Input shape ||
|---|---|
| A 3D (unbatched) or 4D (batched) tensor of images, or a dictionary with an `"images"` key and optional `"labels"`, `"bounding_boxes"` and `"segmentation_masks"` keys. Batched images have shape `(batch, height, width, channels)` in `"channels_last"` format, or `(batch, channels, height, width)` in `"channels_first"` format. Labels have shape `(batch, num_classes)`, or `(num_classes,)` for a single image, and segmentation masks have the same layout as the images. ||

<br />

<br />

<br />

<br />

| ## Output shape ||
|---|---|
| Same as the input, except that each image's bounding boxes become 4 times as many, since an output image can contain boxes from 4 images. ||

<br />

<br />

<br />

| ## References ||
|---|---|
| <br /> - [YOLOv4 paper](https://arxiv.org/abs/2004.10934). - [YOLOv5 implementation](https://github.com/ultralytics/yolov5). - [YOLOX implementation](https://github.com/Megvii-BaseDetection/YOLOX). ||

<br />

<br />

<br />

<br />

| ## Args ||
|---|---|
| `offset` | A float or a tuple of two floats in the range `[0, 1]`. Controls where the mosaic center can be placed, as a fraction of the image width and height measured from the top-left corner. If a tuple `(lower, upper)` is passed, the x and y coordinates of the center are each sampled uniformly between `lower` and `upper`. If a single float is passed, they are sampled between `0.0` and that value. To always use the same center, pass two identical values, for example `(0.5, 0.5)`. Defaults to `(0.25, 0.75)`. |
| `seed` | Integer. Used to create a random seed. |
| `bounding_box_format` | A string specifying the format of the bounding boxes, for example `"xyxy"`. Required if bounding boxes are passed to the layer. Defaults to `None`. |
| `data_format` | string, either `"channels_last"` or `"channels_first"`. The ordering of the dimensions in the inputs. `"channels_last"` corresponds to inputs with shape `(batch, height, width, channels)` while `"channels_first"` corresponds to inputs with shape `(batch, channels, height, width)`. It defaults to the `image_data_format` value found in your Keras config file at `~/.keras/keras.json`. If you never set it, then it will be `"channels_last"`. |
| `**kwargs` | Base layer keyword arguments, such as `name` and `dtype`. |

<br />

#### Example:

```python
(images, labels), _ = keras.datasets.cifar10.load_data()
images, labels = images[:8], labels[:8]
labels = keras.ops.cast(keras.ops.one_hot(labels.flatten(), 10), "float32")
mosaic = keras.layers.Mosaic(seed=42)
output = mosaic({"images": images, "labels": labels})
```

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `input` | Retrieves the input tensor(s) of a symbolic operation. <br /> Only returns the tensor(s) corresponding to the *first time* the operation was called. Note that for a layer called more than once (a shared layer), this property returns the inputs of the first call only, even though the layer has several inputs. There is no public accessor for the inputs of the other calls. Keep a reference to the inputs used in each call instead. |
| `output` | Retrieves the output tensor(s) of a layer. <br /> Only returns the tensor(s) corresponding to the *first time* the operation was called. Note that for a layer called more than once (a shared layer), this property returns the output of the first call only, silently ignoring the later calls. There is no public accessor for the outputs of the other calls. To access the output of a specific call, keep a reference to the tensor returned by that call: ```python inputs_a = keras.Input(shape=(3,)) inputs_b = keras.Input(shape=(3,)) shared = keras.layers.Dense(2) outputs_a = shared(inputs_a)  # First call. outputs_b = shared(inputs_b)  # Second call. # `shared.output` is `outputs_a`, the first call's output, not # `outputs_b`. Use the returned tensors directly instead. model = keras.Model([inputs_a, inputs_b], [outputs_a, outputs_b]) ``` <br /> |

<br />

## Methods

### `convert_weight`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/data_layer.py#L153-L159)

```python
convert_weight(
    weight
)
```

Convert the weight if it is from the a different backend.

### `from_config`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/ops/operation.py#L330-L370)

```python
@classmethod
from_config(
    config
)
```

Creates an operation from its config.

This method is the reverse of `get_config`, capable of instantiating the
same operation from the config dictionary.

> [!NOTE]
> **Note:** If you override this method, you might receive a serialized dtype config, which is a `dict`. You can deserialize it as follows:

```python
if "dtype" in config and isinstance(config["dtype"], dict):
    policy = dtype_policies.deserialize(config["dtype"])
```

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | A Python dictionary, typically the output of `get_config`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An operation instance. ||

<br />

### `get_random_transformation`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/mosaic.py#L163-L233)

```python
get_random_transformation(
    data, training=True, seed=None
)
```

### `transform_bounding_boxes`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/mosaic.py#L338-L405)

```python
transform_bounding_boxes(
    bounding_boxes, transformation, training=True
)
```

### `transform_images`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py#L64-L70)

```python
transform_images(
    images, transformation, training=True
)
```

### `transform_labels`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/mosaic.py#L235-L274)

```python
transform_labels(
    labels, transformation, training=True
)
```

### `transform_segmentation_masks`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py#L83-L90)

```python
transform_segmentation_masks(
    segmentation_masks, transformation, training=True
)
```

### `transform_single_bounding_box`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py#L106-L119)

```python
transform_single_bounding_box(
    bounding_box, transformation, training=True
)
```

### `transform_single_image`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py#L92-L97)

```python
transform_single_image(
    image, transformation, training=True
)
```

### `transform_single_label`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py#L99-L104)

```python
transform_single_label(
    label, transformation, training=True
)
```

### `transform_single_segmentation_mask`

[View source](https://github.com/keras-team/keras/tree/master/keras/src/layers/preprocessing/image_preprocessing/base_image_preprocessing_layer.py#L121-L130)

```python
transform_single_segmentation_mask(
    segmentation_mask, transformation, training=True
)
```

<br />

<br />

<br />

| ## Class Variables ||
|---|---|
| lora_enabled | `False` |

<br />