experiment-speaker-embedding / model_speaker_embedding.py
asahi417's picture
init
c740363
raw
history blame
2.6 kB
"""Meta's w2vBERT based speaker embedding."""
from typing import Optional
import torch
import librosa
import numpy as np
from transformers import AutoModel, AutoFeatureExtractor
############
# W2V BERT #
############
class W2VBERTEmbedding:
def __init__(self, ckpt: str = "facebook/w2v-bert-2.0", mean_pool: bool = True):
self.processor = AutoFeatureExtractor.from_pretrained(ckpt)
self.model = AutoModel.from_pretrained(ckpt)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model.to(self.device)
self.model.eval()
self.mean_pool = mean_pool
def get_speaker_embedding(self, wav: np.ndarray, sampling_rate: Optional[int] = None) -> np.ndarray:
# audio file is decoded on the fly
if sampling_rate != self.processor.sampling_rate:
wav = librosa.resample(wav, orig_sr=sampling_rate, target_sr=self.processor.sampling_rate)
inputs = self.processor(wav, sampling_rate=self.processor.sampling_rate, return_tensors="pt")
with torch.no_grad():
outputs = self.model(**{k: v.to(self.device) for k, v in inputs.items()})
if self.mean_pool:
return outputs.last_hidden_state.mean(1).cpu().numpy()[0]
return outputs.last_hidden_state.cpu().numpy()[0]
##########
# HuBERT #
##########
class HuBERTXLEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/hubert-xlarge-ll60k", mean_pool=mean_pool)
class HuBERTLargeEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/hubert-large-ll60k", mean_pool=mean_pool)
class HuBERTBaseEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/hubert-base-ls960", mean_pool=mean_pool)
###########
# wav2vec #
###########
class Wav2VecEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/wav2vec2-large-xlsr-53", mean_pool=mean_pool)
#########
# XLS-R #
#########
class XLSR2BEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/wav2vec2-xls-r-2b", mean_pool=mean_pool)
class XLSR1BEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/wav2vec2-xls-r-1b", mean_pool=mean_pool)
class XLSR300MEmbedding(W2VBERTEmbedding):
def __init__(self, mean_pool: bool = True):
super().__init__("facebook/wav2vec2-xls-r-300m", mean_pool=mean_pool)