init
Browse files- experiment_cache/.DS_Store +0 -0
- model_speaker_embedding.py +5 -2
experiment_cache/.DS_Store
CHANGED
Binary files a/experiment_cache/.DS_Store and b/experiment_cache/.DS_Store differ
|
|
model_speaker_embedding.py
CHANGED
@@ -11,12 +11,13 @@ from transformers import AutoModel, AutoFeatureExtractor
|
|
11 |
# W2V BERT #
|
12 |
############
|
13 |
class W2VBERTEmbedding:
|
14 |
-
def __init__(self, ckpt: str = "facebook/w2v-bert-2.0"):
|
15 |
self.processor = AutoFeatureExtractor.from_pretrained(ckpt)
|
16 |
self.model = AutoModel.from_pretrained(ckpt)
|
17 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
self.model.to(self.device)
|
19 |
self.model.eval()
|
|
|
20 |
|
21 |
def get_speaker_embedding(self, wav: np.ndarray, sampling_rate: Optional[int] = None) -> np.ndarray:
|
22 |
# audio file is decoded on the fly
|
@@ -25,7 +26,9 @@ class W2VBERTEmbedding:
|
|
25 |
inputs = self.processor(wav, sampling_rate=self.processor.sampling_rate, return_tensors="pt")
|
26 |
with torch.no_grad():
|
27 |
outputs = self.model(**{k: v.to(self.device) for k, v in inputs.items()})
|
28 |
-
|
|
|
|
|
29 |
|
30 |
|
31 |
##########
|
|
|
11 |
# W2V BERT #
|
12 |
############
|
13 |
class W2VBERTEmbedding:
|
14 |
+
def __init__(self, ckpt: str = "facebook/w2v-bert-2.0", mean_pool: bool = True):
|
15 |
self.processor = AutoFeatureExtractor.from_pretrained(ckpt)
|
16 |
self.model = AutoModel.from_pretrained(ckpt)
|
17 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
self.model.to(self.device)
|
19 |
self.model.eval()
|
20 |
+
self.mean_pool = mean_pool
|
21 |
|
22 |
def get_speaker_embedding(self, wav: np.ndarray, sampling_rate: Optional[int] = None) -> np.ndarray:
|
23 |
# audio file is decoded on the fly
|
|
|
26 |
inputs = self.processor(wav, sampling_rate=self.processor.sampling_rate, return_tensors="pt")
|
27 |
with torch.no_grad():
|
28 |
outputs = self.model(**{k: v.to(self.device) for k, v in inputs.items()})
|
29 |
+
if self.mean_pool:
|
30 |
+
return outputs.last_hidden_state.mean(1).cpu().numpy()[0]
|
31 |
+
return outputs.last_hidden_state.cpu().numpy()[0]
|
32 |
|
33 |
|
34 |
##########
|