Open in Google ColabRun this notebook interactively

Example on how load and save from Hugging Face Hub 🔗

Source

Author: Pavel Iakubovskii

from huggingface_hub import notebook_login
/opt/homebrew/Caskroom/miniconda/base/envs/albumentations_examples/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
notebook_login()
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File /opt/homebrew/Caskroom/miniconda/base/envs/albumentations_examples/lib/python3.9/site-packages/huggingface_hub/_login.py:340, in notebook_login(new_session, write_permission)
    339 try:
--> 340     import ipywidgets.widgets as widgets  # type: ignore
    341     from IPython.display import display  # type: ignore
ModuleNotFoundError: No module named 'ipywidgets'

During handling of the above exception, another exception occurred:
ImportError                               Traceback (most recent call last)
Cell In[4], line 1
----> 1 notebook_login()
File /opt/homebrew/Caskroom/miniconda/base/envs/albumentations_examples/lib/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101, in _deprecate_arguments.<locals>._inner_deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     99         message += "\n\n" + custom_message
    100     warnings.warn(message, FutureWarning)
--> 101 return f(*args, **kwargs)
File /opt/homebrew/Caskroom/miniconda/base/envs/albumentations_examples/lib/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:31, in _deprecate_positional_args.<locals>._inner_deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     29 extra_args = len(args) - len(all_args)
     30 if extra_args <= 0:
---> 31     return f(*args, **kwargs)
     32 # extra_args > 0
     33 args_msg = [
     34     f"{name}='{arg}'" if isinstance(arg, str) else f"{name}={arg}"
     35     for name, arg in zip(kwonly_args[:extra_args], args[-extra_args:])
     36 ]
File /opt/homebrew/Caskroom/miniconda/base/envs/albumentations_examples/lib/python3.9/site-packages/huggingface_hub/_login.py:343, in notebook_login(new_session, write_permission)
    341     from IPython.display import display  # type: ignore
    342 except ImportError:
--> 343     raise ImportError(
    344         "The `notebook_login` function can only be used in a notebook (Jupyter or"
    345         " Colab) and you need the `ipywidgets` module: `pip install ipywidgets`."
    346     )
    347 if not new_session and get_token() is not None:
    348     logger.info("User is already logged in.")
ImportError: The `notebook_login` function can only be used in a notebook (Jupyter or Colab) and you need the `ipywidgets` module: `pip install ipywidgets`.
import albumentations as A

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

evaluation_transform = A.Compose(
    [
        A.PadIfNeeded(256, 256),
        A.Normalize(),
    ],
    seed=137,
    strict=True,
)

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"
# 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)