chore: replace load_from_checkpoint by Model.from_pretrained (#588)

* chore: import Model from package root
* chore: import Inference from package root
This commit is contained in:
Hervé BREDIN authored and GitHub committed 2021-01-20 16:39:08 +01:00
1 parent a902052cf2
commit 0b9be93ee1
18 files changed
+125 -117

No files matched your search

+3 -3
View File
@@ -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)
+3 -2
View File
@@ -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"
+2 -2
View File
@@ -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)"
+7 -1
View File
@@ -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"]
+2 -2
View File
@@ -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"]:
+96 -95
View File
@@ -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,
)
@@ -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
+1 -1
View File
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
+1 -1
View File
@@ -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
@@ -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
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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)"
]
+1 -1
View File
@@ -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)"
]