Download code/extract_xvector.py from sendfuze/vipbench: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/datasets/sendfuze/vipbench/resolve/main/code/extract_xvector.py
- Command line
-
hf download hf://datasets/sendfuze/vipbench/code/extract_xvector.py
-
curl -L -o extract_xvector.py https://huggingface.co/datasets/sendfuze/vipbench/resolve/main/code/extract_xvector.py
1.38 kB
| #!/usr/bin/env python3 | |
| """Extract x-vector embeddings using SpeechBrain. | |
| Model: speechbrain/spkrec-xvect-voxceleb (supervised, softmax, 512-dim) | |
| - The classic TDNN x-vector architecture (Snyder et al., ICASSP 2018) | |
| - Trained on VoxCeleb1+2 with softmax loss | |
| - Statistics pooling (mean + std) for utterance-level embeddings | |
| Install: pip install speechbrain | |
| """ | |
| import argparse | |
| import torch | |
| import numpy as np | |
| from extraction_utils import load_audio, extract_all | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu") | |
| parser.add_argument("--base-dir", default=None) | |
| parser.add_argument("--output-dir", default=None) | |
| args = parser.parse_args() | |
| from speechbrain.inference.speaker import EncoderClassifier | |
| print(f"Loading x-vector on {args.device}...") | |
| classifier = EncoderClassifier.from_hparams( | |
| source="speechbrain/spkrec-xvect-voxceleb", | |
| run_opts={"device": args.device}, | |
| ) | |
| def model_fn(audio_path): | |
| audio = load_audio(audio_path, target_sr=16000) | |
| signal = torch.tensor(audio).unsqueeze(0).to(args.device) | |
| embedding = classifier.encode_batch(signal) | |
| return embedding.squeeze().cpu().numpy() | |
| extract_all(model_fn, "xvector", args.base_dir, args.output_dir) | |
| if __name__ == "__main__": | |
| main() | |