speaker
Speaker embedding models (ECAPA-TDNN & X-vector) with pretrained weights.
This module contains faithful PyTorch ports of the ECAPA-TDNN and X-vector
speaker embedding models, the Fbank acoustic frontend, and the input
normalization from the SpeechBrain toolkit
(https://github.com/speechbrain/speechbrain, Apache-2.0 license; original
authors: Hwidong Na (ECAPA-TDNN), Mirco Ravanelli (X-vector, features)).
The port is written so that the pretrained checkpoints published on the
HuggingFace hub (speechbrain/spkrec-ecapa-voxceleb and
speechbrain/spkrec-xvect-voxceleb) load with an exact key match
(strict=True) and reproduce the reference embeddings numerically, without
requiring the SpeechBrain package itself (see issue #4).
AttentiveStatisticsPooling
Bases: Module
Attentive statistics pooling (ported from SpeechBrain's ECAPA_TDNN.py).
Returns the concatenated attention-weighted mean and std of the input.
Source code in speechain/module/encoder/speaker.py
forward(x, lengths=None)
x: (batch, channels, time); lengths: relative lengths or None.
Source code in speechain/module/encoder/speaker.py
BatchNorm1d
Bases: Module
Batch normalization operating on (batch, channels, time) tensors.
Equivalent to SpeechBrain's BatchNorm1d with skip_transpose=True.
Source code in speechain/module/encoder/speaker.py
Conv1d
Bases: Module
1D convolution operating on (batch, channels, time) tensors.
Equivalent to SpeechBrain's Conv1d with skip_transpose=True and its default 'same' padding in 'reflect' mode (this padding choice is numerically verified against the reference implementation in speechain/module/vocoder/hifigan.py).
Source code in speechain/module/encoder/speaker.py
ECAPA_TDNN
Bases: Module
ECAPA-TDNN speaker embedding model (ported from SpeechBrain).
'ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in TDNN Based Speaker Verification' (https://arxiv.org/abs/2005.07143).
The default architecture matches the pretrained
speechbrain/spkrec-ecapa-voxceleb checkpoint
(channels=[1024, 1024, 1024, 1024, 3072]).
Source code in speechain/module/encoder/speaker.py
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 | |
forward(x, lengths=None)
x: (batch, time, channel). Returns (batch, 1, lin_neurons).
Source code in speechain/module/encoder/speaker.py
EncoderClassifier
Bases: Module
Speaker embedding extractor with pretrained ECAPA-TDNN / X-vector models.
Drop-in replacement for SpeechBrain's EncoderClassifier limited to the two
pretrained models used by SpeeChain's TTS data pipeline:
speechbrain/spkrec-ecapa-voxceleb and speechbrain/spkrec-xvect-voxceleb.
The checkpoints are downloaded from the HuggingFace hub on the first call
and loaded into the ported models above with an exact key match.
Source code in speechain/module/encoder/speaker.py
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 | |
encode_batch(wavs, wav_lens=None, normalize=False)
Encodes the input audio into a single vector embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wavs
|
Batch of waveforms of shape (batch, time) at 16 kHz. |
required | |
wav_lens
|
Relative lengths of the waveforms (batch,), where the longest one is 1.0. Used for ignoring the padding part. |
None
|
|
normalize
|
If True, the embeddings are normalized with the global statistics published together with the pretrained checkpoints (mean_var_norm_emb.ckpt). |
False
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: The speaker embeddings of shape (batch, 1, emb_dim). |
Source code in speechain/module/encoder/speaker.py
from_hparams(source, savedir=None, run_opts=None)
classmethod
Load a pretrained speaker embedding model from the HuggingFace hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
The HuggingFace repository id of the pretrained model (e.g., 'speechbrain/spkrec-ecapa-voxceleb'). |
required | |
savedir
|
The directory where the downloaded checkpoints are cached. |
None
|
|
run_opts
|
A dict of run options; only the 'device' entry is used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EncoderClassifier |
The pretrained model in eval() mode. |
Source code in speechain/module/encoder/speaker.py
FbankFrontend
Bases: Module
Fbank acoustic frontend (ported from SpeechBrain's lobes/features.py).
Computes log-mel filterbank features from raw waveforms exactly as the reference implementation does: STFT (center=True, constant padding, periodic Hann window) -> power spectrogram -> triangular mel filters in the HTK mel scale -> 10 * log10 -> per-utterance top_db clamping.
Source code in speechain/module/encoder/speaker.py
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 | |
forward(wav)
wav: (batch, time). Returns (batch, time, n_mels) log-fbanks.
Source code in speechain/module/encoder/speaker.py
Linear
Bases: Module
Linear layer equivalent to SpeechBrain's Linear wrapper.
Source code in speechain/module/encoder/speaker.py
Res2NetBlock
Bases: Module
An implementation of Res2NetBlock w/ dilation (ported from SpeechBrain).
Source code in speechain/module/encoder/speaker.py
SEBlock
Bases: Module
An implementation of squeeze-and-excitation block (ported from SpeechBrain).
Source code in speechain/module/encoder/speaker.py
SERes2NetBlock
Bases: Module
TDNN-Res2Net-TDNN-SEBlock building block of ECAPA-TDNN (ported).
Source code in speechain/module/encoder/speaker.py
SentenceMeanNorm
Bases: Module
Per-utterance mean normalization over the time axis.
Equivalent to SpeechBrain's InputNormalization with norm_type='sentence' and std_norm=False (as used by the pretrained speaker embedding models).
Source code in speechain/module/encoder/speaker.py
forward(x, lengths=None)
x: (batch, time, channels). Returns the mean-normalized tensor.
Source code in speechain/module/encoder/speaker.py
StatisticsPooling
Bases: Module
Statistics pooling layer for the X-vector model (ported from SpeechBrain).
Returns the concatenated mean and std of the input along the time axis. Following the reference implementation, a tiny deterministic-scale perturbation is added to the statistics (see the forward function).
Source code in speechain/module/encoder/speaker.py
forward(x, lengths=None)
x: (batch, time, channels). Returns (batch, 1, 2 * channels).
Source code in speechain/module/encoder/speaker.py
TDNNBlock
Bases: Module
An implementation of TDNN (ported from SpeechBrain's ECAPA_TDNN.py).
Source code in speechain/module/encoder/speaker.py
Xvector
Bases: Module
X-vector speaker embedding model (ported from SpeechBrain's Xvector.py).
The default architecture matches the pretrained
speechbrain/spkrec-xvect-voxceleb checkpoint
(in_channels=24, tdnn_channels=[512, 512, 512, 512, 1500]).
Source code in speechain/module/encoder/speaker.py
forward(x, lens=None)
x: (batch, time, channels). Returns (batch, 1, lin_neurons).