Spaces:
Build error
Build error
feat: init repo
Browse files- 83_epochs.pth +3 -0
- README.md +4 -4
- app.py +127 -0
- attentions.py +311 -0
- commons.py +160 -0
- configs/milky_green.json +96 -0
- configs/nyarumul.json +96 -0
- configs/sovits_pre.json +94 -0
- configs/yilanqiu.json +93 -0
- data_utils.py +411 -0
- hubert.pt +3 -0
- hubert_model.py +223 -0
- infer_tool.py +175 -0
- mg_1255_epochs_v0.0.6.pth +3 -0
- models.py +556 -0
- modules.py +388 -0
- preprocess_wave.py +118 -0
83_epochs.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5b2d02f32e9df815c473e775187a5cbcc3fe60412681ec462d13570d7191b5e3
|
3 |
+
size 221251405
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title: Milky Green
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.6
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
+
title: Milky Green SOVITS
|
3 |
+
emoji: 🍵
|
4 |
+
colorFrom: cyan
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.6
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import gradio as gr
|
4 |
+
import soundfile
|
5 |
+
import torch
|
6 |
+
|
7 |
+
import infer_tool
|
8 |
+
|
9 |
+
convert_cnt = [0]
|
10 |
+
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
# model_name = "83_epochs.pth"
|
12 |
+
model_name = "mg_1255_epochs_v0.0.6.pth"
|
13 |
+
config_name = "milky_green.json"
|
14 |
+
net_g_ms, hubert_soft, feature_input, hps_ms = infer_tool.load_model(f"{model_name}", f"configs/{config_name}")
|
15 |
+
|
16 |
+
# 获取config参数
|
17 |
+
target_sample = hps_ms.data.sampling_rate
|
18 |
+
spk_dict = {
|
19 |
+
"明前奶绿": 0,
|
20 |
+
"云灏": 2,
|
21 |
+
"即霜": 3,
|
22 |
+
"奕兰秋": 4
|
23 |
+
}
|
24 |
+
|
25 |
+
def list_models():
|
26 |
+
global model_name
|
27 |
+
res = []
|
28 |
+
dir = os.getcwd()
|
29 |
+
for f in os.listdir(dir):
|
30 |
+
if(f.startswith("D_")):
|
31 |
+
continue
|
32 |
+
if(f.endswith(".pth")):
|
33 |
+
res.append(f)
|
34 |
+
if len(f) >= len(model_name):
|
35 |
+
model_name = f
|
36 |
+
return res
|
37 |
+
|
38 |
+
def vc_fn(sid, audio_record, audio_upload, tran):
|
39 |
+
print(sid, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
|
40 |
+
if audio_upload is not None:
|
41 |
+
audio_path = audio_upload
|
42 |
+
elif audio_record is not None:
|
43 |
+
audio_path = audio_record
|
44 |
+
else:
|
45 |
+
return "你需要上传wav文件或使用网页内置的录音!", None
|
46 |
+
|
47 |
+
audio, sampling_rate = infer_tool.format_wav(audio_path, target_sample)
|
48 |
+
duration = audio.shape[0] / sampling_rate
|
49 |
+
if duration > 600:
|
50 |
+
return "请上传小于600s的音频,需要转换长音频请使用colab", None
|
51 |
+
|
52 |
+
o_audio, out_sr = infer_tool.infer(audio_path, spk_dict[sid], tran, net_g_ms, hubert_soft, feature_input)
|
53 |
+
out_path = f"./out_temp.wav"
|
54 |
+
soundfile.write(out_path, o_audio, target_sample)
|
55 |
+
infer_tool.f0_plt(audio_path, out_path, tran, hubert_soft, feature_input)
|
56 |
+
mistake, var = infer_tool.calc_error(audio_path, out_path, tran, feature_input)
|
57 |
+
return f"半音偏差:{mistake}\n半音方差:{var}", (
|
58 |
+
target_sample, o_audio), gr.Image.update("temp.jpg")
|
59 |
+
|
60 |
+
def change_model(model):
|
61 |
+
global model_name
|
62 |
+
global net_g_ms
|
63 |
+
global hubert_soft
|
64 |
+
global feature_input
|
65 |
+
global hps_ms
|
66 |
+
model_name = model
|
67 |
+
net_g_ms, hubert_soft, feature_input, hps_ms = infer_tool.load_model(f"{model_name}", f"configs/{config_name}")
|
68 |
+
return "载入模型:"+model_name
|
69 |
+
|
70 |
+
available_models = list_models()
|
71 |
+
|
72 |
+
app = gr.Blocks()
|
73 |
+
with app:
|
74 |
+
with gr.Tabs():
|
75 |
+
with gr.TabItem("Basic"):
|
76 |
+
gr.Markdown(value="""
|
77 |
+
本模型为[sovits_f0](https://huggingface.co/spaces/innnky/nyaru-svc2.0-advanced)魔改。含AI奶绿(aka. [明前奶绿](https://space.bilibili.com/2132180406))音色,支持**60s以内**的**无伴奏**wav、mp3(单声道)格式,或使用**网页内置**的录音(二选一)
|
78 |
+
|
79 |
+
转换效果取决于源音频语气、节奏是否与目标音色相近,以及音域是否超出目标音色音域范围
|
80 |
+
|
81 |
+
奶绿高音数据效果稍差,一些音高过高的需要考虑降调
|
82 |
+
|
83 |
+
该模型的 [github仓库链接](https://github.com/innnky/so-vits-svc),如果想自己制作并训练模型可以访问这个 [github仓库](https://github.com/IceKyrin/sovits_guide)
|
84 |
+
|
85 |
+
""")
|
86 |
+
model_selected = gr.Dropdown(choices=available_models, label = "模型", value=model_name)
|
87 |
+
current_model_text = gr.Markdown("")
|
88 |
+
model_selected.change(change_model, inputs=[model_selected], outputs=[current_model_text])
|
89 |
+
speaker_id = gr.Dropdown(label="音色", choices=['明前奶绿', '云灏', '即霜', "奕兰秋"], value="明前奶绿")
|
90 |
+
record_input = gr.Audio(source="microphone", label="录制你的声音", type="filepath", elem_id="audio_inputs")
|
91 |
+
upload_input = gr.Audio(source="upload", label="上传音频(长度小于60秒)", type="filepath",
|
92 |
+
elem_id="audio_inputs")
|
93 |
+
vc_transform = gr.Number(label="升降半音(整数,可以正负,半音数量,升高八度就是12)", value=0)
|
94 |
+
vc_submit = gr.Button("转换", variant="primary")
|
95 |
+
out_audio = gr.Audio(label="Output Audio")
|
96 |
+
gr.Markdown(value="""
|
97 |
+
输出信息为音高平均偏差半音数量,体现转换音频的跑调情况(一般平均小于0.5个半音)
|
98 |
+
""")
|
99 |
+
out_message = gr.Textbox(label="跑调误差信息")
|
100 |
+
gr.Markdown(value="""f0曲线可以直观的显示跑调情况,蓝色为输入音高,橙色为合成音频的音高
|
101 |
+
|
102 |
+
若**只看见橙色**,说明蓝色曲线被覆盖,转换效果较好
|
103 |
+
|
104 |
+
""")
|
105 |
+
f0_image = gr.Image(label="f0曲线")
|
106 |
+
vc_submit.click(vc_fn, [speaker_id, record_input, upload_input, vc_transform],
|
107 |
+
[out_message, out_audio, f0_image])
|
108 |
+
with gr.TabItem("使用说明"):
|
109 |
+
gr.Markdown(value="""
|
110 |
+
0、合集:https://github.com/IceKyrin/sovits_guide/blob/main/README.md
|
111 |
+
|
112 |
+
1、仅支持sovit_f0(sovits2.0)模型
|
113 |
+
|
114 |
+
2、自行下载hubert-soft-0d54a1f4.pt改名为hubert.pt放置于pth文件夹下(已经下好了)
|
115 |
+
https://github.com/bshall/hubert/releases/tag/v0.1
|
116 |
+
|
117 |
+
3、pth文件夹下放置sovits2.0的模型
|
118 |
+
|
119 |
+
4、与模型配套的xxx.json,需有speaker项——人物列表
|
120 |
+
|
121 |
+
5、放无伴奏的音频、或网页内置录音,不要放奇奇怪怪的格式
|
122 |
+
|
123 |
+
6、仅供交流使用,不对用户行为负责
|
124 |
+
|
125 |
+
""")
|
126 |
+
|
127 |
+
app.launch()
|
attentions.py
ADDED
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as t_func
|
6 |
+
|
7 |
+
import commons
|
8 |
+
from modules import LayerNorm
|
9 |
+
|
10 |
+
|
11 |
+
class Encoder(nn.Module):
|
12 |
+
def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0., window_size=4,
|
13 |
+
**kwargs):
|
14 |
+
super().__init__()
|
15 |
+
self.hidden_channels = hidden_channels
|
16 |
+
self.filter_channels = filter_channels
|
17 |
+
self.n_heads = n_heads
|
18 |
+
self.n_layers = n_layers
|
19 |
+
self.kernel_size = kernel_size
|
20 |
+
self.p_dropout = p_dropout
|
21 |
+
self.window_size = window_size
|
22 |
+
|
23 |
+
self.drop = nn.Dropout(p_dropout)
|
24 |
+
self.attn_layers = nn.ModuleList()
|
25 |
+
self.norm_layers_1 = nn.ModuleList()
|
26 |
+
self.ffn_layers = nn.ModuleList()
|
27 |
+
self.norm_layers_2 = nn.ModuleList()
|
28 |
+
for i in range(self.n_layers):
|
29 |
+
self.attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout,
|
30 |
+
window_size=window_size))
|
31 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
32 |
+
self.ffn_layers.append(
|
33 |
+
FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout))
|
34 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
35 |
+
|
36 |
+
def forward(self, x, x_mask):
|
37 |
+
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
38 |
+
x = x * x_mask
|
39 |
+
for i in range(self.n_layers):
|
40 |
+
y = self.attn_layers[i](x, x, attn_mask)
|
41 |
+
y = self.drop(y)
|
42 |
+
x = self.norm_layers_1[i](x + y)
|
43 |
+
|
44 |
+
y = self.ffn_layers[i](x, x_mask)
|
45 |
+
y = self.drop(y)
|
46 |
+
x = self.norm_layers_2[i](x + y)
|
47 |
+
x = x * x_mask
|
48 |
+
return x
|
49 |
+
|
50 |
+
|
51 |
+
class Decoder(nn.Module):
|
52 |
+
def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0.,
|
53 |
+
proximal_bias=False, proximal_init=True, **kwargs):
|
54 |
+
super().__init__()
|
55 |
+
self.hidden_channels = hidden_channels
|
56 |
+
self.filter_channels = filter_channels
|
57 |
+
self.n_heads = n_heads
|
58 |
+
self.n_layers = n_layers
|
59 |
+
self.kernel_size = kernel_size
|
60 |
+
self.p_dropout = p_dropout
|
61 |
+
self.proximal_bias = proximal_bias
|
62 |
+
self.proximal_init = proximal_init
|
63 |
+
|
64 |
+
self.drop = nn.Dropout(p_dropout)
|
65 |
+
self.self_attn_layers = nn.ModuleList()
|
66 |
+
self.norm_layers_0 = nn.ModuleList()
|
67 |
+
self.encdec_attn_layers = nn.ModuleList()
|
68 |
+
self.norm_layers_1 = nn.ModuleList()
|
69 |
+
self.ffn_layers = nn.ModuleList()
|
70 |
+
self.norm_layers_2 = nn.ModuleList()
|
71 |
+
for i in range(self.n_layers):
|
72 |
+
self.self_attn_layers.append(
|
73 |
+
MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout,
|
74 |
+
proximal_bias=proximal_bias, proximal_init=proximal_init))
|
75 |
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
76 |
+
self.encdec_attn_layers.append(
|
77 |
+
MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout))
|
78 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
79 |
+
self.ffn_layers.append(
|
80 |
+
FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout, causal=True))
|
81 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
82 |
+
|
83 |
+
def forward(self, x, x_mask, h, h_mask):
|
84 |
+
"""
|
85 |
+
x: decoder input
|
86 |
+
h: encoder output
|
87 |
+
"""
|
88 |
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(device=x.device, dtype=x.dtype)
|
89 |
+
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
90 |
+
x = x * x_mask
|
91 |
+
for i in range(self.n_layers):
|
92 |
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
93 |
+
y = self.drop(y)
|
94 |
+
x = self.norm_layers_0[i](x + y)
|
95 |
+
|
96 |
+
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
97 |
+
y = self.drop(y)
|
98 |
+
x = self.norm_layers_1[i](x + y)
|
99 |
+
|
100 |
+
y = self.ffn_layers[i](x, x_mask)
|
101 |
+
y = self.drop(y)
|
102 |
+
x = self.norm_layers_2[i](x + y)
|
103 |
+
x = x * x_mask
|
104 |
+
return x
|
105 |
+
|
106 |
+
|
107 |
+
class MultiHeadAttention(nn.Module):
|
108 |
+
def __init__(self, channels, out_channels, n_heads, p_dropout=0., window_size=None, heads_share=True,
|
109 |
+
block_length=None, proximal_bias=False, proximal_init=False):
|
110 |
+
super().__init__()
|
111 |
+
assert channels % n_heads == 0
|
112 |
+
|
113 |
+
self.channels = channels
|
114 |
+
self.out_channels = out_channels
|
115 |
+
self.n_heads = n_heads
|
116 |
+
self.p_dropout = p_dropout
|
117 |
+
self.window_size = window_size
|
118 |
+
self.heads_share = heads_share
|
119 |
+
self.block_length = block_length
|
120 |
+
self.proximal_bias = proximal_bias
|
121 |
+
self.proximal_init = proximal_init
|
122 |
+
self.attn = None
|
123 |
+
|
124 |
+
self.k_channels = channels // n_heads
|
125 |
+
self.conv_q = nn.Conv1d(channels, channels, 1)
|
126 |
+
self.conv_k = nn.Conv1d(channels, channels, 1)
|
127 |
+
self.conv_v = nn.Conv1d(channels, channels, 1)
|
128 |
+
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
129 |
+
self.drop = nn.Dropout(p_dropout)
|
130 |
+
|
131 |
+
if window_size is not None:
|
132 |
+
n_heads_rel = 1 if heads_share else n_heads
|
133 |
+
rel_stddev = self.k_channels ** -0.5
|
134 |
+
self.emb_rel_k = nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
|
135 |
+
self.emb_rel_v = nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
|
136 |
+
|
137 |
+
nn.init.xavier_uniform_(self.conv_q.weight)
|
138 |
+
nn.init.xavier_uniform_(self.conv_k.weight)
|
139 |
+
nn.init.xavier_uniform_(self.conv_v.weight)
|
140 |
+
if proximal_init:
|
141 |
+
with torch.no_grad():
|
142 |
+
self.conv_k.weight.copy_(self.conv_q.weight)
|
143 |
+
self.conv_k.bias.copy_(self.conv_q.bias)
|
144 |
+
|
145 |
+
def forward(self, x, c, attn_mask=None):
|
146 |
+
q = self.conv_q(x)
|
147 |
+
k = self.conv_k(c)
|
148 |
+
v = self.conv_v(c)
|
149 |
+
|
150 |
+
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
151 |
+
|
152 |
+
x = self.conv_o(x)
|
153 |
+
return x
|
154 |
+
|
155 |
+
def attention(self, query, key, value, mask=None):
|
156 |
+
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
157 |
+
b, d, t_s, t_t = (*key.size(), query.size(2))
|
158 |
+
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
159 |
+
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
160 |
+
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
161 |
+
|
162 |
+
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
163 |
+
if self.window_size is not None:
|
164 |
+
assert t_s == t_t, "Relative attention is only available for self-attention."
|
165 |
+
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
166 |
+
rel_logits = self._matmul_with_relative_keys(query / math.sqrt(self.k_channels), key_relative_embeddings)
|
167 |
+
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
168 |
+
scores = scores + scores_local
|
169 |
+
if self.proximal_bias:
|
170 |
+
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
171 |
+
scores = scores + self._attention_bias_proximal(t_s).to(device=scores.device, dtype=scores.dtype)
|
172 |
+
if mask is not None:
|
173 |
+
scores = scores.masked_fill(mask == 0, -1e4)
|
174 |
+
if self.block_length is not None:
|
175 |
+
assert t_s == t_t, "Local attention is only available for self-attention."
|
176 |
+
block_mask = torch.ones_like(scores).triu(-self.block_length).tril(self.block_length)
|
177 |
+
scores = scores.masked_fill(block_mask == 0, -1e4)
|
178 |
+
p_attn = t_func.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
179 |
+
p_attn = self.drop(p_attn)
|
180 |
+
output = torch.matmul(p_attn, value)
|
181 |
+
if self.window_size is not None:
|
182 |
+
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
183 |
+
value_relative_embeddings = self._get_relative_embeddings(self.emb_rel_v, t_s)
|
184 |
+
output = output + self._matmul_with_relative_values(relative_weights, value_relative_embeddings)
|
185 |
+
output = output.transpose(2, 3).contiguous().view(b, d, t_t) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
186 |
+
return output, p_attn
|
187 |
+
|
188 |
+
def _matmul_with_relative_values(self, x, y):
|
189 |
+
"""
|
190 |
+
x: [b, h, l, m]
|
191 |
+
y: [h or 1, m, d]
|
192 |
+
ret: [b, h, l, d]
|
193 |
+
"""
|
194 |
+
ret = torch.matmul(x, y.unsqueeze(0))
|
195 |
+
return ret
|
196 |
+
|
197 |
+
def _matmul_with_relative_keys(self, x, y):
|
198 |
+
"""
|
199 |
+
x: [b, h, l, d]
|
200 |
+
y: [h or 1, m, d]
|
201 |
+
ret: [b, h, l, m]
|
202 |
+
"""
|
203 |
+
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
204 |
+
return ret
|
205 |
+
|
206 |
+
def _get_relative_embeddings(self, relative_embeddings, length):
|
207 |
+
max_relative_position = 2 * self.window_size + 1
|
208 |
+
# Pad first before slice to avoid using cond ops.
|
209 |
+
pad_length = max(length - (self.window_size + 1), 0)
|
210 |
+
slice_start_position = max((self.window_size + 1) - length, 0)
|
211 |
+
slice_end_position = slice_start_position + 2 * length - 1
|
212 |
+
if pad_length > 0:
|
213 |
+
padded_relative_embeddings = t_func.pad(
|
214 |
+
relative_embeddings,
|
215 |
+
commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]))
|
216 |
+
else:
|
217 |
+
padded_relative_embeddings = relative_embeddings
|
218 |
+
used_relative_embeddings = padded_relative_embeddings[:, slice_start_position:slice_end_position]
|
219 |
+
return used_relative_embeddings
|
220 |
+
|
221 |
+
def _relative_position_to_absolute_position(self, x):
|
222 |
+
"""
|
223 |
+
x: [b, h, l, 2*l-1]
|
224 |
+
ret: [b, h, l, l]
|
225 |
+
"""
|
226 |
+
batch, heads, length, _ = x.size()
|
227 |
+
# Concat columns of pad to shift from relative to absolute indexing.
|
228 |
+
x = t_func.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
|
229 |
+
|
230 |
+
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
231 |
+
x_flat = x.view([batch, heads, length * 2 * length])
|
232 |
+
x_flat = t_func.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]]))
|
233 |
+
|
234 |
+
# Reshape and slice out the padded elements.
|
235 |
+
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[:, :, :length, length - 1:]
|
236 |
+
return x_final
|
237 |
+
|
238 |
+
def _absolute_position_to_relative_position(self, x):
|
239 |
+
"""
|
240 |
+
x: [b, h, l, l]
|
241 |
+
ret: [b, h, l, 2*l-1]
|
242 |
+
"""
|
243 |
+
batch, heads, length, _ = x.size()
|
244 |
+
# padd along column
|
245 |
+
x = t_func.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]]))
|
246 |
+
x_flat = x.view([batch, heads, length ** 2 + length * (length - 1)])
|
247 |
+
# add 0's in the beginning that will skew the elements after reshape
|
248 |
+
x_flat = t_func.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
249 |
+
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
250 |
+
return x_final
|
251 |
+
|
252 |
+
def _attention_bias_proximal(self, length):
|
253 |
+
"""Bias for self-attention to encourage attention to close positions.
|
254 |
+
Args:
|
255 |
+
length: an integer scalar.
|
256 |
+
Returns:
|
257 |
+
a Tensor with shape [1, 1, length, length]
|
258 |
+
"""
|
259 |
+
r = torch.arange(length, dtype=torch.float32)
|
260 |
+
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
261 |
+
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
262 |
+
|
263 |
+
|
264 |
+
class FFN(nn.Module):
|
265 |
+
def __init__(self, in_channels, out_channels, filter_channels, kernel_size, p_dropout=0., activation=None,
|
266 |
+
causal=False):
|
267 |
+
super().__init__()
|
268 |
+
self.in_channels = in_channels
|
269 |
+
self.out_channels = out_channels
|
270 |
+
self.filter_channels = filter_channels
|
271 |
+
self.kernel_size = kernel_size
|
272 |
+
self.p_dropout = p_dropout
|
273 |
+
self.activation = activation
|
274 |
+
self.causal = causal
|
275 |
+
|
276 |
+
if causal:
|
277 |
+
self.padding = self._causal_padding
|
278 |
+
else:
|
279 |
+
self.padding = self._same_padding
|
280 |
+
|
281 |
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
282 |
+
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
283 |
+
self.drop = nn.Dropout(p_dropout)
|
284 |
+
|
285 |
+
def forward(self, x, x_mask):
|
286 |
+
x = self.conv_1(self.padding(x * x_mask))
|
287 |
+
if self.activation == "gelu":
|
288 |
+
x = x * torch.sigmoid(1.702 * x)
|
289 |
+
else:
|
290 |
+
x = torch.relu(x)
|
291 |
+
x = self.drop(x)
|
292 |
+
x = self.conv_2(self.padding(x * x_mask))
|
293 |
+
return x * x_mask
|
294 |
+
|
295 |
+
def _causal_padding(self, x):
|
296 |
+
if self.kernel_size == 1:
|
297 |
+
return x
|
298 |
+
pad_l = self.kernel_size - 1
|
299 |
+
pad_r = 0
|
300 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
301 |
+
x = t_func.pad(x, commons.convert_pad_shape(padding))
|
302 |
+
return x
|
303 |
+
|
304 |
+
def _same_padding(self, x):
|
305 |
+
if self.kernel_size == 1:
|
306 |
+
return x
|
307 |
+
pad_l = (self.kernel_size - 1) // 2
|
308 |
+
pad_r = self.kernel_size // 2
|
309 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
310 |
+
x = t_func.pad(x, commons.convert_pad_shape(padding))
|
311 |
+
return x
|
commons.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from torch.nn import functional as t_func
|
5 |
+
|
6 |
+
|
7 |
+
def init_weights(m, mean=0.0, std=0.01):
|
8 |
+
classname = m.__class__.__name__
|
9 |
+
if classname.find("Conv") != -1:
|
10 |
+
m.weight.data.normal_(mean, std)
|
11 |
+
|
12 |
+
|
13 |
+
def get_padding(kernel_size, dilation=1):
|
14 |
+
return int((kernel_size * dilation - dilation) / 2)
|
15 |
+
|
16 |
+
|
17 |
+
def convert_pad_shape(pad_shape):
|
18 |
+
l = pad_shape[::-1]
|
19 |
+
pad_shape = [item for sublist in l for item in sublist]
|
20 |
+
return pad_shape
|
21 |
+
|
22 |
+
|
23 |
+
def intersperse(lst, item):
|
24 |
+
result = [item] * (len(lst) * 2 + 1)
|
25 |
+
result[1::2] = lst
|
26 |
+
return result
|
27 |
+
|
28 |
+
|
29 |
+
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
30 |
+
"""KL(P||Q)"""
|
31 |
+
kl = (logs_q - logs_p) - 0.5
|
32 |
+
kl += 0.5 * (torch.exp(2. * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2. * logs_q)
|
33 |
+
return kl
|
34 |
+
|
35 |
+
|
36 |
+
def rand_gumbel(shape):
|
37 |
+
"""Sample from the Gumbel distribution, protect from overflows."""
|
38 |
+
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
39 |
+
return -torch.log(-torch.log(uniform_samples))
|
40 |
+
|
41 |
+
|
42 |
+
def rand_gumbel_like(x):
|
43 |
+
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
44 |
+
return g
|
45 |
+
|
46 |
+
|
47 |
+
def slice_segments(x, ids_str, segment_size=4):
|
48 |
+
ret = torch.zeros_like(x[:, :, :segment_size])
|
49 |
+
for i in range(x.size(0)):
|
50 |
+
idx_str = ids_str[i]
|
51 |
+
idx_end = idx_str + segment_size
|
52 |
+
ret[i] = x[i, :, idx_str:idx_end]
|
53 |
+
return ret
|
54 |
+
|
55 |
+
|
56 |
+
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
57 |
+
b, d, t = x.size()
|
58 |
+
if x_lengths is None:
|
59 |
+
x_lengths = t
|
60 |
+
ids_str_max = x_lengths - segment_size + 1
|
61 |
+
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
62 |
+
ret = slice_segments(x, ids_str, segment_size)
|
63 |
+
return ret, ids_str
|
64 |
+
|
65 |
+
|
66 |
+
def get_timing_signal_1d(
|
67 |
+
length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
68 |
+
position = torch.arange(length, dtype=torch.float)
|
69 |
+
num_timescales = channels // 2
|
70 |
+
log_timescale_increment = (
|
71 |
+
math.log(float(max_timescale) / float(min_timescale)) /
|
72 |
+
(num_timescales - 1))
|
73 |
+
inv_timescales = min_timescale * torch.exp(
|
74 |
+
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment)
|
75 |
+
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
76 |
+
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
77 |
+
signal = t_func.pad(signal, [0, 0, 0, channels % 2])
|
78 |
+
signal = signal.view(1, channels, length)
|
79 |
+
return signal
|
80 |
+
|
81 |
+
|
82 |
+
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
83 |
+
b, channels, length = x.size()
|
84 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
85 |
+
return x + signal.to(dtype=x.dtype, device=x.device)
|
86 |
+
|
87 |
+
|
88 |
+
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
89 |
+
b, channels, length = x.size()
|
90 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
91 |
+
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
92 |
+
|
93 |
+
|
94 |
+
def subsequent_mask(length):
|
95 |
+
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
96 |
+
return mask
|
97 |
+
|
98 |
+
|
99 |
+
@torch.jit.script
|
100 |
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
101 |
+
n_channels_int = n_channels[0]
|
102 |
+
in_act = input_a + input_b
|
103 |
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
104 |
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
105 |
+
acts = t_act * s_act
|
106 |
+
return acts
|
107 |
+
|
108 |
+
|
109 |
+
def convert_pad_shape(pad_shape):
|
110 |
+
l = pad_shape[::-1]
|
111 |
+
pad_shape = [item for sublist in l for item in sublist]
|
112 |
+
return pad_shape
|
113 |
+
|
114 |
+
|
115 |
+
def shift_1d(x):
|
116 |
+
x = t_func.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
117 |
+
return x
|
118 |
+
|
119 |
+
|
120 |
+
def sequence_mask(length, max_length=None):
|
121 |
+
if max_length is None:
|
122 |
+
max_length = length.max()
|
123 |
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
124 |
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
125 |
+
|
126 |
+
|
127 |
+
def generate_path(duration, mask):
|
128 |
+
"""
|
129 |
+
duration: [b, 1, t_x]
|
130 |
+
mask: [b, 1, t_y, t_x]
|
131 |
+
"""
|
132 |
+
device = duration.device
|
133 |
+
|
134 |
+
b, _, t_y, t_x = mask.shape
|
135 |
+
cum_duration = torch.cumsum(duration, -1)
|
136 |
+
|
137 |
+
cum_duration_flat = cum_duration.view(b * t_x)
|
138 |
+
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
139 |
+
path = path.view(b, t_x, t_y)
|
140 |
+
path = path - t_func.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
141 |
+
path = path.unsqueeze(1).transpose(2, 3) * mask
|
142 |
+
return path
|
143 |
+
|
144 |
+
|
145 |
+
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
146 |
+
if isinstance(parameters, torch.Tensor):
|
147 |
+
parameters = [parameters]
|
148 |
+
parameters = list(filter(lambda para: para.grad is not None, parameters))
|
149 |
+
norm_type = float(norm_type)
|
150 |
+
if clip_value is not None:
|
151 |
+
clip_value = float(clip_value)
|
152 |
+
|
153 |
+
total_norm = 0
|
154 |
+
for p in parameters:
|
155 |
+
param_norm = p.grad.data.norm(norm_type)
|
156 |
+
total_norm += param_norm.item() ** norm_type
|
157 |
+
if clip_value is not None:
|
158 |
+
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
159 |
+
total_norm = total_norm ** (1. / norm_type)
|
160 |
+
return total_norm
|
configs/milky_green.json
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"train": {
|
3 |
+
"log_interval": 200,
|
4 |
+
"eval_interval": 2000,
|
5 |
+
"seed": 1234,
|
6 |
+
"epochs": 10000,
|
7 |
+
"learning_rate": 2e-4,
|
8 |
+
"betas": [
|
9 |
+
0.8,
|
10 |
+
0.99
|
11 |
+
],
|
12 |
+
"eps": 1e-9,
|
13 |
+
"batch_size": 16,
|
14 |
+
"fp16_run": true,
|
15 |
+
"lr_decay": 0.999875,
|
16 |
+
"segment_size": 8192,
|
17 |
+
"init_lr_ratio": 1,
|
18 |
+
"warmup_epochs": 0,
|
19 |
+
"c_mel": 45,
|
20 |
+
"c_kl": 1.0
|
21 |
+
},
|
22 |
+
"data": {
|
23 |
+
"training_files": "/root/sovits/filelist/train.txt",
|
24 |
+
"validation_files": "/root/sovits/filelist/val.txt",
|
25 |
+
"text_cleaners": [
|
26 |
+
"english_cleaners2"
|
27 |
+
],
|
28 |
+
"max_wav_value": 32768.0,
|
29 |
+
"sampling_rate": 22050,
|
30 |
+
"filter_length": 1024,
|
31 |
+
"hop_length": 256,
|
32 |
+
"win_length": 1024,
|
33 |
+
"n_mel_channels": 80,
|
34 |
+
"mel_fmin": 0.0,
|
35 |
+
"mel_fmax": null,
|
36 |
+
"add_blank": true,
|
37 |
+
"n_speakers": 8,
|
38 |
+
"cleaned_text": true
|
39 |
+
},
|
40 |
+
"model": {
|
41 |
+
"inter_channels": 192,
|
42 |
+
"hidden_channels": 256,
|
43 |
+
"filter_channels": 768,
|
44 |
+
"n_heads": 2,
|
45 |
+
"n_layers": 6,
|
46 |
+
"kernel_size": 3,
|
47 |
+
"p_dropout": 0.1,
|
48 |
+
"resblock": "1",
|
49 |
+
"resblock_kernel_sizes": [
|
50 |
+
3,
|
51 |
+
7,
|
52 |
+
11
|
53 |
+
],
|
54 |
+
"resblock_dilation_sizes": [
|
55 |
+
[
|
56 |
+
1,
|
57 |
+
3,
|
58 |
+
5
|
59 |
+
],
|
60 |
+
[
|
61 |
+
1,
|
62 |
+
3,
|
63 |
+
5
|
64 |
+
],
|
65 |
+
[
|
66 |
+
1,
|
67 |
+
3,
|
68 |
+
5
|
69 |
+
]
|
70 |
+
],
|
71 |
+
"upsample_rates": [
|
72 |
+
8,
|
73 |
+
8,
|
74 |
+
2,
|
75 |
+
2
|
76 |
+
],
|
77 |
+
"upsample_initial_channel": 512,
|
78 |
+
"upsample_kernel_sizes": [
|
79 |
+
16,
|
80 |
+
16,
|
81 |
+
4,
|
82 |
+
4
|
83 |
+
],
|
84 |
+
"n_layers_q": 3,
|
85 |
+
"use_spectral_norm": false,
|
86 |
+
"gin_channels": 256
|
87 |
+
},
|
88 |
+
"speakers": [
|
89 |
+
"nyaru",
|
90 |
+
"taffy",
|
91 |
+
"yunhao",
|
92 |
+
"jishuang",
|
93 |
+
"yilanqiu",
|
94 |
+
"opencpop"
|
95 |
+
]
|
96 |
+
}
|
configs/nyarumul.json
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"train": {
|
3 |
+
"log_interval": 200,
|
4 |
+
"eval_interval": 2000,
|
5 |
+
"seed": 1234,
|
6 |
+
"epochs": 10000,
|
7 |
+
"learning_rate": 2e-4,
|
8 |
+
"betas": [
|
9 |
+
0.8,
|
10 |
+
0.99
|
11 |
+
],
|
12 |
+
"eps": 1e-9,
|
13 |
+
"batch_size": 16,
|
14 |
+
"fp16_run": true,
|
15 |
+
"lr_decay": 0.999875,
|
16 |
+
"segment_size": 8192,
|
17 |
+
"init_lr_ratio": 1,
|
18 |
+
"warmup_epochs": 0,
|
19 |
+
"c_mel": 45,
|
20 |
+
"c_kl": 1.0
|
21 |
+
},
|
22 |
+
"data": {
|
23 |
+
"training_files": "/root/sovits/filelist/train.txt",
|
24 |
+
"validation_files": "/root/sovits/filelist/val.txt",
|
25 |
+
"text_cleaners": [
|
26 |
+
"english_cleaners2"
|
27 |
+
],
|
28 |
+
"max_wav_value": 32768.0,
|
29 |
+
"sampling_rate": 22050,
|
30 |
+
"filter_length": 1024,
|
31 |
+
"hop_length": 256,
|
32 |
+
"win_length": 1024,
|
33 |
+
"n_mel_channels": 80,
|
34 |
+
"mel_fmin": 0.0,
|
35 |
+
"mel_fmax": null,
|
36 |
+
"add_blank": true,
|
37 |
+
"n_speakers": 8,
|
38 |
+
"cleaned_text": true
|
39 |
+
},
|
40 |
+
"model": {
|
41 |
+
"inter_channels": 192,
|
42 |
+
"hidden_channels": 256,
|
43 |
+
"filter_channels": 768,
|
44 |
+
"n_heads": 2,
|
45 |
+
"n_layers": 6,
|
46 |
+
"kernel_size": 3,
|
47 |
+
"p_dropout": 0.1,
|
48 |
+
"resblock": "1",
|
49 |
+
"resblock_kernel_sizes": [
|
50 |
+
3,
|
51 |
+
7,
|
52 |
+
11
|
53 |
+
],
|
54 |
+
"resblock_dilation_sizes": [
|
55 |
+
[
|
56 |
+
1,
|
57 |
+
3,
|
58 |
+
5
|
59 |
+
],
|
60 |
+
[
|
61 |
+
1,
|
62 |
+
3,
|
63 |
+
5
|
64 |
+
],
|
65 |
+
[
|
66 |
+
1,
|
67 |
+
3,
|
68 |
+
5
|
69 |
+
]
|
70 |
+
],
|
71 |
+
"upsample_rates": [
|
72 |
+
8,
|
73 |
+
8,
|
74 |
+
2,
|
75 |
+
2
|
76 |
+
],
|
77 |
+
"upsample_initial_channel": 512,
|
78 |
+
"upsample_kernel_sizes": [
|
79 |
+
16,
|
80 |
+
16,
|
81 |
+
4,
|
82 |
+
4
|
83 |
+
],
|
84 |
+
"n_layers_q": 3,
|
85 |
+
"use_spectral_norm": false,
|
86 |
+
"gin_channels": 256
|
87 |
+
},
|
88 |
+
"speakers": [
|
89 |
+
"nyaru",
|
90 |
+
"taffy",
|
91 |
+
"yunhao",
|
92 |
+
"jishuang",
|
93 |
+
"yilanqiu",
|
94 |
+
"opencpop"
|
95 |
+
]
|
96 |
+
}
|
configs/sovits_pre.json
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"train": {
|
3 |
+
"log_interval": 200,
|
4 |
+
"eval_interval": 2000,
|
5 |
+
"seed": 1234,
|
6 |
+
"epochs": 10000,
|
7 |
+
"learning_rate": 2e-4,
|
8 |
+
"betas": [
|
9 |
+
0.8,
|
10 |
+
0.99
|
11 |
+
],
|
12 |
+
"eps": 1e-9,
|
13 |
+
"batch_size": 16,
|
14 |
+
"fp16_run": true,
|
15 |
+
"lr_decay": 0.999875,
|
16 |
+
"segment_size": 16384,
|
17 |
+
"init_lr_ratio": 1,
|
18 |
+
"warmup_epochs": 0,
|
19 |
+
"c_mel": 45,
|
20 |
+
"c_kl": 1.0
|
21 |
+
},
|
22 |
+
"data": {
|
23 |
+
"training_files": "/root/sovits/filelist/train.txt",
|
24 |
+
"validation_files": "/root/sovits/filelist/val.txt",
|
25 |
+
"text_cleaners": [
|
26 |
+
"english_cleaners2"
|
27 |
+
],
|
28 |
+
"max_wav_value": 32768.0,
|
29 |
+
"sampling_rate": 44100,
|
30 |
+
"filter_length": 2048,
|
31 |
+
"hop_length": 512,
|
32 |
+
"win_length": 2048,
|
33 |
+
"n_mel_channels": 128,
|
34 |
+
"mel_fmin": 0.0,
|
35 |
+
"mel_fmax": null,
|
36 |
+
"add_blank": true,
|
37 |
+
"n_speakers": 4,
|
38 |
+
"cleaned_text": true
|
39 |
+
},
|
40 |
+
"model": {
|
41 |
+
"inter_channels": 192,
|
42 |
+
"hidden_channels": 256,
|
43 |
+
"filter_channels": 768,
|
44 |
+
"n_heads": 2,
|
45 |
+
"n_layers": 6,
|
46 |
+
"kernel_size": 3,
|
47 |
+
"p_dropout": 0.1,
|
48 |
+
"resblock": "1",
|
49 |
+
"resblock_kernel_sizes": [
|
50 |
+
3,
|
51 |
+
7,
|
52 |
+
11
|
53 |
+
],
|
54 |
+
"resblock_dilation_sizes": [
|
55 |
+
[
|
56 |
+
1,
|
57 |
+
3,
|
58 |
+
5
|
59 |
+
],
|
60 |
+
[
|
61 |
+
1,
|
62 |
+
3,
|
63 |
+
5
|
64 |
+
],
|
65 |
+
[
|
66 |
+
1,
|
67 |
+
3,
|
68 |
+
5
|
69 |
+
]
|
70 |
+
],
|
71 |
+
"upsample_rates": [
|
72 |
+
8,
|
73 |
+
8,
|
74 |
+
4,
|
75 |
+
2
|
76 |
+
],
|
77 |
+
"upsample_initial_channel": 512,
|
78 |
+
"upsample_kernel_sizes": [
|
79 |
+
16,
|
80 |
+
16,
|
81 |
+
4,
|
82 |
+
4
|
83 |
+
],
|
84 |
+
"n_layers_q": 3,
|
85 |
+
"use_spectral_norm": false,
|
86 |
+
"gin_channels": 256
|
87 |
+
},
|
88 |
+
"speakers": [
|
89 |
+
"yilanqiu",
|
90 |
+
"opencpop",
|
91 |
+
"yunhao",
|
92 |
+
"jishuang"
|
93 |
+
]
|
94 |
+
}
|
configs/yilanqiu.json
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"train": {
|
3 |
+
"log_interval": 200,
|
4 |
+
"eval_interval": 2000,
|
5 |
+
"seed": 1234,
|
6 |
+
"epochs": 10000,
|
7 |
+
"learning_rate": 2e-4,
|
8 |
+
"betas": [
|
9 |
+
0.8,
|
10 |
+
0.99
|
11 |
+
],
|
12 |
+
"eps": 1e-9,
|
13 |
+
"batch_size": 16,
|
14 |
+
"fp16_run": true,
|
15 |
+
"lr_decay": 0.999875,
|
16 |
+
"segment_size": 8192,
|
17 |
+
"init_lr_ratio": 1,
|
18 |
+
"warmup_epochs": 0,
|
19 |
+
"c_mel": 45,
|
20 |
+
"c_kl": 1.0
|
21 |
+
},
|
22 |
+
"data": {
|
23 |
+
"training_files": "/root/content/qiu/train.txt",
|
24 |
+
"validation_files": "/root/content/qiu/val.txt",
|
25 |
+
"text_cleaners": [
|
26 |
+
"english_cleaners2"
|
27 |
+
],
|
28 |
+
"max_wav_value": 32768.0,
|
29 |
+
"sampling_rate": 22050,
|
30 |
+
"filter_length": 1024,
|
31 |
+
"hop_length": 256,
|
32 |
+
"win_length": 1024,
|
33 |
+
"n_mel_channels": 80,
|
34 |
+
"mel_fmin": 0.0,
|
35 |
+
"mel_fmax": null,
|
36 |
+
"add_blank": true,
|
37 |
+
"n_speakers": 3,
|
38 |
+
"cleaned_text": true
|
39 |
+
},
|
40 |
+
"model": {
|
41 |
+
"inter_channels": 192,
|
42 |
+
"hidden_channels": 256,
|
43 |
+
"filter_channels": 768,
|
44 |
+
"n_heads": 2,
|
45 |
+
"n_layers": 6,
|
46 |
+
"kernel_size": 3,
|
47 |
+
"p_dropout": 0.1,
|
48 |
+
"resblock": "1",
|
49 |
+
"resblock_kernel_sizes": [
|
50 |
+
3,
|
51 |
+
7,
|
52 |
+
11
|
53 |
+
],
|
54 |
+
"resblock_dilation_sizes": [
|
55 |
+
[
|
56 |
+
1,
|
57 |
+
3,
|
58 |
+
5
|
59 |
+
],
|
60 |
+
[
|
61 |
+
1,
|
62 |
+
3,
|
63 |
+
5
|
64 |
+
],
|
65 |
+
[
|
66 |
+
1,
|
67 |
+
3,
|
68 |
+
5
|
69 |
+
]
|
70 |
+
],
|
71 |
+
"upsample_rates": [
|
72 |
+
8,
|
73 |
+
8,
|
74 |
+
2,
|
75 |
+
2
|
76 |
+
],
|
77 |
+
"upsample_initial_channel": 512,
|
78 |
+
"upsample_kernel_sizes": [
|
79 |
+
16,
|
80 |
+
16,
|
81 |
+
4,
|
82 |
+
4
|
83 |
+
],
|
84 |
+
"n_layers_q": 3,
|
85 |
+
"use_spectral_norm": false,
|
86 |
+
"gin_channels": 256
|
87 |
+
},
|
88 |
+
"speakers": [
|
89 |
+
"maolei",
|
90 |
+
"x",
|
91 |
+
"yilanqiu"
|
92 |
+
]
|
93 |
+
}
|
data_utils.py
ADDED
@@ -0,0 +1,411 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
import torch.utils.data
|
7 |
+
from mel_processing import spectrogram_torch
|
8 |
+
|
9 |
+
from utils import load_wav_to_torch, load_filepaths_and_text
|
10 |
+
|
11 |
+
|
12 |
+
def dropout1d(myarray, ratio=0.5):
|
13 |
+
indices = np.random.choice(np.arange(myarray.size), replace=False,
|
14 |
+
size=int(myarray.size * ratio))
|
15 |
+
myarray[indices] = 0
|
16 |
+
return myarray
|
17 |
+
|
18 |
+
|
19 |
+
class TextAudioLoader(torch.utils.data.Dataset):
|
20 |
+
"""
|
21 |
+
1) loads audio, text pairs
|
22 |
+
2) normalizes text and converts them to sequences of integers
|
23 |
+
3) computes spectrograms from audio files.
|
24 |
+
"""
|
25 |
+
|
26 |
+
def __init__(self, audiopaths_and_text, hparams):
|
27 |
+
self.audiopaths_and_text = load_filepaths_and_text(audiopaths_and_text)
|
28 |
+
self.text_cleaners = hparams.text_cleaners
|
29 |
+
self.max_wav_value = hparams.max_wav_value
|
30 |
+
self.sampling_rate = hparams.sampling_rate
|
31 |
+
self.filter_length = hparams.filter_length
|
32 |
+
self.hop_length = hparams.hop_length
|
33 |
+
self.win_length = hparams.win_length
|
34 |
+
self.sampling_rate = hparams.sampling_rate
|
35 |
+
|
36 |
+
self.cleaned_text = getattr(hparams, "cleaned_text", False)
|
37 |
+
|
38 |
+
self.add_blank = hparams.add_blank
|
39 |
+
self.min_text_len = getattr(hparams, "min_text_len", 1)
|
40 |
+
self.max_text_len = getattr(hparams, "max_text_len", 190)
|
41 |
+
|
42 |
+
random.seed(1234)
|
43 |
+
random.shuffle(self.audiopaths_and_text)
|
44 |
+
self._filter()
|
45 |
+
|
46 |
+
def _filter(self):
|
47 |
+
"""
|
48 |
+
Filter text & store spec lengths
|
49 |
+
"""
|
50 |
+
# Store spectrogram lengths for Bucketing
|
51 |
+
# wav_length ~= file_size / (wav_channels * Bytes per dim) = file_size / (1 * 2)
|
52 |
+
# spec_length = wav_length // hop_length
|
53 |
+
lengths = []
|
54 |
+
for audiopath, text, pitch in self.audiopaths_and_text:
|
55 |
+
lengths.append(os.path.getsize(audiopath) // (2 * self.hop_length))
|
56 |
+
self.lengths = lengths
|
57 |
+
|
58 |
+
def get_audio_text_pair(self, audiopath_and_text):
|
59 |
+
# separate filename and text
|
60 |
+
audiopath, text, pitch = audiopath_and_text[0], audiopath_and_text[1], audiopath_and_text[2]
|
61 |
+
text = self.get_text(text)
|
62 |
+
spec, wav = self.get_audio(audiopath)
|
63 |
+
pitch = self.get_pitch(pitch)
|
64 |
+
return text, spec, wav, pitch
|
65 |
+
|
66 |
+
def get_pitch(self, pitch):
|
67 |
+
|
68 |
+
return torch.LongTensor(np.load(pitch))
|
69 |
+
|
70 |
+
def get_audio(self, filename):
|
71 |
+
audio, sampling_rate = load_wav_to_torch(filename)
|
72 |
+
if sampling_rate != self.sampling_rate:
|
73 |
+
raise ValueError("{} {} SR doesn't match target {} SR".format(
|
74 |
+
sampling_rate, self.sampling_rate))
|
75 |
+
audio_norm = audio / self.max_wav_value
|
76 |
+
audio_norm = audio_norm.unsqueeze(0)
|
77 |
+
spec_filename = filename.replace(".wav", ".spec.pt")
|
78 |
+
if os.path.exists(spec_filename):
|
79 |
+
spec = torch.load(spec_filename)
|
80 |
+
else:
|
81 |
+
spec = spectrogram_torch(audio_norm, self.filter_length,
|
82 |
+
self.sampling_rate, self.hop_length, self.win_length,
|
83 |
+
center=False)
|
84 |
+
spec = torch.squeeze(spec, 0)
|
85 |
+
torch.save(spec, spec_filename)
|
86 |
+
return spec, audio_norm
|
87 |
+
|
88 |
+
def get_text(self, text):
|
89 |
+
soft = np.load(text)
|
90 |
+
text_norm = torch.FloatTensor(soft)
|
91 |
+
return text_norm
|
92 |
+
|
93 |
+
def __getitem__(self, index):
|
94 |
+
return self.get_audio_text_pair(self.audiopaths_and_text[index])
|
95 |
+
|
96 |
+
def __len__(self):
|
97 |
+
return len(self.audiopaths_and_text)
|
98 |
+
|
99 |
+
|
100 |
+
class TextAudioCollate:
|
101 |
+
""" Zero-pads model inputs and targets
|
102 |
+
"""
|
103 |
+
|
104 |
+
def __init__(self, return_ids=False):
|
105 |
+
self.return_ids = return_ids
|
106 |
+
|
107 |
+
def __call__(self, batch):
|
108 |
+
"""Collate's training batch from normalized text and aduio
|
109 |
+
PARAMS
|
110 |
+
------
|
111 |
+
batch: [text_normalized, spec_normalized, wav_normalized]
|
112 |
+
"""
|
113 |
+
# Right zero-pad all one-hot text sequences to max input length
|
114 |
+
_, ids_sorted_decreasing = torch.sort(
|
115 |
+
torch.LongTensor([x[1].size(1) for x in batch]),
|
116 |
+
dim=0, descending=True)
|
117 |
+
|
118 |
+
max_text_len = max([len(x[0]) for x in batch])
|
119 |
+
max_spec_len = max([x[1].size(1) for x in batch])
|
120 |
+
max_wav_len = max([x[2].size(1) for x in batch])
|
121 |
+
max_pitch_len = max([x[3].shape[0] for x in batch])
|
122 |
+
# print(batch)
|
123 |
+
|
124 |
+
text_lengths = torch.LongTensor(len(batch))
|
125 |
+
spec_lengths = torch.LongTensor(len(batch))
|
126 |
+
wav_lengths = torch.LongTensor(len(batch))
|
127 |
+
|
128 |
+
text_padded = torch.FloatTensor(len(batch), max_text_len, 256)
|
129 |
+
spec_padded = torch.FloatTensor(len(batch), batch[0][1].size(0), max_spec_len)
|
130 |
+
wav_padded = torch.FloatTensor(len(batch), 1, max_wav_len)
|
131 |
+
pitch_padded = torch.LongTensor(len(batch), max_pitch_len)
|
132 |
+
|
133 |
+
text_padded.zero_()
|
134 |
+
spec_padded.zero_()
|
135 |
+
wav_padded.zero_()
|
136 |
+
pitch_padded.zero_()
|
137 |
+
for i in range(len(ids_sorted_decreasing)):
|
138 |
+
row = batch[ids_sorted_decreasing[i]]
|
139 |
+
|
140 |
+
text = row[0]
|
141 |
+
text_padded[i, :text.size(0), :] = text
|
142 |
+
text_lengths[i] = text.size(0)
|
143 |
+
|
144 |
+
spec = row[1]
|
145 |
+
spec_padded[i, :, :spec.size(1)] = spec
|
146 |
+
spec_lengths[i] = spec.size(1)
|
147 |
+
|
148 |
+
wav = row[2]
|
149 |
+
wav_padded[i, :, :wav.size(1)] = wav
|
150 |
+
wav_lengths[i] = wav.size(1)
|
151 |
+
|
152 |
+
pitch = row[3]
|
153 |
+
pitch_padded[i, :pitch.size(0)] = pitch
|
154 |
+
|
155 |
+
if self.return_ids:
|
156 |
+
return text_padded, text_lengths, spec_padded, spec_lengths, wav_padded, wav_lengths, ids_sorted_decreasing, pitch_padded
|
157 |
+
return text_padded, text_lengths, spec_padded, spec_lengths, wav_padded, wav_lengths, pitch_padded
|
158 |
+
|
159 |
+
|
160 |
+
"""Multi speaker version"""
|
161 |
+
|
162 |
+
|
163 |
+
class TextAudioSpeakerLoader(torch.utils.data.Dataset):
|
164 |
+
"""
|
165 |
+
1) loads audio, speaker_id, text pairs
|
166 |
+
2) normalizes text and converts them to sequences of integers
|
167 |
+
3) computes spectrograms from audio files.
|
168 |
+
"""
|
169 |
+
|
170 |
+
def __init__(self, audiopaths_sid_text, hparams):
|
171 |
+
self.audiopaths_sid_text = load_filepaths_and_text(audiopaths_sid_text)
|
172 |
+
self.text_cleaners = hparams.text_cleaners
|
173 |
+
self.max_wav_value = hparams.max_wav_value
|
174 |
+
self.sampling_rate = hparams.sampling_rate
|
175 |
+
self.filter_length = hparams.filter_length
|
176 |
+
self.hop_length = hparams.hop_length
|
177 |
+
self.win_length = hparams.win_length
|
178 |
+
self.sampling_rate = hparams.sampling_rate
|
179 |
+
|
180 |
+
self.cleaned_text = getattr(hparams, "cleaned_text", False)
|
181 |
+
|
182 |
+
self.add_blank = hparams.add_blank
|
183 |
+
self.min_text_len = getattr(hparams, "min_text_len", 1)
|
184 |
+
self.max_text_len = getattr(hparams, "max_text_len", 190)
|
185 |
+
|
186 |
+
random.seed(1234)
|
187 |
+
random.shuffle(self.audiopaths_sid_text)
|
188 |
+
self._filter()
|
189 |
+
|
190 |
+
def _filter(self):
|
191 |
+
"""
|
192 |
+
Filter text & store spec lengths
|
193 |
+
"""
|
194 |
+
# Store spectrogram lengths for Bucketing
|
195 |
+
# wav_length ~= file_size / (wav_channels * Bytes per dim) = file_size / (1 * 2)
|
196 |
+
# spec_length = wav_length // hop_length
|
197 |
+
|
198 |
+
lengths = []
|
199 |
+
for audiopath, sid, text, pitch in self.audiopaths_sid_text:
|
200 |
+
lengths.append(os.path.getsize(audiopath) // (2 * self.hop_length))
|
201 |
+
self.lengths = lengths
|
202 |
+
|
203 |
+
def get_audio_text_speaker_pair(self, audiopath_sid_text):
|
204 |
+
# separate filename, speaker_id and text
|
205 |
+
audiopath, sid, text, pitch = audiopath_sid_text[0], audiopath_sid_text[1], audiopath_sid_text[2], \
|
206 |
+
audiopath_sid_text[3]
|
207 |
+
text = self.get_text(text)
|
208 |
+
spec, wav = self.get_audio(audiopath)
|
209 |
+
sid = self.get_sid(sid)
|
210 |
+
pitch = self.get_pitch(pitch)
|
211 |
+
|
212 |
+
return text, spec, wav, pitch, sid
|
213 |
+
|
214 |
+
def get_audio(self, filename):
|
215 |
+
audio, sampling_rate = load_wav_to_torch(filename)
|
216 |
+
if sampling_rate != self.sampling_rate:
|
217 |
+
raise ValueError("{} {} SR doesn't match target {} SR".format(
|
218 |
+
sampling_rate, self.sampling_rate))
|
219 |
+
audio_norm = audio / self.max_wav_value
|
220 |
+
audio_norm = audio_norm.unsqueeze(0)
|
221 |
+
spec_filename = filename.replace(".wav", ".spec.pt")
|
222 |
+
if os.path.exists(spec_filename):
|
223 |
+
spec = torch.load(spec_filename)
|
224 |
+
else:
|
225 |
+
spec = spectrogram_torch(audio_norm, self.filter_length,
|
226 |
+
self.sampling_rate, self.hop_length, self.win_length,
|
227 |
+
center=False)
|
228 |
+
spec = torch.squeeze(spec, 0)
|
229 |
+
torch.save(spec, spec_filename)
|
230 |
+
return spec, audio_norm
|
231 |
+
|
232 |
+
def get_text(self, text):
|
233 |
+
soft = np.load(text)
|
234 |
+
text_norm = torch.FloatTensor(soft)
|
235 |
+
return text_norm
|
236 |
+
|
237 |
+
def get_pitch(self, pitch):
|
238 |
+
return torch.LongTensor(np.load(pitch))
|
239 |
+
|
240 |
+
def get_sid(self, sid):
|
241 |
+
sid = torch.LongTensor([int(sid)])
|
242 |
+
return sid
|
243 |
+
|
244 |
+
def __getitem__(self, index):
|
245 |
+
return self.get_audio_text_speaker_pair(self.audiopaths_sid_text[index])
|
246 |
+
|
247 |
+
def __len__(self):
|
248 |
+
return len(self.audiopaths_sid_text)
|
249 |
+
|
250 |
+
|
251 |
+
class TextAudioSpeakerCollate:
|
252 |
+
""" Zero-pads model inputs and targets
|
253 |
+
"""
|
254 |
+
|
255 |
+
def __init__(self, return_ids=False):
|
256 |
+
self.return_ids = return_ids
|
257 |
+
|
258 |
+
def __call__(self, batch):
|
259 |
+
"""Collate's training batch from normalized text, audio and speaker identities
|
260 |
+
PARAMS
|
261 |
+
------
|
262 |
+
batch: [text_normalized, spec_normalized, wav_normalized, sid]
|
263 |
+
"""
|
264 |
+
# Right zero-pad all one-hot text sequences to max input length
|
265 |
+
_, ids_sorted_decreasing = torch.sort(
|
266 |
+
torch.LongTensor([x[1].size(1) for x in batch]),
|
267 |
+
dim=0, descending=True)
|
268 |
+
|
269 |
+
max_text_len = max([len(x[0]) for x in batch])
|
270 |
+
max_spec_len = max([x[1].size(1) for x in batch])
|
271 |
+
max_wav_len = max([x[2].size(1) for x in batch])
|
272 |
+
max_pitch_len = max([x[3].shape[0] for x in batch])
|
273 |
+
|
274 |
+
text_lengths = torch.LongTensor(len(batch))
|
275 |
+
spec_lengths = torch.LongTensor(len(batch))
|
276 |
+
wav_lengths = torch.LongTensor(len(batch))
|
277 |
+
sid = torch.LongTensor(len(batch))
|
278 |
+
|
279 |
+
text_padded = torch.FloatTensor(len(batch), max_text_len, 256)
|
280 |
+
spec_padded = torch.FloatTensor(len(batch), batch[0][1].size(0), max_spec_len)
|
281 |
+
wav_padded = torch.FloatTensor(len(batch), 1, max_wav_len)
|
282 |
+
pitch_padded = torch.LongTensor(len(batch), max_pitch_len)
|
283 |
+
|
284 |
+
text_padded.zero_()
|
285 |
+
spec_padded.zero_()
|
286 |
+
wav_padded.zero_()
|
287 |
+
pitch_padded.zero_()
|
288 |
+
|
289 |
+
for i in range(len(ids_sorted_decreasing)):
|
290 |
+
row = batch[ids_sorted_decreasing[i]]
|
291 |
+
|
292 |
+
text = row[0]
|
293 |
+
text_padded[i, :text.size(0)] = text
|
294 |
+
text_lengths[i] = text.size(0)
|
295 |
+
|
296 |
+
spec = row[1]
|
297 |
+
spec_padded[i, :, :spec.size(1)] = spec
|
298 |
+
spec_lengths[i] = spec.size(1)
|
299 |
+
|
300 |
+
wav = row[2]
|
301 |
+
wav_padded[i, :, :wav.size(1)] = wav
|
302 |
+
wav_lengths[i] = wav.size(1)
|
303 |
+
|
304 |
+
pitch = row[3]
|
305 |
+
pitch_padded[i, :pitch.size(0)] = pitch
|
306 |
+
|
307 |
+
sid[i] = row[4]
|
308 |
+
|
309 |
+
if self.return_ids:
|
310 |
+
return text_padded, text_lengths, spec_padded, spec_lengths, wav_padded, wav_lengths, pitch_padded, sid, ids_sorted_decreasing
|
311 |
+
return text_padded, text_lengths, spec_padded, spec_lengths, wav_padded, wav_lengths, pitch_padded, sid
|
312 |
+
|
313 |
+
|
314 |
+
class DistributedBucketSampler(torch.utils.data.distributed.DistributedSampler):
|
315 |
+
"""
|
316 |
+
Maintain similar input lengths in a batch.
|
317 |
+
Length groups are specified by boundaries.
|
318 |
+
Ex) boundaries = [b1, b2, b3] -> any batch is included either {x | b1 < length(x) <=b2} or {x | b2 < length(x) <= b3}.
|
319 |
+
|
320 |
+
It removes samples which are not included in the boundaries.
|
321 |
+
Ex) boundaries = [b1, b2, b3] -> any x s.t. length(x) <= b1 or length(x) > b3 are discarded.
|
322 |
+
"""
|
323 |
+
|
324 |
+
def __init__(self, dataset, batch_size, boundaries, num_replicas=None, rank=None, shuffle=True):
|
325 |
+
super().__init__(dataset, num_replicas=num_replicas, rank=rank, shuffle=shuffle)
|
326 |
+
self.lengths = dataset.lengths
|
327 |
+
self.batch_size = batch_size
|
328 |
+
self.boundaries = boundaries
|
329 |
+
|
330 |
+
self.buckets, self.num_samples_per_bucket = self._create_buckets()
|
331 |
+
self.total_size = sum(self.num_samples_per_bucket)
|
332 |
+
self.num_samples = self.total_size // self.num_replicas
|
333 |
+
|
334 |
+
def _create_buckets(self):
|
335 |
+
buckets = [[] for _ in range(len(self.boundaries) - 1)]
|
336 |
+
for i in range(len(self.lengths)):
|
337 |
+
length = self.lengths[i]
|
338 |
+
idx_bucket = self._bisect(length)
|
339 |
+
if idx_bucket != -1:
|
340 |
+
buckets[idx_bucket].append(i)
|
341 |
+
|
342 |
+
for i in range(len(buckets) - 1, 0, -1):
|
343 |
+
if len(buckets[i]) == 0:
|
344 |
+
buckets.pop(i)
|
345 |
+
self.boundaries.pop(i + 1)
|
346 |
+
|
347 |
+
num_samples_per_bucket = []
|
348 |
+
for i in range(len(buckets)):
|
349 |
+
len_bucket = len(buckets[i])
|
350 |
+
total_batch_size = self.num_replicas * self.batch_size
|
351 |
+
rem = (total_batch_size - (len_bucket % total_batch_size)) % total_batch_size
|
352 |
+
num_samples_per_bucket.append(len_bucket + rem)
|
353 |
+
return buckets, num_samples_per_bucket
|
354 |
+
|
355 |
+
def __iter__(self):
|
356 |
+
# deterministically shuffle based on epoch
|
357 |
+
g = torch.Generator()
|
358 |
+
g.manual_seed(self.epoch)
|
359 |
+
|
360 |
+
indices = []
|
361 |
+
if self.shuffle:
|
362 |
+
for bucket in self.buckets:
|
363 |
+
indices.append(torch.randperm(len(bucket), generator=g).tolist())
|
364 |
+
else:
|
365 |
+
for bucket in self.buckets:
|
366 |
+
indices.append(list(range(len(bucket))))
|
367 |
+
|
368 |
+
batches = []
|
369 |
+
for i in range(len(self.buckets)):
|
370 |
+
bucket = self.buckets[i]
|
371 |
+
len_bucket = len(bucket)
|
372 |
+
ids_bucket = indices[i]
|
373 |
+
num_samples_bucket = self.num_samples_per_bucket[i]
|
374 |
+
|
375 |
+
# add extra samples to make it evenly divisible
|
376 |
+
rem = num_samples_bucket - len_bucket
|
377 |
+
ids_bucket = ids_bucket + ids_bucket * (rem // len_bucket) + ids_bucket[:(rem % len_bucket)]
|
378 |
+
|
379 |
+
# subsample
|
380 |
+
ids_bucket = ids_bucket[self.rank::self.num_replicas]
|
381 |
+
|
382 |
+
# batching
|
383 |
+
for j in range(len(ids_bucket) // self.batch_size):
|
384 |
+
batch = [bucket[idx] for idx in ids_bucket[j * self.batch_size:(j + 1) * self.batch_size]]
|
385 |
+
batches.append(batch)
|
386 |
+
|
387 |
+
if self.shuffle:
|
388 |
+
batch_ids = torch.randperm(len(batches), generator=g).tolist()
|
389 |
+
batches = [batches[i] for i in batch_ids]
|
390 |
+
self.batches = batches
|
391 |
+
|
392 |
+
assert len(self.batches) * self.batch_size == self.num_samples
|
393 |
+
return iter(self.batches)
|
394 |
+
|
395 |
+
def _bisect(self, x, lo=0, hi=None):
|
396 |
+
if hi is None:
|
397 |
+
hi = len(self.boundaries) - 1
|
398 |
+
|
399 |
+
if hi > lo:
|
400 |
+
mid = (hi + lo) // 2
|
401 |
+
if self.boundaries[mid] < x <= self.boundaries[mid + 1]:
|
402 |
+
return mid
|
403 |
+
elif x <= self.boundaries[mid]:
|
404 |
+
return self._bisect(x, lo, mid)
|
405 |
+
else:
|
406 |
+
return self._bisect(x, mid + 1, hi)
|
407 |
+
else:
|
408 |
+
return -1
|
409 |
+
|
410 |
+
def __len__(self):
|
411 |
+
return self.num_samples // self.batch_size
|
hubert.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e82e7d079df05fe3aa535f6f7d42d309bdae1d2a53324e2b2386c56721f4f649
|
3 |
+
size 378435957
|
hubert_model.py
ADDED
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import random
|
3 |
+
from typing import Optional, Tuple
|
4 |
+
|
5 |
+
import torch
|
6 |
+
import torch.nn as nn
|
7 |
+
import torch.nn.functional as t_func
|
8 |
+
from torch.nn.modules.utils import consume_prefix_in_state_dict_if_present
|
9 |
+
|
10 |
+
|
11 |
+
class Hubert(nn.Module):
|
12 |
+
def __init__(self, num_label_embeddings: int = 100, mask: bool = True):
|
13 |
+
super().__init__()
|
14 |
+
self._mask = mask
|
15 |
+
self.feature_extractor = FeatureExtractor()
|
16 |
+
self.feature_projection = FeatureProjection()
|
17 |
+
self.positional_embedding = PositionalConvEmbedding()
|
18 |
+
self.norm = nn.LayerNorm(768)
|
19 |
+
self.dropout = nn.Dropout(0.1)
|
20 |
+
self.encoder = TransformerEncoder(
|
21 |
+
nn.TransformerEncoderLayer(
|
22 |
+
768, 12, 3072, activation="gelu", batch_first=True
|
23 |
+
),
|
24 |
+
12,
|
25 |
+
)
|
26 |
+
self.proj = nn.Linear(768, 256)
|
27 |
+
|
28 |
+
self.masked_spec_embed = nn.Parameter(torch.FloatTensor(768).uniform_())
|
29 |
+
self.label_embedding = nn.Embedding(num_label_embeddings, 256)
|
30 |
+
|
31 |
+
def mask(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
32 |
+
mask = None
|
33 |
+
if self.training and self._mask:
|
34 |
+
mask = _compute_mask((x.size(0), x.size(1)), 0.8, 10, x.device, 2)
|
35 |
+
x[mask] = self.masked_spec_embed.to(x.dtype)
|
36 |
+
return x, mask
|
37 |
+
|
38 |
+
def encode(
|
39 |
+
self, x: torch.Tensor, layer: Optional[int] = None
|
40 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
41 |
+
x = self.feature_extractor(x)
|
42 |
+
x = self.feature_projection(x.transpose(1, 2))
|
43 |
+
x, mask = self.mask(x)
|
44 |
+
x = x + self.positional_embedding(x)
|
45 |
+
x = self.dropout(self.norm(x))
|
46 |
+
x = self.encoder(x, output_layer=layer)
|
47 |
+
return x, mask
|
48 |
+
|
49 |
+
def logits(self, x: torch.Tensor) -> torch.Tensor:
|
50 |
+
logits = torch.cosine_similarity(
|
51 |
+
x.unsqueeze(2),
|
52 |
+
self.label_embedding.weight.unsqueeze(0).unsqueeze(0),
|
53 |
+
dim=-1,
|
54 |
+
)
|
55 |
+
return logits / 0.1
|
56 |
+
|
57 |
+
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
58 |
+
x, mask = self.encode(x)
|
59 |
+
x = self.proj(x)
|
60 |
+
logits = self.logits(x)
|
61 |
+
return logits, mask
|
62 |
+
|
63 |
+
|
64 |
+
class HubertSoft(Hubert):
|
65 |
+
def __init__(self):
|
66 |
+
super().__init__()
|
67 |
+
|
68 |
+
@torch.inference_mode()
|
69 |
+
def units(self, wav: torch.Tensor) -> torch.Tensor:
|
70 |
+
wav = t_func.pad(wav, ((400 - 320) // 2, (400 - 320) // 2))
|
71 |
+
x, _ = self.encode(wav)
|
72 |
+
return self.proj(x)
|
73 |
+
|
74 |
+
|
75 |
+
class FeatureExtractor(nn.Module):
|
76 |
+
def __init__(self):
|
77 |
+
super().__init__()
|
78 |
+
self.conv0 = nn.Conv1d(1, 512, 10, 5, bias=False)
|
79 |
+
self.norm0 = nn.GroupNorm(512, 512)
|
80 |
+
self.conv1 = nn.Conv1d(512, 512, 3, 2, bias=False)
|
81 |
+
self.conv2 = nn.Conv1d(512, 512, 3, 2, bias=False)
|
82 |
+
self.conv3 = nn.Conv1d(512, 512, 3, 2, bias=False)
|
83 |
+
self.conv4 = nn.Conv1d(512, 512, 3, 2, bias=False)
|
84 |
+
self.conv5 = nn.Conv1d(512, 512, 2, 2, bias=False)
|
85 |
+
self.conv6 = nn.Conv1d(512, 512, 2, 2, bias=False)
|
86 |
+
|
87 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
88 |
+
x = t_func.gelu(self.norm0(self.conv0(x)))
|
89 |
+
x = t_func.gelu(self.conv1(x))
|
90 |
+
x = t_func.gelu(self.conv2(x))
|
91 |
+
x = t_func.gelu(self.conv3(x))
|
92 |
+
x = t_func.gelu(self.conv4(x))
|
93 |
+
x = t_func.gelu(self.conv5(x))
|
94 |
+
x = t_func.gelu(self.conv6(x))
|
95 |
+
return x
|
96 |
+
|
97 |
+
|
98 |
+
class FeatureProjection(nn.Module):
|
99 |
+
def __init__(self):
|
100 |
+
super().__init__()
|
101 |
+
self.norm = nn.LayerNorm(512)
|
102 |
+
self.projection = nn.Linear(512, 768)
|
103 |
+
self.dropout = nn.Dropout(0.1)
|
104 |
+
|
105 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
106 |
+
x = self.norm(x)
|
107 |
+
x = self.projection(x)
|
108 |
+
x = self.dropout(x)
|
109 |
+
return x
|
110 |
+
|
111 |
+
|
112 |
+
class PositionalConvEmbedding(nn.Module):
|
113 |
+
def __init__(self):
|
114 |
+
super().__init__()
|
115 |
+
self.conv = nn.Conv1d(
|
116 |
+
768,
|
117 |
+
768,
|
118 |
+
kernel_size=128,
|
119 |
+
padding=128 // 2,
|
120 |
+
groups=16,
|
121 |
+
)
|
122 |
+
self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
|
123 |
+
|
124 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
125 |
+
x = self.conv(x.transpose(1, 2))
|
126 |
+
x = t_func.gelu(x[:, :, :-1])
|
127 |
+
return x.transpose(1, 2)
|
128 |
+
|
129 |
+
|
130 |
+
class TransformerEncoder(nn.Module):
|
131 |
+
def __init__(
|
132 |
+
self, encoder_layer: nn.TransformerEncoderLayer, num_layers: int
|
133 |
+
) -> None:
|
134 |
+
super(TransformerEncoder, self).__init__()
|
135 |
+
self.layers = nn.ModuleList(
|
136 |
+
[copy.deepcopy(encoder_layer) for _ in range(num_layers)]
|
137 |
+
)
|
138 |
+
self.num_layers = num_layers
|
139 |
+
|
140 |
+
def forward(
|
141 |
+
self,
|
142 |
+
src: torch.Tensor,
|
143 |
+
mask: torch.Tensor = None,
|
144 |
+
src_key_padding_mask: torch.Tensor = None,
|
145 |
+
output_layer: Optional[int] = None,
|
146 |
+
) -> torch.Tensor:
|
147 |
+
output = src
|
148 |
+
for layer in self.layers[:output_layer]:
|
149 |
+
output = layer(
|
150 |
+
output, src_mask=mask, src_key_padding_mask=src_key_padding_mask
|
151 |
+
)
|
152 |
+
return output
|
153 |
+
|
154 |
+
|
155 |
+
def _compute_mask(
|
156 |
+
shape: Tuple[int, int],
|
157 |
+
mask_prob: float,
|
158 |
+
mask_length: int,
|
159 |
+
device: torch.device,
|
160 |
+
min_masks: int = 0,
|
161 |
+
) -> torch.Tensor:
|
162 |
+
batch_size, sequence_length = shape
|
163 |
+
|
164 |
+
if mask_length < 1:
|
165 |
+
raise ValueError("`mask_length` has to be bigger than 0.")
|
166 |
+
|
167 |
+
if mask_length > sequence_length:
|
168 |
+
raise ValueError(
|
169 |
+
f"`mask_length` has to be smaller than `sequence_length`, but got `mask_length`: {mask_length} and `sequence_length`: {sequence_length}`"
|
170 |
+
)
|
171 |
+
|
172 |
+
# compute number of masked spans in batch
|
173 |
+
num_masked_spans = int(mask_prob * sequence_length / mask_length + random.random())
|
174 |
+
num_masked_spans = max(num_masked_spans, min_masks)
|
175 |
+
|
176 |
+
# make sure num masked indices <= sequence_length
|
177 |
+
if num_masked_spans * mask_length > sequence_length:
|
178 |
+
num_masked_spans = sequence_length // mask_length
|
179 |
+
|
180 |
+
# SpecAugment mask to fill
|
181 |
+
mask = torch.zeros((batch_size, sequence_length), device=device, dtype=torch.bool)
|
182 |
+
|
183 |
+
# uniform distribution to sample from, make sure that offset samples are < sequence_length
|
184 |
+
uniform_dist = torch.ones(
|
185 |
+
(batch_size, sequence_length - (mask_length - 1)), device=device
|
186 |
+
)
|
187 |
+
|
188 |
+
# get random indices to mask
|
189 |
+
mask_indices = torch.multinomial(uniform_dist, num_masked_spans)
|
190 |
+
|
191 |
+
# expand masked indices to masked spans
|
192 |
+
mask_indices = (
|
193 |
+
mask_indices.unsqueeze(dim=-1)
|
194 |
+
.expand((batch_size, num_masked_spans, mask_length))
|
195 |
+
.reshape(batch_size, num_masked_spans * mask_length)
|
196 |
+
)
|
197 |
+
offsets = (
|
198 |
+
torch.arange(mask_length, device=device)[None, None, :]
|
199 |
+
.expand((batch_size, num_masked_spans, mask_length))
|
200 |
+
.reshape(batch_size, num_masked_spans * mask_length)
|
201 |
+
)
|
202 |
+
mask_idxs = mask_indices + offsets
|
203 |
+
|
204 |
+
# scatter indices to mask
|
205 |
+
mask = mask.scatter(1, mask_idxs, True)
|
206 |
+
|
207 |
+
return mask
|
208 |
+
|
209 |
+
|
210 |
+
def hubert_soft(
|
211 |
+
path: str
|
212 |
+
) -> HubertSoft:
|
213 |
+
r"""HuBERT-Soft from `"A Comparison of Discrete and Soft Speech Units for Improved Voice Conversion"`.
|
214 |
+
Args:
|
215 |
+
path (str): path of a pretrained model
|
216 |
+
"""
|
217 |
+
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
218 |
+
hubert = HubertSoft()
|
219 |
+
checkpoint = torch.load(path)
|
220 |
+
consume_prefix_in_state_dict_if_present(checkpoint, "module.")
|
221 |
+
hubert.load_state_dict(checkpoint)
|
222 |
+
hubert.eval().to(dev)
|
223 |
+
return hubert
|
infer_tool.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import numpy as np
|
7 |
+
import torch
|
8 |
+
import torchaudio
|
9 |
+
|
10 |
+
import hubert_model
|
11 |
+
import utils
|
12 |
+
from models import SynthesizerTrn
|
13 |
+
from preprocess_wave import FeatureInput
|
14 |
+
|
15 |
+
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
16 |
+
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
|
18 |
+
|
19 |
+
def timeit(func):
|
20 |
+
def run(*args, **kwargs):
|
21 |
+
t = time.time()
|
22 |
+
res = func(*args, **kwargs)
|
23 |
+
print('executing \'%s\' costed %.3fs' % (func.__name__, time.time() - t))
|
24 |
+
return res
|
25 |
+
|
26 |
+
return run
|
27 |
+
|
28 |
+
|
29 |
+
def get_end_file(dir_path, end):
|
30 |
+
file_lists = []
|
31 |
+
for root, dirs, files in os.walk(dir_path):
|
32 |
+
files = [f for f in files if f[0] != '.']
|
33 |
+
dirs[:] = [d for d in dirs if d[0] != '.']
|
34 |
+
for f_file in files:
|
35 |
+
if f_file.endswith(end):
|
36 |
+
file_lists.append(os.path.join(root, f_file).replace("\\", "/"))
|
37 |
+
return file_lists
|
38 |
+
|
39 |
+
|
40 |
+
def load_model(model_path, config_path):
|
41 |
+
# 获取模型配置
|
42 |
+
hps_ms = utils.get_hparams_from_file(config_path)
|
43 |
+
n_g_ms = SynthesizerTrn(
|
44 |
+
178,
|
45 |
+
hps_ms.data.filter_length // 2 + 1,
|
46 |
+
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
47 |
+
n_speakers=hps_ms.data.n_speakers,
|
48 |
+
**hps_ms.model)
|
49 |
+
_ = utils.load_checkpoint(model_path, n_g_ms, None)
|
50 |
+
_ = n_g_ms.eval().to(dev)
|
51 |
+
# 加载hubert
|
52 |
+
hubert_soft = hubert_model.hubert_soft(get_end_file("./", "pt")[0])
|
53 |
+
feature_input = FeatureInput(hps_ms.data.sampling_rate, hps_ms.data.hop_length)
|
54 |
+
return n_g_ms, hubert_soft, feature_input, hps_ms
|
55 |
+
|
56 |
+
|
57 |
+
def resize2d_f0(x, target_len):
|
58 |
+
source = np.array(x)
|
59 |
+
source[source < 0.001] = np.nan
|
60 |
+
target = np.interp(np.arange(0, len(source) * target_len, len(source)) / target_len, np.arange(0, len(source)),
|
61 |
+
source)
|
62 |
+
res = np.nan_to_num(target)
|
63 |
+
return res
|
64 |
+
|
65 |
+
|
66 |
+
def get_units(in_path, hubert_soft):
|
67 |
+
source, sr = torchaudio.load(in_path)
|
68 |
+
source = torchaudio.functional.resample(source, sr, 16000)
|
69 |
+
if len(source.shape) == 2 and source.shape[1] >= 2:
|
70 |
+
source = torch.mean(source, dim=0).unsqueeze(0)
|
71 |
+
source = source.unsqueeze(0).to(dev)
|
72 |
+
with torch.inference_mode():
|
73 |
+
units = hubert_soft.units(source)
|
74 |
+
return units
|
75 |
+
|
76 |
+
|
77 |
+
def transcribe(source_path, length, transform, feature_input):
|
78 |
+
feature_pit = feature_input.compute_f0(source_path)
|
79 |
+
feature_pit = feature_pit * 2 ** (transform / 12)
|
80 |
+
feature_pit = resize2d_f0(feature_pit, length)
|
81 |
+
coarse_pit = feature_input.coarse_f0(feature_pit)
|
82 |
+
return coarse_pit
|
83 |
+
|
84 |
+
|
85 |
+
def get_unit_pitch(in_path, tran, hubert_soft, feature_input):
|
86 |
+
soft = get_units(in_path, hubert_soft).squeeze(0).cpu().numpy()
|
87 |
+
input_pitch = transcribe(in_path, soft.shape[0], tran, feature_input)
|
88 |
+
return soft, input_pitch
|
89 |
+
|
90 |
+
|
91 |
+
def clean_pitch(input_pitch):
|
92 |
+
num_nan = np.sum(input_pitch == 1)
|
93 |
+
if num_nan / len(input_pitch) > 0.9:
|
94 |
+
input_pitch[input_pitch != 1] = 1
|
95 |
+
return input_pitch
|
96 |
+
|
97 |
+
|
98 |
+
def plt_pitch(input_pitch):
|
99 |
+
input_pitch = input_pitch.astype(float)
|
100 |
+
input_pitch[input_pitch == 1] = np.nan
|
101 |
+
return input_pitch
|
102 |
+
|
103 |
+
|
104 |
+
def f0_to_pitch(ff):
|
105 |
+
f0_pitch = 69 + 12 * np.log2(ff / 440)
|
106 |
+
return f0_pitch
|
107 |
+
|
108 |
+
|
109 |
+
def f0_plt(in_path, out_path, tran, hubert_soft, feature_input):
|
110 |
+
s1, input_pitch = get_unit_pitch(in_path, tran, hubert_soft, feature_input)
|
111 |
+
s2, output_pitch = get_unit_pitch(out_path, 0, hubert_soft, feature_input)
|
112 |
+
plt.clf()
|
113 |
+
plt.plot(plt_pitch(input_pitch), color="#66ccff")
|
114 |
+
plt.plot(plt_pitch(output_pitch), color="orange")
|
115 |
+
plt.savefig("temp.jpg")
|
116 |
+
|
117 |
+
|
118 |
+
def calc_error(in_path, out_path, tran, feature_input):
|
119 |
+
input_pitch = feature_input.compute_f0(in_path)
|
120 |
+
output_pitch = feature_input.compute_f0(out_path)
|
121 |
+
sum_y = []
|
122 |
+
if np.sum(input_pitch == 0) / len(input_pitch) > 0.9:
|
123 |
+
mistake, var_take = 0, 0
|
124 |
+
else:
|
125 |
+
for i in range(min(len(input_pitch), len(output_pitch))):
|
126 |
+
if input_pitch[i] > 0 and output_pitch[i] > 0:
|
127 |
+
sum_y.append(abs(f0_to_pitch(output_pitch[i]) - (f0_to_pitch(input_pitch[i]) + tran)))
|
128 |
+
num_y = 0
|
129 |
+
for x in sum_y:
|
130 |
+
num_y += x
|
131 |
+
len_y = len(sum_y) if len(sum_y) else 1
|
132 |
+
mistake = round(float(num_y / len_y), 2)
|
133 |
+
var_take = round(float(np.std(sum_y, ddof=1)), 2)
|
134 |
+
return mistake, var_take
|
135 |
+
|
136 |
+
|
137 |
+
def infer(source_path, speaker_id, tran, net_g_ms, hubert_soft, feature_input):
|
138 |
+
sid = torch.LongTensor([int(speaker_id)]).to(dev)
|
139 |
+
soft, pitch = get_unit_pitch(source_path, tran, hubert_soft, feature_input)
|
140 |
+
pitch = torch.LongTensor(clean_pitch(pitch)).unsqueeze(0).to(dev)
|
141 |
+
stn_tst = torch.FloatTensor(soft)
|
142 |
+
with torch.no_grad():
|
143 |
+
x_tst = stn_tst.unsqueeze(0).to(dev)
|
144 |
+
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(dev)
|
145 |
+
audio = \
|
146 |
+
net_g_ms.infer(x_tst, x_tst_lengths, pitch, sid=sid, noise_scale=0.3, noise_scale_w=0.5,
|
147 |
+
length_scale=1)[0][
|
148 |
+
0, 0].data.float().cpu().numpy()
|
149 |
+
return audio, audio.shape[-1]
|
150 |
+
|
151 |
+
|
152 |
+
def del_temp_wav(path_data):
|
153 |
+
for i in get_end_file(path_data, "wav"): # os.listdir(path_data)#返回一个列表,里面是当前目录下面的所有东西的相对路径
|
154 |
+
os.remove(i)
|
155 |
+
|
156 |
+
|
157 |
+
def format_wav(audio_path, tar_sample):
|
158 |
+
raw_audio, raw_sample_rate = torchaudio.load(audio_path)
|
159 |
+
if len(raw_audio.shape) == 2 and raw_audio.shape[1] >= 2:
|
160 |
+
raw_audio = torch.mean(raw_audio, dim=0).unsqueeze(0)
|
161 |
+
tar_audio = torchaudio.functional.resample(raw_audio, raw_sample_rate, tar_sample)
|
162 |
+
torchaudio.save(audio_path[:-4] + ".wav", tar_audio, tar_sample)
|
163 |
+
return tar_audio, tar_sample
|
164 |
+
|
165 |
+
|
166 |
+
def fill_a_to_b(a, b):
|
167 |
+
if len(a) < len(b):
|
168 |
+
for _ in range(0, len(b) - len(a)):
|
169 |
+
a.append(a[0])
|
170 |
+
|
171 |
+
|
172 |
+
def mkdir(paths: list):
|
173 |
+
for path in paths:
|
174 |
+
if not os.path.exists(path):
|
175 |
+
os.mkdir(path)
|
mg_1255_epochs_v0.0.6.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:df8d8c99b7e1885d771f66d972f31c2d2d74b7d582f3501ae8adefa298a9fbd3
|
3 |
+
size 221251405
|
models.py
ADDED
@@ -0,0 +1,556 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import math
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from torch import nn
|
6 |
+
from torch.nn import Conv1d, ConvTranspose1d, Conv2d
|
7 |
+
from torch.nn import functional as F
|
8 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
9 |
+
|
10 |
+
import attentions
|
11 |
+
import commons
|
12 |
+
import modules
|
13 |
+
from commons import init_weights, get_padding
|
14 |
+
|
15 |
+
|
16 |
+
class StochasticDurationPredictor(nn.Module):
|
17 |
+
def __init__(self, in_channels, filter_channels, kernel_size, p_dropout, n_flows=4, gin_channels=0):
|
18 |
+
super().__init__()
|
19 |
+
filter_channels = in_channels # it needs to be removed from future version.
|
20 |
+
self.in_channels = in_channels
|
21 |
+
self.filter_channels = filter_channels
|
22 |
+
self.kernel_size = kernel_size
|
23 |
+
self.p_dropout = p_dropout
|
24 |
+
self.n_flows = n_flows
|
25 |
+
self.gin_channels = gin_channels
|
26 |
+
|
27 |
+
self.log_flow = modules.Log()
|
28 |
+
self.flows = nn.ModuleList()
|
29 |
+
self.flows.append(modules.ElementwiseAffine(2))
|
30 |
+
for i in range(n_flows):
|
31 |
+
self.flows.append(modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3))
|
32 |
+
self.flows.append(modules.Flip())
|
33 |
+
|
34 |
+
self.post_pre = nn.Conv1d(1, filter_channels, 1)
|
35 |
+
self.post_proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
36 |
+
self.post_convs = modules.DDSConv(filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout)
|
37 |
+
self.post_flows = nn.ModuleList()
|
38 |
+
self.post_flows.append(modules.ElementwiseAffine(2))
|
39 |
+
for i in range(4):
|
40 |
+
self.post_flows.append(modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3))
|
41 |
+
self.post_flows.append(modules.Flip())
|
42 |
+
|
43 |
+
self.pre = nn.Conv1d(in_channels, filter_channels, 1)
|
44 |
+
self.proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
45 |
+
self.convs = modules.DDSConv(filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout)
|
46 |
+
if gin_channels != 0:
|
47 |
+
self.cond = nn.Conv1d(gin_channels, filter_channels, 1)
|
48 |
+
|
49 |
+
def forward(self, x, x_mask, w=None, g=None, reverse=False, noise_scale=1.0):
|
50 |
+
x = torch.detach(x)
|
51 |
+
x = self.pre(x)
|
52 |
+
if g is not None:
|
53 |
+
g = torch.detach(g)
|
54 |
+
x = x + self.cond(g)
|
55 |
+
x = self.convs(x, x_mask)
|
56 |
+
x = self.proj(x) * x_mask
|
57 |
+
|
58 |
+
if not reverse:
|
59 |
+
flows = self.flows
|
60 |
+
assert w is not None
|
61 |
+
|
62 |
+
logdet_tot_q = 0
|
63 |
+
h_w = self.post_pre(w)
|
64 |
+
h_w = self.post_convs(h_w, x_mask)
|
65 |
+
h_w = self.post_proj(h_w) * x_mask
|
66 |
+
e_q = torch.randn(w.size(0), 2, w.size(2)).to(device=x.device, dtype=x.dtype) * x_mask
|
67 |
+
z_q = e_q
|
68 |
+
for flow in self.post_flows:
|
69 |
+
z_q, logdet_q = flow(z_q, x_mask, g=(x + h_w))
|
70 |
+
logdet_tot_q += logdet_q
|
71 |
+
z_u, z1 = torch.split(z_q, [1, 1], 1)
|
72 |
+
u = torch.sigmoid(z_u) * x_mask
|
73 |
+
z0 = (w - u) * x_mask
|
74 |
+
logdet_tot_q += torch.sum((F.logsigmoid(z_u) + F.logsigmoid(-z_u)) * x_mask, [1, 2])
|
75 |
+
logq = torch.sum(-0.5 * (math.log(2 * math.pi) + (e_q ** 2)) * x_mask, [1, 2]) - logdet_tot_q
|
76 |
+
|
77 |
+
logdet_tot = 0
|
78 |
+
z0, logdet = self.log_flow(z0, x_mask)
|
79 |
+
logdet_tot += logdet
|
80 |
+
z = torch.cat([z0, z1], 1)
|
81 |
+
for flow in flows:
|
82 |
+
z, logdet = flow(z, x_mask, g=x, reverse=reverse)
|
83 |
+
logdet_tot = logdet_tot + logdet
|
84 |
+
nll = torch.sum(0.5 * (math.log(2 * math.pi) + (z ** 2)) * x_mask, [1, 2]) - logdet_tot
|
85 |
+
return nll + logq # [b]
|
86 |
+
else:
|
87 |
+
flows = list(reversed(self.flows))
|
88 |
+
flows = flows[:-2] + [flows[-1]] # remove a useless vflow
|
89 |
+
z = torch.randn(x.size(0), 2, x.size(2)).to(device=x.device, dtype=x.dtype) * noise_scale
|
90 |
+
for flow in flows:
|
91 |
+
z = flow(z, x_mask, g=x, reverse=reverse)
|
92 |
+
z0, z1 = torch.split(z, [1, 1], 1)
|
93 |
+
logw = z0
|
94 |
+
return logw
|
95 |
+
|
96 |
+
|
97 |
+
class DurationPredictor(nn.Module):
|
98 |
+
def __init__(self, in_channels, filter_channels, kernel_size, p_dropout, gin_channels=0):
|
99 |
+
super().__init__()
|
100 |
+
|
101 |
+
self.in_channels = in_channels
|
102 |
+
self.filter_channels = filter_channels
|
103 |
+
self.kernel_size = kernel_size
|
104 |
+
self.p_dropout = p_dropout
|
105 |
+
self.gin_channels = gin_channels
|
106 |
+
|
107 |
+
self.drop = nn.Dropout(p_dropout)
|
108 |
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size, padding=kernel_size // 2)
|
109 |
+
self.norm_1 = modules.LayerNorm(filter_channels)
|
110 |
+
self.conv_2 = nn.Conv1d(filter_channels, filter_channels, kernel_size, padding=kernel_size // 2)
|
111 |
+
self.norm_2 = modules.LayerNorm(filter_channels)
|
112 |
+
self.proj = nn.Conv1d(filter_channels, 1, 1)
|
113 |
+
|
114 |
+
if gin_channels != 0:
|
115 |
+
self.cond = nn.Conv1d(gin_channels, in_channels, 1)
|
116 |
+
|
117 |
+
def forward(self, x, x_mask, g=None):
|
118 |
+
x = torch.detach(x)
|
119 |
+
if g is not None:
|
120 |
+
g = torch.detach(g)
|
121 |
+
x = x + self.cond(g)
|
122 |
+
x = self.conv_1(x * x_mask)
|
123 |
+
x = torch.relu(x)
|
124 |
+
x = self.norm_1(x)
|
125 |
+
x = self.drop(x)
|
126 |
+
x = self.conv_2(x * x_mask)
|
127 |
+
x = torch.relu(x)
|
128 |
+
x = self.norm_2(x)
|
129 |
+
x = self.drop(x)
|
130 |
+
x = self.proj(x * x_mask)
|
131 |
+
return x * x_mask
|
132 |
+
|
133 |
+
|
134 |
+
class PitchPredictor(nn.Module):
|
135 |
+
def __init__(self,
|
136 |
+
n_vocab,
|
137 |
+
out_channels,
|
138 |
+
hidden_channels,
|
139 |
+
filter_channels,
|
140 |
+
n_heads,
|
141 |
+
n_layers,
|
142 |
+
kernel_size,
|
143 |
+
p_dropout):
|
144 |
+
super().__init__()
|
145 |
+
self.n_vocab = n_vocab # 音素的个数,中文和英文不同
|
146 |
+
self.out_channels = out_channels
|
147 |
+
self.hidden_channels = hidden_channels
|
148 |
+
self.filter_channels = filter_channels
|
149 |
+
self.n_heads = n_heads
|
150 |
+
self.n_layers = n_layers
|
151 |
+
self.kernel_size = kernel_size
|
152 |
+
self.p_dropout = p_dropout
|
153 |
+
|
154 |
+
self.pitch_net = attentions.Encoder(
|
155 |
+
hidden_channels,
|
156 |
+
filter_channels,
|
157 |
+
n_heads,
|
158 |
+
n_layers,
|
159 |
+
kernel_size,
|
160 |
+
p_dropout)
|
161 |
+
self.proj = nn.Conv1d(hidden_channels, 1, 1)
|
162 |
+
|
163 |
+
def forward(self, x, x_mask):
|
164 |
+
pitch_embedding = self.pitch_net(x * x_mask, x_mask)
|
165 |
+
pitch_embedding = pitch_embedding * x_mask
|
166 |
+
pred_pitch = self.proj(pitch_embedding)
|
167 |
+
return pred_pitch, pitch_embedding
|
168 |
+
|
169 |
+
|
170 |
+
class TextEncoder(nn.Module):
|
171 |
+
def __init__(self,
|
172 |
+
n_vocab,
|
173 |
+
out_channels,
|
174 |
+
hidden_channels,
|
175 |
+
filter_channels,
|
176 |
+
n_heads,
|
177 |
+
n_layers,
|
178 |
+
kernel_size,
|
179 |
+
p_dropout):
|
180 |
+
super().__init__()
|
181 |
+
self.n_vocab = n_vocab
|
182 |
+
self.out_channels = out_channels
|
183 |
+
self.hidden_channels = hidden_channels
|
184 |
+
self.filter_channels = filter_channels
|
185 |
+
self.n_heads = n_heads
|
186 |
+
self.n_layers = n_layers
|
187 |
+
self.kernel_size = kernel_size
|
188 |
+
self.p_dropout = p_dropout
|
189 |
+
|
190 |
+
# self.emb = nn.Embedding(n_vocab, hidden_channels)
|
191 |
+
# nn.init.normal_(self.emb.weight, 0.0, hidden_channels**-0.5)
|
192 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels)
|
193 |
+
nn.init.normal_(self.emb_pitch.weight, 0.0, hidden_channels ** -0.5)
|
194 |
+
|
195 |
+
self.encoder = attentions.Encoder(
|
196 |
+
hidden_channels,
|
197 |
+
filter_channels,
|
198 |
+
n_heads,
|
199 |
+
n_layers,
|
200 |
+
kernel_size,
|
201 |
+
p_dropout)
|
202 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
203 |
+
|
204 |
+
def forward(self, x, x_lengths, pitch):
|
205 |
+
# x = x.transpose(1,2)
|
206 |
+
# x = self.emb(x) * math.sqrt(self.hidden_channels) # [b, t, h]
|
207 |
+
# print(x.shape)
|
208 |
+
x = x + self.emb_pitch(pitch)
|
209 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
210 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
211 |
+
|
212 |
+
x = self.encoder(x * x_mask, x_mask)
|
213 |
+
stats = self.proj(x) * x_mask
|
214 |
+
|
215 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
216 |
+
return x, m, logs, x_mask
|
217 |
+
|
218 |
+
|
219 |
+
class ResidualCouplingBlock(nn.Module):
|
220 |
+
def __init__(self,
|
221 |
+
channels,
|
222 |
+
hidden_channels,
|
223 |
+
kernel_size,
|
224 |
+
dilation_rate,
|
225 |
+
n_layers,
|
226 |
+
n_flows=4,
|
227 |
+
gin_channels=0):
|
228 |
+
super().__init__()
|
229 |
+
self.channels = channels
|
230 |
+
self.hidden_channels = hidden_channels
|
231 |
+
self.kernel_size = kernel_size
|
232 |
+
self.dilation_rate = dilation_rate
|
233 |
+
self.n_layers = n_layers
|
234 |
+
self.n_flows = n_flows
|
235 |
+
self.gin_channels = gin_channels
|
236 |
+
|
237 |
+
self.flows = nn.ModuleList()
|
238 |
+
for i in range(n_flows):
|
239 |
+
self.flows.append(
|
240 |
+
modules.ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers,
|
241 |
+
gin_channels=gin_channels, mean_only=True))
|
242 |
+
self.flows.append(modules.Flip())
|
243 |
+
|
244 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
245 |
+
if not reverse:
|
246 |
+
for flow in self.flows:
|
247 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
248 |
+
else:
|
249 |
+
for flow in reversed(self.flows):
|
250 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
251 |
+
return x
|
252 |
+
|
253 |
+
|
254 |
+
class PosteriorEncoder(nn.Module):
|
255 |
+
def __init__(self,
|
256 |
+
in_channels,
|
257 |
+
out_channels,
|
258 |
+
hidden_channels,
|
259 |
+
kernel_size,
|
260 |
+
dilation_rate,
|
261 |
+
n_layers,
|
262 |
+
gin_channels=0):
|
263 |
+
super().__init__()
|
264 |
+
self.in_channels = in_channels
|
265 |
+
self.out_channels = out_channels
|
266 |
+
self.hidden_channels = hidden_channels
|
267 |
+
self.kernel_size = kernel_size
|
268 |
+
self.dilation_rate = dilation_rate
|
269 |
+
self.n_layers = n_layers
|
270 |
+
self.gin_channels = gin_channels
|
271 |
+
|
272 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
273 |
+
self.enc = modules.WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
|
274 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
275 |
+
|
276 |
+
def forward(self, x, x_lengths, g=None):
|
277 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
278 |
+
x = self.pre(x) * x_mask
|
279 |
+
x = self.enc(x, x_mask, g=g)
|
280 |
+
stats = self.proj(x) * x_mask
|
281 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
282 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
283 |
+
return z, m, logs, x_mask
|
284 |
+
|
285 |
+
|
286 |
+
class Generator(torch.nn.Module):
|
287 |
+
def __init__(self, initial_channel, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates,
|
288 |
+
upsample_initial_channel, upsample_kernel_sizes, gin_channels=0):
|
289 |
+
super(Generator, self).__init__()
|
290 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
291 |
+
self.num_upsamples = len(upsample_rates)
|
292 |
+
self.conv_pre = Conv1d(initial_channel, upsample_initial_channel, 7, 1, padding=3)
|
293 |
+
resblock = modules.ResBlock1 if resblock == '1' else modules.ResBlock2
|
294 |
+
|
295 |
+
self.ups = nn.ModuleList()
|
296 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
297 |
+
self.ups.append(weight_norm(
|
298 |
+
ConvTranspose1d(upsample_initial_channel // (2 ** i), upsample_initial_channel // (2 ** (i + 1)),
|
299 |
+
k, u, padding=(k - u) // 2)))
|
300 |
+
|
301 |
+
self.resblocks = nn.ModuleList()
|
302 |
+
for i in range(len(self.ups)):
|
303 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
304 |
+
for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)):
|
305 |
+
self.resblocks.append(resblock(ch, k, d))
|
306 |
+
|
307 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
308 |
+
self.ups.apply(init_weights)
|
309 |
+
|
310 |
+
if gin_channels != 0:
|
311 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
312 |
+
|
313 |
+
def forward(self, x, g=None):
|
314 |
+
x = self.conv_pre(x)
|
315 |
+
if g is not None:
|
316 |
+
x = x + self.cond(g)
|
317 |
+
|
318 |
+
for i in range(self.num_upsamples):
|
319 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
320 |
+
x = self.ups[i](x)
|
321 |
+
xs = None
|
322 |
+
for j in range(self.num_kernels):
|
323 |
+
if xs is None:
|
324 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
325 |
+
else:
|
326 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
327 |
+
x = xs / self.num_kernels
|
328 |
+
x = F.leaky_relu(x)
|
329 |
+
x = self.conv_post(x)
|
330 |
+
x = torch.tanh(x)
|
331 |
+
|
332 |
+
return x
|
333 |
+
|
334 |
+
def remove_weight_norm(self):
|
335 |
+
print('Removing weight norm...')
|
336 |
+
for l in self.ups:
|
337 |
+
remove_weight_norm(l)
|
338 |
+
for l in self.resblocks:
|
339 |
+
l.remove_weight_norm()
|
340 |
+
|
341 |
+
|
342 |
+
class DiscriminatorP(torch.nn.Module):
|
343 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
344 |
+
super(DiscriminatorP, self).__init__()
|
345 |
+
self.period = period
|
346 |
+
self.use_spectral_norm = use_spectral_norm
|
347 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
348 |
+
self.convs = nn.ModuleList([
|
349 |
+
norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
350 |
+
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
351 |
+
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
352 |
+
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
353 |
+
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
|
354 |
+
])
|
355 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
356 |
+
|
357 |
+
def forward(self, x):
|
358 |
+
fmap = []
|
359 |
+
|
360 |
+
# 1d to 2d
|
361 |
+
b, c, t = x.shape
|
362 |
+
if t % self.period != 0: # pad first
|
363 |
+
n_pad = self.period - (t % self.period)
|
364 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
365 |
+
t = t + n_pad
|
366 |
+
x = x.view(b, c, t // self.period, self.period)
|
367 |
+
|
368 |
+
for l in self.convs:
|
369 |
+
x = l(x)
|
370 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
371 |
+
fmap.append(x)
|
372 |
+
x = self.conv_post(x)
|
373 |
+
fmap.append(x)
|
374 |
+
x = torch.flatten(x, 1, -1)
|
375 |
+
|
376 |
+
return x, fmap
|
377 |
+
|
378 |
+
|
379 |
+
class DiscriminatorS(torch.nn.Module):
|
380 |
+
def __init__(self, use_spectral_norm=False):
|
381 |
+
super(DiscriminatorS, self).__init__()
|
382 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
383 |
+
self.convs = nn.ModuleList([
|
384 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
385 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
386 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
387 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
388 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
389 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
390 |
+
])
|
391 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
392 |
+
|
393 |
+
def forward(self, x):
|
394 |
+
fmap = []
|
395 |
+
|
396 |
+
for l in self.convs:
|
397 |
+
x = l(x)
|
398 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
399 |
+
fmap.append(x)
|
400 |
+
x = self.conv_post(x)
|
401 |
+
fmap.append(x)
|
402 |
+
x = torch.flatten(x, 1, -1)
|
403 |
+
|
404 |
+
return x, fmap
|
405 |
+
|
406 |
+
|
407 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
408 |
+
def __init__(self, use_spectral_norm=False):
|
409 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
410 |
+
periods = [2, 3, 5, 7, 11]
|
411 |
+
|
412 |
+
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
413 |
+
discs = discs + [DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods]
|
414 |
+
self.discriminators = nn.ModuleList(discs)
|
415 |
+
|
416 |
+
def forward(self, y, y_hat):
|
417 |
+
y_d_rs = []
|
418 |
+
y_d_gs = []
|
419 |
+
fmap_rs = []
|
420 |
+
fmap_gs = []
|
421 |
+
for i, d in enumerate(self.discriminators):
|
422 |
+
y_d_r, fmap_r = d(y)
|
423 |
+
y_d_g, fmap_g = d(y_hat)
|
424 |
+
y_d_rs.append(y_d_r)
|
425 |
+
y_d_gs.append(y_d_g)
|
426 |
+
fmap_rs.append(fmap_r)
|
427 |
+
fmap_gs.append(fmap_g)
|
428 |
+
|
429 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
430 |
+
|
431 |
+
|
432 |
+
class SynthesizerTrn(nn.Module):
|
433 |
+
"""
|
434 |
+
Synthesizer for Training
|
435 |
+
"""
|
436 |
+
|
437 |
+
def __init__(self,
|
438 |
+
n_vocab,
|
439 |
+
spec_channels,
|
440 |
+
segment_size,
|
441 |
+
inter_channels,
|
442 |
+
hidden_channels,
|
443 |
+
filter_channels,
|
444 |
+
n_heads,
|
445 |
+
n_layers,
|
446 |
+
kernel_size,
|
447 |
+
p_dropout,
|
448 |
+
resblock,
|
449 |
+
resblock_kernel_sizes,
|
450 |
+
resblock_dilation_sizes,
|
451 |
+
upsample_rates,
|
452 |
+
upsample_initial_channel,
|
453 |
+
upsample_kernel_sizes,
|
454 |
+
n_speakers=0,
|
455 |
+
gin_channels=0,
|
456 |
+
use_sdp=True,
|
457 |
+
**kwargs):
|
458 |
+
|
459 |
+
super().__init__()
|
460 |
+
self.n_vocab = n_vocab
|
461 |
+
self.spec_channels = spec_channels
|
462 |
+
self.inter_channels = inter_channels
|
463 |
+
self.hidden_channels = hidden_channels
|
464 |
+
self.filter_channels = filter_channels
|
465 |
+
self.n_heads = n_heads
|
466 |
+
self.n_layers = n_layers
|
467 |
+
self.kernel_size = kernel_size
|
468 |
+
self.p_dropout = p_dropout
|
469 |
+
self.resblock = resblock
|
470 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
471 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
472 |
+
self.upsample_rates = upsample_rates
|
473 |
+
self.upsample_initial_channel = upsample_initial_channel
|
474 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
475 |
+
self.segment_size = segment_size
|
476 |
+
self.n_speakers = n_speakers
|
477 |
+
self.gin_channels = gin_channels
|
478 |
+
|
479 |
+
self.use_sdp = use_sdp
|
480 |
+
|
481 |
+
self.enc_p = TextEncoder(n_vocab,
|
482 |
+
inter_channels,
|
483 |
+
hidden_channels,
|
484 |
+
filter_channels,
|
485 |
+
n_heads,
|
486 |
+
n_layers,
|
487 |
+
kernel_size,
|
488 |
+
p_dropout)
|
489 |
+
self.dec = Generator(inter_channels, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates,
|
490 |
+
upsample_initial_channel, upsample_kernel_sizes, gin_channels=gin_channels)
|
491 |
+
self.enc_q = PosteriorEncoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16,
|
492 |
+
gin_channels=gin_channels)
|
493 |
+
self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
|
494 |
+
# self.pitch_net = PitchPredictor(n_vocab, inter_channels, hidden_channels, filter_channels, n_heads, n_layers,
|
495 |
+
# kernel_size, p_dropout)
|
496 |
+
|
497 |
+
if use_sdp:
|
498 |
+
self.dp = StochasticDurationPredictor(hidden_channels, 192, 3, 0.5, 4, gin_channels=gin_channels)
|
499 |
+
else:
|
500 |
+
self.dp = DurationPredictor(hidden_channels, 256, 3, 0.5, gin_channels=gin_channels)
|
501 |
+
|
502 |
+
if n_speakers > 1:
|
503 |
+
self.emb_g = nn.Embedding(n_speakers, gin_channels)
|
504 |
+
|
505 |
+
def infer(self, x, x_lengths, pitch, sid=None, noise_scale=1, length_scale=1, noise_scale_w=1., max_len=None):
|
506 |
+
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, pitch)
|
507 |
+
if self.n_speakers > 0:
|
508 |
+
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
509 |
+
else:
|
510 |
+
g = None
|
511 |
+
|
512 |
+
if self.use_sdp:
|
513 |
+
logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w)
|
514 |
+
else:
|
515 |
+
logw = self.dp(x, x_mask, g=g)
|
516 |
+
w = torch.exp(logw) * x_mask * length_scale
|
517 |
+
w_ceil = torch.ceil(w)
|
518 |
+
|
519 |
+
w_ceil = w_ceil * 0 + 2
|
520 |
+
# for index in range(w_ceil.shape[2]):
|
521 |
+
# if index%4 == 0:
|
522 |
+
# w_ceil[0,0,index] = 1.0
|
523 |
+
|
524 |
+
for i in range(w_ceil.shape[2]):
|
525 |
+
sep = 1 / 0.14
|
526 |
+
if i * sep >= w_ceil.shape[2] * 2:
|
527 |
+
break
|
528 |
+
w_ceil[0, 0, int(i * sep / 2)] = 1
|
529 |
+
|
530 |
+
# print(w_ceil)
|
531 |
+
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
532 |
+
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, None), 1).to(x_mask.dtype)
|
533 |
+
|
534 |
+
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
535 |
+
|
536 |
+
attn = commons.generate_path(w_ceil, attn_mask)
|
537 |
+
|
538 |
+
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
539 |
+
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1,
|
540 |
+
2) # [b, t', t], [b, t, d] -> [b, d, t']
|
541 |
+
|
542 |
+
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
543 |
+
|
544 |
+
z = self.flow(z_p, y_mask, g=g, reverse=True)
|
545 |
+
o = self.dec((z * y_mask)[:, :, :max_len], g=g)
|
546 |
+
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|
547 |
+
|
548 |
+
def voice_conversion(self, y, y_lengths, sid_src, sid_tgt):
|
549 |
+
assert self.n_speakers > 0, "n_speakers have to be larger than 0."
|
550 |
+
g_src = self.emb_g(sid_src).unsqueeze(-1)
|
551 |
+
g_tgt = self.emb_g(sid_tgt).unsqueeze(-1)
|
552 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g_src)
|
553 |
+
z_p = self.flow(z, y_mask, g=g_src)
|
554 |
+
z_hat = self.flow(z_p, y_mask, g=g_tgt, reverse=True)
|
555 |
+
o_hat = self.dec(z_hat * y_mask, g=g_tgt)
|
556 |
+
return o_hat, y_mask, (z, z_p, z_hat)
|
modules.py
ADDED
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import Conv1d
|
6 |
+
from torch.nn import functional as t_func
|
7 |
+
from torch.nn.utils import weight_norm, remove_weight_norm
|
8 |
+
|
9 |
+
import commons
|
10 |
+
from commons import init_weights, get_padding
|
11 |
+
from transforms import piecewise_rational_quadratic_transform
|
12 |
+
|
13 |
+
LRELU_SLOPE = 0.1
|
14 |
+
|
15 |
+
|
16 |
+
class LayerNorm(nn.Module):
|
17 |
+
def __init__(self, channels, eps=1e-5):
|
18 |
+
super().__init__()
|
19 |
+
self.channels = channels
|
20 |
+
self.eps = eps
|
21 |
+
|
22 |
+
self.gamma = nn.Parameter(torch.ones(channels))
|
23 |
+
self.beta = nn.Parameter(torch.zeros(channels))
|
24 |
+
|
25 |
+
def forward(self, x):
|
26 |
+
x = x.transpose(1, -1)
|
27 |
+
x = t_func.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
28 |
+
return x.transpose(1, -1)
|
29 |
+
|
30 |
+
|
31 |
+
class ConvReluNorm(nn.Module):
|
32 |
+
def __init__(self, in_channels, hidden_channels, out_channels, kernel_size, n_layers, p_dropout):
|
33 |
+
super().__init__()
|
34 |
+
self.in_channels = in_channels
|
35 |
+
self.hidden_channels = hidden_channels
|
36 |
+
self.out_channels = out_channels
|
37 |
+
self.kernel_size = kernel_size
|
38 |
+
self.n_layers = n_layers
|
39 |
+
self.p_dropout = p_dropout
|
40 |
+
assert n_layers > 1, "Number of layers should be larger than 0."
|
41 |
+
|
42 |
+
self.conv_layers = nn.ModuleList()
|
43 |
+
self.norm_layers = nn.ModuleList()
|
44 |
+
self.conv_layers.append(nn.Conv1d(in_channels, hidden_channels, kernel_size, padding=kernel_size // 2))
|
45 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
46 |
+
self.relu_drop = nn.Sequential(
|
47 |
+
nn.ReLU(),
|
48 |
+
nn.Dropout(p_dropout))
|
49 |
+
for _ in range(n_layers - 1):
|
50 |
+
self.conv_layers.append(nn.Conv1d(hidden_channels, hidden_channels, kernel_size, padding=kernel_size // 2))
|
51 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
52 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
53 |
+
self.proj.weight.data.zero_()
|
54 |
+
self.proj.bias.data.zero_()
|
55 |
+
|
56 |
+
def forward(self, x, x_mask):
|
57 |
+
x_org = x
|
58 |
+
for i in range(self.n_layers):
|
59 |
+
x = self.conv_layers[i](x * x_mask)
|
60 |
+
x = self.norm_layers[i](x)
|
61 |
+
x = self.relu_drop(x)
|
62 |
+
x = x_org + self.proj(x)
|
63 |
+
return x * x_mask
|
64 |
+
|
65 |
+
|
66 |
+
class DDSConv(nn.Module):
|
67 |
+
"""
|
68 |
+
Dialted and Depth-Separable Convolution
|
69 |
+
"""
|
70 |
+
|
71 |
+
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.):
|
72 |
+
super().__init__()
|
73 |
+
self.channels = channels
|
74 |
+
self.kernel_size = kernel_size
|
75 |
+
self.n_layers = n_layers
|
76 |
+
self.p_dropout = p_dropout
|
77 |
+
|
78 |
+
self.drop = nn.Dropout(p_dropout)
|
79 |
+
self.convs_sep = nn.ModuleList()
|
80 |
+
self.convs_1x1 = nn.ModuleList()
|
81 |
+
self.norms_1 = nn.ModuleList()
|
82 |
+
self.norms_2 = nn.ModuleList()
|
83 |
+
for i in range(n_layers):
|
84 |
+
dilation = kernel_size ** i
|
85 |
+
padding = (kernel_size * dilation - dilation) // 2
|
86 |
+
self.convs_sep.append(nn.Conv1d(channels, channels, kernel_size,
|
87 |
+
groups=channels, dilation=dilation, padding=padding
|
88 |
+
))
|
89 |
+
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
90 |
+
self.norms_1.append(LayerNorm(channels))
|
91 |
+
self.norms_2.append(LayerNorm(channels))
|
92 |
+
|
93 |
+
def forward(self, x, x_mask, g=None):
|
94 |
+
if g is not None:
|
95 |
+
x = x + g
|
96 |
+
for i in range(self.n_layers):
|
97 |
+
y = self.convs_sep[i](x * x_mask)
|
98 |
+
y = self.norms_1[i](y)
|
99 |
+
y = t_func.gelu(y)
|
100 |
+
y = self.convs_1x1[i](y)
|
101 |
+
y = self.norms_2[i](y)
|
102 |
+
y = t_func.gelu(y)
|
103 |
+
y = self.drop(y)
|
104 |
+
x = x + y
|
105 |
+
return x * x_mask
|
106 |
+
|
107 |
+
|
108 |
+
class WN(torch.nn.Module):
|
109 |
+
def __init__(self, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=0, p_dropout=0):
|
110 |
+
super(WN, self).__init__()
|
111 |
+
assert (kernel_size % 2 == 1)
|
112 |
+
self.hidden_channels = hidden_channels
|
113 |
+
self.kernel_size = kernel_size,
|
114 |
+
self.dilation_rate = dilation_rate
|
115 |
+
self.n_layers = n_layers
|
116 |
+
self.gin_channels = gin_channels
|
117 |
+
self.p_dropout = p_dropout
|
118 |
+
|
119 |
+
self.in_layers = torch.nn.ModuleList()
|
120 |
+
self.res_skip_layers = torch.nn.ModuleList()
|
121 |
+
self.drop = nn.Dropout(p_dropout)
|
122 |
+
|
123 |
+
if gin_channels != 0:
|
124 |
+
cond_layer = torch.nn.Conv1d(gin_channels, 2 * hidden_channels * n_layers, 1)
|
125 |
+
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name='weight')
|
126 |
+
|
127 |
+
for i in range(n_layers):
|
128 |
+
dilation = dilation_rate ** i
|
129 |
+
padding = int((kernel_size * dilation - dilation) / 2)
|
130 |
+
in_layer = torch.nn.Conv1d(hidden_channels, 2 * hidden_channels, kernel_size,
|
131 |
+
dilation=dilation, padding=padding)
|
132 |
+
in_layer = torch.nn.utils.weight_norm(in_layer, name='weight')
|
133 |
+
self.in_layers.append(in_layer)
|
134 |
+
|
135 |
+
# last one is not necessary
|
136 |
+
if i < n_layers - 1:
|
137 |
+
res_skip_channels = 2 * hidden_channels
|
138 |
+
else:
|
139 |
+
res_skip_channels = hidden_channels
|
140 |
+
|
141 |
+
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
142 |
+
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name='weight')
|
143 |
+
self.res_skip_layers.append(res_skip_layer)
|
144 |
+
|
145 |
+
def forward(self, x, x_mask, g=None, **kwargs):
|
146 |
+
output = torch.zeros_like(x)
|
147 |
+
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
148 |
+
|
149 |
+
if g is not None:
|
150 |
+
g = self.cond_layer(g)
|
151 |
+
|
152 |
+
for i in range(self.n_layers):
|
153 |
+
x_in = self.in_layers[i](x)
|
154 |
+
if g is not None:
|
155 |
+
cond_offset = i * 2 * self.hidden_channels
|
156 |
+
g_l = g[:, cond_offset:cond_offset + 2 * self.hidden_channels, :]
|
157 |
+
else:
|
158 |
+
g_l = torch.zeros_like(x_in)
|
159 |
+
|
160 |
+
acts = commons.fused_add_tanh_sigmoid_multiply(
|
161 |
+
x_in,
|
162 |
+
g_l,
|
163 |
+
n_channels_tensor)
|
164 |
+
acts = self.drop(acts)
|
165 |
+
|
166 |
+
res_skip_acts = self.res_skip_layers[i](acts)
|
167 |
+
if i < self.n_layers - 1:
|
168 |
+
res_acts = res_skip_acts[:, :self.hidden_channels, :]
|
169 |
+
x = (x + res_acts) * x_mask
|
170 |
+
output = output + res_skip_acts[:, self.hidden_channels:, :]
|
171 |
+
else:
|
172 |
+
output = output + res_skip_acts
|
173 |
+
return output * x_mask
|
174 |
+
|
175 |
+
def remove_weight_norm(self):
|
176 |
+
if self.gin_channels != 0:
|
177 |
+
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
178 |
+
for l in self.in_layers:
|
179 |
+
torch.nn.utils.remove_weight_norm(l)
|
180 |
+
for l in self.res_skip_layers:
|
181 |
+
torch.nn.utils.remove_weight_norm(l)
|
182 |
+
|
183 |
+
|
184 |
+
class ResBlock1(torch.nn.Module):
|
185 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
186 |
+
super(ResBlock1, self).__init__()
|
187 |
+
self.convs1 = nn.ModuleList([
|
188 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
189 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
190 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
191 |
+
padding=get_padding(kernel_size, dilation[1]))),
|
192 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
|
193 |
+
padding=get_padding(kernel_size, dilation[2])))
|
194 |
+
])
|
195 |
+
self.convs1.apply(init_weights)
|
196 |
+
|
197 |
+
self.convs2 = nn.ModuleList([
|
198 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
199 |
+
padding=get_padding(kernel_size, 1))),
|
200 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
201 |
+
padding=get_padding(kernel_size, 1))),
|
202 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
203 |
+
padding=get_padding(kernel_size, 1)))
|
204 |
+
])
|
205 |
+
self.convs2.apply(init_weights)
|
206 |
+
|
207 |
+
def forward(self, x, x_mask=None):
|
208 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
209 |
+
xt = t_func.leaky_relu(x, LRELU_SLOPE)
|
210 |
+
if x_mask is not None:
|
211 |
+
xt = xt * x_mask
|
212 |
+
xt = c1(xt)
|
213 |
+
xt = t_func.leaky_relu(xt, LRELU_SLOPE)
|
214 |
+
if x_mask is not None:
|
215 |
+
xt = xt * x_mask
|
216 |
+
xt = c2(xt)
|
217 |
+
x = xt + x
|
218 |
+
if x_mask is not None:
|
219 |
+
x = x * x_mask
|
220 |
+
return x
|
221 |
+
|
222 |
+
def remove_weight_norm(self):
|
223 |
+
for l in self.convs1:
|
224 |
+
remove_weight_norm(l)
|
225 |
+
for l in self.convs2:
|
226 |
+
remove_weight_norm(l)
|
227 |
+
|
228 |
+
|
229 |
+
class ResBlock2(torch.nn.Module):
|
230 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
|
231 |
+
super(ResBlock2, self).__init__()
|
232 |
+
self.convs = nn.ModuleList([
|
233 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
234 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
235 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
236 |
+
padding=get_padding(kernel_size, dilation[1])))
|
237 |
+
])
|
238 |
+
self.convs.apply(init_weights)
|
239 |
+
|
240 |
+
def forward(self, x, x_mask=None):
|
241 |
+
for c in self.convs:
|
242 |
+
xt = t_func.leaky_relu(x, LRELU_SLOPE)
|
243 |
+
if x_mask is not None:
|
244 |
+
xt = xt * x_mask
|
245 |
+
xt = c(xt)
|
246 |
+
x = xt + x
|
247 |
+
if x_mask is not None:
|
248 |
+
x = x * x_mask
|
249 |
+
return x
|
250 |
+
|
251 |
+
def remove_weight_norm(self):
|
252 |
+
for l in self.convs:
|
253 |
+
remove_weight_norm(l)
|
254 |
+
|
255 |
+
|
256 |
+
class Log(nn.Module):
|
257 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
258 |
+
if not reverse:
|
259 |
+
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
260 |
+
logdet = torch.sum(-y, [1, 2])
|
261 |
+
return y, logdet
|
262 |
+
else:
|
263 |
+
x = torch.exp(x) * x_mask
|
264 |
+
return x
|
265 |
+
|
266 |
+
|
267 |
+
class Flip(nn.Module):
|
268 |
+
def forward(self, x, *args, reverse=False, **kwargs):
|
269 |
+
x = torch.flip(x, [1])
|
270 |
+
if not reverse:
|
271 |
+
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
272 |
+
return x, logdet
|
273 |
+
else:
|
274 |
+
return x
|
275 |
+
|
276 |
+
|
277 |
+
class ElementwiseAffine(nn.Module):
|
278 |
+
def __init__(self, channels):
|
279 |
+
super().__init__()
|
280 |
+
self.channels = channels
|
281 |
+
self.m = nn.Parameter(torch.zeros(channels, 1))
|
282 |
+
self.logs = nn.Parameter(torch.zeros(channels, 1))
|
283 |
+
|
284 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
285 |
+
if not reverse:
|
286 |
+
y = self.m + torch.exp(self.logs) * x
|
287 |
+
y = y * x_mask
|
288 |
+
logdet = torch.sum(self.logs * x_mask, [1, 2])
|
289 |
+
return y, logdet
|
290 |
+
else:
|
291 |
+
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
292 |
+
return x
|
293 |
+
|
294 |
+
|
295 |
+
class ResidualCouplingLayer(nn.Module):
|
296 |
+
def __init__(self,
|
297 |
+
channels,
|
298 |
+
hidden_channels,
|
299 |
+
kernel_size,
|
300 |
+
dilation_rate,
|
301 |
+
n_layers,
|
302 |
+
p_dropout=0,
|
303 |
+
gin_channels=0,
|
304 |
+
mean_only=False):
|
305 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
306 |
+
super().__init__()
|
307 |
+
self.channels = channels
|
308 |
+
self.hidden_channels = hidden_channels
|
309 |
+
self.kernel_size = kernel_size
|
310 |
+
self.dilation_rate = dilation_rate
|
311 |
+
self.n_layers = n_layers
|
312 |
+
self.half_channels = channels // 2
|
313 |
+
self.mean_only = mean_only
|
314 |
+
|
315 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
316 |
+
self.enc = WN(hidden_channels, kernel_size, dilation_rate, n_layers, p_dropout=p_dropout,
|
317 |
+
gin_channels=gin_channels)
|
318 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
319 |
+
self.post.weight.data.zero_()
|
320 |
+
self.post.bias.data.zero_()
|
321 |
+
|
322 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
323 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
324 |
+
h = self.pre(x0) * x_mask
|
325 |
+
h = self.enc(h, x_mask, g=g)
|
326 |
+
stats = self.post(h) * x_mask
|
327 |
+
if not self.mean_only:
|
328 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
329 |
+
else:
|
330 |
+
m = stats
|
331 |
+
logs = torch.zeros_like(m)
|
332 |
+
|
333 |
+
if not reverse:
|
334 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
335 |
+
x = torch.cat([x0, x1], 1)
|
336 |
+
logdet = torch.sum(logs, [1, 2])
|
337 |
+
return x, logdet
|
338 |
+
else:
|
339 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
340 |
+
x = torch.cat([x0, x1], 1)
|
341 |
+
return x
|
342 |
+
|
343 |
+
|
344 |
+
class ConvFlow(nn.Module):
|
345 |
+
def __init__(self, in_channels, filter_channels, kernel_size, n_layers, num_bins=10, tail_bound=5.0):
|
346 |
+
super().__init__()
|
347 |
+
self.in_channels = in_channels
|
348 |
+
self.filter_channels = filter_channels
|
349 |
+
self.kernel_size = kernel_size
|
350 |
+
self.n_layers = n_layers
|
351 |
+
self.num_bins = num_bins
|
352 |
+
self.tail_bound = tail_bound
|
353 |
+
self.half_channels = in_channels // 2
|
354 |
+
|
355 |
+
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
356 |
+
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.)
|
357 |
+
self.proj = nn.Conv1d(filter_channels, self.half_channels * (num_bins * 3 - 1), 1)
|
358 |
+
self.proj.weight.data.zero_()
|
359 |
+
self.proj.bias.data.zero_()
|
360 |
+
|
361 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
362 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
363 |
+
h = self.pre(x0)
|
364 |
+
h = self.convs(h, x_mask, g=g)
|
365 |
+
h = self.proj(h) * x_mask
|
366 |
+
|
367 |
+
b, c, t = x0.shape
|
368 |
+
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
369 |
+
|
370 |
+
unnormalized_widths = h[..., :self.num_bins] / math.sqrt(self.filter_channels)
|
371 |
+
unnormalized_heights = h[..., self.num_bins:2 * self.num_bins] / math.sqrt(self.filter_channels)
|
372 |
+
unnormalized_derivatives = h[..., 2 * self.num_bins:]
|
373 |
+
|
374 |
+
x1, logabsdet = piecewise_rational_quadratic_transform(x1,
|
375 |
+
unnormalized_widths,
|
376 |
+
unnormalized_heights,
|
377 |
+
unnormalized_derivatives,
|
378 |
+
inverse=reverse,
|
379 |
+
tails='linear',
|
380 |
+
tail_bound=self.tail_bound
|
381 |
+
)
|
382 |
+
|
383 |
+
x = torch.cat([x0, x1], 1) * x_mask
|
384 |
+
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
385 |
+
if not reverse:
|
386 |
+
return x, logdet
|
387 |
+
else:
|
388 |
+
return x
|
preprocess_wave.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import librosa
|
4 |
+
import numpy as np
|
5 |
+
import pyworld
|
6 |
+
from scipy.io import wavfile
|
7 |
+
|
8 |
+
import utils
|
9 |
+
|
10 |
+
|
11 |
+
class FeatureInput(object):
|
12 |
+
def __init__(self, samplerate=16000, hop_size=160):
|
13 |
+
self.fs = samplerate
|
14 |
+
self.hop = hop_size
|
15 |
+
|
16 |
+
self.f0_bin = 256
|
17 |
+
self.f0_max = 1100.0
|
18 |
+
self.f0_min = 50.0
|
19 |
+
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
|
20 |
+
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
|
21 |
+
|
22 |
+
def compute_f0(self, path):
|
23 |
+
x, sr = librosa.load(path, sr=self.fs)
|
24 |
+
assert sr == self.fs
|
25 |
+
f0, t = pyworld.dio(
|
26 |
+
x.astype(np.double),
|
27 |
+
fs=sr,
|
28 |
+
f0_ceil=800,
|
29 |
+
frame_period=1000 * self.hop / sr,
|
30 |
+
)
|
31 |
+
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs)
|
32 |
+
for index, pitch in enumerate(f0):
|
33 |
+
f0[index] = round(pitch, 1)
|
34 |
+
return f0
|
35 |
+
|
36 |
+
# for numpy # code from diffsinger
|
37 |
+
def coarse_f0(self, f0):
|
38 |
+
f0_mel = 1127 * np.log(1 + f0 / 700)
|
39 |
+
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * (
|
40 |
+
self.f0_bin - 2
|
41 |
+
) / (self.f0_mel_max - self.f0_mel_min) + 1
|
42 |
+
|
43 |
+
# use 0 or 1
|
44 |
+
f0_mel[f0_mel <= 1] = 1
|
45 |
+
f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1
|
46 |
+
f0_coarse = np.rint(f0_mel).astype(np.int)
|
47 |
+
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (
|
48 |
+
f0_coarse.max(),
|
49 |
+
f0_coarse.min(),
|
50 |
+
)
|
51 |
+
return f0_coarse
|
52 |
+
|
53 |
+
# for tensor # code from diffsinger
|
54 |
+
def coarse_f0_ts(self, f0):
|
55 |
+
f0_mel = 1127 * (1 + f0 / 700).log()
|
56 |
+
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * (
|
57 |
+
self.f0_bin - 2
|
58 |
+
) / (self.f0_mel_max - self.f0_mel_min) + 1
|
59 |
+
|
60 |
+
# use 0 or 1
|
61 |
+
f0_mel[f0_mel <= 1] = 1
|
62 |
+
f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1
|
63 |
+
f0_coarse = (f0_mel + 0.5).long()
|
64 |
+
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (
|
65 |
+
f0_coarse.max(),
|
66 |
+
f0_coarse.min(),
|
67 |
+
)
|
68 |
+
return f0_coarse
|
69 |
+
|
70 |
+
def save_wav(self, wav, path):
|
71 |
+
wav *= 32767 / max(0.01, np.max(np.abs(wav))) * 0.6
|
72 |
+
wavfile.write(path, self.fs, wav.astype(np.int16))
|
73 |
+
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
wavPath = "./data/waves"
|
77 |
+
outPath = "./data/label"
|
78 |
+
if not os.path.exists("./data/label"):
|
79 |
+
os.mkdir("./data/label")
|
80 |
+
|
81 |
+
# define model and load checkpoint
|
82 |
+
hps = utils.get_hparams_from_file("./configs/singing_base.json")
|
83 |
+
featureInput = FeatureInput(hps.data.sampling_rate, hps.data.hop_length)
|
84 |
+
vits_file = open("./filelists/vc_file.txt", "w", encoding="utf-8")
|
85 |
+
|
86 |
+
for spks in os.listdir(wavPath):
|
87 |
+
if os.path.isdir(f"./{wavPath}/{spks}"):
|
88 |
+
os.makedirs(f"./{outPath}/{spks}")
|
89 |
+
for file in os.listdir(f"./{wavPath}/{spks}"):
|
90 |
+
if file.endswith(".wav"):
|
91 |
+
file = file[:-4]
|
92 |
+
audio_path = f"./{wavPath}/{spks}/{file}.wav"
|
93 |
+
featur_pit = featureInput.compute_f0(audio_path)
|
94 |
+
coarse_pit = featureInput.coarse_f0(featur_pit)
|
95 |
+
np.save(
|
96 |
+
f"{outPath}/{spks}/{file}_pitch.npy",
|
97 |
+
coarse_pit,
|
98 |
+
allow_pickle=False,
|
99 |
+
)
|
100 |
+
np.save(
|
101 |
+
f"{outPath}/{spks}/{file}_nsff0.npy",
|
102 |
+
featur_pit,
|
103 |
+
allow_pickle=False,
|
104 |
+
)
|
105 |
+
|
106 |
+
path_audio = f"./data/waves/{spks}/{file}.wav"
|
107 |
+
path_spkid = f"./data/spkid/{spks}.npy"
|
108 |
+
path_label = (
|
109 |
+
f"./data/phone/{spks}/{file}.npy" # phone means ppg & hubert
|
110 |
+
)
|
111 |
+
path_pitch = f"./data/label/{spks}/{file}_pitch.npy"
|
112 |
+
path_nsff0 = f"./data/label/{spks}/{file}_nsff0.npy"
|
113 |
+
print(
|
114 |
+
f"{path_audio}|{path_spkid}|{path_label}|{path_pitch}|{path_nsff0}",
|
115 |
+
file=vits_file,
|
116 |
+
)
|
117 |
+
|
118 |
+
vits_file.close()
|