diff --git a/CHANGELOG.md b/CHANGELOG.md index 0169845d..47bec945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - fix(model): restore `fbank_only` mode in WeSpeaker embedding models - fix(task): avoid training crashes with `spawn`/`forkserver` workers, including Python 3.14's default on Linux, by warning and using `num_workers=0` +- fix(pipeline): fix `VBxClustering` crashing when more speakers are requested than there are embeddings to cluster [@MohammadHijjawi97](https://github.com/MohammadHijjawi97) - fix(pipeline): omit unset `token` and `cache_dir` when expanding `$model/` references so loaders can inherit runtime options - fix(model): honor the configured linear hidden size in `ToTaToNet` [@antonsoo](https://github.com/antonsoo) - fix(io): fix `Audio.crop` raising when an in-memory waveform is cropped up to its very end [@MohammadHijjawi97](https://github.com/MohammadHijjawi97) diff --git a/src/pyannote/audio/pipelines/clustering.py b/src/pyannote/audio/pipelines/clustering.py index f17e37d1..183c4c7d 100644 --- a/src/pyannote/audio/pipelines/clustering.py +++ b/src/pyannote/audio/pipelines/clustering.py @@ -584,7 +584,17 @@ class VBxClustering(BaseClustering): embeddings, segmentations=segmentations ) - if train_embeddings.shape[0] < 2: + num_embeddings, _ = train_embeddings.shape + + # there cannot be more clusters than training embeddings + num_clusters, min_clusters, max_clusters = self.set_num_clusters( + num_embeddings, + num_clusters=num_clusters, + min_clusters=min_clusters, + max_clusters=max_clusters, + ) + + if num_embeddings < 2: # do NOT apply clustering when the number of training embeddings is less than 2 num_chunks, num_speakers, _ = embeddings.shape hard_clusters = np.zeros((num_chunks, num_speakers), dtype=np.int8) diff --git a/tests/test_clustering.py b/tests/test_clustering.py index 535da47d..c3f81383 100644 --- a/tests/test_clustering.py +++ b/tests/test_clustering.py @@ -1,6 +1,7 @@ import numpy as np +from pyannote.core import SlidingWindow, SlidingWindowFeature -from pyannote.audio.pipelines.clustering import AgglomerativeClustering +from pyannote.audio.pipelines.clustering import AgglomerativeClustering, VBxClustering def test_agglomerative_clustering_num_cluster(): @@ -27,3 +28,47 @@ def test_agglomerative_clustering_num_cluster(): embeddings=embeddings, min_clusters=2, max_clusters=2, num_clusters=2 ) assert np.array_equal(clusters, np.array([0, 1])) + + +class IdentityPLDA: + """Minimal stand-in for PLDA (identity transform into PLDA space)""" + + def __init__(self, dimension: int): + self.phi = np.ones(dimension) + + def __call__(self, embeddings: np.ndarray) -> np.ndarray: + return embeddings + + +def test_vbx_clustering_more_clusters_than_embeddings(): + """ + Make sure VBxClustering does not crash when more clusters are requested + than there are embeddings to cluster (e.g. `num_speakers` larger than + what a short file can provide) and caps the number of clusters instead. + """ + + num_chunks, num_frames, num_speakers, dimension = 3, 10, 1, 4 + + # 3 (orthogonal) embeddings + embeddings = np.eye(dimension)[:num_chunks, np.newaxis, :] + segmentations = SlidingWindowFeature( + np.ones((num_chunks, num_frames, num_speakers)), + SlidingWindow(start=0.0, duration=10.0, step=1.0), + ) + + clustering = VBxClustering(plda=IdentityPLDA(dimension)).instantiate( + {"threshold": 0.6, "Fa": 0.07, "Fb": 0.8} + ) + + # request 5 clusters + hard_clusters, soft_clusters, centroids = clustering( + embeddings, + segmentations=segmentations, + num_clusters=5, + min_clusters=5, + max_clusters=5, + ) + + assert centroids.shape == (num_chunks, dimension) + assert soft_clusters.shape == (num_chunks, num_speakers, num_chunks) + assert np.array_equal(np.sort(hard_clusters.ravel()), np.arange(num_chunks))