mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
improve(utils): add option to load model checkpoint from BytesIO (#1873)
This commit is contained in:
1 parent
5e56f06866
commit
799fffd768
2 files changed
+18
-7
No files matched your search
@@ -65,6 +65,7 @@ Models can now be stored alongside their pipelines in the same repository, strea
|
||||
- improve(setup): switch to `lightning` from `pytorch-lightning`
|
||||
- improve(utils): improve dependency check when loading pretrained models and/or pipeline
|
||||
- improve(utils): add option to skip dependency check
|
||||
- improve(utils): add option to load a pretrained model checkpoint from an `io.BytesIO` buffer
|
||||
|
||||
### Fixes
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import io
|
||||
import warnings
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
@@ -499,7 +500,7 @@ class Model(lightning.LightningModule):
|
||||
@classmethod
|
||||
def from_pretrained(
|
||||
cls,
|
||||
checkpoint: Union[Path, Text],
|
||||
checkpoint: Union[Path, Text, io.BytesIO],
|
||||
map_location=None,
|
||||
strict: bool = True,
|
||||
subfolder: Optional[str] = None,
|
||||
@@ -512,11 +513,12 @@ class Model(lightning.LightningModule):
|
||||
|
||||
Parameters
|
||||
----------
|
||||
checkpoint : Path or str
|
||||
checkpoint : Path, str, or byte buffer
|
||||
Model checkpoint, provided as one of the following:
|
||||
* path to a local `pytorch_model.bin` model checkpoint
|
||||
* path to a local directory containing such a file
|
||||
* identifier of a model on Huggingface hub
|
||||
* pre-loaded model checkpoint as a byte buffer
|
||||
map_location: optional
|
||||
Same role as in torch.load().
|
||||
Defaults to `lambda storage, loc: storage`.
|
||||
@@ -546,9 +548,13 @@ class Model(lightning.LightningModule):
|
||||
`huggingface_hub.hf_hub_download`
|
||||
|
||||
"""
|
||||
if isinstance(checkpoint, io.BytesIO) or os.path.isfile(checkpoint):
|
||||
# if checkpoint is a BytesIO object or a file path, use it as is
|
||||
path_to_model_checkpoint = checkpoint
|
||||
|
||||
# if checkpoint is a directory, look for the model checkpoint
|
||||
# inside this directory (or inside a subfolder if specified)
|
||||
if os.path.isdir(checkpoint):
|
||||
elif os.path.isdir(checkpoint):
|
||||
if subfolder:
|
||||
path_to_model_checkpoint = (
|
||||
Path(checkpoint) / subfolder / AssetFileName.Model.value
|
||||
@@ -556,10 +562,6 @@ class Model(lightning.LightningModule):
|
||||
else:
|
||||
path_to_model_checkpoint = Path(checkpoint) / AssetFileName.Model.value
|
||||
|
||||
# if checkpoint is a file, use it as is
|
||||
elif os.path.isfile(checkpoint):
|
||||
path_to_model_checkpoint = checkpoint
|
||||
|
||||
# otherwise, assume that the checkpoint is hosted on HF model hub
|
||||
else:
|
||||
_, _, path_to_model_checkpoint = download_from_hf_hub(
|
||||
@@ -594,6 +596,10 @@ class Model(lightning.LightningModule):
|
||||
Klass = getattr(module, class_name)
|
||||
|
||||
try:
|
||||
# if checkpoint is a BytesIO object, seek to the beginning so we can load it again
|
||||
if isinstance(path_to_model_checkpoint, io.BytesIO):
|
||||
path_to_model_checkpoint.seek(0)
|
||||
|
||||
model = Klass.load_from_checkpoint(
|
||||
path_to_model_checkpoint,
|
||||
map_location=map_location,
|
||||
@@ -602,6 +608,10 @@ class Model(lightning.LightningModule):
|
||||
)
|
||||
except RuntimeError as e:
|
||||
if "loss_func" in str(e):
|
||||
# if checkpoint is a BytesIO object, seek to the beginning so we can load it again
|
||||
if isinstance(path_to_model_checkpoint, io.BytesIO):
|
||||
path_to_model_checkpoint.seek(0)
|
||||
|
||||
msg = (
|
||||
"Model has been trained with a task-dependent loss function. "
|
||||
"Set 'strict' to False to load the model without its loss function "
|
||||
|
||||
Reference in new issue
Block a user