From 022d04036350b2fc0ee2a64f77549d32933a42f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Mon, 1 Dec 2025 12:13:17 +0100 Subject: [PATCH 1/6] feat(util): add `Calibration.safe_transform` method (#1959) --- CHANGELOG.md | 4 ++++ src/pyannote/audio/core/calibration.py | 28 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a767b539..9eb2dd1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## next + +- feat(util): add `Calibration.safe_transform` method (supports NaNs as well as any shape) + ## Version 4.0.2 (2025-11-19) - BREAKING(util): make `Binarize.__call__` return `string` tracks (instead of `int`) [@benniekiss](https://github.com/benniekiss/) diff --git a/src/pyannote/audio/core/calibration.py b/src/pyannote/audio/core/calibration.py index 632ff09c..6bfbca13 100644 --- a/src/pyannote/audio/core/calibration.py +++ b/src/pyannote/audio/core/calibration.py @@ -40,6 +40,34 @@ class Calibration(IsotonicRegression): def __init__(self): super().__init__(y_min=0.0, y_max=1.0, increasing="auto", out_of_bounds="clip") + def safe_transform( + self, + values: np.ndarray, + nan_value: float = 2.0, + ) -> np.ndarray: + """Apply calibration handling NaN values and any shape gracefully + + Parameters + ---------- + values : np.ndarray + Values to calibrate + nan_value : float, optional + Value to use in place of NaN values during calibration. Default is 2.0. + + Returns + ------- + calibrated_values : np.ndarray + Calibrated values + """ + # temporarily replace NaN values with `nan_value` so `transform()` does not fail + transformed = np.nan_to_num(values.reshape(-1), nan=nan_value) + + # apply calibration + transformed: np.ndarray = self.transform(transformed) + + # recover original shape + return transformed.reshape(values.shape) + def save(self, path: str): """Save fitted calibration to disk From 222c5b59ceecf359745222bd3a78c8eaf1c07b11 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 4 Dec 2025 09:28:51 +0100 Subject: [PATCH 2/6] fix(model): fix `Model.from_pretrained` to support `lightning` 2.6+ (#1962) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Hervé BREDIN Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- CHANGELOG.md | 1 + src/pyannote/audio/core/model.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eb2dd1d..73ceddcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## next - feat(util): add `Calibration.safe_transform` method (supports NaNs as well as any shape) +- fix(model): fix `Model.from_pretrained` to support `lightning` 2.6+ ## Version 4.0.2 (2025-11-19) diff --git a/src/pyannote/audio/core/model.py b/src/pyannote/audio/core/model.py index 8a2f9f2f..35904d61 100644 --- a/src/pyannote/audio/core/model.py +++ b/src/pyannote/audio/core/model.py @@ -599,7 +599,9 @@ class Model(lightning.LightningModule): map_location = default_map_location # load checkpoint using lightning - loaded_checkpoint = pl_load(path_to_model_checkpoint, map_location=map_location) + loaded_checkpoint = pl_load( + path_to_model_checkpoint, map_location=map_location, weights_only=False + ) # check that the checkpoint is compatible with the current version versions = loaded_checkpoint["pyannote.audio"]["versions"] @@ -620,6 +622,7 @@ class Model(lightning.LightningModule): path_to_model_checkpoint, map_location=map_location, strict=strict, + weights_only=False, **kwargs, ) except RuntimeError as e: @@ -638,6 +641,7 @@ class Model(lightning.LightningModule): path_to_model_checkpoint, map_location=map_location, strict=False, + weights_only=False, **kwargs, ) return model From 2941364af689b6a970c03f5c4ee20c65f4cb53d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Thu, 4 Dec 2025 09:35:52 +0100 Subject: [PATCH 3/6] feat(cli): add --revision option to most `pyannote.audio` commands (#1958) --- CHANGELOG.md | 1 + src/pyannote/audio/__main__.py | 116 ++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73ceddcd..31496ea5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## next +- feat(cli): add `--revision` option to most CLI commands - feat(util): add `Calibration.safe_transform` method (supports NaNs as well as any shape) - fix(model): fix `Model.from_pretrained` to support `lightning` 2.6+ diff --git a/src/pyannote/audio/__main__.py b/src/pyannote/audio/__main__.py index c828c7c7..e7620e44 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, Model +from pyannote.audio import Audio, Model, Pipeline from pyannote.core import Annotation from pyannote.metrics.base import BaseMetric from pyannote.metrics.diarization import DiarizationErrorRate, JaccardErrorRate @@ -285,16 +285,20 @@ def download( help="Pretrained pipeline (e.g. pyannote/speaker-diarization-community-1)" ), ], - token: Annotated[ - str, - typer.Argument( - help="Huggingface token to be used for downloading from Huggingface hub." - ), - ], - cache: Annotated[ - Path, + revision: Annotated[ + Optional[str], typer.Option( - help="Path to the folder where files downloaded from Huggingface hub are stored.", + help="Pretrained pipeline revision.", + ), + ] = None, + token: Annotated[ + Optional[str], + typer.Argument(help="Huggingface token."), + ] = None, + cache: Annotated[ + Optional[Path], + typer.Option( + help="Path to the folder where files downloaded from Huggingface are stored.", exists=True, dir_okay=True, file_okay=False, @@ -309,7 +313,7 @@ def download( # load pretrained pipeline pretrained_pipeline = Pipeline.from_pretrained( - pipeline, token=token, cache_dir=cache + pipeline, revision=revision, token=token, cache_dir=cache ) if pretrained_pipeline is None: print(f"Could not load pretrained pipeline from {pipeline}.") @@ -335,7 +339,7 @@ def apply( ), ], into: Annotated[ - Path, + Optional[Path], typer.Option( help="Path to file or directory where results are saved.", exists=False, @@ -345,13 +349,20 @@ def apply( resolve_path=True, ), ] = None, - device: Annotated[ - Device, typer.Option(help="Accelerator to use (CPU, CUDA, MPS)") - ] = Device.AUTO, - cache: Annotated[ - Path, + revision: Annotated[ + Optional[str], typer.Option( - help="Path to the folder where files downloaded from Huggingface hub are stored.", + help="Pretrained pipeline revision.", + ), + ] = None, + token: Annotated[ + Optional[str], + typer.Argument(help="Huggingface token."), + ] = None, + cache: Annotated[ + Optional[Path], + typer.Option( + help="Path to the folder where files downloaded from Huggingface are stored.", exists=True, dir_okay=True, file_okay=False, @@ -359,13 +370,18 @@ def apply( resolve_path=True, ), ] = None, + device: Annotated[ + Device, typer.Option(help="Accelerator to use (CPU, CUDA, MPS)") + ] = Device.AUTO, ): """ Apply a pretrained PIPELINE to an AUDIO file or directory """ # load pretrained pipeline - pretrained_pipeline = Pipeline.from_pretrained(pipeline, cache_dir=cache) + pretrained_pipeline = Pipeline.from_pretrained( + pipeline, revision=revision, token=token, cache_dir=cache + ) if pretrained_pipeline is None: print(f"Could not load pretrained pipeline from {pipeline}.") raise typer.exit(code=1) @@ -375,7 +391,6 @@ def apply( pretrained_pipeline.to(torch_device) if audio.is_dir(): - if into is None or not into.is_dir(): typer.echo("When AUDIO is a directory, INTO must also be a directory.") raise typer.exit(code=1) @@ -385,7 +400,6 @@ def apply( jsons: list[Path | None] = [into / (path.stem + ".json") for path in inputs] else: - if not (into is None or into.is_file()): typer.echo("When AUDIO is a file, INTO must also be a file.") raise typer.exit(code=1) @@ -395,7 +409,6 @@ def apply( jsons: list[Path | None] = [into.with_suffix(".json") if into else None] for current_input, current_rttm, current_json in zip(inputs, rttms, jsons): - prediction = pretrained_pipeline(current_input) speaker_diarization = get_diarization(prediction) @@ -522,6 +535,27 @@ def benchmark( case_sensitive=False, ), ] = Subset.test, + revision: Annotated[ + Optional[str], + typer.Option( + help="Pretrained pipeline revision.", + ), + ] = None, + token: Annotated[ + Optional[str], + typer.Argument(help="Huggingface token."), + ] = None, + cache: Annotated[ + Optional[Path], + typer.Option( + help="Path to the folder where files downloaded from Huggingface are stored.", + exists=True, + dir_okay=True, + file_okay=False, + writable=True, + resolve_path=True, + ), + ] = None, device: Annotated[ Device, typer.Option(help="Accelerator to use (CPU, CUDA, MPS)") ] = Device.AUTO, @@ -538,17 +572,6 @@ def benchmark( num_speakers: Annotated[ NumSpeakers, typer.Option(help="Number of speakers (oracle or auto)") ] = NumSpeakers.AUTO, - cache: Annotated[ - Path, - typer.Option( - help="Path to the folder where files downloaded from Huggingface hub are stored.", - exists=True, - dir_okay=True, - file_okay=False, - writable=True, - resolve_path=True, - ), - ] = None, optimize: Annotated[ bool, typer.Option( @@ -562,10 +585,7 @@ def benchmark( ), ] = False, per_file: Annotated[ - bool, - typer.Option( - help="Save one RTTM/JSON file per processed audio file." - ) + bool, typer.Option(help="Save one RTTM/JSON file per processed audio file.") ] = False, ): """ @@ -578,7 +598,9 @@ def benchmark( """ # load pretrained pipeline - pretrained_pipeline = Pipeline.from_pretrained(pipeline, cache_dir=cache) + pretrained_pipeline = Pipeline.from_pretrained( + pipeline, revision=revision, token=token, cache_dir=cache, + ) if pretrained_pipeline is None: print(f"Could not load pretrained pipeline from {pipeline}.") raise typer.exit(code=1) @@ -808,7 +830,9 @@ def benchmark( yaml.dump({"min_duration_off": best_min_duration_off}, yml) if not per_file: - optimized_rttm_file = into / f"{benchmark_name}.OptimizedMinDurationOff.rttm" + optimized_rttm_file = ( + into / f"{benchmark_name}.OptimizedMinDurationOff.rttm" + ) # make sure we don't overwrite previous results if optimized_rttm_file.exists(): @@ -816,7 +840,9 @@ def benchmark( for file in files: if per_file: - optimized_rttm_file = rttm_dir / f"{file['uri']}.OptimizedMinDurationOff.rttm" + optimized_rttm_file = ( + rttm_dir / f"{file['uri']}.OptimizedMinDurationOff.rttm" + ) with open(optimized_rttm_file, "w" if per_file else "a") as rttm: file["best_speaker_diarization"].write_rttm(rttm) @@ -851,11 +877,11 @@ def strip( """ 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 + "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( From 8ec12c0b89809361d6ece4f51192aafe2a424727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Sun, 7 Dec 2025 07:36:59 +0100 Subject: [PATCH 4/6] setup: update to pyannote.database >= 6.1.1 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 7f86184b..8f45f3de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ dependencies = [ "opentelemetry-sdk>=1.34.0", "opentelemetry-exporter-otlp>=1.34.0", "pyannote-core>=6.0.1", - "pyannote-database>=6.0.0", + "pyannote-database>=6.1.1", "pyannote-metrics>=4.0.0", "pyannote-pipeline>=4.0.0", "pytorch-metric-learning>=2.8.1", From ff553dd0c64c5b17a23e41e198726df4ee7c7112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Sun, 7 Dec 2025 07:38:02 +0100 Subject: [PATCH 5/6] doc: update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31496ea5..35ab65c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - feat(cli): add `--revision` option to most CLI commands - feat(util): add `Calibration.safe_transform` method (supports NaNs as well as any shape) - fix(model): fix `Model.from_pretrained` to support `lightning` 2.6+ +- setup: update `pyannote-database` dependency to `6.1+` ## Version 4.0.2 (2025-11-19) From 7ce1f69b42b76d8287921b3bc402d48f3a79c1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Sun, 7 Dec 2025 07:42:39 +0100 Subject: [PATCH 6/6] doc: update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35ab65c0..95a90213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## next +## Version 4.0.3 (2025-12-07) - feat(cli): add `--revision` option to most CLI commands - feat(util): add `Calibration.safe_transform` method (supports NaNs as well as any shape)