File size: 2,463 Bytes
1d5cf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Extract WeSpeaker ResNet34-LM embeddings through pyannote.audio.

Model: pyannote/wespeaker-voxceleb-resnet34-LM (supervised, ArcMargin/AAM-Softmax
with large-margin fine-tuning, VoxCeleb2 dev, 256-dim). This is the official
WeSpeaker ResNet34-LM checkpoint as packaged for pyannote.audio. One embedding
per file (window="whole"): 80-dim Kaldi fbank, per-utterance mean normalization,
ResNet34, temporal statistics pooling.

Install: pip install pyannote.audio==3.4.0
The released embeddings were extracted with pyannote.audio 3.4.0, torch 2.6.0
(CUDA 12.4) and checkpoint revision 837717ddb9ff5507820346191109dc79c958d614.

Usage:
    python3 extract_wespeaker.py
    python3 extract_wespeaker.py --audio-dir data/experiment2/audio \
        --output-dir ../data/experiment2/embeddings
"""

import argparse
import torch
from extraction_utils import load_audio, extract_all

CHECKPOINT = "pyannote/wespeaker-voxceleb-resnet34-LM"
REVISION = "837717ddb9ff5507820346191109dc79c958d614"


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)
    parser.add_argument("--audio-dir", default=None,
                        help="folder with reference/ and comparison/ (default: data/audio)")
    args = parser.parse_args()

    from pyannote.audio import Inference, Model
    from pyannote.audio.core.task import Problem, Resolution, Specifications
    from torch.torch_version import TorchVersion

    print(f"Loading {CHECKPOINT}@{REVISION} on {args.device}...")
    # torch>=2.6 loads checkpoints with weights_only=True; allowlist the four
    # non-tensor globals this checkpoint stores instead of disabling the check.
    with torch.serialization.safe_globals([Specifications, Problem, Resolution, TorchVersion]):
        model = Model.from_pretrained(f"{CHECKPOINT}@{REVISION}")
    inference = Inference(model, window="whole", device=torch.device(args.device))

    def model_fn(audio_path):
        audio = load_audio(audio_path, target_sr=16000)
        waveform = torch.from_numpy(audio).unsqueeze(0)  # (1, T)
        return inference({"waveform": waveform, "sample_rate": 16000})

    extract_all(model_fn, "wespeaker_resnet34_lm", args.base_dir, args.output_dir,
                args.audio_dir)


if __name__ == "__main__":
    main()