diff --git a/pyannote/audio/cli/__init__.py b/pyannote/audio/cli/__init__.py index e69de29b..61d98b82 100644 --- a/pyannote/audio/cli/__init__.py +++ b/pyannote/audio/cli/__init__.py @@ -0,0 +1,27 @@ +# MIT License +# +# 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 +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from .pretrained import pretrained + +__all__ = [ + "pretrained", +] diff --git a/pyannote/audio/cli/pretrained.py b/pyannote/audio/cli/pretrained.py new file mode 100644 index 00000000..b3a2a73d --- /dev/null +++ b/pyannote/audio/cli/pretrained.py @@ -0,0 +1,32 @@ +# MIT License +# +# 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 +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +from typing import Text + +import torch + +from pyannote.audio import Model + + +def pretrained(checkpoint: Text): + return Model.from_pretrained(checkpoint, map_location=torch.device("cpu")) diff --git a/pyannote/audio/cli/train.py b/pyannote/audio/cli/train.py index 60474a4c..c68aaeaf 100644 --- a/pyannote/audio/cli/train.py +++ b/pyannote/audio/cli/train.py @@ -42,6 +42,12 @@ from pyannote.database import FileFinder, get_protocol @hydra.main(config_path="train_config", config_name="config") def main(cfg: DictConfig) -> Optional[float]: + if cfg.trainer.get("resume_from_checkpoint", None) is not None: + raise ValueError( + "trainer.resume_from_checkpoint is not supported. " + "use model=pretrained model.checkpoint=... instead." + ) + # make sure to set the random seed before the instantiation of Trainer # so that each model initializes with the same weights when using DDP. seed = int(os.environ.get("PL_GLOBAL_SEED", "0")) @@ -63,16 +69,16 @@ def main(cfg: DictConfig) -> Optional[float]: else None ) - # instantiate task and model + # instantiate task task = instantiate(cfg.task, protocol, augmentation=augmentation) - model = instantiate(cfg.model, task=task) + + model = instantiate(cfg.model) + model.task = task # setup optimizer and scheduler task.setup(stage="fit") model.setup(stage="fit") - # TODO: catch resume_from_checkpoint before it is too late (i.e. before it overrides optimizer) - model.optimizer = instantiate(cfg.optimizer, model.parameters()) monitor, direction = task.val_monitor @@ -85,7 +91,7 @@ def main(cfg: DictConfig) -> Optional[float]: "scheduler": ReduceLROnPlateau( model.optimizer, mode=direction, - factor=0.1, + factor=0.5, patience=20, threshold=0.0001, threshold_mode="rel", @@ -138,6 +144,7 @@ def main(cfg: DictConfig) -> Optional[float]: ) # TODO: defaults to one-GPU training (one GPU is available) + trainer = instantiate(cfg.trainer, callbacks=callbacks, logger=logger) trainer.fit(model) diff --git a/pyannote/audio/cli/train_config/__init__.py b/pyannote/audio/cli/train_config/__init__.py index e69de29b..85cd53e5 100644 --- a/pyannote/audio/cli/train_config/__init__.py +++ b/pyannote/audio/cli/train_config/__init__.py @@ -0,0 +1,21 @@ +# MIT License +# +# 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 +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. diff --git a/pyannote/audio/cli/train_config/model/pretrained.yaml b/pyannote/audio/cli/train_config/model/pretrained.yaml new file mode 100644 index 00000000..34dfa967 --- /dev/null +++ b/pyannote/audio/cli/train_config/model/pretrained.yaml @@ -0,0 +1,3 @@ +# @package _group_ +_target_: pyannote.audio.cli.pretrained +checkpoint: ???