feat(cli): add support for pretrained models (#602)

This commit is contained in:
Hervé BREDIN authored and GitHub committed 2021-01-28 14:15:03 +01:00
1 parent 54868ff99b
commit b6acc0b25a
5 files changed
+95 -5

No files matched your search

+27
View File
@@ -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",
]
+32
View File
@@ -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"))
+12 -5
View File
@@ -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)
@@ -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.
@@ -0,0 +1,3 @@
# @package _group_
_target_: pyannote.audio.cli.pretrained
checkpoint: ???