View source on GitHub
|
Mosaic implements the Mosaic data augmentation technique.
Inherits From: Layer, Operation
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().
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.
|
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. |
References | |
|---|---|
Example:
(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})
Methods
convert_weight
convert_weight(
weight
)
Convert the weight if it is from the a different backend.
from_config
@classmethodfrom_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.
if "dtype" in config and isinstance(config["dtype"], dict):
policy = dtype_policies.deserialize(config["dtype"])
| Args | |
|---|---|
config
|
A Python dictionary, typically the output of get_config.
|
| Returns | |
|---|---|
| An operation instance. |
get_random_transformation
get_random_transformation(
data, training=True, seed=None
)
transform_bounding_boxes
transform_bounding_boxes(
bounding_boxes, transformation, training=True
)
transform_images
transform_images(
images, transformation, training=True
)
transform_labels
transform_labels(
labels, transformation, training=True
)
transform_segmentation_masks
transform_segmentation_masks(
segmentation_masks, transformation, training=True
)
transform_single_bounding_box
transform_single_bounding_box(
bounding_box, transformation, training=True
)
transform_single_image
transform_single_image(
image, transformation, training=True
)
transform_single_label
transform_single_label(
label, transformation, training=True
)
transform_single_segmentation_mask
transform_single_segmentation_mask(
segmentation_mask, transformation, training=True
)
Class Variables | |
|---|---|
| lora_enabled |
False
|
View source on GitHub