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()

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.

Serializableclass

Serializable()

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. 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
)

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
--

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 search for Enum objects and convert them to their value. Also handle any Mapping or Sequence types.

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 and save it to either a file specified by a path or a file-like object in either JSON or YAML format. Args: transform (Serializable): The transform pipeline to serialize. filepath_or_buffer (Union[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 (str): The format to serialize the data in. Valid options are 'json' and 'yaml'. Defaults to 'json'. on_not_implemented_error (str): Determines 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'. 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 pipeline from a file or file-like object and construct a transform pipeline. Args: filepath_or_buffer (Union[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 (Literal["json", "yaml"]): The format of the serialized data. Defaults to 'json'. nonserializable (Optional[dict[str, Any]]): 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]
)

The function `get_shortest_class_fullname` takes a class object as input and returns its shortened full name. :param cls: The parameter `cls` is of type `Type[BasicCompose]`, which means it expects a class that is a subclass of `BasicCompose` :type cls: Type[BasicCompose] :return: a string, which is the shortened version of the full class name.

Parameters

NameTypeDefaultDescription
clstype[Any]--