-
-
Notifications
You must be signed in to change notification settings - Fork 705
/
speaker_verification.py
883 lines (711 loc) · 27.7 KB
/
speaker_verification.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# MIT License
#
# Copyright (c) 2021 CNRS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import warnings
from functools import cached_property
from pathlib import Path
from typing import Text, Union
import numpy as np
import torch
import torch.nn.functional as F
import torchaudio
import torchaudio.compliance.kaldi as kaldi
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import RepositoryNotFoundError
from torch.nn.utils.rnn import pad_sequence
from pyannote.audio import Inference, Model, Pipeline
from pyannote.audio.core.inference import BaseInference
from pyannote.audio.core.io import AudioFile
from pyannote.audio.core.model import CACHE_DIR
from pyannote.audio.pipelines.utils import PipelineModel, get_model
backend = torchaudio.get_audio_backend()
try:
from speechbrain.pretrained import (
EncoderClassifier as SpeechBrain_EncoderClassifier,
)
SPEECHBRAIN_IS_AVAILABLE = True
except ImportError:
SPEECHBRAIN_IS_AVAILABLE = False
finally:
torchaudio.set_audio_backend(backend)
try:
from nemo.collections.asr.models import (
EncDecSpeakerLabelModel as NeMo_EncDecSpeakerLabelModel,
)
NEMO_IS_AVAILABLE = True
except ImportError:
NEMO_IS_AVAILABLE = False
try:
import onnxruntime as ort
ONNX_IS_AVAILABLE = True
except ImportError:
ONNX_IS_AVAILABLE = False
class NeMoPretrainedSpeakerEmbedding(BaseInference):
def __init__(
self,
embedding: Text = "nvidia/speakerverification_en_titanet_large",
device: torch.device = None,
):
if not NEMO_IS_AVAILABLE:
raise ImportError(
f"'NeMo' must be installed to use '{embedding}' embeddings. "
"Visit https://nvidia.github.io/NeMo/ for installation instructions."
)
super().__init__()
self.embedding = embedding
self.device = device or torch.device("cpu")
self.model_ = NeMo_EncDecSpeakerLabelModel.from_pretrained(self.embedding)
self.model_.freeze()
self.model_.to(self.device)
def to(self, device: torch.device):
if not isinstance(device, torch.device):
raise TypeError(
f"`device` must be an instance of `torch.device`, got `{type(device).__name__}`"
)
self.model_.to(device)
self.device = device
return self
@cached_property
def sample_rate(self) -> int:
return self.model_._cfg.train_ds.get("sample_rate", 16000)
@cached_property
def dimension(self) -> int:
input_signal = torch.rand(1, self.sample_rate).to(self.device)
input_signal_length = torch.tensor([self.sample_rate]).to(self.device)
_, embeddings = self.model_(
input_signal=input_signal, input_signal_length=input_signal_length
)
_, dimension = embeddings.shape
return dimension
@cached_property
def metric(self) -> str:
return "cosine"
@cached_property
def min_num_samples(self) -> int:
lower, upper = 2, round(0.5 * self.sample_rate)
middle = (lower + upper) // 2
while lower + 1 < upper:
try:
input_signal = torch.rand(1, middle).to(self.device)
input_signal_length = torch.tensor([middle]).to(self.device)
_ = self.model_(
input_signal=input_signal, input_signal_length=input_signal_length
)
upper = middle
except RuntimeError:
lower = middle
middle = (lower + upper) // 2
return upper
def __call__(
self, waveforms: torch.Tensor, masks: torch.Tensor = None
) -> np.ndarray:
"""
Parameters
----------
waveforms : (batch_size, num_channels, num_samples)
Only num_channels == 1 is supported.
masks : (batch_size, num_samples), optional
Returns
-------
embeddings : (batch_size, dimension)
"""
batch_size, num_channels, num_samples = waveforms.shape
assert num_channels == 1
waveforms = waveforms.squeeze(dim=1)
if masks is None:
signals = waveforms.squeeze(dim=1)
wav_lens = signals.shape[1] * torch.ones(batch_size)
else:
batch_size_masks, _ = masks.shape
assert batch_size == batch_size_masks
# TODO: speed up the creation of "signals"
# preliminary profiling experiments show
# that it accounts for 15% of __call__
# (the remaining 85% being the actual forward pass)
imasks = F.interpolate(
masks.unsqueeze(dim=1), size=num_samples, mode="nearest"
).squeeze(dim=1)
imasks = imasks > 0.5
signals = pad_sequence(
[waveform[imask] for waveform, imask in zip(waveforms, imasks)],
batch_first=True,
)
wav_lens = imasks.sum(dim=1)
max_len = wav_lens.max()
# corner case: every signal is too short
if max_len < self.min_num_samples:
return np.NAN * np.zeros((batch_size, self.dimension))
too_short = wav_lens < self.min_num_samples
wav_lens[too_short] = max_len
_, embeddings = self.model_(
input_signal=waveforms.to(self.device),
input_signal_length=wav_lens.to(self.device),
)
embeddings = embeddings.cpu().numpy()
embeddings[too_short.cpu().numpy()] = np.NAN
return embeddings
class SpeechBrainPretrainedSpeakerEmbedding(BaseInference):
"""Pretrained SpeechBrain speaker embedding
Parameters
----------
embedding : str
Name of SpeechBrain model
device : torch.device, optional
Device
use_auth_token : str, optional
When loading private huggingface.co models, set `use_auth_token`
to True or to a string containing your hugginface.co authentication
token that can be obtained by running `huggingface-cli login`
Usage
-----
>>> get_embedding = SpeechBrainPretrainedSpeakerEmbedding("speechbrain/spkrec-ecapa-voxceleb")
>>> assert waveforms.ndim == 3
>>> batch_size, num_channels, num_samples = waveforms.shape
>>> assert num_channels == 1
>>> embeddings = get_embedding(waveforms)
>>> assert embeddings.ndim == 2
>>> assert embeddings.shape[0] == batch_size
>>> assert binary_masks.ndim == 1
>>> assert binary_masks.shape[0] == batch_size
>>> embeddings = get_embedding(waveforms, masks=binary_masks)
"""
def __init__(
self,
embedding: Text = "speechbrain/spkrec-ecapa-voxceleb",
device: torch.device = None,
use_auth_token: Union[Text, None] = None,
):
if not SPEECHBRAIN_IS_AVAILABLE:
raise ImportError(
f"'speechbrain' must be installed to use '{embedding}' embeddings. "
"Visit https://speechbrain.github.io for installation instructions."
)
super().__init__()
if "@" in embedding:
self.embedding = embedding.split("@")[0]
self.revision = embedding.split("@")[1]
else:
self.embedding = embedding
self.revision = None
self.device = device or torch.device("cpu")
self.use_auth_token = use_auth_token
self.classifier_ = SpeechBrain_EncoderClassifier.from_hparams(
source=self.embedding,
savedir=f"{CACHE_DIR}/speechbrain",
run_opts={"device": self.device},
use_auth_token=self.use_auth_token,
revision=self.revision,
)
def to(self, device: torch.device):
if not isinstance(device, torch.device):
raise TypeError(
f"`device` must be an instance of `torch.device`, got `{type(device).__name__}`"
)
self.classifier_ = SpeechBrain_EncoderClassifier.from_hparams(
source=self.embedding,
savedir=f"{CACHE_DIR}/speechbrain",
run_opts={"device": device},
use_auth_token=self.use_auth_token,
revision=self.revision,
)
self.device = device
return self
@cached_property
def sample_rate(self) -> int:
return self.classifier_.audio_normalizer.sample_rate
@cached_property
def dimension(self) -> int:
dummy_waveforms = torch.rand(1, 16000).to(self.device)
*_, dimension = self.classifier_.encode_batch(dummy_waveforms).shape
return dimension
@cached_property
def metric(self) -> str:
return "cosine"
@cached_property
def min_num_samples(self) -> int:
with torch.inference_mode():
lower, upper = 2, round(0.5 * self.sample_rate)
middle = (lower + upper) // 2
while lower + 1 < upper:
try:
_ = self.classifier_.encode_batch(
torch.randn(1, middle).to(self.device)
)
upper = middle
except RuntimeError:
lower = middle
middle = (lower + upper) // 2
return upper
def __call__(
self, waveforms: torch.Tensor, masks: torch.Tensor = None
) -> np.ndarray:
"""
Parameters
----------
waveforms : (batch_size, num_channels, num_samples)
Only num_channels == 1 is supported.
masks : (batch_size, num_samples), optional
Returns
-------
embeddings : (batch_size, dimension)
"""
batch_size, num_channels, num_samples = waveforms.shape
assert num_channels == 1
waveforms = waveforms.squeeze(dim=1)
if masks is None:
signals = waveforms.squeeze(dim=1)
wav_lens = signals.shape[1] * torch.ones(batch_size)
else:
batch_size_masks, _ = masks.shape
assert batch_size == batch_size_masks
# TODO: speed up the creation of "signals"
# preliminary profiling experiments show
# that it accounts for 15% of __call__
# (the remaining 85% being the actual forward pass)
imasks = F.interpolate(
masks.unsqueeze(dim=1), size=num_samples, mode="nearest"
).squeeze(dim=1)
imasks = imasks > 0.5
signals = pad_sequence(
[
waveform[imask].contiguous()
for waveform, imask in zip(waveforms, imasks)
],
batch_first=True,
)
wav_lens = imasks.sum(dim=1)
max_len = wav_lens.max()
# corner case: every signal is too short
if max_len < self.min_num_samples:
return np.NAN * np.zeros((batch_size, self.dimension))
too_short = wav_lens < self.min_num_samples
wav_lens = wav_lens / max_len
wav_lens[too_short] = 1.0
embeddings = (
self.classifier_.encode_batch(signals, wav_lens=wav_lens)
.squeeze(dim=1)
.cpu()
.numpy()
)
embeddings[too_short.cpu().numpy()] = np.NAN
return embeddings
class WeSpeakerPretrainedSpeakerEmbedding(BaseInference):
"""Pretrained WeSpeaker speaker embedding
Parameters
----------
embedding : str
Path to WeSpeaker pretrained speaker embedding
device : torch.device, optional
Device
Usage
-----
>>> get_embedding = WeSpeakerPretrainedSpeakerEmbedding("hbredin/wespeaker-voxceleb-resnet34-LM")
>>> assert waveforms.ndim == 3
>>> batch_size, num_channels, num_samples = waveforms.shape
>>> assert num_channels == 1
>>> embeddings = get_embedding(waveforms)
>>> assert embeddings.ndim == 2
>>> assert embeddings.shape[0] == batch_size
>>> assert binary_masks.ndim == 1
>>> assert binary_masks.shape[0] == batch_size
>>> embeddings = get_embedding(waveforms, masks=binary_masks)
"""
def __init__(
self,
embedding: Text = "hbredin/wespeaker-voxceleb-resnet34-LM",
device: torch.device = None,
):
if not ONNX_IS_AVAILABLE:
raise ImportError(
f"'onnxruntime' must be installed to use '{embedding}' embeddings. "
)
super().__init__()
if not Path(embedding).exists():
try:
embedding = hf_hub_download(
repo_id=embedding,
filename="speaker-embedding.onnx",
)
except RepositoryNotFoundError:
raise ValueError(
f"Could not find '{embedding}' on huggingface.co nor on local disk."
)
self.embedding = embedding
self.to(device or torch.device("cpu"))
def to(self, device: torch.device):
if not isinstance(device, torch.device):
raise TypeError(
f"`device` must be an instance of `torch.device`, got `{type(device).__name__}`"
)
if device.type == "cpu":
providers = ["CPUExecutionProvider"]
elif device.type == "cuda":
providers = [
(
"CUDAExecutionProvider",
{
"cudnn_conv_algo_search": "DEFAULT", # EXHAUSTIVE / HEURISTIC / DEFAULT
},
)
]
else:
warnings.warn(
f"Unsupported device type: {device.type}, falling back to CPU"
)
device = torch.device("cpu")
providers = ["CPUExecutionProvider"]
sess_options = ort.SessionOptions()
sess_options.inter_op_num_threads = 1
sess_options.intra_op_num_threads = 1
self.session_ = ort.InferenceSession(
self.embedding, sess_options=sess_options, providers=providers
)
self.device = device
return self
@cached_property
def sample_rate(self) -> int:
return 16000
@cached_property
def dimension(self) -> int:
dummy_waveforms = torch.rand(1, 1, 16000)
features = self.compute_fbank(dummy_waveforms)
embeddings = self.session_.run(
output_names=["embs"], input_feed={"feats": features.numpy()}
)[0]
_, dimension = embeddings.shape
return dimension
@cached_property
def metric(self) -> str:
return "cosine"
@cached_property
def min_num_samples(self) -> int:
lower, upper = 2, round(0.5 * self.sample_rate)
middle = (lower + upper) // 2
while lower + 1 < upper:
try:
features = self.compute_fbank(torch.randn(1, 1, middle))
except AssertionError:
lower = middle
middle = (lower + upper) // 2
continue
embeddings = self.session_.run(
output_names=["embs"], input_feed={"feats": features.numpy()}
)[0]
if np.any(np.isnan(embeddings)):
lower = middle
else:
upper = middle
middle = (lower + upper) // 2
return upper
@cached_property
def min_num_frames(self) -> int:
return self.compute_fbank(torch.randn(1, 1, self.min_num_samples)).shape[1]
def compute_fbank(
self,
waveforms: torch.Tensor,
num_mel_bins: int = 80,
frame_length: int = 25,
frame_shift: int = 10,
dither: float = 0.0,
) -> torch.Tensor:
"""Extract fbank features
Parameters
----------
waveforms : (batch_size, num_channels, num_samples)
Returns
-------
fbank : (batch_size, num_frames, num_mel_bins)
Source: https://github.com/wenet-e2e/wespeaker/blob/45941e7cba2c3ea99e232d02bedf617fc71b0dad/wespeaker/bin/infer_onnx.py#L30C1-L50
"""
waveforms = waveforms * (1 << 15)
features = torch.stack(
[
kaldi.fbank(
waveform,
num_mel_bins=num_mel_bins,
frame_length=frame_length,
frame_shift=frame_shift,
dither=dither,
sample_frequency=self.sample_rate,
window_type="hamming",
use_energy=False,
)
for waveform in waveforms
]
)
return features - torch.mean(features, dim=1, keepdim=True)
def __call__(
self, waveforms: torch.Tensor, masks: torch.Tensor = None
) -> np.ndarray:
"""
Parameters
----------
waveforms : (batch_size, num_channels, num_samples)
Only num_channels == 1 is supported.
masks : (batch_size, num_samples), optional
Returns
-------
embeddings : (batch_size, dimension)
"""
batch_size, num_channels, num_samples = waveforms.shape
assert num_channels == 1
features = self.compute_fbank(waveforms)
_, num_frames, _ = features.shape
if masks is None:
embeddings = self.session_.run(
output_names=["embs"], input_feed={"feats": features.numpy()}
)[0]
return embeddings
batch_size_masks, _ = masks.shape
assert batch_size == batch_size_masks
imasks = F.interpolate(
masks.unsqueeze(dim=1), size=num_frames, mode="nearest"
).squeeze(dim=1)
imasks = imasks > 0.5
embeddings = np.NAN * np.zeros((batch_size, self.dimension))
for f, (feature, imask) in enumerate(zip(features, imasks)):
masked_feature = feature[imask]
if masked_feature.shape[0] < self.min_num_frames:
continue
embeddings[f] = self.session_.run(
output_names=["embs"],
input_feed={"feats": masked_feature.numpy()[None]},
)[0][0]
return embeddings
class PyannoteAudioPretrainedSpeakerEmbedding(BaseInference):
"""Pretrained pyannote.audio speaker embedding
Parameters
----------
embedding : PipelineModel
pyannote.audio model
device : torch.device, optional
Device
use_auth_token : str, optional
When loading private huggingface.co models, set `use_auth_token`
to True or to a string containing your hugginface.co authentication
token that can be obtained by running `huggingface-cli login`
Usage
-----
>>> get_embedding = PyannoteAudioPretrainedSpeakerEmbedding("pyannote/embedding")
>>> assert waveforms.ndim == 3
>>> batch_size, num_channels, num_samples = waveforms.shape
>>> assert num_channels == 1
>>> embeddings = get_embedding(waveforms)
>>> assert embeddings.ndim == 2
>>> assert embeddings.shape[0] == batch_size
>>> assert masks.ndim == 1
>>> assert masks.shape[0] == batch_size
>>> embeddings = get_embedding(waveforms, masks=masks)
"""
def __init__(
self,
embedding: PipelineModel = "pyannote/embedding",
device: torch.device = None,
use_auth_token: Union[Text, None] = None,
):
super().__init__()
self.embedding = embedding
self.device = device or torch.device("cpu")
self.model_: Model = get_model(self.embedding, use_auth_token=use_auth_token)
self.model_.eval()
self.model_.to(self.device)
def to(self, device: torch.device):
if not isinstance(device, torch.device):
raise TypeError(
f"`device` must be an instance of `torch.device`, got `{type(device).__name__}`"
)
self.model_.to(device)
self.device = device
return self
@cached_property
def sample_rate(self) -> int:
return self.model_.audio.sample_rate
@cached_property
def dimension(self) -> int:
return self.model_.example_output.dimension
@cached_property
def metric(self) -> str:
return "cosine"
@cached_property
def min_num_samples(self) -> int:
with torch.inference_mode():
lower, upper = 2, round(0.5 * self.sample_rate)
middle = (lower + upper) // 2
while lower + 1 < upper:
try:
_ = self.model_(torch.randn(1, 1, middle).to(self.device))
upper = middle
except RuntimeError:
lower = middle
middle = (lower + upper) // 2
return upper
def __call__(
self, waveforms: torch.Tensor, masks: torch.Tensor = None
) -> np.ndarray:
with torch.inference_mode():
if masks is None:
embeddings = self.model_(waveforms.to(self.device))
else:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
embeddings = self.model_(
waveforms.to(self.device), weights=masks.to(self.device)
)
return embeddings.cpu().numpy()
def PretrainedSpeakerEmbedding(
embedding: PipelineModel,
device: torch.device = None,
use_auth_token: Union[Text, None] = None,
):
"""Pretrained speaker embedding
Parameters
----------
embedding : Text
Can be a SpeechBrain (e.g. "speechbrain/spkrec-ecapa-voxceleb")
or a pyannote.audio model.
device : torch.device, optional
Device
use_auth_token : str, optional
When loading private huggingface.co models, set `use_auth_token`
to True or to a string containing your hugginface.co authentication
token that can be obtained by running `huggingface-cli login`
Usage
-----
>>> get_embedding = PretrainedSpeakerEmbedding("pyannote/embedding")
>>> get_embedding = PretrainedSpeakerEmbedding("speechbrain/spkrec-ecapa-voxceleb")
>>> get_embedding = PretrainedSpeakerEmbedding("nvidia/speakerverification_en_titanet_large")
>>> assert waveforms.ndim == 3
>>> batch_size, num_channels, num_samples = waveforms.shape
>>> assert num_channels == 1
>>> embeddings = get_embedding(waveforms)
>>> assert embeddings.ndim == 2
>>> assert embeddings.shape[0] == batch_size
>>> assert masks.ndim == 1
>>> assert masks.shape[0] == batch_size
>>> embeddings = get_embedding(waveforms, masks=masks)
"""
if isinstance(embedding, str) and "speechbrain" in embedding:
return SpeechBrainPretrainedSpeakerEmbedding(
embedding, device=device, use_auth_token=use_auth_token
)
elif isinstance(embedding, str) and "nvidia" in embedding:
return NeMoPretrainedSpeakerEmbedding(embedding, device=device)
elif isinstance(embedding, str) and "wespeaker" in embedding:
return WeSpeakerPretrainedSpeakerEmbedding(embedding, device=device)
else:
return PyannoteAudioPretrainedSpeakerEmbedding(
embedding, device=device, use_auth_token=use_auth_token
)
class SpeakerEmbedding(Pipeline):
"""Speaker embedding pipeline
This pipeline assumes that each file contains exactly one speaker
and extracts one single embedding from the whole file.
Parameters
----------
embedding : Model, str, or dict, optional
Pretrained embedding model. Defaults to "pyannote/embedding".
See pyannote.audio.pipelines.utils.get_model for supported format.
segmentation : Model, str, or dict, optional
Pretrained segmentation (or voice activity detection) model.
See pyannote.audio.pipelines.utils.get_model for supported format.
Defaults to no voice activity detection.
use_auth_token : str, optional
When loading private huggingface.co models, set `use_auth_token`
to True or to a string containing your hugginface.co authentication
token that can be obtained by running `huggingface-cli login`
Usage
-----
>>> from pyannote.audio.pipelines import SpeakerEmbedding
>>> pipeline = SpeakerEmbedding()
>>> emb1 = pipeline("speaker1.wav")
>>> emb2 = pipeline("speaker2.wav")
>>> from scipy.spatial.distance import cdist
>>> distance = cdist(emb1, emb2, metric="cosine")[0,0]
"""
def __init__(
self,
embedding: PipelineModel = "pyannote/embedding",
segmentation: PipelineModel = None,
use_auth_token: Union[Text, None] = None,
):
super().__init__()
self.embedding = embedding
self.segmentation = segmentation
self.embedding_model_: Model = get_model(
embedding, use_auth_token=use_auth_token
)
if self.segmentation is not None:
segmentation_model: Model = get_model(
self.segmentation, use_auth_token=use_auth_token
)
self._segmentation = Inference(
segmentation_model,
pre_aggregation_hook=lambda scores: np.max(
scores, axis=-1, keepdims=True
),
)
def apply(self, file: AudioFile) -> np.ndarray:
device = self.embedding_model_.device
# read audio file and send it to GPU
waveform = self.embedding_model_.audio(file)[0][None].to(device)
if self.segmentation is None:
weights = None
else:
# obtain voice activity scores
weights = self._segmentation(file).data
# HACK -- this should be fixed upstream
weights[np.isnan(weights)] = 0.0
weights = torch.from_numpy(weights**3)[None, :, 0].to(device)
# extract speaker embedding on parts of
with torch.no_grad():
return self.embedding_model_(waveform, weights=weights).cpu().numpy()
def main(
protocol: str = "VoxCeleb.SpeakerVerification.VoxCeleb1",
subset: str = "test",
embedding: str = "pyannote/embedding",
segmentation: str = None,
):
import typer
from pyannote.database import FileFinder, get_protocol
from pyannote.metrics.binary_classification import det_curve
from scipy.spatial.distance import cdist
from tqdm import tqdm
pipeline = SpeakerEmbedding(embedding=embedding, segmentation=segmentation)
protocol = get_protocol(protocol, preprocessors={"audio": FileFinder()})
y_true, y_pred = [], []
emb = dict()
trials = getattr(protocol, f"{subset}_trial")()
for t, trial in enumerate(tqdm(trials)):
audio1 = trial["file1"]["audio"]
if audio1 not in emb:
emb[audio1] = pipeline(audio1)
audio2 = trial["file2"]["audio"]
if audio2 not in emb:
emb[audio2] = pipeline(audio2)
y_pred.append(cdist(emb[audio1], emb[audio2], metric="cosine")[0][0])
y_true.append(trial["reference"])
_, _, _, eer = det_curve(y_true, np.array(y_pred), distances=True)
typer.echo(
f"{protocol.name} | {subset} | {embedding} | {segmentation} | EER = {100 * eer:.3f}%"
)
if __name__ == "__main__":
import typer
typer.run(main)