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.

Serializableclass

Serializable()

SerializableMetaclass

SerializableMeta(
    name: str,
    bases: tuple[type, ...]
)

A metaclass that is used to register classes in `SERIALIZABLE_REGISTRY` or `NON_SERIALIZABLE_REGISTRY` so they can be found later while deserializing transformation pipeline using classes full names.

Parameters

NameTypeDefaultDescription
namestr--
basestuple[type, ...]--

check_data_formatfunction

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

Parameters

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

from_dictfunction

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

Args: transform_dict: A dictionary with serialized transform pipeline. nonserializable (dict): A dictionary that contains non-serializable transforms. This dictionary is required when you are restoring a pipeline that contains non-serializable transforms. Keys in that dictionary should be named same as `name` arguments in respective transforms from a serialized pipeline.

Parameters

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

get_shortest_class_fullnamefunction

get_shortest_class_fullname(
    cls: type[Any]
)

The function `get_shortest_class_fullname` takes a class object as input and returns its shortened full name.

Parameters

NameTypeDefaultDescription
clstype[Any]--

instantiate_nonserializablefunction

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

Parameters

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

loadfunction

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

Load a serialized pipeline from a file or file-like object and construct a transform pipeline.

Parameters

NameTypeDefaultDescription
filepath_or_buffer
One of:
  • str
  • Path
  • TextIO
-The file path or file-like object to read the serialized data from. If a string is provided, it is interpreted as a path to a file. If a file-like object is provided, the serialized data will be read from it directly.
data_format
One of:
  • 'json'
  • 'yaml'
jsonThe format of the serialized data. Defaults to 'json'.
nonserializable
One of:
  • dict[str, Any]
  • None
NoneA 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.

register_additional_transformsfunction

register_additional_transforms()

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

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 and save it to either a file specified by a path or a file-like object in either JSON or YAML format.

Parameters

NameTypeDefaultDescription
transformSerializable-The transform pipeline to serialize.
filepath_or_buffer
One of:
  • str
  • Path
  • TextIO
-The file path or file-like object to write the serialized data to. If a string is provided, it is interpreted as a path to a file. If a file-like object is provided, the serialized data will be written to it directly.
data_format
One of:
  • 'json'
  • 'yaml'
jsonThe format to serialize the data in. Valid options are 'json' and 'yaml'. Defaults to 'json'.
on_not_implemented_error
One of:
  • 'raise'
  • 'warn'
raiseDetermines the behavior if a transform does not implement the `to_dict` method. If set to 'raise', a `NotImplementedError` is raised. If set to 'warn', the exception is ignored, and no transform arguments are saved. Defaults to 'raise'.

serialize_enumfunction

serialize_enum(
    obj: Any
)

Recursively search for Enum objects and convert them to their value. Also handle any Mapping or Sequence types.

Parameters

NameTypeDefaultDescription
objAny--

shorten_class_namefunction

shorten_class_name(
    class_fullname: str
)

Parameters

NameTypeDefaultDescription
class_fullnamestr--

to_dictfunction

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

Take a transform pipeline and convert it to a serializable representation that uses only standard python data types: dictionaries, lists, strings, integers, and floats.

Parameters

NameTypeDefaultDescription
transformSerializable-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_errorstrraise`raise` or `warn`.