class documentation

Augmentation engine backed by Albumentations.

Configuration Format

The configuration contains a list of transformations, each specified by its name and optional parameters as described in the AlbumentationConfigItem schema.

The name must be either a valid name of an Albumentations transformation, or a name of a custom transformation registered in the TRANSFORMATIONS registry.

For example:

[
    {
        "name": "Affine",
        "params": {
            "rotate": 30,
            "scale": 0.5,
            "p": 0.3,
        },
    },
    {
        "name": "MixUp",
        "params": {
            "alpha": [0.3, 0.7],
            "p": 0.5,
        },
    },
    {
        "name": "CustomResize",
        "use_for_resizing": True,
    },
]

Transformation Order

The order of transformations provided in the configuration is not guaranteed to be preserved. The transformations are divided into the following groups and are applied in this order:

  1. Batch transformations: Subclasses of BatchTransform.
  2. Spatial transformations: Subclasses of A.DualTransform.
  3. Custom transformations: Subclasses of A.BasicTransform, but not subclasses of more specific base classes above.
  4. Pixel transformations: Subclasses of A.ImageOnlyTransform. These transformations act only on the image.

Supported Augmentations

Albumentations Augmentations

All augmentations provided by the Albumentations library are supported.

Batch Augmentations

MixUp

MixUp is a data augmentation technique that blends 2 source images into a single image using a weight coefficient alpha.

Mosaic4

Mosaic4 transformation combines 4 images into a single image by placing them in a 2×2 grid.

Augmenting Unsupported Types

Albumentations does not natively support all label types supported by Luxonis Data Format. This section describes how unsupported types are handled.

Note that the following applies only to officially supported augmentations. Custom augmentations can be implemented to handle arbitrary types.

Classification

Classification can be properly augmented only for multi-label tasks, where each class is tied to a bounding box. In such cases, the classes belonging to bboxes falling outside the image are removed. In other cases, the classification annotation is kept as is.

Metadata

Metadata labels can contain arbitrary data and their semantics are unknown to the augmentation engine. Therefore, the only transformation applied to metadata is discarding metadata associated with boxes falling outside the image.

Arrays

Arrays are dealt with in the same way as metadata. The only transformation applied to arrays is discarding arrays associated with bboxes falling outside the image.

Oriented Bounding Boxes

(Not yet implemented)

Oriented bounding boxes are of shape (N, 5) where the last element of each row contains the angle of the bbox. This format is not supported by Albumentations, however, Albumentations support angle to be a part of the keypoints. So, the oriented bounding boxes are split into regular bounding boxes and a set of keypoints that represent the center of the bbox and contain the angle as the third coordinate.

Both the keypoints and the bboxes are augmented separately. At the end, the angle is extracted from the keypoints and added back to the bounding boxes. The keypoints are discarded.

Custom Augmentations

Custom augmentations can be implemented by creating a subclass of A.BasicTransform and registering it in the TRANSFORMATIONS registry.

Possible target types that the augmentation can receive are:

  • "image":

    The image. All augmentations should usually support this target. For subclasses of A.ImageOnlyTransform or A.DualTransform this means overriding apply.

  • "bboxes":

    Bounding boxes. For subclasses of A.DualTransform, this means overriding apply_to_bboxes.

  • "keypoints":

    Keypoints. For subclasses of A.DualTransform, this means overriding apply_to_keypoints.

  • "mask":

    Segmentation masks. For subclasses of A.DualTransform, this means overriding apply_to_mask.

  • "instance_mask":

    Instance segmentation masks. For subclasses of BatchTransform, this means overriding apply_to_instance_mask.

    Subclasses of A.DualTransform do not support this target; instance masks are treated as regular masks instead.

    Custom augmentations can support instance masks by implementing their own logic for handling them and overriding the targets property to include the "instance_mask" target.

  • "array":

    Arbitrary arrays. Can only be supported by custom augmentations by implementing their own logic and adding the "array" target to the targets property.

  • "metadata":

    Metadata labels. Same situation as with the "array" type.

  • "classification":

    One-hot encoded multi-task classification labels. Same situation as with the "array" type.

For example:

class CustomArrayAugmentation(A.BasicTransform):

    @property
    @override
    def targets(self):
        return {
            "image": self.apply,
            "array": self.apply_to_array,
        }

    @override
    def apply(self, image, **kwargs):
        ...

    def apply_to_array(
        self, array: np.ndarray, **kwargs
    ) -> np.ndarray:
        ...
Method __init__ Create an Albumentations-backed augmentation pipeline.
Method apply Apply the augmentation pipeline to the data.
Property batch_size The batch size required by the augmentation pipeline.
Static Method _create_default_resize_transform Undocumented
Static Method _create_transformation Undocumented
Static Method _mark_invisible_keypoints Mark keypoints outside the image bounds as invisible.
Static Method _resolve_pipeline_stage Undocumented
Static Method _task_to_target_name Undocumented
Static Method _wrap_transform Wrap an Albumentations composition for loader data dictionaries.
Method _check_augmentation_warnings Undocumented
Method _postprocess Postprocess the augmented data back to LDF format.
Method _preprocess_batch Preprocess a batch of labels.
Instance Variable _batch_transform Undocumented
Instance Variable _bbox_area_threshold Undocumented
Instance Variable _custom_transform Undocumented
Instance Variable _image_size Undocumented
Instance Variable _n_classes Undocumented
Instance Variable _pixel_transform Undocumented
Instance Variable _resize_transform Undocumented
Instance Variable _source_names Undocumented
Instance Variable _spatial_transform Undocumented
Instance Variable _target_names_to_tasks Undocumented
Instance Variable _targets Undocumented
@deprecated('is_validation_pipeline', suggest={'is_validation_pipeline': 'pipeline_stage'}, additional_message='Use `pipeline_stage=\'train\'`, `\'val\'`, or `\'test\'` instead.')
@override
def __init__(self, height: int, width: int, targets: dict[str, str], n_classes: dict[str, int], source_names: list[str], config: Iterable[Params], keep_aspect_ratio: bool = True, is_validation_pipeline: bool | None = None, pipeline_stage: Literal['train', 'val', 'test'] | None = None, min_bbox_visibility: float = 0.0, seed: int | None = None, bbox_area_threshold: float = 0.0004):

Create an Albumentations-backed augmentation pipeline.

Parameters
height:intTarget output image height.
width:intTarget output image width.
targets:dict[str, str]Task names mapped to task types. Supported task types are "array", "classification", "segmentation", "instance_segmentation", "boundingbox", "keypoints", and metadata tasks.
n_classes:dict[str, int]Number of classes per task.
source_names:list[str]Source names that should be treated as image targets.
config:Iterable[Params]Iterable of augmentation configuration dictionaries.
keep_aspect_ratio:boolWhether the default resize transform should preserve image aspect ratio.
is_validation_pipeline:bool | None

Optional legacy flag selecting evaluation behavior.

Deprecated since version 0.5.0: use pipeline_stage instead.
pipeline_stage:Literal['train', 'val', 'test'] | NoneOptional explicit pipeline stage. Valid values are "train", "val", and "test".
min_bbox_visibility:floatMinimum visible fraction of a bounding box after augmentation.
seed:int | NoneOptional random seed.
bbox_area_threshold:floatMinimum normalized bounding-box area kept after augmentation.
Raises
ValueErrorIf a target task type is unsupported, more than one transform is marked for resizing, or a configured transform is not an Albumentations transform.
TypeErrorIf a resizing transform has a non-numeric probability p.
@override
def apply(self, input_batch: list[LoaderMultiOutput]) -> LoaderMultiOutput:

Apply the augmentation pipeline to the data.

Parameters
input_batch:list[LoaderMultiOutput]Loader outputs to augment. The number of items must match the engine's batch size.
Returns
LoaderMultiOutputAugmented loader output.
@property
@override
batch_size: int =

The batch size required by the augmentation pipeline.

The batch size is the number of images requested by the augmentation pipeline in case of batch-based augmentations.

For example, if the augmentation pipeline contains the MixUp augmentation, the batch size should be 2.

If the pipeline requires MixUp and also Mosaic4 augmentations, the batch size should be 8 = (2⋅4).

@staticmethod
def _create_default_resize_transform(keep_aspect_ratio: bool, height: int, width: int, p: float = 1.0) -> A.DualTransform:

Undocumented

@staticmethod
def _create_transformation(config: AlbumentationConfigItem) -> A.BasicTransform:

Undocumented

@staticmethod
def _mark_invisible_keypoints(keypoints: np.ndarray, shape: tuple[int, int], **_) -> np.ndarray:

Mark keypoints outside the image bounds as invisible.

Parameters
keypoints:np.ndarrayKeypoints with visibility stored in the last column.
shape:tuple[int, int]Image shape used for bounds checking.
**_Undocumented
Returns
np.ndarrayCopy of keypoints with out-of-bounds visibility set to zero.
@staticmethod
def _resolve_pipeline_stage(pipeline_stage: Literal['train', 'val', 'test'] | None, is_validation_pipeline: bool | None) -> Literal['train', 'val', 'test']:

Undocumented

@staticmethod
def _task_to_target_name(task: str) -> str:

Undocumented

@staticmethod
def _wrap_transform(transform: A.BaseCompose, is_pixel: bool = False, source_names: list[str] | None = None) -> Callable[..., Data]:

Wrap an Albumentations composition for loader data dictionaries.

Parameters
transform:A.BaseComposeAlbumentations composition to wrap.
is_pixel:boolWhether the composition contains pixel-only transforms that should be replayed across image sources.
source_names:list[str] | NoneImage source names replayed for pixel-only transforms.
Returns
Callable[..., Data]Callable that applies the composition to a data dictionary.
Raises
ValueErrorIf is_pixel is true and source_names is not provided.
def _check_augmentation_warnings(self, config_item: AlbumentationConfigItem, available_target_types: set):

Undocumented

def _postprocess(self, data: Data, n_keypoints: dict[str, int]) -> LoaderMultiOutput:

Postprocess the augmented data back to LDF format.

Discards labels associated with bounding boxes that are outside the image.

Parameters
data:DataAugmented data keyed by target name.
n_keypoints:dict[str, int]Mapping from task names to keypoint counts.
Returns
LoaderMultiOutputAugmented images and labels.
def _preprocess_batch(self, labels_batch: list[LoaderMultiOutput]) -> tuple[list[Data], dict[str, int]]:

Preprocess a batch of labels.

Parameters
labels_batch:list[LoaderMultiOutput]Loader outputs to preprocess.
Returns
tuple[list[Data], dict[str, int]]Preprocessed data and keypoint counts for each task.
_batch_transform =

Undocumented

_bbox_area_threshold =

Undocumented

_custom_transform =

Undocumented

_image_size =

Undocumented

_n_classes =

Undocumented

_pixel_transform =

Undocumented

_resize_transform =

Undocumented

_source_names =

Undocumented

_spatial_transform =

Undocumented

_target_names_to_tasks: dict =

Undocumented

_targets: dict[str, TargetType] =

Undocumented