diff --git a/.gitignore b/.gitignore index 05a97e26..84925c64 100644 --- a/.gitignore +++ b/.gitignore @@ -82,3 +82,6 @@ gh-pages.pub .vscode/ **/lightning_logs/** + +# Version Output +pyannote/audio/version.py diff --git a/notebook/sharing.ipynb b/notebook/sharing.ipynb new file mode 100644 index 00000000..afe9b2a4 --- /dev/null +++ b/notebook/sharing.ipynb @@ -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 +} diff --git a/pyannote/audio/__init__.py b/pyannote/audio/__init__.py index 32b7e223..9c7e5c65 100644 --- a/pyannote/audio/__init__.py +++ b/pyannote/audio/__init__.py @@ -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 diff --git a/pyannote/audio/core/model.py b/pyannote/audio/core/model.py index d9828384..97448ee0 100644 --- a/pyannote/audio/core/model.py +++ b/pyannote/audio/core/model.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 6950d50e..be4eace4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py index ef7a84e1..bba8bb6e 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/version.txt b/version.txt new file mode 100644 index 00000000..8acdd82b --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.0.1