Skip to content

Example on how load and save from Hugging Face Hub

Source

Author: Pavel Iakubovskii

Python
!pip install -U albumentations
Python
from huggingface_hub import notebook_login
Python
notebook_login()
Python
import albumentations as A
import numpy as np

transform = A.Compose([
    A.RandomCrop(256, 256),
    A.HorizontalFlip(),
    A.RandomBrightnessContrast(),
    A.RGBShift(),
    A.Normalize(),
])

evaluation_transform = A.Compose([
    A.PadIfNeeded(256, 256),
    A.Normalize(),
])

transform.save_pretrained("qubvel-hf/albu", key="train")
# ^ this will save the transform to a directory "qubvel-hf/albu" with filename "albumentations_config_train.json"

transform.save_pretrained("qubvel-hf/albu", key="train", push_to_hub=True)
# ^ this will save the transform to a directory "qubvel-hf/albu" with filename "albumentations_config_train.json"
# + push the transform to the Hub to the repository "qubvel-hf/albu"

transform.push_to_hub("qubvel-hf/albu", key="train")
# ^ this will push the transform to the Hub to the repository "qubvel-hf/albu" (without saving it locally)

loaded_transform = A.Compose.from_pretrained("qubvel-hf/albu", key="train")
# ^ this will load the transform from local folder if exist or from the Hub repository "qubvel-hf/albu"

evaluation_transform.save_pretrained("qubvel-hf/albu", key="eval", push_to_hub=True)
# ^ this will save the transform to a directory "qubvel-hf/albu" with filename "albumentations_config_eval.json"

loaded_evaluation_transform = A.Compose.from_pretrained("qubvel-hf/albu", key="eval")
# ^ this will load the transform from the Hub repository "qubvel-hf/albu"
Python
# check
import numpy as np

image = np.random.randint(0, 255, (100, 200, 3), dtype=np.uint8)

preprocessed_image_1 = evaluation_transform(image=image)["image"]
preprocessed_image_2 = loaded_evaluation_transform(image=image)["image"]

assert np.allclose(preprocessed_image_1, preprocessed_image_2)