diff --git a/CHANGELOG.md b/CHANGELOG.md index 17a6f9c9..30d7dc7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,8 +54,9 @@ Models can now be stored alongside their pipelines in the same repository, strea - feat(utils): add `FilterByNumberOfSpeakers` protocol files filter - feat(core): add `Calibration` class to calibrate logits/distances into probabilities - feat(metric): add `DetectionErrorRate`, `SegmentationErrorRate`, `DiarizationPrecision`, and `DiarizationRecall` metrics -- feat(cli): add CLI to download, apply, benchmark, or optimize pipelines - +- feat(cli): add CLI to download, apply, benchmark, and optimize pipelines +- feat(cli): add CLI to strip checkpoints to their bare inference minimum + ### Improvements - improve(model): improve WavLM (un)freezing support for `SSeRiouSS` architecture ([@clement-pages](https://github.com/clement-pages/)) diff --git a/src/pyannote/audio/__main__.py b/src/pyannote/audio/__main__.py index 2eee7e07..94600d5d 100644 --- a/src/pyannote/audio/__main__.py +++ b/src/pyannote/audio/__main__.py @@ -40,7 +40,7 @@ import pyannote.database import torch import typer import yaml -from pyannote.audio import Audio, Pipeline +from pyannote.audio import Audio, Pipeline, Model from pyannote.core import Annotation from pyannote.metrics.base import BaseMetric from pyannote.metrics.diarization import DiarizationErrorRate, JaccardErrorRate @@ -759,5 +759,57 @@ def benchmark( file["best_speaker_diarization"].write_rttm(rttm) +@app.command("strip") +def strip( + checkpoint: Annotated[ + Path, + typer.Argument( + help="Path to pyannote.audio model checkpoint", + exists=True, + dir_okay=False, + file_okay=True, + resolve_path=True, + ), + ], + into: Annotated[ + Path, + typer.Argument( + help="Path to the stripped checkpoint", + exists=False, + dir_okay=False, + file_okay=True, + writable=True, + resolve_path=True, + ), + ], +): + """ + Strip a pretrained CHECKPOINT to only keep the parts needed for inference. + """ + + keys = [ + "pytorch-lightning_version", # * pytorch-lightning needs + "hparams_name", # those values to initialize + "hyper_parameters", # the model architecture + "state_dict", # * actual weights + "pyannote.audio", # * pyannote.audio dependencies + ] + + old_checkpoint = torch.load( + checkpoint, map_location=torch.device("cpu"), weights_only=False + ) + new_checkpoint = { + key: value for key, value in old_checkpoint.items() if key in keys + } + torch.save(new_checkpoint, into) + + # check that the stripped checkpoint can be loaded again + try: + _ = Model.from_pretrained(into) + except Exception as e: + sys.exit( + f"Something went wrong while stripping the checkpoint as it could not be reloaded: {e}" + ) + if __name__ == "__main__": app()