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.
Members
- classSerializable
- classSerializableMeta
- functioncheck_data_format
- functionfrom_dict
- functionget_shortest_class_fullname
- functioninstantiate_nonserializable
- functionload
- functionregister_additional_transforms
- functionsave
- functionserialize_enum
- functionshorten_class_name
- functionto_dict
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
Name | Type | Default | Description |
---|---|---|---|
name | str | - | - |
bases | tuple[type, ...] | - | - |
check_data_formatfunction
check_data_format(
data_format: Literal['json', 'yaml']
)
Parameters
Name | Type | Default | Description |
---|---|---|---|
data_format | One of:
| - | - |
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
Name | Type | Default | Description |
---|---|---|---|
transform_dict | dict[str, Any] | - | - |
nonserializable | One of:
| 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
Name | Type | Default | Description |
---|---|---|---|
cls | type[Any] | - | - |
instantiate_nonserializablefunction
instantiate_nonserializable(
transform: dict[str, Any],
nonserializable: dict[str, Any] | None = None
)
Parameters
Name | Type | Default | Description |
---|---|---|---|
transform | dict[str, Any] | - | - |
nonserializable | One of:
| 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
Name | Type | Default | Description |
---|---|---|---|
filepath_or_buffer | One of:
| - | 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 | The format of the serialized data. Defaults to 'json'. |
nonserializable | One of:
| 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.
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
Name | Type | Default | Description |
---|---|---|---|
transform | Serializable | - | The transform pipeline to serialize. |
filepath_or_buffer | One of:
| - | 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 | The format to serialize the data in. Valid options are 'json' and 'yaml'. Defaults to 'json'. |
on_not_implemented_error | One of:
| raise | 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'. |
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
Name | Type | Default | Description |
---|---|---|---|
obj | Any | - | - |
shorten_class_namefunction
shorten_class_name(
class_fullname: str
)
Parameters
Name | Type | Default | Description |
---|---|---|---|
class_fullname | str | - | - |
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
Name | Type | Default | Description |
---|---|---|---|
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 | `raise` or `warn`. |