wq2012/SpectralCluster

Python re-implementation of the (constrained) spectral clustering algorithms used in Google's speaker diarization papers.

What it solves

Speaker diarization is the task of determining who spoke when in an audio recording. This library provides spectral clustering algorithms that group similar voice embeddings into speaker clusters, a key step in diarization. It also adds advanced techniques like constrained clustering based on speaker turns and efficient streaming clustering.

How it works

The core is the SpectralClusterer class with a predict method that takes a numpy array of embeddings and returns cluster labels. It builds an affinity matrix, optionally refines it (e.g., Gaussian blur, thresholding), computes a Laplacian matrix, performs eigen decomposition, and runs K-Means. The library supports many configuration options: different Laplacian types, distance metrics, auto-tuning of thresholds, fallback clusterers for small datasets, and hierarchical pre-clustering to speed up large inputs. It also includes constrained spectral clustering using a constraint matrix from speaker turn scores, and a MultiStageClusterer for streaming predictions where each new embedding updates labels for all previous inputs.

Who it’s for

This library is aimed at developers and researchers working on speaker diarization systems, or anyone needing a flexible, well-tested spectral clustering implementation with multiple advanced features. It is a demonstration of algorithms from academic papers and is not the original production code from Google, so it should be used as a reference or starting point rather than a drop-in production solution.

Highlights

  • Implements algorithms from three papers: refined Laplacian, constrained spectral clustering, and multi-stage clustering.
  • Simple predict API with many customization options.
  • Supports refinement operations on the affinity matrix (Gaussian blur, thresholding).
  • Multiple Laplacian matrix types (None, unnormalized, graph cut, random walk).
  • Custom distance metrics for K-Means (cosine, euclidean, etc.).
  • Auto-tune feature for the p_percentile parameter.
  • Fallback clusterer for small datasets and single-cluster decisions.
  • Speed-up via hierarchical pre-clustering (max_spectral_size).
  • Constrained clustering with speaker turn information.
  • Streaming multi-stage clustering that updates labels incrementally.

Related

  • Project
  • Project
  • Project
  • Project