fix(diarization): deterministic & robust offline VBx re-clustering (K-Means n_init)#735
Open
testfields wants to merge 1 commit into
Open
Conversation
…st-of-N) The offline VBx speaker-count adjustment (re-clustering detected clusters down to the constrained count) called KMeansClustering with a random seed and a single initialization. This is both non-deterministic and fragile: small/boundary speakers collapse run-to-run (observed on a 4-speaker meeting clip, cause-(ii) swinging ~10%↔~30% across runs; the smallest speaker's recall flips 80%↔0%). - KMeansClustering: default unseeded fallback now uses a fixed seed (0) instead of UInt64.random; add clusterWithCentroidsNInit which runs N deterministic initializations (seeds base..base+N-1) and returns the lowest-inertia result (sklearn-style n_init). - VBxClustering: the speaker-count re-clustering now uses n_init=10, baseSeed=0. Result: re-clustering is fully deterministic and robustly keeps fragile speakers (the 4-speaker clip now scores ~9.2% consistently across 5+ runs and on-device). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The offline diarization speaker-count adjustment — re-clustering VBx-detected clusters down to the constrained count (
numSpeakers/min–max) — callsKMeansClusteringwith a random seed and a single initialization. This makes the result both non-deterministic and fragile: small / boundary speakers collapse run-to-run.Reproduction
A 4-speaker Japanese meeting clip (~7 min, 16 kHz mono),
process --mode offline --num-speakers 4 --step-ratio 0.1, repeated runs on identical audio + config:The only thing varying between runs is the K-Means random seed; the smallest speaker flips between kept and merged-away.
Root cause
KMeansClustering.clusterWithCentroidsinitializes its RNG asSeededRNG(seed: seed ?? UInt64.random(in: 0...UInt64.max))→ a random seed whenever the caller doesn't pass one.VBxClusteringspeaker-count re-clustering (Speaker count N outside bounds […]; re-clustering to K) callsKMeansClustering.clusterWithCentroids(...)without a seed and with a single init (non_init/ best-of-N inertia selection).So the final hard assignment of an over-segmented frame set down to
Kspeakers depends on one random K-Means initialization, which is unstable for fragile / imbalanced speaker sets.This re-clustering path was introduced in #236 (which made the
numSpeakersconstraint actually apply the K-Means centroids); it simply never seeded the K-Means or usedn_init, so the constrained result was left non-deterministic.Fix
KMeansClustering:0) instead ofUInt64.random→ deterministic by default.clusterWithCentroidsNInit(embeddings:numClusters:maxIterations:nInit:baseSeed:)which runsnInitdeterministic initializations (seedsbaseSeed … baseSeed+nInit-1) and returns the lowest-inertia result (sklearn-stylen_init).VBxClustering: the speaker-count re-clustering now callsclusterWithCentroidsNInit(nInit: 10, baseSeed: 0).No breaking API changes —
clusterWithCentroidskeeps its signature (its unseeded path is just deterministic now);clusterWithCentroidsNInitis additive.Result
Re-clustering is now fully deterministic and robustly retains fragile speakers. The 4-speaker clip scores ~9.2% consistently across 5+ CLI runs and on-device (CoreML / ANE, sandboxed macOS app).
Related