albumentations.core.serialization


Module for serialization and deserialization of Albumentations transforms. This module provides functionality to serialize transforms to JSON or YAML format and deserialize them back. It implements the Serializable interface that allows transforms to be converted to and from dictionaries, which can then be saved to disk or transmitted over a network. This is particularly useful for saving augmentation pipelines and restoring them later with the exact same configuration.

shorten_class_namefunction

shorten_class_name(
    class_fullname: str
)

Parameters

NameTypeDefaultDescription
class_fullnamestr--

SerializableMetaclass

SerializableMeta()

Metaclass that registers transform classes for lookup by full name during deserialization. Uses SERIALIZABLE_REGISTRY / NON_SERIALIZABLE_REGISTRY.

Serializableclass

Serializable()

to_dictfunction

to_dict(
    transform: Serializable,
    on_not_implemented_error: str = raise
)

Convert a transform to a serializable dict of standard Python types. Delegates to transform.to_dict; on_not_implemented_error: raise or warn. Args: transform (Serializable): A transform that should be serialized. If the transform doesn't implement the `to_dict` method and `on_not_implemented_error` equals to 'raise' then `NotImplementedError` is raised. If `on_not_implemented_error` equals to 'warn' then `NotImplementedError` will be ignored but no transform parameters will be serialized. on_not_implemented_error (str): `raise` or `warn`.

Parameters

NameTypeDefaultDescription
transformSerializable--
on_not_implemented_errorstrraise-

instantiate_nonserializablefunction

instantiate_nonserializable(
    transform: dict[str, Any],
    nonserializable: dict[str, Any] | None
)

Parameters

NameTypeDefaultDescription
transformdict[str, Any]--
nonserializable
One of:
  • dict[str, Any]
  • None
--

from_dictfunction

from_dict(
    transform_dict: dict[str, Any],
    nonserializable: dict[str, Any] | None
)

Restore a transform (or pipeline) from a serialized dict. Pass nonserializable for Lambda/custom transforms keyed by name. Args: transform_dict (dict[str, Any]): Serialized transform pipeline. nonserializable (dict[str, Any] | None): Optional dict of non-serializable transforms keyed by name.

Parameters

NameTypeDefaultDescription
transform_dictdict[str, Any]--
nonserializable
One of:
  • dict[str, Any]
  • None
--

check_data_formatfunction

check_data_format(
    data_format: Literal['json', 'yaml']
)

Parameters

NameTypeDefaultDescription
data_format
One of:
  • 'json'
  • 'yaml'
--

serialize_enumfunction

serialize_enum(
    obj: Any
)

Recursively replace Enum instances with their value; traverse Mappings and Sequences. Call before saving pipeline to JSON/YAML.

Parameters

NameTypeDefaultDescription
objAny--

savefunction

save(
    transform: Serializable,
    filepath_or_buffer: str | Path | TextIO,
    data_format: Literal['json', 'yaml'] = json,
    on_not_implemented_error: Literal['raise', 'warn'] = raise
)

Serialize a transform pipeline to a file or file-like object in JSON or YAML. Use on_not_implemented_error to raise or warn if a transform lacks to_dict. Args: transform (Serializable): The transform pipeline to serialize. filepath_or_buffer (str | Path | TextIO): The file path or file-like object to write the serialized data to. String is interpreted as a path; file-like object is written to directly. data_format (Literal['json', 'yaml']): The format to serialize the data in. Defaults to 'json'. on_not_implemented_error (Literal['raise', 'warn']): If a transform does not implement to_dict: 'raise' raises NotImplementedError; 'warn' ignores and omits transform arguments. Defaults to 'raise'. Raises: ValueError: If `data_format` is 'yaml' but PyYAML is not installed.

Parameters

NameTypeDefaultDescription
transformSerializable--
filepath_or_buffer
One of:
  • str
  • Path
  • TextIO
--
data_format
One of:
  • 'json'
  • 'yaml'
json-
on_not_implemented_error
One of:
  • 'raise'
  • 'warn'
raise-

loadfunction

load(
    filepath_or_buffer: str | Path | TextIO,
    data_format: Literal['json', 'yaml'] = json,
    nonserializable: dict[str, Any] | None
)

Load a serialized transform pipeline from file or file-like object (JSON or YAML). Pass nonserializable for Lambda/custom. Args: filepath_or_buffer (str | Path | TextIO): The file path or file-like object to read the serialized data from. String is interpreted as a path; file-like object is read from directly. data_format (Literal['json', 'yaml']): The format of the serialized data. Defaults to 'json'. nonserializable (dict[str, Any] | None): A dictionary that contains non-serializable transforms. This dictionary is required when restoring a pipeline that contains non-serializable transforms. Keys in the dictionary should be named the same as the `name` arguments in respective transforms from the serialized pipeline. Defaults to None. Returns: object: The deserialized transform pipeline. Raises: ValueError: If `data_format` is 'yaml' but PyYAML is not installed.

Parameters

NameTypeDefaultDescription
filepath_or_buffer
One of:
  • str
  • Path
  • TextIO
--
data_format
One of:
  • 'json'
  • 'yaml'
json-
nonserializable
One of:
  • dict[str, Any]
  • None
--

register_additional_transformsfunction

register_additional_transforms()

Register transforms that are not imported directly into the `albumentations` module by checking the availability of optional dependencies.

get_shortest_class_fullnamefunction

get_shortest_class_fullname(
    cls: type[Any]
)

Return the shortest full class name for a class (e.g. module.ClassName or alias). Used for serialization registry lookup. Args: cls (type[Any]): Class (e.g. a transform or Compose subclass). Returns: str: Shortened full class name.

Parameters

NameTypeDefaultDescription
clstype[Any]--