init
Browse files- __pycache__/model_hubert.cpython-310.pyc +0 -0
- __pycache__/model_w2v_bert.cpython-310.pyc +0 -0
- __pycache__/model_xls.cpython-310.pyc +0 -0
- model_hubert.py → model_speaker_embedding.py +43 -8
- model_w2v_bert.py +0 -28
- model_xls.py +0 -47
- test.py +1 -3
__pycache__/model_hubert.cpython-310.pyc
DELETED
Binary file (2.1 kB)
|
|
__pycache__/model_w2v_bert.cpython-310.pyc
DELETED
Binary file (1.71 kB)
|
|
__pycache__/model_xls.cpython-310.pyc
DELETED
Binary file (1.85 kB)
|
|
model_hubert.py → model_speaker_embedding.py
RENAMED
@@ -1,18 +1,20 @@
|
|
1 |
-
"""Meta's
|
2 |
- feature dimension: 1024
|
3 |
-
- source: https://huggingface.co/facebook/
|
4 |
"""
|
5 |
from typing import Optional
|
6 |
|
7 |
import torch
|
8 |
import librosa
|
9 |
import numpy as np
|
10 |
-
from transformers import
|
11 |
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
self.processor = AutoFeatureExtractor.from_pretrained(ckpt)
|
17 |
self.model = AutoModel.from_pretrained(ckpt)
|
18 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -29,12 +31,45 @@ class HuBERTXLEmbedding:
|
|
29 |
return outputs.last_hidden_state.mean(1).cpu().numpy()[0]
|
30 |
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def __init__(self):
|
34 |
super().__init__("facebook/hubert-large-ll60k")
|
35 |
|
36 |
|
37 |
-
class HuBERTBaseEmbedding(
|
38 |
def __init__(self):
|
39 |
super().__init__("facebook/hubert-base-ls960")
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Meta's w2vBERT based speaker embedding.
|
2 |
- feature dimension: 1024
|
3 |
+
- source: https://huggingface.co/facebook/w2v-bert-2.0
|
4 |
"""
|
5 |
from typing import Optional
|
6 |
|
7 |
import torch
|
8 |
import librosa
|
9 |
import numpy as np
|
10 |
+
from transformers import AutoModel, AutoFeatureExtractor
|
11 |
|
12 |
|
13 |
+
############
|
14 |
+
# W2V BERT #
|
15 |
+
############
|
16 |
+
class W2VBERTEmbedding:
|
17 |
+
def __init__(self, ckpt: str = "facebook/w2v-bert-2.0"):
|
18 |
self.processor = AutoFeatureExtractor.from_pretrained(ckpt)
|
19 |
self.model = AutoModel.from_pretrained(ckpt)
|
20 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
31 |
return outputs.last_hidden_state.mean(1).cpu().numpy()[0]
|
32 |
|
33 |
|
34 |
+
##########
|
35 |
+
# HuBERT #
|
36 |
+
##########
|
37 |
+
class HuBERTXLEmbedding(W2VBERTEmbedding):
|
38 |
+
def __init__(self):
|
39 |
+
super().__init__("facebook/hubert-xlarge-ll60k")
|
40 |
+
|
41 |
+
|
42 |
+
class HuBERTLargeEmbedding(W2VBERTEmbedding):
|
43 |
def __init__(self):
|
44 |
super().__init__("facebook/hubert-large-ll60k")
|
45 |
|
46 |
|
47 |
+
class HuBERTBaseEmbedding(W2VBERTEmbedding):
|
48 |
def __init__(self):
|
49 |
super().__init__("facebook/hubert-base-ls960")
|
50 |
|
51 |
+
|
52 |
+
###########
|
53 |
+
# wav2vec #
|
54 |
+
###########
|
55 |
+
class Wav2VecEmbedding(W2VBERTEmbedding):
|
56 |
+
def __init__(self):
|
57 |
+
super().__init__("facebook/wav2vec2-large-xlsr-53")
|
58 |
+
|
59 |
+
|
60 |
+
#########
|
61 |
+
# XLS-R #
|
62 |
+
#########
|
63 |
+
class XLSR2BEmbedding(W2VBERTEmbedding):
|
64 |
+
def __init__(self):
|
65 |
+
super().__init__("facebook/wav2vec2-xls-r-2b")
|
66 |
+
|
67 |
+
|
68 |
+
class XLSR1BEmbedding(W2VBERTEmbedding):
|
69 |
+
def __init__(self):
|
70 |
+
super().__init__("facebook/wav2vec2-xls-r-1b")
|
71 |
+
|
72 |
+
|
73 |
+
class XLSR300MEmbedding(W2VBERTEmbedding):
|
74 |
+
def __init__(self):
|
75 |
+
super().__init__("facebook/wav2vec2-xls-r-300m")
|
model_w2v_bert.py
DELETED
@@ -1,28 +0,0 @@
|
|
1 |
-
"""Meta's w2vBERT based speaker embedding.
|
2 |
-
- feature dimension: 1024
|
3 |
-
- source: https://huggingface.co/facebook/w2v-bert-2.0
|
4 |
-
"""
|
5 |
-
from typing import Optional
|
6 |
-
|
7 |
-
import torch
|
8 |
-
import librosa
|
9 |
-
import numpy as np
|
10 |
-
from transformers import Wav2Vec2BertModel, AutoFeatureExtractor
|
11 |
-
|
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 = Wav2Vec2BertModel.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
|
23 |
-
if sampling_rate != self.processor.sampling_rate:
|
24 |
-
wav = librosa.resample(wav, orig_sr=sampling_rate, target_sr=self.processor.sampling_rate)
|
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 |
-
return outputs.last_hidden_state.mean(1).cpu().numpy()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_xls.py
DELETED
@@ -1,47 +0,0 @@
|
|
1 |
-
"""Meta's XLS-R based speaker embedding.
|
2 |
-
- feature dimension: 768
|
3 |
-
- source: https://huggingface.co/docs/transformers/en/model_doc/wav2vec2#transformers.models.wav2vec2.modeling_wav2vec2.Wav2Vec2ForPreTrainingOutput
|
4 |
-
"""
|
5 |
-
from typing import Optional
|
6 |
-
|
7 |
-
import torch
|
8 |
-
import librosa
|
9 |
-
import numpy as np
|
10 |
-
from transformers import AutoFeatureExtractor, AutoModelForPreTraining
|
11 |
-
|
12 |
-
|
13 |
-
class Wav2VecEmbedding:
|
14 |
-
|
15 |
-
def __init__(self, ckpt: str = "facebook/wav2vec2-large-xlsr-53"):
|
16 |
-
self.processor = AutoFeatureExtractor.from_pretrained(ckpt)
|
17 |
-
self.model = AutoModelForPreTraining.from_pretrained(ckpt)
|
18 |
-
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
-
self.model.to(self.device)
|
20 |
-
self.model.eval()
|
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
|
24 |
-
if sampling_rate != self.processor.sampling_rate:
|
25 |
-
wav = librosa.resample(wav, orig_sr=sampling_rate, target_sr=self.processor.sampling_rate)
|
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 |
-
return outputs.projected_states.mean(1).cpu().numpy()[0]
|
30 |
-
|
31 |
-
|
32 |
-
class XLSR2BEmbedding(Wav2VecEmbedding):
|
33 |
-
|
34 |
-
def __init__(self):
|
35 |
-
super().__init__("facebook/wav2vec2-xls-r-2b")
|
36 |
-
|
37 |
-
|
38 |
-
class XLSR1BEmbedding(Wav2VecEmbedding):
|
39 |
-
|
40 |
-
def __init__(self):
|
41 |
-
super().__init__("facebook/wav2vec2-xls-r-1b")
|
42 |
-
|
43 |
-
|
44 |
-
class XLSR300MEmbedding(Wav2VecEmbedding):
|
45 |
-
|
46 |
-
def __init__(self):
|
47 |
-
super().__init__("facebook/wav2vec2-xls-r-300m")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.py
CHANGED
@@ -2,9 +2,7 @@ import librosa
|
|
2 |
from model_clap import CLAPEmbedding
|
3 |
from model_meta_voice import MetaVoiceEmbedding
|
4 |
from model_pyannote_embedding import PyannoteEmbedding
|
5 |
-
from
|
6 |
-
from model_xls import XLSR300MEmbedding
|
7 |
-
from model_hubert import HuBERTXLEmbedding
|
8 |
|
9 |
|
10 |
def test():
|
|
|
2 |
from model_clap import CLAPEmbedding
|
3 |
from model_meta_voice import MetaVoiceEmbedding
|
4 |
from model_pyannote_embedding import PyannoteEmbedding
|
5 |
+
from model_speaker_embedding import W2VBERTEmbedding, XLSR300MEmbedding, HuBERTXLEmbedding
|
|
|
|
|
6 |
|
7 |
|
8 |
def test():
|