From 0b9be93ee1fb234c88f0e32163e9cce2a12ac8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Wed, 20 Jan 2021 16:39:08 +0100 Subject: [PATCH] chore: replace load_from_checkpoint by Model.from_pretrained (#588) * chore: import Model from package root * chore: import Inference from package root --- README.md | 6 +- notebook/inference.ipynb | 5 +- notebook/sharing.ipynb | 4 +- pyannote/audio/__init__.py | 8 +- pyannote/audio/core/inference.py | 4 +- pyannote/audio/core/model.py | 191 +++++++++--------- .../pipelines/overlapped_speech_detection.py | 2 +- pyannote/audio/pipelines/segmentation.py | 2 +- .../pipelines/speaker_change_detection.py | 2 +- .../audio/pipelines/speaker_diarization.py | 2 +- .../audio/pipelines/speech_turn_assignment.py | 2 +- .../audio/pipelines/speech_turn_clustering.py | 2 +- .../pipelines/speech_turn_segmentation.py | 2 +- pyannote/audio/pipelines/utils.py | 2 +- .../pipelines/voice_activity_detection.py | 2 +- tests/inference_test.py | 2 +- tutorials/overlapped_speech_detection.ipynb | 2 +- tutorials/voice_activity_detection.ipynb | 2 +- 18 files changed, 125 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index 6c5cef44..1cc32f2a 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ trainer.fit(model) Predictions are obtained by wrapping the model into the `Inference` engine. ```python -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference inference = Inference(model) predictions = inference('audio.wav') ``` @@ -83,8 +83,8 @@ Fine-tuning is as easy as setting the `task` attribute, freezing early layers an *Here, we fine-tune on AMI dataset a voice activity detection model pretrained on DIHARD dataset.* ```python -from pyannote.audio.core.model import load_from_checkpoint -model = load_from_checkpoint('hbredin/VoiceActivityDetection-PyanNet-DIHARD') +from pyannote.audio import Model +model = Model.from_pretrained('hbredin/VoiceActivityDetection-PyanNet-DIHARD') model.task = VoiceActivityDetection(ami) model.freeze_up_to('sincnet') trainer.fit(model) diff --git a/notebook/inference.ipynb b/notebook/inference.ipynb index 1e486101..fab9766d 100644 --- a/notebook/inference.ipynb +++ b/notebook/inference.ipynb @@ -54,7 +54,7 @@ "metadata": {}, "outputs": [], "source": [ - "from pyannote.audio.core.inference import Inference\n", + "from pyannote.audio import Inference\n", "inference = Inference(model, step=0.1, batch_size=128)" ] }, @@ -130,7 +130,8 @@ "metadata": {}, "outputs": [], "source": [ - "model = SimpleSegmentationModel.load_from_checkpoint('inference/vad/lightning_logs/version_0/checkpoints/epoch=0-step=8.ckpt')\n", + "from pyannote.audio import Model\n", + "model = Model.from_pretrained('inference/vad/lightning_logs/version_0/checkpoints/epoch=0-step=8.ckpt')\n", "inference = Inference(model, step=0.1, batch_size=128)\n", "scores = inference(dev_file)\n", "scores" diff --git a/notebook/sharing.ipynb b/notebook/sharing.ipynb index 0222b93a..343495f7 100644 --- a/notebook/sharing.ipynb +++ b/notebook/sharing.ipynb @@ -47,8 +47,8 @@ "metadata": {}, "outputs": [], "source": [ - "from pyannote.audio.core.model import load_from_checkpoint\n", - "model = load_from_checkpoint('sharing/lightning_logs/version_0/checkpoints/epoch=0-step=3.ckpt')\n", + "from pyannote.audio import Model\n", + "model = Model.from_pretrained('sharing/lightning_logs/version_0/checkpoints/epoch=0-step=3.ckpt')\n", "assert isinstance(model, SimpleSegmentationModel)\n", "\n", "# checkpoint should work with a URL as well (it relies on pl_load)" diff --git a/pyannote/audio/__init__.py b/pyannote/audio/__init__.py index 9c7e5c65..b1dfd85b 100644 --- a/pyannote/audio/__init__.py +++ b/pyannote/audio/__init__.py @@ -1,6 +1,6 @@ # MIT License # -# Copyright (c) 2020 CNRS +# Copyright (c) 2020-2021 CNRS # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -24,3 +24,9 @@ try: from .version import __version__, git_version # noqa: F401 except ImportError: pass + + +from .core.inference import Inference +from .core.model import Model + +__all__ = ["Model", "Inference"] diff --git a/pyannote/audio/core/inference.py b/pyannote/audio/core/inference.py index 44f88faf..6f54c73c 100644 --- a/pyannote/audio/core/inference.py +++ b/pyannote/audio/core/inference.py @@ -32,7 +32,7 @@ from einops import rearrange from pytorch_lightning.utilities.memory import is_oom_error from pyannote.audio.core.io import AudioFile -from pyannote.audio.core.model import Model, load_from_checkpoint +from pyannote.audio.core.model import Model from pyannote.audio.core.task import Resolution from pyannote.audio.utils.permutation import permutate from pyannote.audio.utils.progress import InferenceProgressHook @@ -86,7 +86,7 @@ class Inference: self.model = ( model if isinstance(model, Model) - else load_from_checkpoint(Path(model), map_location=device, strict=False) + else Model.from_pretrained(Path(model), map_location=device, strict=False) ) if window not in ["sliding", "whole"]: diff --git a/pyannote/audio/core/model.py b/pyannote/audio/core/model.py index 0124e6a9..262f29e0 100644 --- a/pyannote/audio/core/model.py +++ b/pyannote/audio/core/model.py @@ -665,99 +665,100 @@ class Model(pl.LightningModule): return self._helper_by_name(modules, recurse=recurse, requires_grad=True) - -def load_from_checkpoint( - checkpoint_path: Union[Path, Text], - map_location=None, - hparams_file: Union[Path, Text] = None, - strict: bool = True, - use_auth_token: Union[Text, None] = None, - **kwargs, -) -> Model: - """Load model from checkpoint - - Parameters - ---------- - checkpoint_path : Path or str - Path to checkpoint, or a remote URL, or a model identifier from - the huggingface.co model hub. - map_location: optional - If your checkpoint saved a GPU model and you now load on CPUs - or a different number of GPUs, use this to map to the new setup. - The behaviour is the same as in torch.load(). - hparams_file : Path or str, optional - Path to a .yaml file with hierarchical structure as in this example: - drop_prob: 0.2 - dataloader: - batch_size: 32 - You most likely won’t need this since Lightning will always save the - hyperparameters to the checkpoint. However, if your checkpoint weights - do not have the hyperparameters saved, use this method to pass in a .yaml - file with the hparams you would like to use. These will be converted - into a dict and passed into your Model for use. - strict : bool, optional - Whether to strictly enforce that the keys in checkpoint_path match - the keys returned by this module’s state dict. Defaults to True. - use_auth_token : str, optional - When loading a private huggingface.co model, set `use_auth_token` - to True or to a string containing your hugginface.co authentication - token that can be obtained by running `huggingface-cli login` - kwargs: optional - Any extra keyword args needed to init the model. - Can also be used to override saved hyperparameter values. - - Returns - ------- - model : Model - Model - """ - - # pytorch-lightning expects str, not Path. - checkpoint_path = str(checkpoint_path) - if hparams_file is not None: - hparams_file = str(hparams_file) - - # resolve the checkpoint_path to - # something that pl will handle - if os.path.isfile(checkpoint_path): - path_for_pl = checkpoint_path - elif urlparse(checkpoint_path).scheme in ("http", "https"): - path_for_pl = checkpoint_path - else: - # Finally, let's try to find it on Hugging Face model hub - # e.g. julien-c/voice-activity-detection is a valid model id - # and julien-c/voice-activity-detection@main supports specifying a commit/branch/tag. - if "@" in checkpoint_path: - model_id = checkpoint_path.split("@")[0] - revision = checkpoint_path.split("@")[1] - else: - model_id = checkpoint_path - revision = None - url = hf_hub_url( - model_id=model_id, filename=HF_PYTORCH_WEIGHTS_NAME, revision=revision - ) - - path_for_pl = cached_download( - url=url, - library_name="pyannote", - library_version=__version__, - cache_dir=CACHE_DIR, - use_auth_token=use_auth_token, - ) - - # obtain model class from the checkpoint - checkpoint = pl_load(path_for_pl, map_location=map_location) - - module_name: str = checkpoint["pyannote.audio"]["architecture"]["module"] - module = import_module(module_name) - - class_name: str = checkpoint["pyannote.audio"]["architecture"]["class"] - Klass: Model = getattr(module, class_name) - - return Klass.load_from_checkpoint( - path_for_pl, - map_location=map_location, - hparams_file=hparams_file, - strict=strict, + @classmethod + def from_pretrained( + cls, + checkpoint_path: Union[Path, Text], + map_location=None, + hparams_file: Union[Path, Text] = None, + strict: bool = True, + use_auth_token: Union[Text, None] = None, **kwargs, - ) + ) -> "Model": + """Load pretrained model + + Parameters + ---------- + checkpoint_path : Path or str + Path to checkpoint, or a remote URL, or a model identifier from + the huggingface.co model hub. + map_location: optional + If your checkpoint saved a GPU model and you now load on CPUs + or a different number of GPUs, use this to map to the new setup. + The behaviour is the same as in torch.load(). + hparams_file : Path or str, optional + Path to a .yaml file with hierarchical structure as in this example: + drop_prob: 0.2 + dataloader: + batch_size: 32 + You most likely won’t need this since Lightning will always save the + hyperparameters to the checkpoint. However, if your checkpoint weights + do not have the hyperparameters saved, use this method to pass in a .yaml + file with the hparams you would like to use. These will be converted + into a dict and passed into your Model for use. + strict : bool, optional + Whether to strictly enforce that the keys in checkpoint_path match + the keys returned by this module’s state dict. Defaults to True. + use_auth_token : str, optional + When loading a private huggingface.co model, set `use_auth_token` + to True or to a string containing your hugginface.co authentication + token that can be obtained by running `huggingface-cli login` + kwargs: optional + Any extra keyword args needed to init the model. + Can also be used to override saved hyperparameter values. + + Returns + ------- + model : Model + Model + """ + + # pytorch-lightning expects str, not Path. + checkpoint_path = str(checkpoint_path) + if hparams_file is not None: + hparams_file = str(hparams_file) + + # resolve the checkpoint_path to + # something that pl will handle + if os.path.isfile(checkpoint_path): + path_for_pl = checkpoint_path + elif urlparse(checkpoint_path).scheme in ("http", "https"): + path_for_pl = checkpoint_path + else: + # Finally, let's try to find it on Hugging Face model hub + # e.g. julien-c/voice-activity-detection is a valid model id + # and julien-c/voice-activity-detection@main supports specifying a commit/branch/tag. + if "@" in checkpoint_path: + model_id = checkpoint_path.split("@")[0] + revision = checkpoint_path.split("@")[1] + else: + model_id = checkpoint_path + revision = None + url = hf_hub_url( + model_id=model_id, filename=HF_PYTORCH_WEIGHTS_NAME, revision=revision + ) + + path_for_pl = cached_download( + url=url, + library_name="pyannote", + library_version=__version__, + cache_dir=CACHE_DIR, + use_auth_token=use_auth_token, + ) + + # obtain model class from the checkpoint + checkpoint = pl_load(path_for_pl, map_location=map_location) + + module_name: str = checkpoint["pyannote.audio"]["architecture"]["module"] + module = import_module(module_name) + + class_name: str = checkpoint["pyannote.audio"]["architecture"]["class"] + Klass = getattr(module, class_name) + + return Klass.load_from_checkpoint( + path_for_pl, + map_location=map_location, + hparams_file=hparams_file, + strict=strict, + **kwargs, + ) diff --git a/pyannote/audio/pipelines/overlapped_speech_detection.py b/pyannote/audio/pipelines/overlapped_speech_detection.py index 21aa1bdc..a2ae5787 100644 --- a/pyannote/audio/pipelines/overlapped_speech_detection.py +++ b/pyannote/audio/pipelines/overlapped_speech_detection.py @@ -24,7 +24,7 @@ from typing import Optional, Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.audio.utils.signal import Binarize from pyannote.core import Annotation, Timeline diff --git a/pyannote/audio/pipelines/segmentation.py b/pyannote/audio/pipelines/segmentation.py index dbaad1da..a1d85333 100644 --- a/pyannote/audio/pipelines/segmentation.py +++ b/pyannote/audio/pipelines/segmentation.py @@ -24,7 +24,7 @@ from typing import Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.audio.utils.signal import Binarize from pyannote.core import Annotation, Segment, SlidingWindowFeature, Timeline diff --git a/pyannote/audio/pipelines/speaker_change_detection.py b/pyannote/audio/pipelines/speaker_change_detection.py index 7095c771..d97beedc 100644 --- a/pyannote/audio/pipelines/speaker_change_detection.py +++ b/pyannote/audio/pipelines/speaker_change_detection.py @@ -24,7 +24,7 @@ from typing import Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.audio.utils.signal import Peak from pyannote.core import Annotation diff --git a/pyannote/audio/pipelines/speaker_diarization.py b/pyannote/audio/pipelines/speaker_diarization.py index e0efdb81..0917f02b 100644 --- a/pyannote/audio/pipelines/speaker_diarization.py +++ b/pyannote/audio/pipelines/speaker_diarization.py @@ -22,7 +22,7 @@ from typing import Mapping, Optional, Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.core import Annotation from pyannote.database import get_annotated diff --git a/pyannote/audio/pipelines/speech_turn_assignment.py b/pyannote/audio/pipelines/speech_turn_assignment.py index 6655307f..09dee1c2 100644 --- a/pyannote/audio/pipelines/speech_turn_assignment.py +++ b/pyannote/audio/pipelines/speech_turn_assignment.py @@ -23,7 +23,7 @@ from typing import Optional, Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.core import Annotation from pyannote.pipeline import Pipeline diff --git a/pyannote/audio/pipelines/speech_turn_clustering.py b/pyannote/audio/pipelines/speech_turn_clustering.py index 19b2c1f8..d808b260 100644 --- a/pyannote/audio/pipelines/speech_turn_clustering.py +++ b/pyannote/audio/pipelines/speech_turn_clustering.py @@ -22,7 +22,7 @@ from typing import Optional, Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.core import Annotation from pyannote.pipeline import Pipeline diff --git a/pyannote/audio/pipelines/speech_turn_segmentation.py b/pyannote/audio/pipelines/speech_turn_segmentation.py index b8891555..8b3efd6a 100644 --- a/pyannote/audio/pipelines/speech_turn_segmentation.py +++ b/pyannote/audio/pipelines/speech_turn_segmentation.py @@ -22,7 +22,7 @@ from typing import Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.core import Annotation from pyannote.pipeline import Pipeline diff --git a/pyannote/audio/pipelines/utils.py b/pyannote/audio/pipelines/utils.py index cd581f5f..fc8b9805 100644 --- a/pyannote/audio/pipelines/utils.py +++ b/pyannote/audio/pipelines/utils.py @@ -24,7 +24,7 @@ from typing import Union import numpy as np -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.core import Annotation, SlidingWindowFeature diff --git a/pyannote/audio/pipelines/voice_activity_detection.py b/pyannote/audio/pipelines/voice_activity_detection.py index 24c82308..232b3dc1 100644 --- a/pyannote/audio/pipelines/voice_activity_detection.py +++ b/pyannote/audio/pipelines/voice_activity_detection.py @@ -24,7 +24,7 @@ from typing import Text, Union -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.io import AudioFile from pyannote.audio.utils.signal import Binarize from pyannote.core import Annotation diff --git a/tests/inference_test.py b/tests/inference_test.py index 59ffb101..af080c3c 100644 --- a/tests/inference_test.py +++ b/tests/inference_test.py @@ -2,7 +2,7 @@ import numpy as np import pytest import pytorch_lightning as pl -from pyannote.audio.core.inference import Inference +from pyannote.audio import Inference from pyannote.audio.core.task import Resolution from pyannote.audio.models.segmentation.debug import ( MultiTaskSegmentationModel, diff --git a/tutorials/overlapped_speech_detection.ipynb b/tutorials/overlapped_speech_detection.ipynb index 3e7cfb1f..78c6372c 100644 --- a/tutorials/overlapped_speech_detection.ipynb +++ b/tutorials/overlapped_speech_detection.ipynb @@ -197,7 +197,7 @@ "metadata": {}, "outputs": [], "source": [ - "from pyannote.audio.core.inference import Inference\n", + "from pyannote.audio import Inference\n", "inference = Inference(model)\n", "osd_probability = inference(test_file)" ] diff --git a/tutorials/voice_activity_detection.ipynb b/tutorials/voice_activity_detection.ipynb index f2ef0648..9be23eb0 100644 --- a/tutorials/voice_activity_detection.ipynb +++ b/tutorials/voice_activity_detection.ipynb @@ -189,7 +189,7 @@ "metadata": {}, "outputs": [], "source": [ - "from pyannote.audio.core.inference import Inference\n", + "from pyannote.audio import Inference\n", "inference = Inference(model)\n", "vad_probability = inference(test_file)" ]