Spaces:
Running
Running
Update GPT_SoVITS/feature_extractor/cnhubert.py
Browse files
GPT_SoVITS/feature_extractor/cnhubert.py
CHANGED
@@ -1,104 +1,95 @@
|
|
1 |
-
import time
|
2 |
-
|
3 |
-
import librosa
|
4 |
-
import torch
|
5 |
-
import torch.nn.functional as F
|
6 |
-
import soundfile as sf
|
7 |
-
import logging
|
8 |
-
|
9 |
-
logging.getLogger("numba").setLevel(logging.WARNING)
|
10 |
-
|
11 |
-
from transformers import (
|
12 |
-
Wav2Vec2FeatureExtractor,
|
13 |
-
HubertModel,
|
14 |
-
)
|
15 |
-
|
16 |
-
import
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
self.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
feats
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
#
|
39 |
-
#
|
40 |
-
#
|
41 |
-
# self.
|
42 |
-
#
|
43 |
-
#
|
44 |
-
#
|
45 |
-
# feats
|
46 |
-
#
|
47 |
-
#
|
48 |
-
#
|
49 |
-
#
|
50 |
-
#
|
51 |
-
# self.
|
52 |
-
#
|
53 |
-
#
|
54 |
-
#
|
55 |
-
# feats
|
56 |
-
#
|
57 |
-
#
|
58 |
-
#
|
59 |
-
#
|
60 |
-
#
|
61 |
-
# self.
|
62 |
-
#
|
63 |
-
#
|
64 |
-
#
|
65 |
-
# feats
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
model
|
71 |
-
model
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
#
|
76 |
-
# model
|
77 |
-
# model
|
78 |
-
#
|
79 |
-
#
|
80 |
-
#
|
81 |
-
# model
|
82 |
-
# model
|
83 |
-
#
|
84 |
-
#
|
85 |
-
#
|
86 |
-
# model
|
87 |
-
# model
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
if __name__ == "__main__":
|
98 |
-
model = get_model()
|
99 |
-
src_path = "/Users/Shared/原音频2.wav"
|
100 |
-
wav_16k_tensor = utils.load_wav_to_torch_and_resample(src_path, 16000)
|
101 |
-
model = model
|
102 |
-
wav_16k_tensor = wav_16k_tensor
|
103 |
-
feats = get_content(model, wav_16k_tensor)
|
104 |
-
print(feats.shape)
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
import librosa
|
4 |
+
import torch
|
5 |
+
import torch.nn.functional as F
|
6 |
+
import soundfile as sf
|
7 |
+
import logging
|
8 |
+
|
9 |
+
logging.getLogger("numba").setLevel(logging.WARNING)
|
10 |
+
|
11 |
+
from transformers import (
|
12 |
+
Wav2Vec2FeatureExtractor,
|
13 |
+
HubertModel,
|
14 |
+
)
|
15 |
+
|
16 |
+
import torch.nn as nn
|
17 |
+
|
18 |
+
cnhubert_base_path = None
|
19 |
+
|
20 |
+
|
21 |
+
class CNHubert(nn.Module):
|
22 |
+
def __init__(self):
|
23 |
+
super().__init__()
|
24 |
+
self.model = HubertModel.from_pretrained(cnhubert_base_path)
|
25 |
+
self.feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(
|
26 |
+
cnhubert_base_path
|
27 |
+
)
|
28 |
+
|
29 |
+
def forward(self, x):
|
30 |
+
input_values = self.feature_extractor(
|
31 |
+
x, return_tensors="pt", sampling_rate=16000
|
32 |
+
).input_values.to(x.device)
|
33 |
+
feats = self.model(input_values)["last_hidden_state"]
|
34 |
+
return feats
|
35 |
+
|
36 |
+
|
37 |
+
# class CNHubertLarge(nn.Module):
|
38 |
+
# def __init__(self):
|
39 |
+
# super().__init__()
|
40 |
+
# self.model = HubertModel.from_pretrained("/data/docker/liujing04/gpt-vits/chinese-hubert-large")
|
41 |
+
# self.feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("/data/docker/liujing04/gpt-vits/chinese-hubert-large")
|
42 |
+
# def forward(self, x):
|
43 |
+
# input_values = self.feature_extractor(x, return_tensors="pt", sampling_rate=16000).input_values.to(x.device)
|
44 |
+
# feats = self.model(input_values)["last_hidden_state"]
|
45 |
+
# return feats
|
46 |
+
#
|
47 |
+
# class CVec(nn.Module):
|
48 |
+
# def __init__(self):
|
49 |
+
# super().__init__()
|
50 |
+
# self.model = HubertModel.from_pretrained("/data/docker/liujing04/vc-webui-big/hubert_base")
|
51 |
+
# self.feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("/data/docker/liujing04/vc-webui-big/hubert_base")
|
52 |
+
# def forward(self, x):
|
53 |
+
# input_values = self.feature_extractor(x, return_tensors="pt", sampling_rate=16000).input_values.to(x.device)
|
54 |
+
# feats = self.model(input_values)["last_hidden_state"]
|
55 |
+
# return feats
|
56 |
+
#
|
57 |
+
# class cnw2v2base(nn.Module):
|
58 |
+
# def __init__(self):
|
59 |
+
# super().__init__()
|
60 |
+
# self.model = Wav2Vec2Model.from_pretrained("/data/docker/liujing04/gpt-vits/chinese-wav2vec2-base")
|
61 |
+
# self.feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("/data/docker/liujing04/gpt-vits/chinese-wav2vec2-base")
|
62 |
+
# def forward(self, x):
|
63 |
+
# input_values = self.feature_extractor(x, return_tensors="pt", sampling_rate=16000).input_values.to(x.device)
|
64 |
+
# feats = self.model(input_values)["last_hidden_state"]
|
65 |
+
# return feats
|
66 |
+
|
67 |
+
|
68 |
+
def get_model():
|
69 |
+
model = CNHubert()
|
70 |
+
model.eval()
|
71 |
+
return model
|
72 |
+
|
73 |
+
|
74 |
+
# def get_large_model():
|
75 |
+
# model = CNHubertLarge()
|
76 |
+
# model.eval()
|
77 |
+
# return model
|
78 |
+
#
|
79 |
+
# def get_model_cvec():
|
80 |
+
# model = CVec()
|
81 |
+
# model.eval()
|
82 |
+
# return model
|
83 |
+
#
|
84 |
+
# def get_model_cnw2v2base():
|
85 |
+
# model = cnw2v2base()
|
86 |
+
# model.eval()
|
87 |
+
# return model
|
88 |
+
|
89 |
+
|
90 |
+
def get_content(hmodel, wav_16k_tensor):
|
91 |
+
with torch.no_grad():
|
92 |
+
feats = hmodel(wav_16k_tensor)
|
93 |
+
return feats.transpose(1, 2)
|
94 |
+
|
95 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|