mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
feat: add generic load_from_checkpoint and initial version checking (#12) + versioning fixes
* feat: add generic load_from_checkpoint and initial version checking Addresses #8 * fix: remove FIXME as torch.hub takes care of caching downloads * setup: switch to pytorch-lightning 1.0.0 * feat: check pytorch-lightning version * feat: add version compatibility check * test: add basic "sharing" notebook * fix: remove references to local files * fix: fix versioning until setuptools_scm really supports semver * Revert "fix: fix versioning until setuptools_scm really supports semver" This reverts commit a16ec7721aae4d1da42d86da6da908570d88b951. * Using package after setup * fix: make setuptools_scm version semver-compliant * fix: remove setuptools_scm_git_semver hack * fix: skip pyannote.audio version checking * Copying torchaudio version * Stripping whitespace from version Co-authored-by: Harry <himion0@gmail.com>
This commit is contained in:
1 parent
af020928c0
commit
95cb72b5c1
7 files changed
+196
-11
No files matched your search
@@ -82,3 +82,6 @@ gh-pages.pub
|
||||
.vscode/
|
||||
|
||||
**/lightning_logs/**
|
||||
|
||||
# Version Output
|
||||
pyannote/audio/version.py
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pyannote.database import get_protocol, FileFinder\n",
|
||||
"protocol = get_protocol('Debug.SpeakerDiarization.Debug', \n",
|
||||
" preprocessors={\"audio\": FileFinder()})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Train a model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pyannote.audio.tasks.voice_activity_detection.task import VoiceActivityDetection\n",
|
||||
"from pyannote.audio.models.debug import SimpleSegmentationModel\n",
|
||||
"import pytorch_lightning as pl\n",
|
||||
"\n",
|
||||
"vad = VoiceActivityDetection(protocol, duration=2., batch_size=32, num_workers=4)\n",
|
||||
"model = SimpleSegmentationModel(task=vad)\n",
|
||||
"trainer = pl.Trainer(max_epochs=1, default_root_dir='sharing/')\n",
|
||||
"_ = trainer.fit(model, vad)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Load a model without knowing its class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"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.ckpt')\n",
|
||||
"assert isinstance(model, SimpleSegmentationModel)\n",
|
||||
"\n",
|
||||
"# checkpoint should work with a URL as well (it relies on pl_load)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
@@ -20,10 +20,7 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from pkg_resources import get_distribution, DistributionNotFound
|
||||
|
||||
try:
|
||||
__version__ = get_distribution(__name__).version
|
||||
except DistributionNotFound:
|
||||
# package is not installed
|
||||
from .version import __version__, git_version # noqa: F401
|
||||
except ImportError:
|
||||
pass
|
||||
@@ -20,13 +20,18 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from typing import Optional
|
||||
import warnings
|
||||
from importlib import import_module
|
||||
from typing import Any, Dict, Optional, Text
|
||||
|
||||
import pytorch_lightning as pl
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from pyannote.audio import __version__
|
||||
from pyannote.audio.core.io import Audio
|
||||
from pyannote.audio.core.task import Problem, Task
|
||||
from pytorch_lightning.utilities.cloud_io import load as pl_load
|
||||
from semver import VersionInfo
|
||||
|
||||
|
||||
class Model(pl.LightningModule):
|
||||
@@ -76,6 +81,8 @@ class Model(pl.LightningModule):
|
||||
|
||||
if stage == "fit":
|
||||
|
||||
# TODO: move these to self.hparams.task
|
||||
|
||||
# keep track of the classes here because it is used
|
||||
# to setup the final classification layer (even when stage != fit)
|
||||
self.hparams.classes = self.task.specifications.classes
|
||||
@@ -107,7 +114,40 @@ class Model(pl.LightningModule):
|
||||
self.task.example_input_array()
|
||||
)
|
||||
|
||||
def on_load_checkpoint(self, checkpoint):
|
||||
@staticmethod
|
||||
def check_version(library: Text, theirs: Text, mine: Text):
|
||||
theirs = VersionInfo.parse(theirs)
|
||||
mine = VersionInfo.parse(mine)
|
||||
if theirs.major != mine.major:
|
||||
warnings.warn(
|
||||
f"Model was trained with {library} {theirs}, yours is {mine}. "
|
||||
f"Bad things will probably happen unless you update {library} to {theirs.major}.x."
|
||||
)
|
||||
if theirs.minor > mine.minor:
|
||||
warnings.warn(
|
||||
f"Model was trained with {library} {theirs}, yours is {mine}. "
|
||||
f"This should be OK but you might want to update {library}."
|
||||
)
|
||||
|
||||
def on_load_checkpoint(self, checkpoint: Dict[str, Any]):
|
||||
|
||||
try:
|
||||
self.check_version(
|
||||
"pyannote.audio",
|
||||
checkpoint["pyannote.audio"]["versions"]["pyannote.audio"],
|
||||
__version__,
|
||||
)
|
||||
except ValueError:
|
||||
warnings.warn("FIXME: could not check pyannote.audio version")
|
||||
|
||||
self.check_version(
|
||||
"torch",
|
||||
checkpoint["pyannote.audio"]["versions"]["torch"],
|
||||
torch.__version__,
|
||||
)
|
||||
self.check_version(
|
||||
"pytorch-lightning", checkpoint["pytorch-lightning_version"], pl.__version__
|
||||
)
|
||||
|
||||
# only hyper-parameters defined in __init__ are loaded automatically.
|
||||
# therefore, we have to manually load hyper-parameters that were
|
||||
@@ -122,6 +162,24 @@ class Model(pl.LightningModule):
|
||||
# we can actually setup() the model.
|
||||
self.setup()
|
||||
|
||||
def on_save_checkpoint(self, checkpoint: Dict[str, Any]):
|
||||
|
||||
# put everything pyannote.audio-specific under pyannote.audio
|
||||
# to avoid any future conflicts with pytorch-lightning updates
|
||||
checkpoint["pyannote.audio"] = {
|
||||
"versions": {
|
||||
"torch": torch.__version__,
|
||||
"pyannote.audio": __version__,
|
||||
},
|
||||
"model": {
|
||||
"module": self.__class__.__module__,
|
||||
"class": self.__class__.__name__,
|
||||
},
|
||||
}
|
||||
|
||||
# TODO give self.task a chance to save some hyper-parameters as well (e.g. chunk duration)
|
||||
# TODO self.task.on_save_checkpoint(checkpoint)
|
||||
|
||||
def forward(self, waveforms: torch.Tensor) -> torch.Tensor:
|
||||
msg = "Class {self.__class__.__name__} should define a `forward` method."
|
||||
raise NotImplementedError(msg)
|
||||
@@ -147,3 +205,29 @@ class Model(pl.LightningModule):
|
||||
# optimizer is defined by the task for the same reason as above
|
||||
def configure_optimizers(self):
|
||||
return self.task.configure_optimizers(self)
|
||||
|
||||
|
||||
def load_from_checkpoint(checkpoint_path: str, map_location=None) -> Model:
|
||||
"""Load model from checkpoint
|
||||
|
||||
Parameters
|
||||
----------
|
||||
checkpoint_path: str
|
||||
Path to checkpoint. This can also be a URL.
|
||||
|
||||
Returns
|
||||
-------
|
||||
model : Model
|
||||
Model
|
||||
"""
|
||||
|
||||
# obtain model class from the checkpoint
|
||||
checkpoint = pl_load(checkpoint_path, map_location=map_location)
|
||||
|
||||
module_name: str = checkpoint["pyannote.audio"]["model"]["module"]
|
||||
module = import_module(module_name)
|
||||
|
||||
class_name: str = checkpoint["pyannote.audio"]["model"]["class"]
|
||||
Klass: Model = getattr(module, class_name)
|
||||
|
||||
return Klass.load_from_checkpoint(checkpoint_path)
|
||||
+2
-1
@@ -3,8 +3,9 @@ librosa >= 0.8
|
||||
pyannote.core >= 4.1
|
||||
pyannote.database >= 4.0.1
|
||||
pyscaffold
|
||||
pytorch_lightning >= 1.0.0rc4
|
||||
pytorch_lightning >= 1.0.0
|
||||
pytorch_metric_learning >= 0.9.93
|
||||
semver >= 2.10.2
|
||||
soundfile >= 0.10.2
|
||||
torch >= 1.6
|
||||
torchaudio >= 0.6
|
||||
@@ -1,4 +1,6 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from pkg_resources import VersionConflict, require
|
||||
from setuptools import find_packages, setup
|
||||
@@ -16,13 +18,31 @@ except VersionConflict:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ROOT_DIR = Path(__file__).parent.resolve()
|
||||
# Creating the version file
|
||||
|
||||
with open("version.txt") as f:
|
||||
version = f.read()
|
||||
|
||||
version = version.strip()
|
||||
sha = "Unknown"
|
||||
|
||||
if os.getenv("BUILD_VERSION"):
|
||||
version = os.getenv("BUILD_VERSION")
|
||||
elif sha != "Unknown":
|
||||
version += "+" + sha[:7]
|
||||
print("-- Building version " + version)
|
||||
|
||||
version_path = ROOT_DIR / "pyannote" / "audio" / "version.py"
|
||||
|
||||
with open(version_path, "w") as f:
|
||||
f.write("__version__ = '{}'\n".format(version))
|
||||
|
||||
if __name__ == "__main__":
|
||||
setup(
|
||||
name="pyannote.audio",
|
||||
namespace_packages=["pyannote"],
|
||||
use_scm_version=True,
|
||||
setup_requires=["setuptools_scm"],
|
||||
version=version,
|
||||
packages=find_packages(),
|
||||
install_requires=requirements,
|
||||
description="Neural building blocks for speaker diarization",
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
0.0.1
|
||||
Reference in new issue
Block a user