Upload 67 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- config.py +38 -0
- go-web.bat +1 -0
- go.bat +1 -0
- hubert_base.pt +3 -0
- infer-web.py +193 -0
- infer.py +48 -0
- infer_pack/__pycache__/attentions.cpython-39.pyc +0 -0
- infer_pack/__pycache__/commons.cpython-39.pyc +0 -0
- infer_pack/__pycache__/models.cpython-39.pyc +0 -0
- infer_pack/__pycache__/modules.cpython-39.pyc +0 -0
- infer_pack/__pycache__/transforms.cpython-39.pyc +0 -0
- infer_pack/attentions.py +417 -0
- infer_pack/commons.py +164 -0
- infer_pack/models.py +664 -0
- infer_pack/modules.py +522 -0
- infer_pack/transforms.py +193 -0
- infer_uvr5.py +108 -0
- slicer.py +151 -0
- trainset_preprocess_pipeline.py +63 -0
- uvr5_pack/__pycache__/utils.cpython-39.pyc +0 -0
- uvr5_pack/lib_v5/__pycache__/layers_123821KB.cpython-39.pyc +0 -0
- uvr5_pack/lib_v5/__pycache__/model_param_init.cpython-39.pyc +0 -0
- uvr5_pack/lib_v5/__pycache__/nets_61968KB.cpython-39.pyc +0 -0
- uvr5_pack/lib_v5/__pycache__/spec_utils.cpython-39.pyc +0 -0
- uvr5_pack/lib_v5/dataset.py +170 -0
- uvr5_pack/lib_v5/layers.py +116 -0
- uvr5_pack/lib_v5/layers_123812KB .py +116 -0
- uvr5_pack/lib_v5/layers_123821KB.py +116 -0
- uvr5_pack/lib_v5/layers_33966KB.py +122 -0
- uvr5_pack/lib_v5/layers_537227KB.py +122 -0
- uvr5_pack/lib_v5/layers_537238KB.py +122 -0
- uvr5_pack/lib_v5/model_param_init.py +60 -0
- uvr5_pack/lib_v5/modelparams/1band_sr16000_hl512.json +19 -0
- uvr5_pack/lib_v5/modelparams/1band_sr32000_hl512.json +19 -0
- uvr5_pack/lib_v5/modelparams/1band_sr33075_hl384.json +19 -0
- uvr5_pack/lib_v5/modelparams/1band_sr44100_hl1024.json +19 -0
- uvr5_pack/lib_v5/modelparams/1band_sr44100_hl256.json +19 -0
- uvr5_pack/lib_v5/modelparams/1band_sr44100_hl512.json +19 -0
- uvr5_pack/lib_v5/modelparams/1band_sr44100_hl512_cut.json +19 -0
- uvr5_pack/lib_v5/modelparams/2band_32000.json +30 -0
- uvr5_pack/lib_v5/modelparams/2band_44100_lofi.json +30 -0
- uvr5_pack/lib_v5/modelparams/2band_48000.json +30 -0
- uvr5_pack/lib_v5/modelparams/3band_44100.json +42 -0
- uvr5_pack/lib_v5/modelparams/3band_44100_mid.json +43 -0
- uvr5_pack/lib_v5/modelparams/3band_44100_msb2.json +43 -0
- uvr5_pack/lib_v5/modelparams/4band_44100.json +54 -0
- uvr5_pack/lib_v5/modelparams/4band_44100_mid.json +55 -0
- uvr5_pack/lib_v5/modelparams/4band_44100_msb.json +55 -0
- uvr5_pack/lib_v5/modelparams/4band_44100_msb2.json +55 -0
- uvr5_pack/lib_v5/modelparams/4band_44100_reverse.json +55 -0
config.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
############离线VC参数
|
2 |
+
inp_root=r"白鹭霜华长条"#对输入目录下所有音频进行转换,别放非音频文件
|
3 |
+
opt_root=r"opt"#输出目录
|
4 |
+
f0_up_key=0#升降调,整数,男转女12,女转男-12
|
5 |
+
person=r"weights\洛天依v3.pt"#目前只有洛天依v3
|
6 |
+
############硬件参数
|
7 |
+
device = "cuda:0"#填写cuda:x或cpu,x指代第几张卡,只支持N卡加速
|
8 |
+
is_half=True#9-10-20-30-40系显卡无脑True,不影响质量,>=20显卡开启有加速
|
9 |
+
n_cpu=0#默认0用上所有线程,写数字限制CPU资源使用
|
10 |
+
############下头别动
|
11 |
+
import torch
|
12 |
+
if(torch.cuda.is_available()==False):
|
13 |
+
print("没有发现支持的N卡,使用CPU进行推理")
|
14 |
+
device="cpu"
|
15 |
+
is_half=False
|
16 |
+
if(device!="cpu"):
|
17 |
+
gpu_name=torch.cuda.get_device_name(int(device.split(":")[-1]))
|
18 |
+
if("16"in gpu_name):
|
19 |
+
print("16系显卡强制单精度")
|
20 |
+
is_half=False
|
21 |
+
from multiprocessing import cpu_count
|
22 |
+
if(n_cpu==0):n_cpu=cpu_count()
|
23 |
+
if(is_half==True):
|
24 |
+
#6G显存配置
|
25 |
+
x_pad = 3
|
26 |
+
x_query = 10
|
27 |
+
x_center = 60
|
28 |
+
x_max = 65
|
29 |
+
else:
|
30 |
+
#5G显存配置
|
31 |
+
x_pad = 1
|
32 |
+
# x_query = 6
|
33 |
+
# x_center = 30
|
34 |
+
# x_max = 32
|
35 |
+
#6G显存配置
|
36 |
+
x_query = 6
|
37 |
+
x_center = 38
|
38 |
+
x_max = 41
|
go-web.bat
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
runtime\python.exe infer-web.py
|
go.bat
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
runtime\python.exe infer.py
|
hubert_base.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f54b40fd2802423a5643779c4861af1e9ee9c1564dc9d32f54f20b5ffba7db96
|
3 |
+
size 189507909
|
infer-web.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch, pdb, os,traceback,sys,warnings,shutil
|
2 |
+
now_dir=os.getcwd()
|
3 |
+
sys.path.append(now_dir)
|
4 |
+
tmp=os.path.join(now_dir,"TEMP")
|
5 |
+
shutil.rmtree(tmp,ignore_errors=True)
|
6 |
+
os.makedirs(tmp,exist_ok=True)
|
7 |
+
os.environ["TEMP"]=tmp
|
8 |
+
warnings.filterwarnings("ignore")
|
9 |
+
torch.manual_seed(114514)
|
10 |
+
from infer_pack.models import SynthesizerTrnMs256NSF as SynthesizerTrn256
|
11 |
+
from scipy.io import wavfile
|
12 |
+
from fairseq import checkpoint_utils
|
13 |
+
import gradio as gr
|
14 |
+
import librosa
|
15 |
+
import logging
|
16 |
+
from vc_infer_pipeline import VC
|
17 |
+
import soundfile as sf
|
18 |
+
from config import is_half,device,is_half
|
19 |
+
from infer_uvr5 import _audio_pre_
|
20 |
+
logging.getLogger('numba').setLevel(logging.WARNING)
|
21 |
+
|
22 |
+
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)
|
23 |
+
hubert_model = models[0]
|
24 |
+
hubert_model = hubert_model.to(device)
|
25 |
+
if(is_half):hubert_model = hubert_model.half()
|
26 |
+
else:hubert_model = hubert_model.float()
|
27 |
+
hubert_model.eval()
|
28 |
+
|
29 |
+
|
30 |
+
weight_root="weights"
|
31 |
+
weight_uvr5_root="uvr5_weights"
|
32 |
+
names=[]
|
33 |
+
for name in os.listdir(weight_root):names.append(name.replace(".pt",""))
|
34 |
+
uvr5_names=[]
|
35 |
+
for name in os.listdir(weight_uvr5_root):uvr5_names.append(name.replace(".pth",""))
|
36 |
+
|
37 |
+
def get_vc(sid):
|
38 |
+
person = "%s/%s.pt" % (weight_root, sid)
|
39 |
+
cpt = torch.load(person, map_location="cpu")
|
40 |
+
dv = cpt["dv"]
|
41 |
+
tgt_sr = cpt["config"][-1]
|
42 |
+
net_g = SynthesizerTrn256(*cpt["config"], is_half=is_half)
|
43 |
+
net_g.load_state_dict(cpt["weight"], strict=True)
|
44 |
+
net_g.eval().to(device)
|
45 |
+
if (is_half):net_g = net_g.half()
|
46 |
+
else:net_g = net_g.float()
|
47 |
+
vc = VC(tgt_sr, device, is_half)
|
48 |
+
return dv,tgt_sr,net_g,vc
|
49 |
+
|
50 |
+
def vc_single(sid,input_audio,f0_up_key,f0_file):
|
51 |
+
if input_audio is None:return "You need to upload an audio", None
|
52 |
+
f0_up_key = int(f0_up_key)
|
53 |
+
try:
|
54 |
+
if(type(input_audio)==str):
|
55 |
+
print("processing %s" % input_audio)
|
56 |
+
audio, sampling_rate = sf.read(input_audio)
|
57 |
+
else:
|
58 |
+
sampling_rate, audio = input_audio
|
59 |
+
audio = audio.astype("float32") / 32768
|
60 |
+
if(type(sid)==str):dv, tgt_sr, net_g, vc=get_vc(sid)
|
61 |
+
else:dv,tgt_sr,net_g,vc=sid
|
62 |
+
if len(audio.shape) > 1:
|
63 |
+
audio = librosa.to_mono(audio.transpose(1, 0))
|
64 |
+
if sampling_rate != 16000:
|
65 |
+
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
|
66 |
+
times = [0, 0, 0]
|
67 |
+
audio_opt=vc.pipeline(hubert_model,net_g,dv,audio,times,f0_up_key,f0_file=f0_file)
|
68 |
+
print(times)
|
69 |
+
return "Success", (tgt_sr, audio_opt)
|
70 |
+
except:
|
71 |
+
info=traceback.format_exc()
|
72 |
+
print(info)
|
73 |
+
return info,(None,None)
|
74 |
+
finally:
|
75 |
+
print("clean_empty_cache")
|
76 |
+
del net_g,dv,vc
|
77 |
+
torch.cuda.empty_cache()
|
78 |
+
|
79 |
+
def vc_multi(sid,dir_path,opt_root,paths,f0_up_key):
|
80 |
+
try:
|
81 |
+
dir_path=dir_path.strip(" ")#防止小白拷路径头尾带了空格
|
82 |
+
opt_root=opt_root.strip(" ")
|
83 |
+
os.makedirs(opt_root, exist_ok=True)
|
84 |
+
dv, tgt_sr, net_g, vc = get_vc(sid)
|
85 |
+
try:
|
86 |
+
if(dir_path!=""):paths=[os.path.join(dir_path,name)for name in os.listdir(dir_path)]
|
87 |
+
else:paths=[path.name for path in paths]
|
88 |
+
except:
|
89 |
+
traceback.print_exc()
|
90 |
+
paths = [path.name for path in paths]
|
91 |
+
infos=[]
|
92 |
+
for path in paths:
|
93 |
+
info,opt=vc_single([dv,tgt_sr,net_g,vc],path,f0_up_key,f0_file=None)
|
94 |
+
if(info=="Success"):
|
95 |
+
try:
|
96 |
+
tgt_sr,audio_opt=opt
|
97 |
+
wavfile.write("%s/%s" % (opt_root, os.path.basename(path)), tgt_sr, audio_opt)
|
98 |
+
except:
|
99 |
+
info=traceback.format_exc()
|
100 |
+
infos.append("%s->%s"%(os.path.basename(path),info))
|
101 |
+
return "\n".join(infos)
|
102 |
+
except:
|
103 |
+
return traceback.format_exc()
|
104 |
+
finally:
|
105 |
+
print("clean_empty_cache")
|
106 |
+
del net_g,dv,vc
|
107 |
+
torch.cuda.empty_cache()
|
108 |
+
|
109 |
+
def uvr(model_name,inp_root,save_root_vocal,save_root_ins):
|
110 |
+
infos = []
|
111 |
+
try:
|
112 |
+
inp_root = inp_root.strip(" ")# 防止小白拷路径头尾带了空格
|
113 |
+
save_root_vocal = save_root_vocal.strip(" ")
|
114 |
+
save_root_ins = save_root_ins.strip(" ")
|
115 |
+
pre_fun = _audio_pre_(model_path=os.path.join(weight_uvr5_root,model_name+".pth"), device=device, is_half=is_half)
|
116 |
+
for name in os.listdir(inp_root):
|
117 |
+
inp_path=os.path.join(inp_root,name)
|
118 |
+
try:
|
119 |
+
pre_fun._path_audio_(inp_path , save_root_ins,save_root_vocal)
|
120 |
+
infos.append("%s->Success"%(os.path.basename(inp_path)))
|
121 |
+
except:
|
122 |
+
infos.append("%s->%s" % (os.path.basename(inp_path),traceback.format_exc()))
|
123 |
+
except:
|
124 |
+
infos.append(traceback.format_exc())
|
125 |
+
finally:
|
126 |
+
try:
|
127 |
+
del pre_fun.model
|
128 |
+
del pre_fun
|
129 |
+
except:
|
130 |
+
traceback.print_exc()
|
131 |
+
print("clean_empty_cache")
|
132 |
+
torch.cuda.empty_cache()
|
133 |
+
return "\n".join(infos)
|
134 |
+
|
135 |
+
with gr.Blocks() as app:
|
136 |
+
with gr.Tabs():
|
137 |
+
with gr.TabItem("推理"):
|
138 |
+
with gr.Group():
|
139 |
+
gr.Markdown(value="""
|
140 |
+
使用软件者、传播软件导出的声音者自负全责。如不认可该条款,则不能使用/引用软件包内所有代码和文件。<br>
|
141 |
+
目前仅开放白菜音色,后续将扩展为本地训练推理工具,用户可训练自己的音色进行社区共享。<br>
|
142 |
+
男转女推荐+12key,女转男推荐-12key,如果音域爆炸导致音色失真也可以自己调整到合适音域
|
143 |
+
""")
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column():
|
146 |
+
sid0 = gr.Dropdown(label="音色", choices=names)
|
147 |
+
vc_transform0 = gr.Number(label="变调(整数,半音数量,升八度12降八度-12)", value=12)
|
148 |
+
f0_file = gr.File(label="F0曲线文件,可选,一行一个音高,代替默认F0及升降调")
|
149 |
+
input_audio0 = gr.Audio(label="上传音频")
|
150 |
+
but0=gr.Button("转换", variant="primary")
|
151 |
+
with gr.Column():
|
152 |
+
vc_output1 = gr.Textbox(label="输出信息")
|
153 |
+
vc_output2 = gr.Audio(label="输出音频")
|
154 |
+
but0.click(vc_single, [sid0, input_audio0, vc_transform0,f0_file], [vc_output1, vc_output2])
|
155 |
+
with gr.Group():
|
156 |
+
gr.Markdown(value="""
|
157 |
+
批量转换,上传多个音频文件,在指定文件夹(默认opt)下输出转换的音频。<br>
|
158 |
+
合格的文件夹路径格式举例:E:\codes\py39\\vits_vc_gpu\白鹭霜华测试样例(去文件管理器地址栏拷就行了)
|
159 |
+
""")
|
160 |
+
with gr.Row():
|
161 |
+
with gr.Column():
|
162 |
+
sid1 = gr.Dropdown(label="音色", choices=names)
|
163 |
+
vc_transform1 = gr.Number(label="变调(整数,半音数量,升八度12降八度-12)", value=12)
|
164 |
+
opt_input = gr.Textbox(label="指定输出文件夹",value="opt")
|
165 |
+
with gr.Column():
|
166 |
+
dir_input = gr.Textbox(label="输入待处理音频文件夹路径")
|
167 |
+
inputs = gr.File(file_count="multiple", label="也可批量输入音频文件,二选一,优先读文件夹")
|
168 |
+
but1=gr.Button("转换", variant="primary")
|
169 |
+
vc_output3 = gr.Textbox(label="输出信息")
|
170 |
+
but1.click(vc_multi, [sid1, dir_input,opt_input,inputs, vc_transform1], [vc_output3])
|
171 |
+
|
172 |
+
with gr.TabItem("数据处理"):
|
173 |
+
with gr.Group():
|
174 |
+
gr.Markdown(value="""
|
175 |
+
人声伴奏分离批量处理,使用UVR5模型。<br>
|
176 |
+
不带和声用HP2,带和声且提取的人声不需要和声用HP5<br>
|
177 |
+
合格的文件夹路径格式举例:E:\codes\py39\\vits_vc_gpu\白鹭霜华测试样例(去文件管理器地址栏拷就行了)
|
178 |
+
""")
|
179 |
+
with gr.Row():
|
180 |
+
with gr.Column():
|
181 |
+
dir_wav_input = gr.Textbox(label="输入待处理音频文件夹路径")
|
182 |
+
wav_inputs = gr.File(file_count="multiple", label="也可批量输入音频文件,二选一,优先读文件夹")
|
183 |
+
with gr.Column():
|
184 |
+
model_choose = gr.Dropdown(label="模型", choices=uvr5_names)
|
185 |
+
opt_vocal_root = gr.Textbox(label="指定输出人声文件夹",value="opt")
|
186 |
+
opt_ins_root = gr.Textbox(label="指定输出乐器文件夹",value="opt")
|
187 |
+
but2=gr.Button("转换", variant="primary")
|
188 |
+
vc_output4 = gr.Textbox(label="输出信息")
|
189 |
+
but2.click(uvr, [model_choose, dir_wav_input,opt_vocal_root,opt_ins_root], [vc_output4])
|
190 |
+
with gr.TabItem("训练-待开放"):pass
|
191 |
+
|
192 |
+
# app.launch(server_name="0.0.0.0",server_port=7860)
|
193 |
+
app.launch(server_name="127.0.0.1",server_port=7860)
|
infer.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch, pdb, os,sys,librosa,warnings,traceback
|
2 |
+
warnings.filterwarnings("ignore")
|
3 |
+
torch.manual_seed(114514)
|
4 |
+
sys.path.append(os.getcwd())
|
5 |
+
from config import inp_root,opt_root,f0_up_key,person,is_half,device
|
6 |
+
os.makedirs(opt_root,exist_ok=True)
|
7 |
+
import soundfile as sf
|
8 |
+
from infer_pack.models import SynthesizerTrnMs256NSF as SynthesizerTrn256
|
9 |
+
from scipy.io import wavfile
|
10 |
+
from fairseq import checkpoint_utils
|
11 |
+
import scipy.signal as signal
|
12 |
+
from vc_infer_pipeline import VC
|
13 |
+
|
14 |
+
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)
|
15 |
+
model = models[0]
|
16 |
+
model = model.to(device)
|
17 |
+
if(is_half):model = model.half()
|
18 |
+
else:model = model.float()
|
19 |
+
model.eval()
|
20 |
+
|
21 |
+
cpt=torch.load(person,map_location="cpu")
|
22 |
+
dv=cpt["dv"]
|
23 |
+
tgt_sr=cpt["config"][-1]
|
24 |
+
net_g = SynthesizerTrn256(*cpt["config"],is_half=is_half)
|
25 |
+
net_g.load_state_dict(cpt["weight"],strict=True)
|
26 |
+
net_g.eval().to(device)
|
27 |
+
if(is_half):net_g = net_g.half()
|
28 |
+
else:net_g = net_g.float()
|
29 |
+
|
30 |
+
vc=VC(tgt_sr,device,is_half)
|
31 |
+
|
32 |
+
for name in os.listdir(inp_root):
|
33 |
+
try:
|
34 |
+
wav_path="%s\%s"%(inp_root,name)
|
35 |
+
print("processing %s"%wav_path)
|
36 |
+
audio, sampling_rate = sf.read(wav_path)
|
37 |
+
if len(audio.shape) > 1:
|
38 |
+
audio = librosa.to_mono(audio.transpose(1, 0))
|
39 |
+
if sampling_rate != vc.sr:
|
40 |
+
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=vc.sr)
|
41 |
+
|
42 |
+
times = [0, 0, 0]
|
43 |
+
audio_opt=vc.pipeline(model,net_g,dv,audio,times,f0_up_key,f0_file=None)
|
44 |
+
wavfile.write("%s/%s"%(opt_root,name), tgt_sr, audio_opt)
|
45 |
+
except:
|
46 |
+
traceback.print_exc()
|
47 |
+
|
48 |
+
print(times)
|
infer_pack/__pycache__/attentions.cpython-39.pyc
ADDED
Binary file (9.92 kB). View file
|
|
infer_pack/__pycache__/commons.cpython-39.pyc
ADDED
Binary file (5.88 kB). View file
|
|
infer_pack/__pycache__/models.cpython-39.pyc
ADDED
Binary file (16.7 kB). View file
|
|
infer_pack/__pycache__/modules.cpython-39.pyc
ADDED
Binary file (11.9 kB). View file
|
|
infer_pack/__pycache__/transforms.cpython-39.pyc
ADDED
Binary file (3.92 kB). View file
|
|
infer_pack/attentions.py
ADDED
@@ -0,0 +1,417 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import math
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
from torch import nn
|
6 |
+
from torch.nn import functional as F
|
7 |
+
|
8 |
+
from infer_pack import commons
|
9 |
+
from infer_pack import modules
|
10 |
+
from infer_pack.modules import LayerNorm
|
11 |
+
|
12 |
+
|
13 |
+
class Encoder(nn.Module):
|
14 |
+
def __init__(
|
15 |
+
self,
|
16 |
+
hidden_channels,
|
17 |
+
filter_channels,
|
18 |
+
n_heads,
|
19 |
+
n_layers,
|
20 |
+
kernel_size=1,
|
21 |
+
p_dropout=0.0,
|
22 |
+
window_size=10,
|
23 |
+
**kwargs
|
24 |
+
):
|
25 |
+
super().__init__()
|
26 |
+
self.hidden_channels = hidden_channels
|
27 |
+
self.filter_channels = filter_channels
|
28 |
+
self.n_heads = n_heads
|
29 |
+
self.n_layers = n_layers
|
30 |
+
self.kernel_size = kernel_size
|
31 |
+
self.p_dropout = p_dropout
|
32 |
+
self.window_size = window_size
|
33 |
+
|
34 |
+
self.drop = nn.Dropout(p_dropout)
|
35 |
+
self.attn_layers = nn.ModuleList()
|
36 |
+
self.norm_layers_1 = nn.ModuleList()
|
37 |
+
self.ffn_layers = nn.ModuleList()
|
38 |
+
self.norm_layers_2 = nn.ModuleList()
|
39 |
+
for i in range(self.n_layers):
|
40 |
+
self.attn_layers.append(
|
41 |
+
MultiHeadAttention(
|
42 |
+
hidden_channels,
|
43 |
+
hidden_channels,
|
44 |
+
n_heads,
|
45 |
+
p_dropout=p_dropout,
|
46 |
+
window_size=window_size,
|
47 |
+
)
|
48 |
+
)
|
49 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
50 |
+
self.ffn_layers.append(
|
51 |
+
FFN(
|
52 |
+
hidden_channels,
|
53 |
+
hidden_channels,
|
54 |
+
filter_channels,
|
55 |
+
kernel_size,
|
56 |
+
p_dropout=p_dropout,
|
57 |
+
)
|
58 |
+
)
|
59 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
60 |
+
|
61 |
+
def forward(self, x, x_mask):
|
62 |
+
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
63 |
+
x = x * x_mask
|
64 |
+
for i in range(self.n_layers):
|
65 |
+
y = self.attn_layers[i](x, x, attn_mask)
|
66 |
+
y = self.drop(y)
|
67 |
+
x = self.norm_layers_1[i](x + y)
|
68 |
+
|
69 |
+
y = self.ffn_layers[i](x, x_mask)
|
70 |
+
y = self.drop(y)
|
71 |
+
x = self.norm_layers_2[i](x + y)
|
72 |
+
x = x * x_mask
|
73 |
+
return x
|
74 |
+
|
75 |
+
|
76 |
+
class Decoder(nn.Module):
|
77 |
+
def __init__(
|
78 |
+
self,
|
79 |
+
hidden_channels,
|
80 |
+
filter_channels,
|
81 |
+
n_heads,
|
82 |
+
n_layers,
|
83 |
+
kernel_size=1,
|
84 |
+
p_dropout=0.0,
|
85 |
+
proximal_bias=False,
|
86 |
+
proximal_init=True,
|
87 |
+
**kwargs
|
88 |
+
):
|
89 |
+
super().__init__()
|
90 |
+
self.hidden_channels = hidden_channels
|
91 |
+
self.filter_channels = filter_channels
|
92 |
+
self.n_heads = n_heads
|
93 |
+
self.n_layers = n_layers
|
94 |
+
self.kernel_size = kernel_size
|
95 |
+
self.p_dropout = p_dropout
|
96 |
+
self.proximal_bias = proximal_bias
|
97 |
+
self.proximal_init = proximal_init
|
98 |
+
|
99 |
+
self.drop = nn.Dropout(p_dropout)
|
100 |
+
self.self_attn_layers = nn.ModuleList()
|
101 |
+
self.norm_layers_0 = nn.ModuleList()
|
102 |
+
self.encdec_attn_layers = nn.ModuleList()
|
103 |
+
self.norm_layers_1 = nn.ModuleList()
|
104 |
+
self.ffn_layers = nn.ModuleList()
|
105 |
+
self.norm_layers_2 = nn.ModuleList()
|
106 |
+
for i in range(self.n_layers):
|
107 |
+
self.self_attn_layers.append(
|
108 |
+
MultiHeadAttention(
|
109 |
+
hidden_channels,
|
110 |
+
hidden_channels,
|
111 |
+
n_heads,
|
112 |
+
p_dropout=p_dropout,
|
113 |
+
proximal_bias=proximal_bias,
|
114 |
+
proximal_init=proximal_init,
|
115 |
+
)
|
116 |
+
)
|
117 |
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
118 |
+
self.encdec_attn_layers.append(
|
119 |
+
MultiHeadAttention(
|
120 |
+
hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
|
121 |
+
)
|
122 |
+
)
|
123 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
124 |
+
self.ffn_layers.append(
|
125 |
+
FFN(
|
126 |
+
hidden_channels,
|
127 |
+
hidden_channels,
|
128 |
+
filter_channels,
|
129 |
+
kernel_size,
|
130 |
+
p_dropout=p_dropout,
|
131 |
+
causal=True,
|
132 |
+
)
|
133 |
+
)
|
134 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
135 |
+
|
136 |
+
def forward(self, x, x_mask, h, h_mask):
|
137 |
+
"""
|
138 |
+
x: decoder input
|
139 |
+
h: encoder output
|
140 |
+
"""
|
141 |
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
142 |
+
device=x.device, dtype=x.dtype
|
143 |
+
)
|
144 |
+
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
145 |
+
x = x * x_mask
|
146 |
+
for i in range(self.n_layers):
|
147 |
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
148 |
+
y = self.drop(y)
|
149 |
+
x = self.norm_layers_0[i](x + y)
|
150 |
+
|
151 |
+
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
152 |
+
y = self.drop(y)
|
153 |
+
x = self.norm_layers_1[i](x + y)
|
154 |
+
|
155 |
+
y = self.ffn_layers[i](x, x_mask)
|
156 |
+
y = self.drop(y)
|
157 |
+
x = self.norm_layers_2[i](x + y)
|
158 |
+
x = x * x_mask
|
159 |
+
return x
|
160 |
+
|
161 |
+
|
162 |
+
class MultiHeadAttention(nn.Module):
|
163 |
+
def __init__(
|
164 |
+
self,
|
165 |
+
channels,
|
166 |
+
out_channels,
|
167 |
+
n_heads,
|
168 |
+
p_dropout=0.0,
|
169 |
+
window_size=None,
|
170 |
+
heads_share=True,
|
171 |
+
block_length=None,
|
172 |
+
proximal_bias=False,
|
173 |
+
proximal_init=False,
|
174 |
+
):
|
175 |
+
super().__init__()
|
176 |
+
assert channels % n_heads == 0
|
177 |
+
|
178 |
+
self.channels = channels
|
179 |
+
self.out_channels = out_channels
|
180 |
+
self.n_heads = n_heads
|
181 |
+
self.p_dropout = p_dropout
|
182 |
+
self.window_size = window_size
|
183 |
+
self.heads_share = heads_share
|
184 |
+
self.block_length = block_length
|
185 |
+
self.proximal_bias = proximal_bias
|
186 |
+
self.proximal_init = proximal_init
|
187 |
+
self.attn = None
|
188 |
+
|
189 |
+
self.k_channels = channels // n_heads
|
190 |
+
self.conv_q = nn.Conv1d(channels, channels, 1)
|
191 |
+
self.conv_k = nn.Conv1d(channels, channels, 1)
|
192 |
+
self.conv_v = nn.Conv1d(channels, channels, 1)
|
193 |
+
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
194 |
+
self.drop = nn.Dropout(p_dropout)
|
195 |
+
|
196 |
+
if window_size is not None:
|
197 |
+
n_heads_rel = 1 if heads_share else n_heads
|
198 |
+
rel_stddev = self.k_channels**-0.5
|
199 |
+
self.emb_rel_k = nn.Parameter(
|
200 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
201 |
+
* rel_stddev
|
202 |
+
)
|
203 |
+
self.emb_rel_v = nn.Parameter(
|
204 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
205 |
+
* rel_stddev
|
206 |
+
)
|
207 |
+
|
208 |
+
nn.init.xavier_uniform_(self.conv_q.weight)
|
209 |
+
nn.init.xavier_uniform_(self.conv_k.weight)
|
210 |
+
nn.init.xavier_uniform_(self.conv_v.weight)
|
211 |
+
if proximal_init:
|
212 |
+
with torch.no_grad():
|
213 |
+
self.conv_k.weight.copy_(self.conv_q.weight)
|
214 |
+
self.conv_k.bias.copy_(self.conv_q.bias)
|
215 |
+
|
216 |
+
def forward(self, x, c, attn_mask=None):
|
217 |
+
q = self.conv_q(x)
|
218 |
+
k = self.conv_k(c)
|
219 |
+
v = self.conv_v(c)
|
220 |
+
|
221 |
+
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
222 |
+
|
223 |
+
x = self.conv_o(x)
|
224 |
+
return x
|
225 |
+
|
226 |
+
def attention(self, query, key, value, mask=None):
|
227 |
+
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
228 |
+
b, d, t_s, t_t = (*key.size(), query.size(2))
|
229 |
+
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
230 |
+
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
231 |
+
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
232 |
+
|
233 |
+
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
234 |
+
if self.window_size is not None:
|
235 |
+
assert (
|
236 |
+
t_s == t_t
|
237 |
+
), "Relative attention is only available for self-attention."
|
238 |
+
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
239 |
+
rel_logits = self._matmul_with_relative_keys(
|
240 |
+
query / math.sqrt(self.k_channels), key_relative_embeddings
|
241 |
+
)
|
242 |
+
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
243 |
+
scores = scores + scores_local
|
244 |
+
if self.proximal_bias:
|
245 |
+
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
246 |
+
scores = scores + self._attention_bias_proximal(t_s).to(
|
247 |
+
device=scores.device, dtype=scores.dtype
|
248 |
+
)
|
249 |
+
if mask is not None:
|
250 |
+
scores = scores.masked_fill(mask == 0, -1e4)
|
251 |
+
if self.block_length is not None:
|
252 |
+
assert (
|
253 |
+
t_s == t_t
|
254 |
+
), "Local attention is only available for self-attention."
|
255 |
+
block_mask = (
|
256 |
+
torch.ones_like(scores)
|
257 |
+
.triu(-self.block_length)
|
258 |
+
.tril(self.block_length)
|
259 |
+
)
|
260 |
+
scores = scores.masked_fill(block_mask == 0, -1e4)
|
261 |
+
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
262 |
+
p_attn = self.drop(p_attn)
|
263 |
+
output = torch.matmul(p_attn, value)
|
264 |
+
if self.window_size is not None:
|
265 |
+
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
266 |
+
value_relative_embeddings = self._get_relative_embeddings(
|
267 |
+
self.emb_rel_v, t_s
|
268 |
+
)
|
269 |
+
output = output + self._matmul_with_relative_values(
|
270 |
+
relative_weights, value_relative_embeddings
|
271 |
+
)
|
272 |
+
output = (
|
273 |
+
output.transpose(2, 3).contiguous().view(b, d, t_t)
|
274 |
+
) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
275 |
+
return output, p_attn
|
276 |
+
|
277 |
+
def _matmul_with_relative_values(self, x, y):
|
278 |
+
"""
|
279 |
+
x: [b, h, l, m]
|
280 |
+
y: [h or 1, m, d]
|
281 |
+
ret: [b, h, l, d]
|
282 |
+
"""
|
283 |
+
ret = torch.matmul(x, y.unsqueeze(0))
|
284 |
+
return ret
|
285 |
+
|
286 |
+
def _matmul_with_relative_keys(self, x, y):
|
287 |
+
"""
|
288 |
+
x: [b, h, l, d]
|
289 |
+
y: [h or 1, m, d]
|
290 |
+
ret: [b, h, l, m]
|
291 |
+
"""
|
292 |
+
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
293 |
+
return ret
|
294 |
+
|
295 |
+
def _get_relative_embeddings(self, relative_embeddings, length):
|
296 |
+
max_relative_position = 2 * self.window_size + 1
|
297 |
+
# Pad first before slice to avoid using cond ops.
|
298 |
+
pad_length = max(length - (self.window_size + 1), 0)
|
299 |
+
slice_start_position = max((self.window_size + 1) - length, 0)
|
300 |
+
slice_end_position = slice_start_position + 2 * length - 1
|
301 |
+
if pad_length > 0:
|
302 |
+
padded_relative_embeddings = F.pad(
|
303 |
+
relative_embeddings,
|
304 |
+
commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
|
305 |
+
)
|
306 |
+
else:
|
307 |
+
padded_relative_embeddings = relative_embeddings
|
308 |
+
used_relative_embeddings = padded_relative_embeddings[
|
309 |
+
:, slice_start_position:slice_end_position
|
310 |
+
]
|
311 |
+
return used_relative_embeddings
|
312 |
+
|
313 |
+
def _relative_position_to_absolute_position(self, x):
|
314 |
+
"""
|
315 |
+
x: [b, h, l, 2*l-1]
|
316 |
+
ret: [b, h, l, l]
|
317 |
+
"""
|
318 |
+
batch, heads, length, _ = x.size()
|
319 |
+
# Concat columns of pad to shift from relative to absolute indexing.
|
320 |
+
x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
|
321 |
+
|
322 |
+
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
323 |
+
x_flat = x.view([batch, heads, length * 2 * length])
|
324 |
+
x_flat = F.pad(
|
325 |
+
x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
|
326 |
+
)
|
327 |
+
|
328 |
+
# Reshape and slice out the padded elements.
|
329 |
+
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
|
330 |
+
:, :, :length, length - 1 :
|
331 |
+
]
|
332 |
+
return x_final
|
333 |
+
|
334 |
+
def _absolute_position_to_relative_position(self, x):
|
335 |
+
"""
|
336 |
+
x: [b, h, l, l]
|
337 |
+
ret: [b, h, l, 2*l-1]
|
338 |
+
"""
|
339 |
+
batch, heads, length, _ = x.size()
|
340 |
+
# padd along column
|
341 |
+
x = F.pad(
|
342 |
+
x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
|
343 |
+
)
|
344 |
+
x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
|
345 |
+
# add 0's in the beginning that will skew the elements after reshape
|
346 |
+
x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
347 |
+
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
348 |
+
return x_final
|
349 |
+
|
350 |
+
def _attention_bias_proximal(self, length):
|
351 |
+
"""Bias for self-attention to encourage attention to close positions.
|
352 |
+
Args:
|
353 |
+
length: an integer scalar.
|
354 |
+
Returns:
|
355 |
+
a Tensor with shape [1, 1, length, length]
|
356 |
+
"""
|
357 |
+
r = torch.arange(length, dtype=torch.float32)
|
358 |
+
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
359 |
+
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
360 |
+
|
361 |
+
|
362 |
+
class FFN(nn.Module):
|
363 |
+
def __init__(
|
364 |
+
self,
|
365 |
+
in_channels,
|
366 |
+
out_channels,
|
367 |
+
filter_channels,
|
368 |
+
kernel_size,
|
369 |
+
p_dropout=0.0,
|
370 |
+
activation=None,
|
371 |
+
causal=False,
|
372 |
+
):
|
373 |
+
super().__init__()
|
374 |
+
self.in_channels = in_channels
|
375 |
+
self.out_channels = out_channels
|
376 |
+
self.filter_channels = filter_channels
|
377 |
+
self.kernel_size = kernel_size
|
378 |
+
self.p_dropout = p_dropout
|
379 |
+
self.activation = activation
|
380 |
+
self.causal = causal
|
381 |
+
|
382 |
+
if causal:
|
383 |
+
self.padding = self._causal_padding
|
384 |
+
else:
|
385 |
+
self.padding = self._same_padding
|
386 |
+
|
387 |
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
388 |
+
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
389 |
+
self.drop = nn.Dropout(p_dropout)
|
390 |
+
|
391 |
+
def forward(self, x, x_mask):
|
392 |
+
x = self.conv_1(self.padding(x * x_mask))
|
393 |
+
if self.activation == "gelu":
|
394 |
+
x = x * torch.sigmoid(1.702 * x)
|
395 |
+
else:
|
396 |
+
x = torch.relu(x)
|
397 |
+
x = self.drop(x)
|
398 |
+
x = self.conv_2(self.padding(x * x_mask))
|
399 |
+
return x * x_mask
|
400 |
+
|
401 |
+
def _causal_padding(self, x):
|
402 |
+
if self.kernel_size == 1:
|
403 |
+
return x
|
404 |
+
pad_l = self.kernel_size - 1
|
405 |
+
pad_r = 0
|
406 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
407 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
408 |
+
return x
|
409 |
+
|
410 |
+
def _same_padding(self, x):
|
411 |
+
if self.kernel_size == 1:
|
412 |
+
return x
|
413 |
+
pad_l = (self.kernel_size - 1) // 2
|
414 |
+
pad_r = self.kernel_size // 2
|
415 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
416 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
417 |
+
return x
|
infer_pack/commons.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
|
7 |
+
|
8 |
+
def init_weights(m, mean=0.0, std=0.01):
|
9 |
+
classname = m.__class__.__name__
|
10 |
+
if classname.find("Conv") != -1:
|
11 |
+
m.weight.data.normal_(mean, std)
|
12 |
+
|
13 |
+
|
14 |
+
def get_padding(kernel_size, dilation=1):
|
15 |
+
return int((kernel_size * dilation - dilation) / 2)
|
16 |
+
|
17 |
+
|
18 |
+
def convert_pad_shape(pad_shape):
|
19 |
+
l = pad_shape[::-1]
|
20 |
+
pad_shape = [item for sublist in l for item in sublist]
|
21 |
+
return pad_shape
|
22 |
+
|
23 |
+
|
24 |
+
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
25 |
+
"""KL(P||Q)"""
|
26 |
+
kl = (logs_q - logs_p) - 0.5
|
27 |
+
kl += (
|
28 |
+
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
|
29 |
+
)
|
30 |
+
return kl
|
31 |
+
|
32 |
+
|
33 |
+
def rand_gumbel(shape):
|
34 |
+
"""Sample from the Gumbel distribution, protect from overflows."""
|
35 |
+
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
36 |
+
return -torch.log(-torch.log(uniform_samples))
|
37 |
+
|
38 |
+
|
39 |
+
def rand_gumbel_like(x):
|
40 |
+
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
41 |
+
return g
|
42 |
+
|
43 |
+
|
44 |
+
def slice_segments(x, ids_str, segment_size=4):
|
45 |
+
ret = torch.zeros_like(x[:, :, :segment_size])
|
46 |
+
for i in range(x.size(0)):
|
47 |
+
idx_str = ids_str[i]
|
48 |
+
idx_end = idx_str + segment_size
|
49 |
+
ret[i] = x[i, :, idx_str:idx_end]
|
50 |
+
return ret
|
51 |
+
def slice_segments2(x, ids_str, segment_size=4):
|
52 |
+
ret = torch.zeros_like(x[:, :segment_size])
|
53 |
+
for i in range(x.size(0)):
|
54 |
+
idx_str = ids_str[i]
|
55 |
+
idx_end = idx_str + segment_size
|
56 |
+
ret[i] = x[i, idx_str:idx_end]
|
57 |
+
return ret
|
58 |
+
|
59 |
+
|
60 |
+
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
61 |
+
b, d, t = x.size()
|
62 |
+
if x_lengths is None:
|
63 |
+
x_lengths = t
|
64 |
+
ids_str_max = x_lengths - segment_size + 1
|
65 |
+
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
66 |
+
ret = slice_segments(x, ids_str, segment_size)
|
67 |
+
return ret, ids_str
|
68 |
+
|
69 |
+
|
70 |
+
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
71 |
+
position = torch.arange(length, dtype=torch.float)
|
72 |
+
num_timescales = channels // 2
|
73 |
+
log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
|
74 |
+
num_timescales - 1
|
75 |
+
)
|
76 |
+
inv_timescales = min_timescale * torch.exp(
|
77 |
+
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
|
78 |
+
)
|
79 |
+
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
80 |
+
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
81 |
+
signal = F.pad(signal, [0, 0, 0, channels % 2])
|
82 |
+
signal = signal.view(1, channels, length)
|
83 |
+
return signal
|
84 |
+
|
85 |
+
|
86 |
+
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
87 |
+
b, channels, length = x.size()
|
88 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
89 |
+
return x + signal.to(dtype=x.dtype, device=x.device)
|
90 |
+
|
91 |
+
|
92 |
+
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
93 |
+
b, channels, length = x.size()
|
94 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
95 |
+
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
96 |
+
|
97 |
+
|
98 |
+
def subsequent_mask(length):
|
99 |
+
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
100 |
+
return mask
|
101 |
+
|
102 |
+
|
103 |
+
@torch.jit.script
|
104 |
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
105 |
+
n_channels_int = n_channels[0]
|
106 |
+
in_act = input_a + input_b
|
107 |
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
108 |
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
109 |
+
acts = t_act * s_act
|
110 |
+
return acts
|
111 |
+
|
112 |
+
|
113 |
+
def convert_pad_shape(pad_shape):
|
114 |
+
l = pad_shape[::-1]
|
115 |
+
pad_shape = [item for sublist in l for item in sublist]
|
116 |
+
return pad_shape
|
117 |
+
|
118 |
+
|
119 |
+
def shift_1d(x):
|
120 |
+
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
121 |
+
return x
|
122 |
+
|
123 |
+
|
124 |
+
def sequence_mask(length, max_length=None):
|
125 |
+
if max_length is None:
|
126 |
+
max_length = length.max()
|
127 |
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
128 |
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
129 |
+
|
130 |
+
|
131 |
+
def generate_path(duration, mask):
|
132 |
+
"""
|
133 |
+
duration: [b, 1, t_x]
|
134 |
+
mask: [b, 1, t_y, t_x]
|
135 |
+
"""
|
136 |
+
device = duration.device
|
137 |
+
|
138 |
+
b, _, t_y, t_x = mask.shape
|
139 |
+
cum_duration = torch.cumsum(duration, -1)
|
140 |
+
|
141 |
+
cum_duration_flat = cum_duration.view(b * t_x)
|
142 |
+
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
143 |
+
path = path.view(b, t_x, t_y)
|
144 |
+
path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
145 |
+
path = path.unsqueeze(1).transpose(2, 3) * mask
|
146 |
+
return path
|
147 |
+
|
148 |
+
|
149 |
+
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
150 |
+
if isinstance(parameters, torch.Tensor):
|
151 |
+
parameters = [parameters]
|
152 |
+
parameters = list(filter(lambda p: p.grad is not None, parameters))
|
153 |
+
norm_type = float(norm_type)
|
154 |
+
if clip_value is not None:
|
155 |
+
clip_value = float(clip_value)
|
156 |
+
|
157 |
+
total_norm = 0
|
158 |
+
for p in parameters:
|
159 |
+
param_norm = p.grad.data.norm(norm_type)
|
160 |
+
total_norm += param_norm.item() ** norm_type
|
161 |
+
if clip_value is not None:
|
162 |
+
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
163 |
+
total_norm = total_norm ** (1.0 / norm_type)
|
164 |
+
return total_norm
|
infer_pack/models.py
ADDED
@@ -0,0 +1,664 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math,pdb,os
|
2 |
+
from time import time as ttime
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
from infer_pack import modules
|
7 |
+
from infer_pack import attentions
|
8 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
9 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
10 |
+
from infer_pack.commons import init_weights
|
11 |
+
import numpy as np
|
12 |
+
from infer_pack import commons
|
13 |
+
class TextEncoder256(nn.Module):
|
14 |
+
def __init__(
|
15 |
+
self, out_channels, hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout, f0=True ):
|
16 |
+
super().__init__()
|
17 |
+
self.out_channels = out_channels
|
18 |
+
self.hidden_channels = hidden_channels
|
19 |
+
self.filter_channels = filter_channels
|
20 |
+
self.n_heads = n_heads
|
21 |
+
self.n_layers = n_layers
|
22 |
+
self.kernel_size = kernel_size
|
23 |
+
self.p_dropout = p_dropout
|
24 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
25 |
+
self.lrelu=nn.LeakyReLU(0.1,inplace=True)
|
26 |
+
if(f0==True):
|
27 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
28 |
+
self.encoder = attentions.Encoder(
|
29 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
30 |
+
)
|
31 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
32 |
+
|
33 |
+
def forward(self, phone, pitch, lengths):
|
34 |
+
if(pitch==None):
|
35 |
+
x = self.emb_phone(phone)
|
36 |
+
else:
|
37 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
38 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
39 |
+
x=self.lrelu(x)
|
40 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
41 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
42 |
+
x.dtype
|
43 |
+
)
|
44 |
+
x = self.encoder(x * x_mask, x_mask)
|
45 |
+
stats = self.proj(x) * x_mask
|
46 |
+
|
47 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
48 |
+
return m, logs, x_mask
|
49 |
+
class TextEncoder256km(nn.Module):
|
50 |
+
def __init__(
|
51 |
+
self, out_channels, hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout, f0=True ):
|
52 |
+
super().__init__()
|
53 |
+
self.out_channels = out_channels
|
54 |
+
self.hidden_channels = hidden_channels
|
55 |
+
self.filter_channels = filter_channels
|
56 |
+
self.n_heads = n_heads
|
57 |
+
self.n_layers = n_layers
|
58 |
+
self.kernel_size = kernel_size
|
59 |
+
self.p_dropout = p_dropout
|
60 |
+
# self.emb_phone = nn.Linear(256, hidden_channels)
|
61 |
+
self.emb_phone = nn.Embedding(500, hidden_channels)
|
62 |
+
self.lrelu=nn.LeakyReLU(0.1,inplace=True)
|
63 |
+
if(f0==True):
|
64 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
65 |
+
self.encoder = attentions.Encoder(
|
66 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
67 |
+
)
|
68 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
69 |
+
|
70 |
+
def forward(self, phone, pitch, lengths):
|
71 |
+
if(pitch==None):
|
72 |
+
x = self.emb_phone(phone)
|
73 |
+
else:
|
74 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
75 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
76 |
+
x=self.lrelu(x)
|
77 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
78 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
79 |
+
x.dtype
|
80 |
+
)
|
81 |
+
x = self.encoder(x * x_mask, x_mask)
|
82 |
+
stats = self.proj(x) * x_mask
|
83 |
+
|
84 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
85 |
+
return m, logs, x_mask
|
86 |
+
class ResidualCouplingBlock(nn.Module):
|
87 |
+
def __init__(
|
88 |
+
self,
|
89 |
+
channels,
|
90 |
+
hidden_channels,
|
91 |
+
kernel_size,
|
92 |
+
dilation_rate,
|
93 |
+
n_layers,
|
94 |
+
n_flows=4,
|
95 |
+
gin_channels=0,
|
96 |
+
):
|
97 |
+
super().__init__()
|
98 |
+
self.channels = channels
|
99 |
+
self.hidden_channels = hidden_channels
|
100 |
+
self.kernel_size = kernel_size
|
101 |
+
self.dilation_rate = dilation_rate
|
102 |
+
self.n_layers = n_layers
|
103 |
+
self.n_flows = n_flows
|
104 |
+
self.gin_channels = gin_channels
|
105 |
+
|
106 |
+
self.flows = nn.ModuleList()
|
107 |
+
for i in range(n_flows):
|
108 |
+
self.flows.append(
|
109 |
+
modules.ResidualCouplingLayer(
|
110 |
+
channels,
|
111 |
+
hidden_channels,
|
112 |
+
kernel_size,
|
113 |
+
dilation_rate,
|
114 |
+
n_layers,
|
115 |
+
gin_channels=gin_channels,
|
116 |
+
mean_only=True,
|
117 |
+
)
|
118 |
+
)
|
119 |
+
self.flows.append(modules.Flip())
|
120 |
+
|
121 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
122 |
+
if not reverse:
|
123 |
+
for flow in self.flows:
|
124 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
125 |
+
else:
|
126 |
+
for flow in reversed(self.flows):
|
127 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
128 |
+
return x
|
129 |
+
|
130 |
+
def remove_weight_norm(self):
|
131 |
+
for i in range(self.n_flows):
|
132 |
+
self.flows[i * 2].remove_weight_norm()
|
133 |
+
class PosteriorEncoder(nn.Module):
|
134 |
+
def __init__(
|
135 |
+
self,
|
136 |
+
in_channels,
|
137 |
+
out_channels,
|
138 |
+
hidden_channels,
|
139 |
+
kernel_size,
|
140 |
+
dilation_rate,
|
141 |
+
n_layers,
|
142 |
+
gin_channels=0,
|
143 |
+
):
|
144 |
+
super().__init__()
|
145 |
+
self.in_channels = in_channels
|
146 |
+
self.out_channels = out_channels
|
147 |
+
self.hidden_channels = hidden_channels
|
148 |
+
self.kernel_size = kernel_size
|
149 |
+
self.dilation_rate = dilation_rate
|
150 |
+
self.n_layers = n_layers
|
151 |
+
self.gin_channels = gin_channels
|
152 |
+
|
153 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
154 |
+
self.enc = modules.WN(
|
155 |
+
hidden_channels,
|
156 |
+
kernel_size,
|
157 |
+
dilation_rate,
|
158 |
+
n_layers,
|
159 |
+
gin_channels=gin_channels,
|
160 |
+
)
|
161 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
162 |
+
|
163 |
+
def forward(self, x, x_lengths, g=None):
|
164 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
165 |
+
x.dtype
|
166 |
+
)
|
167 |
+
x = self.pre(x) * x_mask
|
168 |
+
x = self.enc(x, x_mask, g=g)
|
169 |
+
stats = self.proj(x) * x_mask
|
170 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
171 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
172 |
+
return z, m, logs, x_mask
|
173 |
+
|
174 |
+
def remove_weight_norm(self):
|
175 |
+
self.enc.remove_weight_norm()
|
176 |
+
class Generator(torch.nn.Module):
|
177 |
+
def __init__(
|
178 |
+
self,
|
179 |
+
initial_channel,
|
180 |
+
resblock,
|
181 |
+
resblock_kernel_sizes,
|
182 |
+
resblock_dilation_sizes,
|
183 |
+
upsample_rates,
|
184 |
+
upsample_initial_channel,
|
185 |
+
upsample_kernel_sizes,
|
186 |
+
gin_channels=0,
|
187 |
+
):
|
188 |
+
super(Generator, self).__init__()
|
189 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
190 |
+
self.num_upsamples = len(upsample_rates)
|
191 |
+
self.conv_pre = Conv1d(
|
192 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
193 |
+
)
|
194 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
195 |
+
|
196 |
+
self.ups = nn.ModuleList()
|
197 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
198 |
+
self.ups.append(
|
199 |
+
weight_norm(
|
200 |
+
ConvTranspose1d(
|
201 |
+
upsample_initial_channel // (2**i),
|
202 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
203 |
+
k,
|
204 |
+
u,
|
205 |
+
padding=(k - u) // 2,
|
206 |
+
)
|
207 |
+
)
|
208 |
+
)
|
209 |
+
|
210 |
+
self.resblocks = nn.ModuleList()
|
211 |
+
for i in range(len(self.ups)):
|
212 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
213 |
+
for j, (k, d) in enumerate(
|
214 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
215 |
+
):
|
216 |
+
self.resblocks.append(resblock(ch, k, d))
|
217 |
+
|
218 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
219 |
+
self.ups.apply(init_weights)
|
220 |
+
|
221 |
+
if gin_channels != 0:
|
222 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
223 |
+
|
224 |
+
def forward(self, x, g=None):
|
225 |
+
x = self.conv_pre(x)
|
226 |
+
if g is not None:
|
227 |
+
x = x + self.cond(g)
|
228 |
+
|
229 |
+
for i in range(self.num_upsamples):
|
230 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
231 |
+
x = self.ups[i](x)
|
232 |
+
xs = None
|
233 |
+
for j in range(self.num_kernels):
|
234 |
+
if xs is None:
|
235 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
236 |
+
else:
|
237 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
238 |
+
x = xs / self.num_kernels
|
239 |
+
x = F.leaky_relu(x)
|
240 |
+
x = self.conv_post(x)
|
241 |
+
x = torch.tanh(x)
|
242 |
+
|
243 |
+
return x
|
244 |
+
|
245 |
+
def remove_weight_norm(self):
|
246 |
+
for l in self.ups:
|
247 |
+
remove_weight_norm(l)
|
248 |
+
for l in self.resblocks:
|
249 |
+
l.remove_weight_norm()
|
250 |
+
class SineGen(torch.nn.Module):
|
251 |
+
""" Definition of sine generator
|
252 |
+
SineGen(samp_rate, harmonic_num = 0,
|
253 |
+
sine_amp = 0.1, noise_std = 0.003,
|
254 |
+
voiced_threshold = 0,
|
255 |
+
flag_for_pulse=False)
|
256 |
+
samp_rate: sampling rate in Hz
|
257 |
+
harmonic_num: number of harmonic overtones (default 0)
|
258 |
+
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
259 |
+
noise_std: std of Gaussian noise (default 0.003)
|
260 |
+
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
261 |
+
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
262 |
+
Note: when flag_for_pulse is True, the first time step of a voiced
|
263 |
+
segment is always sin(np.pi) or cos(0)
|
264 |
+
"""
|
265 |
+
|
266 |
+
def __init__(self, samp_rate, harmonic_num=0,
|
267 |
+
sine_amp=0.1, noise_std=0.003,
|
268 |
+
voiced_threshold=0,
|
269 |
+
flag_for_pulse=False):
|
270 |
+
super(SineGen, self).__init__()
|
271 |
+
self.sine_amp = sine_amp
|
272 |
+
self.noise_std = noise_std
|
273 |
+
self.harmonic_num = harmonic_num
|
274 |
+
self.dim = self.harmonic_num + 1
|
275 |
+
self.sampling_rate = samp_rate
|
276 |
+
self.voiced_threshold = voiced_threshold
|
277 |
+
|
278 |
+
def _f02uv(self, f0):
|
279 |
+
# generate uv signal
|
280 |
+
uv = torch.ones_like(f0)
|
281 |
+
uv = uv * (f0 > self.voiced_threshold)
|
282 |
+
return uv
|
283 |
+
|
284 |
+
def forward(self, f0,upp):
|
285 |
+
""" sine_tensor, uv = forward(f0)
|
286 |
+
input F0: tensor(batchsize=1, length, dim=1)
|
287 |
+
f0 for unvoiced steps should be 0
|
288 |
+
output sine_tensor: tensor(batchsize=1, length, dim)
|
289 |
+
output uv: tensor(batchsize=1, length, 1)
|
290 |
+
"""
|
291 |
+
with torch.no_grad():
|
292 |
+
f0 = f0[:, None].transpose(1, 2)
|
293 |
+
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,device=f0.device)
|
294 |
+
# fundamental component
|
295 |
+
f0_buf[:, :, 0] = f0[:, :, 0]
|
296 |
+
for idx in np.arange(self.harmonic_num):f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (idx + 2)# idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
|
297 |
+
rad_values = (f0_buf / self.sampling_rate) % 1###%1意味着n_har的乘积无法后处理优化
|
298 |
+
rand_ini = torch.rand(f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device)
|
299 |
+
rand_ini[:, 0] = 0
|
300 |
+
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
301 |
+
tmp_over_one = torch.cumsum(rad_values, 1)# % 1 #####%1意味着后面的cumsum无法再优化
|
302 |
+
tmp_over_one*=upp
|
303 |
+
tmp_over_one=F.interpolate(tmp_over_one.transpose(2, 1), scale_factor=upp, mode='linear', align_corners=True).transpose(2, 1)
|
304 |
+
rad_values=F.interpolate(rad_values.transpose(2, 1), scale_factor=upp, mode='nearest').transpose(2, 1)#######
|
305 |
+
tmp_over_one%=1
|
306 |
+
tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0
|
307 |
+
cumsum_shift = torch.zeros_like(rad_values)
|
308 |
+
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
309 |
+
sine_waves = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * np.pi)
|
310 |
+
sine_waves = sine_waves * self.sine_amp
|
311 |
+
uv = self._f02uv(f0)
|
312 |
+
uv = F.interpolate(uv.transpose(2, 1), scale_factor=upp, mode='nearest').transpose(2, 1)
|
313 |
+
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
314 |
+
noise = noise_amp * torch.randn_like(sine_waves)
|
315 |
+
sine_waves = sine_waves * uv + noise
|
316 |
+
return sine_waves, uv, noise
|
317 |
+
class SourceModuleHnNSF(torch.nn.Module):
|
318 |
+
""" SourceModule for hn-nsf
|
319 |
+
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
320 |
+
add_noise_std=0.003, voiced_threshod=0)
|
321 |
+
sampling_rate: sampling_rate in Hz
|
322 |
+
harmonic_num: number of harmonic above F0 (default: 0)
|
323 |
+
sine_amp: amplitude of sine source signal (default: 0.1)
|
324 |
+
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
325 |
+
note that amplitude of noise in unvoiced is decided
|
326 |
+
by sine_amp
|
327 |
+
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
328 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
329 |
+
F0_sampled (batchsize, length, 1)
|
330 |
+
Sine_source (batchsize, length, 1)
|
331 |
+
noise_source (batchsize, length 1)
|
332 |
+
uv (batchsize, length, 1)
|
333 |
+
"""
|
334 |
+
|
335 |
+
def __init__(self, sampling_rate, harmonic_num=0, sine_amp=0.1,
|
336 |
+
add_noise_std=0.003, voiced_threshod=0,is_half=True):
|
337 |
+
super(SourceModuleHnNSF, self).__init__()
|
338 |
+
|
339 |
+
self.sine_amp = sine_amp
|
340 |
+
self.noise_std = add_noise_std
|
341 |
+
self.is_half=is_half
|
342 |
+
# to produce sine waveforms
|
343 |
+
self.l_sin_gen = SineGen(sampling_rate, harmonic_num,
|
344 |
+
sine_amp, add_noise_std, voiced_threshod)
|
345 |
+
|
346 |
+
# to merge source harmonics into a single excitation
|
347 |
+
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
348 |
+
self.l_tanh = torch.nn.Tanh()
|
349 |
+
|
350 |
+
def forward(self, x,upp=None):
|
351 |
+
sine_wavs, uv, _ = self.l_sin_gen(x,upp)
|
352 |
+
if(self.is_half==True):sine_wavs=sine_wavs.half()
|
353 |
+
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
354 |
+
return sine_merge,None,None# noise, uv
|
355 |
+
class GeneratorNSF(torch.nn.Module):
|
356 |
+
def __init__(
|
357 |
+
self,
|
358 |
+
initial_channel,
|
359 |
+
resblock,
|
360 |
+
resblock_kernel_sizes,
|
361 |
+
resblock_dilation_sizes,
|
362 |
+
upsample_rates,
|
363 |
+
upsample_initial_channel,
|
364 |
+
upsample_kernel_sizes,
|
365 |
+
gin_channels=0,
|
366 |
+
sr=40000,
|
367 |
+
is_half=False
|
368 |
+
):
|
369 |
+
super(GeneratorNSF, self).__init__()
|
370 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
371 |
+
self.num_upsamples = len(upsample_rates)
|
372 |
+
|
373 |
+
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(upsample_rates))
|
374 |
+
self.m_source = SourceModuleHnNSF(
|
375 |
+
sampling_rate=sr,
|
376 |
+
harmonic_num=0,
|
377 |
+
is_half=is_half
|
378 |
+
)
|
379 |
+
self.noise_convs = nn.ModuleList()
|
380 |
+
self.conv_pre = Conv1d(
|
381 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
382 |
+
)
|
383 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
384 |
+
|
385 |
+
self.ups = nn.ModuleList()
|
386 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
387 |
+
c_cur = upsample_initial_channel // (2 ** (i + 1))
|
388 |
+
self.ups.append(
|
389 |
+
weight_norm(
|
390 |
+
ConvTranspose1d(
|
391 |
+
upsample_initial_channel // (2**i),
|
392 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
393 |
+
k,
|
394 |
+
u,
|
395 |
+
padding=(k - u) // 2,
|
396 |
+
)
|
397 |
+
)
|
398 |
+
)
|
399 |
+
if i + 1 < len(upsample_rates):
|
400 |
+
stride_f0 = np.prod(upsample_rates[i + 1:])
|
401 |
+
self.noise_convs.append(Conv1d(
|
402 |
+
1, c_cur, kernel_size=stride_f0 * 2, stride=stride_f0, padding=stride_f0 // 2))
|
403 |
+
else:
|
404 |
+
self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
|
405 |
+
|
406 |
+
self.resblocks = nn.ModuleList()
|
407 |
+
for i in range(len(self.ups)):
|
408 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
409 |
+
for j, (k, d) in enumerate(
|
410 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
411 |
+
):
|
412 |
+
self.resblocks.append(resblock(ch, k, d))
|
413 |
+
|
414 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
415 |
+
self.ups.apply(init_weights)
|
416 |
+
|
417 |
+
if gin_channels != 0:
|
418 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
419 |
+
|
420 |
+
self.upp=np.prod(upsample_rates)
|
421 |
+
|
422 |
+
def forward(self, x, f0,g=None):
|
423 |
+
har_source, noi_source, uv = self.m_source(f0,self.upp)
|
424 |
+
har_source = har_source.transpose(1, 2)
|
425 |
+
x = self.conv_pre(x)
|
426 |
+
if g is not None:
|
427 |
+
x = x + self.cond(g)
|
428 |
+
|
429 |
+
for i in range(self.num_upsamples):
|
430 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
431 |
+
x = self.ups[i](x)
|
432 |
+
x_source = self.noise_convs[i](har_source)
|
433 |
+
x = x + x_source
|
434 |
+
xs = None
|
435 |
+
for j in range(self.num_kernels):
|
436 |
+
if xs is None:
|
437 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
438 |
+
else:
|
439 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
440 |
+
x = xs / self.num_kernels
|
441 |
+
x = F.leaky_relu(x)
|
442 |
+
x = self.conv_post(x)
|
443 |
+
x = torch.tanh(x)
|
444 |
+
return x
|
445 |
+
|
446 |
+
def remove_weight_norm(self):
|
447 |
+
for l in self.ups:
|
448 |
+
remove_weight_norm(l)
|
449 |
+
for l in self.resblocks:
|
450 |
+
l.remove_weight_norm()
|
451 |
+
class SynthesizerTrnMs256NSF(nn.Module):
|
452 |
+
"""
|
453 |
+
Synthesizer for Training
|
454 |
+
"""
|
455 |
+
|
456 |
+
def __init__(
|
457 |
+
self,
|
458 |
+
spec_channels,
|
459 |
+
segment_size,
|
460 |
+
inter_channels,
|
461 |
+
hidden_channels,
|
462 |
+
filter_channels,
|
463 |
+
n_heads,
|
464 |
+
n_layers,
|
465 |
+
kernel_size,
|
466 |
+
p_dropout,
|
467 |
+
resblock,
|
468 |
+
resblock_kernel_sizes,
|
469 |
+
resblock_dilation_sizes,
|
470 |
+
upsample_rates,
|
471 |
+
upsample_initial_channel,
|
472 |
+
upsample_kernel_sizes,
|
473 |
+
spk_embed_dim,
|
474 |
+
gin_channels=0,
|
475 |
+
sr=40000,
|
476 |
+
**kwargs
|
477 |
+
):
|
478 |
+
|
479 |
+
super().__init__()
|
480 |
+
self.spec_channels = spec_channels
|
481 |
+
self.inter_channels = inter_channels
|
482 |
+
self.hidden_channels = hidden_channels
|
483 |
+
self.filter_channels = filter_channels
|
484 |
+
self.n_heads = n_heads
|
485 |
+
self.n_layers = n_layers
|
486 |
+
self.kernel_size = kernel_size
|
487 |
+
self.p_dropout = p_dropout
|
488 |
+
self.resblock = resblock
|
489 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
490 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
491 |
+
self.upsample_rates = upsample_rates
|
492 |
+
self.upsample_initial_channel = upsample_initial_channel
|
493 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
494 |
+
self.segment_size = segment_size
|
495 |
+
self.gin_channels = gin_channels
|
496 |
+
self.spk_embed_dim=spk_embed_dim
|
497 |
+
self.enc_p = TextEncoder256(
|
498 |
+
inter_channels,
|
499 |
+
hidden_channels,
|
500 |
+
filter_channels,
|
501 |
+
n_heads,
|
502 |
+
n_layers,
|
503 |
+
kernel_size,
|
504 |
+
p_dropout,
|
505 |
+
)
|
506 |
+
self.dec = GeneratorNSF(
|
507 |
+
inter_channels,
|
508 |
+
resblock,
|
509 |
+
resblock_kernel_sizes,
|
510 |
+
resblock_dilation_sizes,
|
511 |
+
upsample_rates,
|
512 |
+
upsample_initial_channel,
|
513 |
+
upsample_kernel_sizes,
|
514 |
+
gin_channels=0,
|
515 |
+
sr=sr,
|
516 |
+
is_half=kwargs["is_half"]
|
517 |
+
)
|
518 |
+
self.enc_q = PosteriorEncoder(
|
519 |
+
spec_channels,
|
520 |
+
inter_channels,
|
521 |
+
hidden_channels,
|
522 |
+
5,
|
523 |
+
1,
|
524 |
+
16,
|
525 |
+
gin_channels=gin_channels,
|
526 |
+
)
|
527 |
+
self.flow = ResidualCouplingBlock(
|
528 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
529 |
+
)
|
530 |
+
self.emb_g = nn.Linear(self.spk_embed_dim, gin_channels)
|
531 |
+
|
532 |
+
def remove_weight_norm(self):
|
533 |
+
self.dec.remove_weight_norm()
|
534 |
+
self.flow.remove_weight_norm()
|
535 |
+
self.enc_q.remove_weight_norm()
|
536 |
+
|
537 |
+
def infer(self, phone, phone_lengths, pitch,pitchf, ds,max_len=None):
|
538 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
539 |
+
if("float16"in str(m_p.dtype)):ds=ds.half()
|
540 |
+
ds=ds.to(m_p.device)
|
541 |
+
g = self.emb_g(ds).unsqueeze(-1) # [b, h, 1]#
|
542 |
+
z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66) * x_mask
|
543 |
+
|
544 |
+
z = self.flow(z_p, x_mask, g=g, reverse=True)
|
545 |
+
o = self.dec((z * x_mask)[:, :, :max_len],pitchf, g=None)
|
546 |
+
return o, x_mask, (z, z_p, m_p, logs_p)
|
547 |
+
class SynthesizerTrn256NSFkm(nn.Module):
|
548 |
+
"""
|
549 |
+
Synthesizer for Training
|
550 |
+
"""
|
551 |
+
|
552 |
+
def __init__(
|
553 |
+
self,
|
554 |
+
spec_channels,
|
555 |
+
segment_size,
|
556 |
+
inter_channels,
|
557 |
+
hidden_channels,
|
558 |
+
filter_channels,
|
559 |
+
n_heads,
|
560 |
+
n_layers,
|
561 |
+
kernel_size,
|
562 |
+
p_dropout,
|
563 |
+
resblock,
|
564 |
+
resblock_kernel_sizes,
|
565 |
+
resblock_dilation_sizes,
|
566 |
+
upsample_rates,
|
567 |
+
upsample_initial_channel,
|
568 |
+
upsample_kernel_sizes,
|
569 |
+
spk_embed_dim,
|
570 |
+
gin_channels=0,
|
571 |
+
sr=40000,
|
572 |
+
**kwargs
|
573 |
+
):
|
574 |
+
|
575 |
+
super().__init__()
|
576 |
+
self.spec_channels = spec_channels
|
577 |
+
self.inter_channels = inter_channels
|
578 |
+
self.hidden_channels = hidden_channels
|
579 |
+
self.filter_channels = filter_channels
|
580 |
+
self.n_heads = n_heads
|
581 |
+
self.n_layers = n_layers
|
582 |
+
self.kernel_size = kernel_size
|
583 |
+
self.p_dropout = p_dropout
|
584 |
+
self.resblock = resblock
|
585 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
586 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
587 |
+
self.upsample_rates = upsample_rates
|
588 |
+
self.upsample_initial_channel = upsample_initial_channel
|
589 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
590 |
+
self.segment_size = segment_size
|
591 |
+
self.gin_channels = gin_channels
|
592 |
+
|
593 |
+
self.enc_p = TextEncoder256km(
|
594 |
+
inter_channels,
|
595 |
+
hidden_channels,
|
596 |
+
filter_channels,
|
597 |
+
n_heads,
|
598 |
+
n_layers,
|
599 |
+
kernel_size,
|
600 |
+
p_dropout,
|
601 |
+
)
|
602 |
+
self.dec = GeneratorNSF(
|
603 |
+
inter_channels,
|
604 |
+
resblock,
|
605 |
+
resblock_kernel_sizes,
|
606 |
+
resblock_dilation_sizes,
|
607 |
+
upsample_rates,
|
608 |
+
upsample_initial_channel,
|
609 |
+
upsample_kernel_sizes,
|
610 |
+
gin_channels=0,
|
611 |
+
sr=sr,
|
612 |
+
is_half=kwargs["is_half"]
|
613 |
+
)
|
614 |
+
self.enc_q = PosteriorEncoder(
|
615 |
+
spec_channels,
|
616 |
+
inter_channels,
|
617 |
+
hidden_channels,
|
618 |
+
5,
|
619 |
+
1,
|
620 |
+
16,
|
621 |
+
gin_channels=gin_channels,
|
622 |
+
)
|
623 |
+
self.flow = ResidualCouplingBlock(
|
624 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
625 |
+
)
|
626 |
+
|
627 |
+
def remove_weight_norm(self):
|
628 |
+
self.dec.remove_weight_norm()
|
629 |
+
self.flow.remove_weight_norm()
|
630 |
+
self.enc_q.remove_weight_norm()
|
631 |
+
|
632 |
+
def forward(self, phone, phone_lengths, pitch, pitchf, y, y_lengths):
|
633 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
634 |
+
|
635 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=None)
|
636 |
+
z_p = self.flow(z, y_mask, g=None)
|
637 |
+
|
638 |
+
z_slice, ids_slice = commons.rand_slice_segments(
|
639 |
+
z, y_lengths, self.segment_size
|
640 |
+
)
|
641 |
+
|
642 |
+
pitchf = commons.slice_segments2(
|
643 |
+
pitchf, ids_slice, self.segment_size
|
644 |
+
)
|
645 |
+
o = self.dec(z_slice, pitchf,g=None)
|
646 |
+
return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
|
647 |
+
|
648 |
+
def infer(self, phone, phone_lengths, pitch, nsff0,max_len=None):
|
649 |
+
# torch.cuda.synchronize()
|
650 |
+
# t0=ttime()
|
651 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
652 |
+
# torch.cuda.synchronize()
|
653 |
+
# t1=ttime()
|
654 |
+
z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66) * x_mask
|
655 |
+
# torch.cuda.synchronize()
|
656 |
+
# t2=ttime()
|
657 |
+
z = self.flow(z_p, x_mask, g=None, reverse=True)
|
658 |
+
# torch.cuda.synchronize()
|
659 |
+
# t3=ttime()
|
660 |
+
o = self.dec((z * x_mask)[:, :, :max_len], nsff0,g=None)
|
661 |
+
# torch.cuda.synchronize()
|
662 |
+
# t4=ttime()
|
663 |
+
# print(1233333333333333333333333,t1-t0,t2-t1,t3-t2,t4-t3)
|
664 |
+
return o, x_mask, (z, z_p, m_p, logs_p)
|
infer_pack/modules.py
ADDED
@@ -0,0 +1,522 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import math
|
3 |
+
import numpy as np
|
4 |
+
import scipy
|
5 |
+
import torch
|
6 |
+
from torch import nn
|
7 |
+
from torch.nn import functional as F
|
8 |
+
|
9 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
10 |
+
from torch.nn.utils import weight_norm, remove_weight_norm
|
11 |
+
|
12 |
+
from infer_pack import commons
|
13 |
+
from infer_pack.commons import init_weights, get_padding
|
14 |
+
from infer_pack.transforms import piecewise_rational_quadratic_transform
|
15 |
+
|
16 |
+
|
17 |
+
LRELU_SLOPE = 0.1
|
18 |
+
|
19 |
+
|
20 |
+
class LayerNorm(nn.Module):
|
21 |
+
def __init__(self, channels, eps=1e-5):
|
22 |
+
super().__init__()
|
23 |
+
self.channels = channels
|
24 |
+
self.eps = eps
|
25 |
+
|
26 |
+
self.gamma = nn.Parameter(torch.ones(channels))
|
27 |
+
self.beta = nn.Parameter(torch.zeros(channels))
|
28 |
+
|
29 |
+
def forward(self, x):
|
30 |
+
x = x.transpose(1, -1)
|
31 |
+
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
32 |
+
return x.transpose(1, -1)
|
33 |
+
|
34 |
+
|
35 |
+
class ConvReluNorm(nn.Module):
|
36 |
+
def __init__(
|
37 |
+
self,
|
38 |
+
in_channels,
|
39 |
+
hidden_channels,
|
40 |
+
out_channels,
|
41 |
+
kernel_size,
|
42 |
+
n_layers,
|
43 |
+
p_dropout,
|
44 |
+
):
|
45 |
+
super().__init__()
|
46 |
+
self.in_channels = in_channels
|
47 |
+
self.hidden_channels = hidden_channels
|
48 |
+
self.out_channels = out_channels
|
49 |
+
self.kernel_size = kernel_size
|
50 |
+
self.n_layers = n_layers
|
51 |
+
self.p_dropout = p_dropout
|
52 |
+
assert n_layers > 1, "Number of layers should be larger than 0."
|
53 |
+
|
54 |
+
self.conv_layers = nn.ModuleList()
|
55 |
+
self.norm_layers = nn.ModuleList()
|
56 |
+
self.conv_layers.append(
|
57 |
+
nn.Conv1d(
|
58 |
+
in_channels, hidden_channels, kernel_size, padding=kernel_size // 2
|
59 |
+
)
|
60 |
+
)
|
61 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
62 |
+
self.relu_drop = nn.Sequential(nn.ReLU(), nn.Dropout(p_dropout))
|
63 |
+
for _ in range(n_layers - 1):
|
64 |
+
self.conv_layers.append(
|
65 |
+
nn.Conv1d(
|
66 |
+
hidden_channels,
|
67 |
+
hidden_channels,
|
68 |
+
kernel_size,
|
69 |
+
padding=kernel_size // 2,
|
70 |
+
)
|
71 |
+
)
|
72 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
73 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
74 |
+
self.proj.weight.data.zero_()
|
75 |
+
self.proj.bias.data.zero_()
|
76 |
+
|
77 |
+
def forward(self, x, x_mask):
|
78 |
+
x_org = x
|
79 |
+
for i in range(self.n_layers):
|
80 |
+
x = self.conv_layers[i](x * x_mask)
|
81 |
+
x = self.norm_layers[i](x)
|
82 |
+
x = self.relu_drop(x)
|
83 |
+
x = x_org + self.proj(x)
|
84 |
+
return x * x_mask
|
85 |
+
|
86 |
+
|
87 |
+
class DDSConv(nn.Module):
|
88 |
+
"""
|
89 |
+
Dialted and Depth-Separable Convolution
|
90 |
+
"""
|
91 |
+
|
92 |
+
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0):
|
93 |
+
super().__init__()
|
94 |
+
self.channels = channels
|
95 |
+
self.kernel_size = kernel_size
|
96 |
+
self.n_layers = n_layers
|
97 |
+
self.p_dropout = p_dropout
|
98 |
+
|
99 |
+
self.drop = nn.Dropout(p_dropout)
|
100 |
+
self.convs_sep = nn.ModuleList()
|
101 |
+
self.convs_1x1 = nn.ModuleList()
|
102 |
+
self.norms_1 = nn.ModuleList()
|
103 |
+
self.norms_2 = nn.ModuleList()
|
104 |
+
for i in range(n_layers):
|
105 |
+
dilation = kernel_size**i
|
106 |
+
padding = (kernel_size * dilation - dilation) // 2
|
107 |
+
self.convs_sep.append(
|
108 |
+
nn.Conv1d(
|
109 |
+
channels,
|
110 |
+
channels,
|
111 |
+
kernel_size,
|
112 |
+
groups=channels,
|
113 |
+
dilation=dilation,
|
114 |
+
padding=padding,
|
115 |
+
)
|
116 |
+
)
|
117 |
+
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
118 |
+
self.norms_1.append(LayerNorm(channels))
|
119 |
+
self.norms_2.append(LayerNorm(channels))
|
120 |
+
|
121 |
+
def forward(self, x, x_mask, g=None):
|
122 |
+
if g is not None:
|
123 |
+
x = x + g
|
124 |
+
for i in range(self.n_layers):
|
125 |
+
y = self.convs_sep[i](x * x_mask)
|
126 |
+
y = self.norms_1[i](y)
|
127 |
+
y = F.gelu(y)
|
128 |
+
y = self.convs_1x1[i](y)
|
129 |
+
y = self.norms_2[i](y)
|
130 |
+
y = F.gelu(y)
|
131 |
+
y = self.drop(y)
|
132 |
+
x = x + y
|
133 |
+
return x * x_mask
|
134 |
+
|
135 |
+
|
136 |
+
class WN(torch.nn.Module):
|
137 |
+
def __init__(
|
138 |
+
self,
|
139 |
+
hidden_channels,
|
140 |
+
kernel_size,
|
141 |
+
dilation_rate,
|
142 |
+
n_layers,
|
143 |
+
gin_channels=0,
|
144 |
+
p_dropout=0,
|
145 |
+
):
|
146 |
+
super(WN, self).__init__()
|
147 |
+
assert kernel_size % 2 == 1
|
148 |
+
self.hidden_channels = hidden_channels
|
149 |
+
self.kernel_size = (kernel_size,)
|
150 |
+
self.dilation_rate = dilation_rate
|
151 |
+
self.n_layers = n_layers
|
152 |
+
self.gin_channels = gin_channels
|
153 |
+
self.p_dropout = p_dropout
|
154 |
+
|
155 |
+
self.in_layers = torch.nn.ModuleList()
|
156 |
+
self.res_skip_layers = torch.nn.ModuleList()
|
157 |
+
self.drop = nn.Dropout(p_dropout)
|
158 |
+
|
159 |
+
if gin_channels != 0:
|
160 |
+
cond_layer = torch.nn.Conv1d(
|
161 |
+
gin_channels, 2 * hidden_channels * n_layers, 1
|
162 |
+
)
|
163 |
+
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name="weight")
|
164 |
+
|
165 |
+
for i in range(n_layers):
|
166 |
+
dilation = dilation_rate**i
|
167 |
+
padding = int((kernel_size * dilation - dilation) / 2)
|
168 |
+
in_layer = torch.nn.Conv1d(
|
169 |
+
hidden_channels,
|
170 |
+
2 * hidden_channels,
|
171 |
+
kernel_size,
|
172 |
+
dilation=dilation,
|
173 |
+
padding=padding,
|
174 |
+
)
|
175 |
+
in_layer = torch.nn.utils.weight_norm(in_layer, name="weight")
|
176 |
+
self.in_layers.append(in_layer)
|
177 |
+
|
178 |
+
# last one is not necessary
|
179 |
+
if i < n_layers - 1:
|
180 |
+
res_skip_channels = 2 * hidden_channels
|
181 |
+
else:
|
182 |
+
res_skip_channels = hidden_channels
|
183 |
+
|
184 |
+
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
185 |
+
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name="weight")
|
186 |
+
self.res_skip_layers.append(res_skip_layer)
|
187 |
+
|
188 |
+
def forward(self, x, x_mask, g=None, **kwargs):
|
189 |
+
output = torch.zeros_like(x)
|
190 |
+
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
191 |
+
|
192 |
+
if g is not None:
|
193 |
+
g = self.cond_layer(g)
|
194 |
+
|
195 |
+
for i in range(self.n_layers):
|
196 |
+
x_in = self.in_layers[i](x)
|
197 |
+
if g is not None:
|
198 |
+
cond_offset = i * 2 * self.hidden_channels
|
199 |
+
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
200 |
+
else:
|
201 |
+
g_l = torch.zeros_like(x_in)
|
202 |
+
|
203 |
+
acts = commons.fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor)
|
204 |
+
acts = self.drop(acts)
|
205 |
+
|
206 |
+
res_skip_acts = self.res_skip_layers[i](acts)
|
207 |
+
if i < self.n_layers - 1:
|
208 |
+
res_acts = res_skip_acts[:, : self.hidden_channels, :]
|
209 |
+
x = (x + res_acts) * x_mask
|
210 |
+
output = output + res_skip_acts[:, self.hidden_channels :, :]
|
211 |
+
else:
|
212 |
+
output = output + res_skip_acts
|
213 |
+
return output * x_mask
|
214 |
+
|
215 |
+
def remove_weight_norm(self):
|
216 |
+
if self.gin_channels != 0:
|
217 |
+
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
218 |
+
for l in self.in_layers:
|
219 |
+
torch.nn.utils.remove_weight_norm(l)
|
220 |
+
for l in self.res_skip_layers:
|
221 |
+
torch.nn.utils.remove_weight_norm(l)
|
222 |
+
|
223 |
+
|
224 |
+
class ResBlock1(torch.nn.Module):
|
225 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
226 |
+
super(ResBlock1, self).__init__()
|
227 |
+
self.convs1 = nn.ModuleList(
|
228 |
+
[
|
229 |
+
weight_norm(
|
230 |
+
Conv1d(
|
231 |
+
channels,
|
232 |
+
channels,
|
233 |
+
kernel_size,
|
234 |
+
1,
|
235 |
+
dilation=dilation[0],
|
236 |
+
padding=get_padding(kernel_size, dilation[0]),
|
237 |
+
)
|
238 |
+
),
|
239 |
+
weight_norm(
|
240 |
+
Conv1d(
|
241 |
+
channels,
|
242 |
+
channels,
|
243 |
+
kernel_size,
|
244 |
+
1,
|
245 |
+
dilation=dilation[1],
|
246 |
+
padding=get_padding(kernel_size, dilation[1]),
|
247 |
+
)
|
248 |
+
),
|
249 |
+
weight_norm(
|
250 |
+
Conv1d(
|
251 |
+
channels,
|
252 |
+
channels,
|
253 |
+
kernel_size,
|
254 |
+
1,
|
255 |
+
dilation=dilation[2],
|
256 |
+
padding=get_padding(kernel_size, dilation[2]),
|
257 |
+
)
|
258 |
+
),
|
259 |
+
]
|
260 |
+
)
|
261 |
+
self.convs1.apply(init_weights)
|
262 |
+
|
263 |
+
self.convs2 = nn.ModuleList(
|
264 |
+
[
|
265 |
+
weight_norm(
|
266 |
+
Conv1d(
|
267 |
+
channels,
|
268 |
+
channels,
|
269 |
+
kernel_size,
|
270 |
+
1,
|
271 |
+
dilation=1,
|
272 |
+
padding=get_padding(kernel_size, 1),
|
273 |
+
)
|
274 |
+
),
|
275 |
+
weight_norm(
|
276 |
+
Conv1d(
|
277 |
+
channels,
|
278 |
+
channels,
|
279 |
+
kernel_size,
|
280 |
+
1,
|
281 |
+
dilation=1,
|
282 |
+
padding=get_padding(kernel_size, 1),
|
283 |
+
)
|
284 |
+
),
|
285 |
+
weight_norm(
|
286 |
+
Conv1d(
|
287 |
+
channels,
|
288 |
+
channels,
|
289 |
+
kernel_size,
|
290 |
+
1,
|
291 |
+
dilation=1,
|
292 |
+
padding=get_padding(kernel_size, 1),
|
293 |
+
)
|
294 |
+
),
|
295 |
+
]
|
296 |
+
)
|
297 |
+
self.convs2.apply(init_weights)
|
298 |
+
|
299 |
+
def forward(self, x, x_mask=None):
|
300 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
301 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
302 |
+
if x_mask is not None:
|
303 |
+
xt = xt * x_mask
|
304 |
+
xt = c1(xt)
|
305 |
+
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
306 |
+
if x_mask is not None:
|
307 |
+
xt = xt * x_mask
|
308 |
+
xt = c2(xt)
|
309 |
+
x = xt + x
|
310 |
+
if x_mask is not None:
|
311 |
+
x = x * x_mask
|
312 |
+
return x
|
313 |
+
|
314 |
+
def remove_weight_norm(self):
|
315 |
+
for l in self.convs1:
|
316 |
+
remove_weight_norm(l)
|
317 |
+
for l in self.convs2:
|
318 |
+
remove_weight_norm(l)
|
319 |
+
|
320 |
+
|
321 |
+
class ResBlock2(torch.nn.Module):
|
322 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
|
323 |
+
super(ResBlock2, self).__init__()
|
324 |
+
self.convs = nn.ModuleList(
|
325 |
+
[
|
326 |
+
weight_norm(
|
327 |
+
Conv1d(
|
328 |
+
channels,
|
329 |
+
channels,
|
330 |
+
kernel_size,
|
331 |
+
1,
|
332 |
+
dilation=dilation[0],
|
333 |
+
padding=get_padding(kernel_size, dilation[0]),
|
334 |
+
)
|
335 |
+
),
|
336 |
+
weight_norm(
|
337 |
+
Conv1d(
|
338 |
+
channels,
|
339 |
+
channels,
|
340 |
+
kernel_size,
|
341 |
+
1,
|
342 |
+
dilation=dilation[1],
|
343 |
+
padding=get_padding(kernel_size, dilation[1]),
|
344 |
+
)
|
345 |
+
),
|
346 |
+
]
|
347 |
+
)
|
348 |
+
self.convs.apply(init_weights)
|
349 |
+
|
350 |
+
def forward(self, x, x_mask=None):
|
351 |
+
for c in self.convs:
|
352 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
353 |
+
if x_mask is not None:
|
354 |
+
xt = xt * x_mask
|
355 |
+
xt = c(xt)
|
356 |
+
x = xt + x
|
357 |
+
if x_mask is not None:
|
358 |
+
x = x * x_mask
|
359 |
+
return x
|
360 |
+
|
361 |
+
def remove_weight_norm(self):
|
362 |
+
for l in self.convs:
|
363 |
+
remove_weight_norm(l)
|
364 |
+
|
365 |
+
|
366 |
+
class Log(nn.Module):
|
367 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
368 |
+
if not reverse:
|
369 |
+
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
370 |
+
logdet = torch.sum(-y, [1, 2])
|
371 |
+
return y, logdet
|
372 |
+
else:
|
373 |
+
x = torch.exp(x) * x_mask
|
374 |
+
return x
|
375 |
+
|
376 |
+
|
377 |
+
class Flip(nn.Module):
|
378 |
+
def forward(self, x, *args, reverse=False, **kwargs):
|
379 |
+
x = torch.flip(x, [1])
|
380 |
+
if not reverse:
|
381 |
+
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
382 |
+
return x, logdet
|
383 |
+
else:
|
384 |
+
return x
|
385 |
+
|
386 |
+
|
387 |
+
class ElementwiseAffine(nn.Module):
|
388 |
+
def __init__(self, channels):
|
389 |
+
super().__init__()
|
390 |
+
self.channels = channels
|
391 |
+
self.m = nn.Parameter(torch.zeros(channels, 1))
|
392 |
+
self.logs = nn.Parameter(torch.zeros(channels, 1))
|
393 |
+
|
394 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
395 |
+
if not reverse:
|
396 |
+
y = self.m + torch.exp(self.logs) * x
|
397 |
+
y = y * x_mask
|
398 |
+
logdet = torch.sum(self.logs * x_mask, [1, 2])
|
399 |
+
return y, logdet
|
400 |
+
else:
|
401 |
+
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
402 |
+
return x
|
403 |
+
|
404 |
+
|
405 |
+
class ResidualCouplingLayer(nn.Module):
|
406 |
+
def __init__(
|
407 |
+
self,
|
408 |
+
channels,
|
409 |
+
hidden_channels,
|
410 |
+
kernel_size,
|
411 |
+
dilation_rate,
|
412 |
+
n_layers,
|
413 |
+
p_dropout=0,
|
414 |
+
gin_channels=0,
|
415 |
+
mean_only=False,
|
416 |
+
):
|
417 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
418 |
+
super().__init__()
|
419 |
+
self.channels = channels
|
420 |
+
self.hidden_channels = hidden_channels
|
421 |
+
self.kernel_size = kernel_size
|
422 |
+
self.dilation_rate = dilation_rate
|
423 |
+
self.n_layers = n_layers
|
424 |
+
self.half_channels = channels // 2
|
425 |
+
self.mean_only = mean_only
|
426 |
+
|
427 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
428 |
+
self.enc = WN(
|
429 |
+
hidden_channels,
|
430 |
+
kernel_size,
|
431 |
+
dilation_rate,
|
432 |
+
n_layers,
|
433 |
+
p_dropout=p_dropout,
|
434 |
+
gin_channels=gin_channels,
|
435 |
+
)
|
436 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
437 |
+
self.post.weight.data.zero_()
|
438 |
+
self.post.bias.data.zero_()
|
439 |
+
|
440 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
441 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
442 |
+
h = self.pre(x0) * x_mask
|
443 |
+
h = self.enc(h, x_mask, g=g)
|
444 |
+
stats = self.post(h) * x_mask
|
445 |
+
if not self.mean_only:
|
446 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
447 |
+
else:
|
448 |
+
m = stats
|
449 |
+
logs = torch.zeros_like(m)
|
450 |
+
|
451 |
+
if not reverse:
|
452 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
453 |
+
x = torch.cat([x0, x1], 1)
|
454 |
+
logdet = torch.sum(logs, [1, 2])
|
455 |
+
return x, logdet
|
456 |
+
else:
|
457 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
458 |
+
x = torch.cat([x0, x1], 1)
|
459 |
+
return x
|
460 |
+
|
461 |
+
def remove_weight_norm(self):
|
462 |
+
self.enc.remove_weight_norm()
|
463 |
+
|
464 |
+
|
465 |
+
class ConvFlow(nn.Module):
|
466 |
+
def __init__(
|
467 |
+
self,
|
468 |
+
in_channels,
|
469 |
+
filter_channels,
|
470 |
+
kernel_size,
|
471 |
+
n_layers,
|
472 |
+
num_bins=10,
|
473 |
+
tail_bound=5.0,
|
474 |
+
):
|
475 |
+
super().__init__()
|
476 |
+
self.in_channels = in_channels
|
477 |
+
self.filter_channels = filter_channels
|
478 |
+
self.kernel_size = kernel_size
|
479 |
+
self.n_layers = n_layers
|
480 |
+
self.num_bins = num_bins
|
481 |
+
self.tail_bound = tail_bound
|
482 |
+
self.half_channels = in_channels // 2
|
483 |
+
|
484 |
+
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
485 |
+
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.0)
|
486 |
+
self.proj = nn.Conv1d(
|
487 |
+
filter_channels, self.half_channels * (num_bins * 3 - 1), 1
|
488 |
+
)
|
489 |
+
self.proj.weight.data.zero_()
|
490 |
+
self.proj.bias.data.zero_()
|
491 |
+
|
492 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
493 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
494 |
+
h = self.pre(x0)
|
495 |
+
h = self.convs(h, x_mask, g=g)
|
496 |
+
h = self.proj(h) * x_mask
|
497 |
+
|
498 |
+
b, c, t = x0.shape
|
499 |
+
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
500 |
+
|
501 |
+
unnormalized_widths = h[..., : self.num_bins] / math.sqrt(self.filter_channels)
|
502 |
+
unnormalized_heights = h[..., self.num_bins : 2 * self.num_bins] / math.sqrt(
|
503 |
+
self.filter_channels
|
504 |
+
)
|
505 |
+
unnormalized_derivatives = h[..., 2 * self.num_bins :]
|
506 |
+
|
507 |
+
x1, logabsdet = piecewise_rational_quadratic_transform(
|
508 |
+
x1,
|
509 |
+
unnormalized_widths,
|
510 |
+
unnormalized_heights,
|
511 |
+
unnormalized_derivatives,
|
512 |
+
inverse=reverse,
|
513 |
+
tails="linear",
|
514 |
+
tail_bound=self.tail_bound,
|
515 |
+
)
|
516 |
+
|
517 |
+
x = torch.cat([x0, x1], 1) * x_mask
|
518 |
+
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
519 |
+
if not reverse:
|
520 |
+
return x, logdet
|
521 |
+
else:
|
522 |
+
return x
|
infer_pack/transforms.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch.nn import functional as F
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
|
7 |
+
DEFAULT_MIN_BIN_WIDTH = 1e-3
|
8 |
+
DEFAULT_MIN_BIN_HEIGHT = 1e-3
|
9 |
+
DEFAULT_MIN_DERIVATIVE = 1e-3
|
10 |
+
|
11 |
+
|
12 |
+
def piecewise_rational_quadratic_transform(inputs,
|
13 |
+
unnormalized_widths,
|
14 |
+
unnormalized_heights,
|
15 |
+
unnormalized_derivatives,
|
16 |
+
inverse=False,
|
17 |
+
tails=None,
|
18 |
+
tail_bound=1.,
|
19 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
20 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
21 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE):
|
22 |
+
|
23 |
+
if tails is None:
|
24 |
+
spline_fn = rational_quadratic_spline
|
25 |
+
spline_kwargs = {}
|
26 |
+
else:
|
27 |
+
spline_fn = unconstrained_rational_quadratic_spline
|
28 |
+
spline_kwargs = {
|
29 |
+
'tails': tails,
|
30 |
+
'tail_bound': tail_bound
|
31 |
+
}
|
32 |
+
|
33 |
+
outputs, logabsdet = spline_fn(
|
34 |
+
inputs=inputs,
|
35 |
+
unnormalized_widths=unnormalized_widths,
|
36 |
+
unnormalized_heights=unnormalized_heights,
|
37 |
+
unnormalized_derivatives=unnormalized_derivatives,
|
38 |
+
inverse=inverse,
|
39 |
+
min_bin_width=min_bin_width,
|
40 |
+
min_bin_height=min_bin_height,
|
41 |
+
min_derivative=min_derivative,
|
42 |
+
**spline_kwargs
|
43 |
+
)
|
44 |
+
return outputs, logabsdet
|
45 |
+
|
46 |
+
|
47 |
+
def searchsorted(bin_locations, inputs, eps=1e-6):
|
48 |
+
bin_locations[..., -1] += eps
|
49 |
+
return torch.sum(
|
50 |
+
inputs[..., None] >= bin_locations,
|
51 |
+
dim=-1
|
52 |
+
) - 1
|
53 |
+
|
54 |
+
|
55 |
+
def unconstrained_rational_quadratic_spline(inputs,
|
56 |
+
unnormalized_widths,
|
57 |
+
unnormalized_heights,
|
58 |
+
unnormalized_derivatives,
|
59 |
+
inverse=False,
|
60 |
+
tails='linear',
|
61 |
+
tail_bound=1.,
|
62 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
63 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
64 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE):
|
65 |
+
inside_interval_mask = (inputs >= -tail_bound) & (inputs <= tail_bound)
|
66 |
+
outside_interval_mask = ~inside_interval_mask
|
67 |
+
|
68 |
+
outputs = torch.zeros_like(inputs)
|
69 |
+
logabsdet = torch.zeros_like(inputs)
|
70 |
+
|
71 |
+
if tails == 'linear':
|
72 |
+
unnormalized_derivatives = F.pad(unnormalized_derivatives, pad=(1, 1))
|
73 |
+
constant = np.log(np.exp(1 - min_derivative) - 1)
|
74 |
+
unnormalized_derivatives[..., 0] = constant
|
75 |
+
unnormalized_derivatives[..., -1] = constant
|
76 |
+
|
77 |
+
outputs[outside_interval_mask] = inputs[outside_interval_mask]
|
78 |
+
logabsdet[outside_interval_mask] = 0
|
79 |
+
else:
|
80 |
+
raise RuntimeError('{} tails are not implemented.'.format(tails))
|
81 |
+
|
82 |
+
outputs[inside_interval_mask], logabsdet[inside_interval_mask] = rational_quadratic_spline(
|
83 |
+
inputs=inputs[inside_interval_mask],
|
84 |
+
unnormalized_widths=unnormalized_widths[inside_interval_mask, :],
|
85 |
+
unnormalized_heights=unnormalized_heights[inside_interval_mask, :],
|
86 |
+
unnormalized_derivatives=unnormalized_derivatives[inside_interval_mask, :],
|
87 |
+
inverse=inverse,
|
88 |
+
left=-tail_bound, right=tail_bound, bottom=-tail_bound, top=tail_bound,
|
89 |
+
min_bin_width=min_bin_width,
|
90 |
+
min_bin_height=min_bin_height,
|
91 |
+
min_derivative=min_derivative
|
92 |
+
)
|
93 |
+
|
94 |
+
return outputs, logabsdet
|
95 |
+
|
96 |
+
def rational_quadratic_spline(inputs,
|
97 |
+
unnormalized_widths,
|
98 |
+
unnormalized_heights,
|
99 |
+
unnormalized_derivatives,
|
100 |
+
inverse=False,
|
101 |
+
left=0., right=1., bottom=0., top=1.,
|
102 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
103 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
104 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE):
|
105 |
+
if torch.min(inputs) < left or torch.max(inputs) > right:
|
106 |
+
raise ValueError('Input to a transform is not within its domain')
|
107 |
+
|
108 |
+
num_bins = unnormalized_widths.shape[-1]
|
109 |
+
|
110 |
+
if min_bin_width * num_bins > 1.0:
|
111 |
+
raise ValueError('Minimal bin width too large for the number of bins')
|
112 |
+
if min_bin_height * num_bins > 1.0:
|
113 |
+
raise ValueError('Minimal bin height too large for the number of bins')
|
114 |
+
|
115 |
+
widths = F.softmax(unnormalized_widths, dim=-1)
|
116 |
+
widths = min_bin_width + (1 - min_bin_width * num_bins) * widths
|
117 |
+
cumwidths = torch.cumsum(widths, dim=-1)
|
118 |
+
cumwidths = F.pad(cumwidths, pad=(1, 0), mode='constant', value=0.0)
|
119 |
+
cumwidths = (right - left) * cumwidths + left
|
120 |
+
cumwidths[..., 0] = left
|
121 |
+
cumwidths[..., -1] = right
|
122 |
+
widths = cumwidths[..., 1:] - cumwidths[..., :-1]
|
123 |
+
|
124 |
+
derivatives = min_derivative + F.softplus(unnormalized_derivatives)
|
125 |
+
|
126 |
+
heights = F.softmax(unnormalized_heights, dim=-1)
|
127 |
+
heights = min_bin_height + (1 - min_bin_height * num_bins) * heights
|
128 |
+
cumheights = torch.cumsum(heights, dim=-1)
|
129 |
+
cumheights = F.pad(cumheights, pad=(1, 0), mode='constant', value=0.0)
|
130 |
+
cumheights = (top - bottom) * cumheights + bottom
|
131 |
+
cumheights[..., 0] = bottom
|
132 |
+
cumheights[..., -1] = top
|
133 |
+
heights = cumheights[..., 1:] - cumheights[..., :-1]
|
134 |
+
|
135 |
+
if inverse:
|
136 |
+
bin_idx = searchsorted(cumheights, inputs)[..., None]
|
137 |
+
else:
|
138 |
+
bin_idx = searchsorted(cumwidths, inputs)[..., None]
|
139 |
+
|
140 |
+
input_cumwidths = cumwidths.gather(-1, bin_idx)[..., 0]
|
141 |
+
input_bin_widths = widths.gather(-1, bin_idx)[..., 0]
|
142 |
+
|
143 |
+
input_cumheights = cumheights.gather(-1, bin_idx)[..., 0]
|
144 |
+
delta = heights / widths
|
145 |
+
input_delta = delta.gather(-1, bin_idx)[..., 0]
|
146 |
+
|
147 |
+
input_derivatives = derivatives.gather(-1, bin_idx)[..., 0]
|
148 |
+
input_derivatives_plus_one = derivatives[..., 1:].gather(-1, bin_idx)[..., 0]
|
149 |
+
|
150 |
+
input_heights = heights.gather(-1, bin_idx)[..., 0]
|
151 |
+
|
152 |
+
if inverse:
|
153 |
+
a = (((inputs - input_cumheights) * (input_derivatives
|
154 |
+
+ input_derivatives_plus_one
|
155 |
+
- 2 * input_delta)
|
156 |
+
+ input_heights * (input_delta - input_derivatives)))
|
157 |
+
b = (input_heights * input_derivatives
|
158 |
+
- (inputs - input_cumheights) * (input_derivatives
|
159 |
+
+ input_derivatives_plus_one
|
160 |
+
- 2 * input_delta))
|
161 |
+
c = - input_delta * (inputs - input_cumheights)
|
162 |
+
|
163 |
+
discriminant = b.pow(2) - 4 * a * c
|
164 |
+
assert (discriminant >= 0).all()
|
165 |
+
|
166 |
+
root = (2 * c) / (-b - torch.sqrt(discriminant))
|
167 |
+
outputs = root * input_bin_widths + input_cumwidths
|
168 |
+
|
169 |
+
theta_one_minus_theta = root * (1 - root)
|
170 |
+
denominator = input_delta + ((input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
171 |
+
* theta_one_minus_theta)
|
172 |
+
derivative_numerator = input_delta.pow(2) * (input_derivatives_plus_one * root.pow(2)
|
173 |
+
+ 2 * input_delta * theta_one_minus_theta
|
174 |
+
+ input_derivatives * (1 - root).pow(2))
|
175 |
+
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
176 |
+
|
177 |
+
return outputs, -logabsdet
|
178 |
+
else:
|
179 |
+
theta = (inputs - input_cumwidths) / input_bin_widths
|
180 |
+
theta_one_minus_theta = theta * (1 - theta)
|
181 |
+
|
182 |
+
numerator = input_heights * (input_delta * theta.pow(2)
|
183 |
+
+ input_derivatives * theta_one_minus_theta)
|
184 |
+
denominator = input_delta + ((input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
185 |
+
* theta_one_minus_theta)
|
186 |
+
outputs = input_cumheights + numerator / denominator
|
187 |
+
|
188 |
+
derivative_numerator = input_delta.pow(2) * (input_derivatives_plus_one * theta.pow(2)
|
189 |
+
+ 2 * input_delta * theta_one_minus_theta
|
190 |
+
+ input_derivatives * (1 - theta).pow(2))
|
191 |
+
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
192 |
+
|
193 |
+
return outputs, logabsdet
|
infer_uvr5.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os,sys,torch,warnings,pdb
|
2 |
+
warnings.filterwarnings("ignore")
|
3 |
+
import librosa
|
4 |
+
import importlib
|
5 |
+
import numpy as np
|
6 |
+
import hashlib , math
|
7 |
+
from tqdm import tqdm
|
8 |
+
from uvr5_pack.lib_v5 import spec_utils
|
9 |
+
from uvr5_pack.utils import _get_name_params,inference
|
10 |
+
from uvr5_pack.lib_v5.model_param_init import ModelParameters
|
11 |
+
from scipy.io import wavfile
|
12 |
+
|
13 |
+
class _audio_pre_():
|
14 |
+
def __init__(self, model_path,device,is_half):
|
15 |
+
self.model_path = model_path
|
16 |
+
self.device = device
|
17 |
+
self.data = {
|
18 |
+
# Processing Options
|
19 |
+
'postprocess': False,
|
20 |
+
'tta': False,
|
21 |
+
# Constants
|
22 |
+
'window_size': 512,
|
23 |
+
'agg': 10,
|
24 |
+
'high_end_process': 'mirroring',
|
25 |
+
}
|
26 |
+
nn_arch_sizes = [
|
27 |
+
31191, # default
|
28 |
+
33966,61968, 123821, 123812, 537238 # custom
|
29 |
+
]
|
30 |
+
self.nn_architecture = list('{}KB'.format(s) for s in nn_arch_sizes)
|
31 |
+
model_size = math.ceil(os.stat(model_path ).st_size / 1024)
|
32 |
+
nn_architecture = '{}KB'.format(min(nn_arch_sizes, key=lambda x:abs(x-model_size)))
|
33 |
+
nets = importlib.import_module('uvr5_pack.lib_v5.nets' + f'_{nn_architecture}'.replace('_{}KB'.format(nn_arch_sizes[0]), ''), package=None)
|
34 |
+
model_hash = hashlib.md5(open(model_path,'rb').read()).hexdigest()
|
35 |
+
param_name ,model_params_d = _get_name_params(model_path , model_hash)
|
36 |
+
|
37 |
+
mp = ModelParameters(model_params_d)
|
38 |
+
model = nets.CascadedASPPNet(mp.param['bins'] * 2)
|
39 |
+
cpk = torch.load( model_path , map_location='cpu')
|
40 |
+
model.load_state_dict(cpk)
|
41 |
+
model.eval()
|
42 |
+
if(is_half==True):model = model.half().to(device)
|
43 |
+
else:model = model.to(device)
|
44 |
+
|
45 |
+
self.mp = mp
|
46 |
+
self.model = model
|
47 |
+
|
48 |
+
def _path_audio_(self, music_file ,ins_root=None,vocal_root=None):
|
49 |
+
if(ins_root is None and vocal_root is None):return "No save root."
|
50 |
+
name=os.path.basename(music_file)
|
51 |
+
if(ins_root is not None):os.makedirs(ins_root, exist_ok=True)
|
52 |
+
if(vocal_root is not None):os.makedirs(vocal_root , exist_ok=True)
|
53 |
+
X_wave, y_wave, X_spec_s, y_spec_s = {}, {}, {}, {}
|
54 |
+
bands_n = len(self.mp.param['band'])
|
55 |
+
# print(bands_n)
|
56 |
+
for d in range(bands_n, 0, -1):
|
57 |
+
bp = self.mp.param['band'][d]
|
58 |
+
if d == bands_n: # high-end band
|
59 |
+
X_wave[d], _ = librosa.core.load(
|
60 |
+
music_file, bp['sr'], False, dtype=np.float32, res_type=bp['res_type'])
|
61 |
+
if X_wave[d].ndim == 1:
|
62 |
+
X_wave[d] = np.asfortranarray([X_wave[d], X_wave[d]])
|
63 |
+
else: # lower bands
|
64 |
+
X_wave[d] = librosa.core.resample(X_wave[d+1], self.mp.param['band'][d+1]['sr'], bp['sr'], res_type=bp['res_type'])
|
65 |
+
# Stft of wave source
|
66 |
+
X_spec_s[d] = spec_utils.wave_to_spectrogram_mt(X_wave[d], bp['hl'], bp['n_fft'], self.mp.param['mid_side'], self.mp.param['mid_side_b2'], self.mp.param['reverse'])
|
67 |
+
# pdb.set_trace()
|
68 |
+
if d == bands_n and self.data['high_end_process'] != 'none':
|
69 |
+
input_high_end_h = (bp['n_fft']//2 - bp['crop_stop']) + ( self.mp.param['pre_filter_stop'] - self.mp.param['pre_filter_start'])
|
70 |
+
input_high_end = X_spec_s[d][:, bp['n_fft']//2-input_high_end_h:bp['n_fft']//2, :]
|
71 |
+
|
72 |
+
X_spec_m = spec_utils.combine_spectrograms(X_spec_s, self.mp)
|
73 |
+
aggresive_set = float(self.data['agg']/100)
|
74 |
+
aggressiveness = {'value': aggresive_set, 'split_bin': self.mp.param['band'][1]['crop_stop']}
|
75 |
+
with torch.no_grad():
|
76 |
+
pred, X_mag, X_phase = inference(X_spec_m,self.device,self.model, aggressiveness,self.data)
|
77 |
+
# Postprocess
|
78 |
+
if self.data['postprocess']:
|
79 |
+
pred_inv = np.clip(X_mag - pred, 0, np.inf)
|
80 |
+
pred = spec_utils.mask_silence(pred, pred_inv)
|
81 |
+
y_spec_m = pred * X_phase
|
82 |
+
v_spec_m = X_spec_m - y_spec_m
|
83 |
+
|
84 |
+
if (ins_root is not None):
|
85 |
+
if self.data['high_end_process'].startswith('mirroring'):
|
86 |
+
input_high_end_ = spec_utils.mirroring(self.data['high_end_process'], y_spec_m, input_high_end, self.mp)
|
87 |
+
wav_instrument = spec_utils.cmb_spectrogram_to_wave(y_spec_m, self.mp,input_high_end_h, input_high_end_)
|
88 |
+
else:
|
89 |
+
wav_instrument = spec_utils.cmb_spectrogram_to_wave(y_spec_m, self.mp)
|
90 |
+
print ('%s instruments done'%name)
|
91 |
+
wavfile.write(os.path.join(ins_root, 'instrument_{}.wav'.format(name) ), self.mp.param['sr'], (np.array(wav_instrument)*32768).astype("int16")) #
|
92 |
+
if (vocal_root is not None):
|
93 |
+
if self.data['high_end_process'].startswith('mirroring'):
|
94 |
+
input_high_end_ = spec_utils.mirroring(self.data['high_end_process'], v_spec_m, input_high_end, self.mp)
|
95 |
+
wav_vocals = spec_utils.cmb_spectrogram_to_wave(v_spec_m, self.mp, input_high_end_h, input_high_end_)
|
96 |
+
else:
|
97 |
+
wav_vocals = spec_utils.cmb_spectrogram_to_wave(v_spec_m, self.mp)
|
98 |
+
print ('%s vocals done'%name)
|
99 |
+
wavfile.write(os.path.join(vocal_root , 'vocal_{}.wav'.format(name) ), self.mp.param['sr'], (np.array(wav_vocals)*32768).astype("int16"))
|
100 |
+
|
101 |
+
if __name__ == '__main__':
|
102 |
+
device = 'cuda'
|
103 |
+
is_half=True
|
104 |
+
model_path='uvr5_weights/2_HP-UVR.pth'
|
105 |
+
pre_fun = _audio_pre_(model_path=model_path,device=device,is_half=True)
|
106 |
+
audio_path = '神女劈观.aac'
|
107 |
+
save_path = 'opt'
|
108 |
+
pre_fun._path_audio_(audio_path , save_path,save_path)
|
slicer.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os.path
|
2 |
+
from argparse import ArgumentParser
|
3 |
+
import time
|
4 |
+
|
5 |
+
import librosa
|
6 |
+
import numpy as np
|
7 |
+
import soundfile
|
8 |
+
from scipy.ndimage import maximum_filter1d, uniform_filter1d
|
9 |
+
|
10 |
+
|
11 |
+
def timeit(func):
|
12 |
+
def run(*args, **kwargs):
|
13 |
+
t = time.time()
|
14 |
+
res = func(*args, **kwargs)
|
15 |
+
print('executing \'%s\' costed %.3fs' % (func.__name__, time.time() - t))
|
16 |
+
return res
|
17 |
+
return run
|
18 |
+
|
19 |
+
|
20 |
+
# @timeit
|
21 |
+
def _window_maximum(arr, win_sz):
|
22 |
+
return maximum_filter1d(arr, size=win_sz)[win_sz // 2: win_sz // 2 + arr.shape[0] - win_sz + 1]
|
23 |
+
|
24 |
+
|
25 |
+
# @timeit
|
26 |
+
def _window_rms(arr, win_sz):
|
27 |
+
filtered = np.sqrt(uniform_filter1d(np.power(arr, 2), win_sz) - np.power(uniform_filter1d(arr, win_sz), 2))
|
28 |
+
return filtered[win_sz // 2: win_sz // 2 + arr.shape[0] - win_sz + 1]
|
29 |
+
|
30 |
+
|
31 |
+
def level2db(levels, eps=1e-12):
|
32 |
+
return 20 * np.log10(np.clip(levels, a_min=eps, a_max=1))
|
33 |
+
|
34 |
+
|
35 |
+
def _apply_slice(audio, begin, end):
|
36 |
+
if len(audio.shape) > 1:
|
37 |
+
return audio[:, begin: end]
|
38 |
+
else:
|
39 |
+
return audio[begin: end]
|
40 |
+
|
41 |
+
|
42 |
+
class Slicer:
|
43 |
+
def __init__(self,
|
44 |
+
sr: int,
|
45 |
+
db_threshold: float = -40,
|
46 |
+
min_length: int = 5000,
|
47 |
+
win_l: int = 300,
|
48 |
+
win_s: int = 20,
|
49 |
+
max_silence_kept: int = 500):
|
50 |
+
self.db_threshold = db_threshold
|
51 |
+
self.min_samples = round(sr * min_length / 1000)
|
52 |
+
self.win_ln = round(sr * win_l / 1000)
|
53 |
+
self.win_sn = round(sr * win_s / 1000)
|
54 |
+
self.max_silence = round(sr * max_silence_kept / 1000)
|
55 |
+
if not self.min_samples >= self.win_ln >= self.win_sn:
|
56 |
+
raise ValueError('The following condition must be satisfied: min_length >= win_l >= win_s')
|
57 |
+
if not self.max_silence >= self.win_sn:
|
58 |
+
raise ValueError('The following condition must be satisfied: max_silence_kept >= win_s')
|
59 |
+
|
60 |
+
@timeit
|
61 |
+
def slice(self, audio):
|
62 |
+
if len(audio.shape) > 1:
|
63 |
+
samples = librosa.to_mono(audio)
|
64 |
+
else:
|
65 |
+
samples = audio
|
66 |
+
if samples.shape[0] <= self.min_samples:
|
67 |
+
return [audio]
|
68 |
+
# get absolute amplitudes
|
69 |
+
abs_amp = np.abs(samples - np.mean(samples))
|
70 |
+
# calculate local maximum with large window
|
71 |
+
win_max_db = level2db(_window_maximum(abs_amp, win_sz=self.win_ln))
|
72 |
+
sil_tags = []
|
73 |
+
left = right = 0
|
74 |
+
while right < win_max_db.shape[0]:
|
75 |
+
if win_max_db[right] < self.db_threshold:
|
76 |
+
right += 1
|
77 |
+
elif left == right:
|
78 |
+
left += 1
|
79 |
+
right += 1
|
80 |
+
else:
|
81 |
+
if left == 0:
|
82 |
+
split_loc_l = left
|
83 |
+
else:
|
84 |
+
sil_left_n = min(self.max_silence, (right + self.win_ln - left) // 2)
|
85 |
+
rms_db_left = level2db(_window_rms(samples[left: left + sil_left_n], win_sz=self.win_sn))
|
86 |
+
split_win_l = left + np.argmin(rms_db_left)
|
87 |
+
split_loc_l = split_win_l + np.argmin(abs_amp[split_win_l: split_win_l + self.win_sn])
|
88 |
+
if len(sil_tags) != 0 and split_loc_l - sil_tags[-1][1] < self.min_samples and right < win_max_db.shape[0] - 1:
|
89 |
+
right += 1
|
90 |
+
left = right
|
91 |
+
continue
|
92 |
+
if right == win_max_db.shape[0] - 1:
|
93 |
+
split_loc_r = right + self.win_ln
|
94 |
+
else:
|
95 |
+
sil_right_n = min(self.max_silence, (right + self.win_ln - left) // 2)
|
96 |
+
rms_db_right = level2db(_window_rms(samples[right + self.win_ln - sil_right_n: right + self.win_ln], win_sz=self.win_sn))
|
97 |
+
split_win_r = right + self.win_ln - sil_right_n + np.argmin(rms_db_right)
|
98 |
+
split_loc_r = split_win_r + np.argmin(abs_amp[split_win_r: split_win_r + self.win_sn])
|
99 |
+
sil_tags.append((split_loc_l, split_loc_r))
|
100 |
+
right += 1
|
101 |
+
left = right
|
102 |
+
if left != right:
|
103 |
+
sil_left_n = min(self.max_silence, (right + self.win_ln - left) // 2)
|
104 |
+
rms_db_left = level2db(_window_rms(samples[left: left + sil_left_n], win_sz=self.win_sn))
|
105 |
+
split_win_l = left + np.argmin(rms_db_left)
|
106 |
+
split_loc_l = split_win_l + np.argmin(abs_amp[split_win_l: split_win_l + self.win_sn])
|
107 |
+
sil_tags.append((split_loc_l, samples.shape[0]))
|
108 |
+
if len(sil_tags) == 0:
|
109 |
+
return [audio]
|
110 |
+
else:
|
111 |
+
chunks = []
|
112 |
+
if sil_tags[0][0] > 0:
|
113 |
+
chunks.append(_apply_slice(audio, 0, sil_tags[0][0]))
|
114 |
+
for i in range(0, len(sil_tags) - 1):
|
115 |
+
chunks.append(_apply_slice(audio, sil_tags[i][1], sil_tags[i + 1][0]))
|
116 |
+
if sil_tags[-1][1] < samples.shape[0] - 1:
|
117 |
+
chunks.append(_apply_slice(audio, sil_tags[-1][1], samples.shape[0]))
|
118 |
+
return chunks
|
119 |
+
|
120 |
+
|
121 |
+
def main():
|
122 |
+
parser = ArgumentParser()
|
123 |
+
parser.add_argument('audio', type=str, help='The audio to be sliced')
|
124 |
+
parser.add_argument('--out', type=str, help='Output directory of the sliced audio clips')
|
125 |
+
parser.add_argument('--db_thresh', type=float, required=False, default=-40, help='The dB threshold for silence detection')
|
126 |
+
parser.add_argument('--min_len', type=int, required=False, default=5000, help='The minimum milliseconds required for each sliced audio clip')
|
127 |
+
parser.add_argument('--win_l', type=int, required=False, default=300, help='Size of the large sliding window, presented in milliseconds')
|
128 |
+
parser.add_argument('--win_s', type=int, required=False, default=20, help='Size of the small sliding window, presented in milliseconds')
|
129 |
+
parser.add_argument('--max_sil_kept', type=int, required=False, default=500, help='The maximum silence length kept around the sliced audio, presented in milliseconds')
|
130 |
+
args = parser.parse_args()
|
131 |
+
out = args.out
|
132 |
+
if out is None:
|
133 |
+
out = os.path.dirname(os.path.abspath(args.audio))
|
134 |
+
audio, sr = librosa.load(args.audio, sr=None)
|
135 |
+
slicer = Slicer(
|
136 |
+
sr=sr,
|
137 |
+
db_threshold=args.db_thresh,
|
138 |
+
min_length=args.min_len,
|
139 |
+
win_l=args.win_l,
|
140 |
+
win_s=args.win_s,
|
141 |
+
max_silence_kept=args.max_sil_kept
|
142 |
+
)
|
143 |
+
chunks = slicer.slice(audio)
|
144 |
+
if not os.path.exists(args.out):
|
145 |
+
os.makedirs(args.out)
|
146 |
+
for i, chunk in enumerate(chunks):
|
147 |
+
soundfile.write(os.path.join(out, f'%s_%d.wav' % (os.path.basename(args.audio).rsplit('.', maxsplit=1)[0], i)), chunk, sr)
|
148 |
+
|
149 |
+
|
150 |
+
if __name__ == '__main__':
|
151 |
+
main()
|
trainset_preprocess_pipeline.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np,ffmpeg,os,traceback
|
2 |
+
from slicer import Slicer
|
3 |
+
slicer = Slicer(
|
4 |
+
sr=40000,
|
5 |
+
db_threshold=-32,
|
6 |
+
min_length=800,
|
7 |
+
win_l=400,
|
8 |
+
win_s=20,
|
9 |
+
max_silence_kept=150
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
def p0_load_audio(file, sr):#str-ing
|
16 |
+
try:
|
17 |
+
out, _ = (
|
18 |
+
ffmpeg.input(file, threads=0)
|
19 |
+
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=sr)
|
20 |
+
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
21 |
+
)
|
22 |
+
except ffmpeg.Error as e:
|
23 |
+
raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
|
24 |
+
return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0
|
25 |
+
|
26 |
+
def p1_trim_audio(slicer,audio):return slicer.slice(audio)
|
27 |
+
|
28 |
+
def p2_avg_cut(audio,sr,per=3.7,overlap=0.3,tail=4):
|
29 |
+
i = 0
|
30 |
+
audios=[]
|
31 |
+
while (1):
|
32 |
+
start = int(sr * (per - overlap) * i)
|
33 |
+
i += 1
|
34 |
+
if (len(audio[start:]) > tail * sr):
|
35 |
+
audios.append(audio[start:start + int(per * sr)])
|
36 |
+
else:
|
37 |
+
audios.append(audio[start:])
|
38 |
+
break
|
39 |
+
return audios
|
40 |
+
|
41 |
+
def p2b_get_vol(audio):return np.square(audio).mean()
|
42 |
+
|
43 |
+
def p3_norm(audio,alpha=0.8,maxx=0.95):return audio / np.abs(audio).max() * (maxx * alpha) + (1-alpha) * audio
|
44 |
+
|
45 |
+
def pipeline(inp_root,sr1=40000,sr2=16000,if_trim=True,if_avg_cut=True,if_norm=True,save_root1=None,save_root2=None):
|
46 |
+
if(save_root1==None and save_root2==None):return "No save root."
|
47 |
+
name2vol={}
|
48 |
+
infos=[]
|
49 |
+
names=[]
|
50 |
+
for name in os.listdir(inp_root):
|
51 |
+
try:
|
52 |
+
inp_path=os.path.join(inp_root,name)
|
53 |
+
audio=p0_load_audio(inp_path)
|
54 |
+
except:
|
55 |
+
infos.append("%s\t%s"%(name,traceback.format_exc()))
|
56 |
+
continue
|
57 |
+
if(if_trim==True):res1s=p1_trim_audio(audio)
|
58 |
+
else:res1s=[audio]
|
59 |
+
for i0,res1 in res1s:
|
60 |
+
if(if_avg_cut==True):res2=p2_avg_cut(res1)
|
61 |
+
else:res2=[res1]
|
62 |
+
|
63 |
+
|
uvr5_pack/__pycache__/utils.cpython-39.pyc
ADDED
Binary file (6.87 kB). View file
|
|
uvr5_pack/lib_v5/__pycache__/layers_123821KB.cpython-39.pyc
ADDED
Binary file (4.14 kB). View file
|
|
uvr5_pack/lib_v5/__pycache__/model_param_init.cpython-39.pyc
ADDED
Binary file (1.63 kB). View file
|
|
uvr5_pack/lib_v5/__pycache__/nets_61968KB.cpython-39.pyc
ADDED
Binary file (3.46 kB). View file
|
|
uvr5_pack/lib_v5/__pycache__/spec_utils.cpython-39.pyc
ADDED
Binary file (13.3 kB). View file
|
|
uvr5_pack/lib_v5/dataset.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
import torch.utils.data
|
7 |
+
from tqdm import tqdm
|
8 |
+
|
9 |
+
from uvr5_pack.lib_v5 import spec_utils
|
10 |
+
|
11 |
+
|
12 |
+
class VocalRemoverValidationSet(torch.utils.data.Dataset):
|
13 |
+
|
14 |
+
def __init__(self, patch_list):
|
15 |
+
self.patch_list = patch_list
|
16 |
+
|
17 |
+
def __len__(self):
|
18 |
+
return len(self.patch_list)
|
19 |
+
|
20 |
+
def __getitem__(self, idx):
|
21 |
+
path = self.patch_list[idx]
|
22 |
+
data = np.load(path)
|
23 |
+
|
24 |
+
X, y = data['X'], data['y']
|
25 |
+
|
26 |
+
X_mag = np.abs(X)
|
27 |
+
y_mag = np.abs(y)
|
28 |
+
|
29 |
+
return X_mag, y_mag
|
30 |
+
|
31 |
+
|
32 |
+
def make_pair(mix_dir, inst_dir):
|
33 |
+
input_exts = ['.wav', '.m4a', '.mp3', '.mp4', '.flac']
|
34 |
+
|
35 |
+
X_list = sorted([
|
36 |
+
os.path.join(mix_dir, fname)
|
37 |
+
for fname in os.listdir(mix_dir)
|
38 |
+
if os.path.splitext(fname)[1] in input_exts])
|
39 |
+
y_list = sorted([
|
40 |
+
os.path.join(inst_dir, fname)
|
41 |
+
for fname in os.listdir(inst_dir)
|
42 |
+
if os.path.splitext(fname)[1] in input_exts])
|
43 |
+
|
44 |
+
filelist = list(zip(X_list, y_list))
|
45 |
+
|
46 |
+
return filelist
|
47 |
+
|
48 |
+
|
49 |
+
def train_val_split(dataset_dir, split_mode, val_rate, val_filelist):
|
50 |
+
if split_mode == 'random':
|
51 |
+
filelist = make_pair(
|
52 |
+
os.path.join(dataset_dir, 'mixtures'),
|
53 |
+
os.path.join(dataset_dir, 'instruments'))
|
54 |
+
|
55 |
+
random.shuffle(filelist)
|
56 |
+
|
57 |
+
if len(val_filelist) == 0:
|
58 |
+
val_size = int(len(filelist) * val_rate)
|
59 |
+
train_filelist = filelist[:-val_size]
|
60 |
+
val_filelist = filelist[-val_size:]
|
61 |
+
else:
|
62 |
+
train_filelist = [
|
63 |
+
pair for pair in filelist
|
64 |
+
if list(pair) not in val_filelist]
|
65 |
+
elif split_mode == 'subdirs':
|
66 |
+
if len(val_filelist) != 0:
|
67 |
+
raise ValueError('The `val_filelist` option is not available in `subdirs` mode')
|
68 |
+
|
69 |
+
train_filelist = make_pair(
|
70 |
+
os.path.join(dataset_dir, 'training/mixtures'),
|
71 |
+
os.path.join(dataset_dir, 'training/instruments'))
|
72 |
+
|
73 |
+
val_filelist = make_pair(
|
74 |
+
os.path.join(dataset_dir, 'validation/mixtures'),
|
75 |
+
os.path.join(dataset_dir, 'validation/instruments'))
|
76 |
+
|
77 |
+
return train_filelist, val_filelist
|
78 |
+
|
79 |
+
|
80 |
+
def augment(X, y, reduction_rate, reduction_mask, mixup_rate, mixup_alpha):
|
81 |
+
perm = np.random.permutation(len(X))
|
82 |
+
for i, idx in enumerate(tqdm(perm)):
|
83 |
+
if np.random.uniform() < reduction_rate:
|
84 |
+
y[idx] = spec_utils.reduce_vocal_aggressively(X[idx], y[idx], reduction_mask)
|
85 |
+
|
86 |
+
if np.random.uniform() < 0.5:
|
87 |
+
# swap channel
|
88 |
+
X[idx] = X[idx, ::-1]
|
89 |
+
y[idx] = y[idx, ::-1]
|
90 |
+
if np.random.uniform() < 0.02:
|
91 |
+
# mono
|
92 |
+
X[idx] = X[idx].mean(axis=0, keepdims=True)
|
93 |
+
y[idx] = y[idx].mean(axis=0, keepdims=True)
|
94 |
+
if np.random.uniform() < 0.02:
|
95 |
+
# inst
|
96 |
+
X[idx] = y[idx]
|
97 |
+
|
98 |
+
if np.random.uniform() < mixup_rate and i < len(perm) - 1:
|
99 |
+
lam = np.random.beta(mixup_alpha, mixup_alpha)
|
100 |
+
X[idx] = lam * X[idx] + (1 - lam) * X[perm[i + 1]]
|
101 |
+
y[idx] = lam * y[idx] + (1 - lam) * y[perm[i + 1]]
|
102 |
+
|
103 |
+
return X, y
|
104 |
+
|
105 |
+
|
106 |
+
def make_padding(width, cropsize, offset):
|
107 |
+
left = offset
|
108 |
+
roi_size = cropsize - left * 2
|
109 |
+
if roi_size == 0:
|
110 |
+
roi_size = cropsize
|
111 |
+
right = roi_size - (width % roi_size) + left
|
112 |
+
|
113 |
+
return left, right, roi_size
|
114 |
+
|
115 |
+
|
116 |
+
def make_training_set(filelist, cropsize, patches, sr, hop_length, n_fft, offset):
|
117 |
+
len_dataset = patches * len(filelist)
|
118 |
+
|
119 |
+
X_dataset = np.zeros(
|
120 |
+
(len_dataset, 2, n_fft // 2 + 1, cropsize), dtype=np.complex64)
|
121 |
+
y_dataset = np.zeros(
|
122 |
+
(len_dataset, 2, n_fft // 2 + 1, cropsize), dtype=np.complex64)
|
123 |
+
|
124 |
+
for i, (X_path, y_path) in enumerate(tqdm(filelist)):
|
125 |
+
X, y = spec_utils.cache_or_load(X_path, y_path, sr, hop_length, n_fft)
|
126 |
+
coef = np.max([np.abs(X).max(), np.abs(y).max()])
|
127 |
+
X, y = X / coef, y / coef
|
128 |
+
|
129 |
+
l, r, roi_size = make_padding(X.shape[2], cropsize, offset)
|
130 |
+
X_pad = np.pad(X, ((0, 0), (0, 0), (l, r)), mode='constant')
|
131 |
+
y_pad = np.pad(y, ((0, 0), (0, 0), (l, r)), mode='constant')
|
132 |
+
|
133 |
+
starts = np.random.randint(0, X_pad.shape[2] - cropsize, patches)
|
134 |
+
ends = starts + cropsize
|
135 |
+
for j in range(patches):
|
136 |
+
idx = i * patches + j
|
137 |
+
X_dataset[idx] = X_pad[:, :, starts[j]:ends[j]]
|
138 |
+
y_dataset[idx] = y_pad[:, :, starts[j]:ends[j]]
|
139 |
+
|
140 |
+
return X_dataset, y_dataset
|
141 |
+
|
142 |
+
|
143 |
+
def make_validation_set(filelist, cropsize, sr, hop_length, n_fft, offset):
|
144 |
+
patch_list = []
|
145 |
+
patch_dir = 'cs{}_sr{}_hl{}_nf{}_of{}'.format(cropsize, sr, hop_length, n_fft, offset)
|
146 |
+
os.makedirs(patch_dir, exist_ok=True)
|
147 |
+
|
148 |
+
for i, (X_path, y_path) in enumerate(tqdm(filelist)):
|
149 |
+
basename = os.path.splitext(os.path.basename(X_path))[0]
|
150 |
+
|
151 |
+
X, y = spec_utils.cache_or_load(X_path, y_path, sr, hop_length, n_fft)
|
152 |
+
coef = np.max([np.abs(X).max(), np.abs(y).max()])
|
153 |
+
X, y = X / coef, y / coef
|
154 |
+
|
155 |
+
l, r, roi_size = make_padding(X.shape[2], cropsize, offset)
|
156 |
+
X_pad = np.pad(X, ((0, 0), (0, 0), (l, r)), mode='constant')
|
157 |
+
y_pad = np.pad(y, ((0, 0), (0, 0), (l, r)), mode='constant')
|
158 |
+
|
159 |
+
len_dataset = int(np.ceil(X.shape[2] / roi_size))
|
160 |
+
for j in range(len_dataset):
|
161 |
+
outpath = os.path.join(patch_dir, '{}_p{}.npz'.format(basename, j))
|
162 |
+
start = j * roi_size
|
163 |
+
if not os.path.exists(outpath):
|
164 |
+
np.savez(
|
165 |
+
outpath,
|
166 |
+
X=X_pad[:, :, start:start + cropsize],
|
167 |
+
y=y_pad[:, :, start:start + cropsize])
|
168 |
+
patch_list.append(outpath)
|
169 |
+
|
170 |
+
return VocalRemoverValidationSet(patch_list)
|
uvr5_pack/lib_v5/layers.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
from uvr5_pack.lib_v5 import spec_utils
|
6 |
+
|
7 |
+
|
8 |
+
class Conv2DBNActiv(nn.Module):
|
9 |
+
|
10 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
11 |
+
super(Conv2DBNActiv, self).__init__()
|
12 |
+
self.conv = nn.Sequential(
|
13 |
+
nn.Conv2d(
|
14 |
+
nin, nout,
|
15 |
+
kernel_size=ksize,
|
16 |
+
stride=stride,
|
17 |
+
padding=pad,
|
18 |
+
dilation=dilation,
|
19 |
+
bias=False),
|
20 |
+
nn.BatchNorm2d(nout),
|
21 |
+
activ()
|
22 |
+
)
|
23 |
+
|
24 |
+
def __call__(self, x):
|
25 |
+
return self.conv(x)
|
26 |
+
|
27 |
+
|
28 |
+
class SeperableConv2DBNActiv(nn.Module):
|
29 |
+
|
30 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
31 |
+
super(SeperableConv2DBNActiv, self).__init__()
|
32 |
+
self.conv = nn.Sequential(
|
33 |
+
nn.Conv2d(
|
34 |
+
nin, nin,
|
35 |
+
kernel_size=ksize,
|
36 |
+
stride=stride,
|
37 |
+
padding=pad,
|
38 |
+
dilation=dilation,
|
39 |
+
groups=nin,
|
40 |
+
bias=False),
|
41 |
+
nn.Conv2d(
|
42 |
+
nin, nout,
|
43 |
+
kernel_size=1,
|
44 |
+
bias=False),
|
45 |
+
nn.BatchNorm2d(nout),
|
46 |
+
activ()
|
47 |
+
)
|
48 |
+
|
49 |
+
def __call__(self, x):
|
50 |
+
return self.conv(x)
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
|
55 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
|
56 |
+
super(Encoder, self).__init__()
|
57 |
+
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
58 |
+
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
|
59 |
+
|
60 |
+
def __call__(self, x):
|
61 |
+
skip = self.conv1(x)
|
62 |
+
h = self.conv2(skip)
|
63 |
+
|
64 |
+
return h, skip
|
65 |
+
|
66 |
+
|
67 |
+
class Decoder(nn.Module):
|
68 |
+
|
69 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
|
70 |
+
super(Decoder, self).__init__()
|
71 |
+
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
72 |
+
self.dropout = nn.Dropout2d(0.1) if dropout else None
|
73 |
+
|
74 |
+
def __call__(self, x, skip=None):
|
75 |
+
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
|
76 |
+
if skip is not None:
|
77 |
+
skip = spec_utils.crop_center(skip, x)
|
78 |
+
x = torch.cat([x, skip], dim=1)
|
79 |
+
h = self.conv(x)
|
80 |
+
|
81 |
+
if self.dropout is not None:
|
82 |
+
h = self.dropout(h)
|
83 |
+
|
84 |
+
return h
|
85 |
+
|
86 |
+
|
87 |
+
class ASPPModule(nn.Module):
|
88 |
+
|
89 |
+
def __init__(self, nin, nout, dilations=(4, 8, 16), activ=nn.ReLU):
|
90 |
+
super(ASPPModule, self).__init__()
|
91 |
+
self.conv1 = nn.Sequential(
|
92 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
93 |
+
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
94 |
+
)
|
95 |
+
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
96 |
+
self.conv3 = SeperableConv2DBNActiv(
|
97 |
+
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
|
98 |
+
self.conv4 = SeperableConv2DBNActiv(
|
99 |
+
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
|
100 |
+
self.conv5 = SeperableConv2DBNActiv(
|
101 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
102 |
+
self.bottleneck = nn.Sequential(
|
103 |
+
Conv2DBNActiv(nin * 5, nout, 1, 1, 0, activ=activ),
|
104 |
+
nn.Dropout2d(0.1)
|
105 |
+
)
|
106 |
+
|
107 |
+
def forward(self, x):
|
108 |
+
_, _, h, w = x.size()
|
109 |
+
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
|
110 |
+
feat2 = self.conv2(x)
|
111 |
+
feat3 = self.conv3(x)
|
112 |
+
feat4 = self.conv4(x)
|
113 |
+
feat5 = self.conv5(x)
|
114 |
+
out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
|
115 |
+
bottle = self.bottleneck(out)
|
116 |
+
return bottle
|
uvr5_pack/lib_v5/layers_123812KB .py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
from uvr5_pack.lib_v5 import spec_utils
|
6 |
+
|
7 |
+
|
8 |
+
class Conv2DBNActiv(nn.Module):
|
9 |
+
|
10 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
11 |
+
super(Conv2DBNActiv, self).__init__()
|
12 |
+
self.conv = nn.Sequential(
|
13 |
+
nn.Conv2d(
|
14 |
+
nin, nout,
|
15 |
+
kernel_size=ksize,
|
16 |
+
stride=stride,
|
17 |
+
padding=pad,
|
18 |
+
dilation=dilation,
|
19 |
+
bias=False),
|
20 |
+
nn.BatchNorm2d(nout),
|
21 |
+
activ()
|
22 |
+
)
|
23 |
+
|
24 |
+
def __call__(self, x):
|
25 |
+
return self.conv(x)
|
26 |
+
|
27 |
+
|
28 |
+
class SeperableConv2DBNActiv(nn.Module):
|
29 |
+
|
30 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
31 |
+
super(SeperableConv2DBNActiv, self).__init__()
|
32 |
+
self.conv = nn.Sequential(
|
33 |
+
nn.Conv2d(
|
34 |
+
nin, nin,
|
35 |
+
kernel_size=ksize,
|
36 |
+
stride=stride,
|
37 |
+
padding=pad,
|
38 |
+
dilation=dilation,
|
39 |
+
groups=nin,
|
40 |
+
bias=False),
|
41 |
+
nn.Conv2d(
|
42 |
+
nin, nout,
|
43 |
+
kernel_size=1,
|
44 |
+
bias=False),
|
45 |
+
nn.BatchNorm2d(nout),
|
46 |
+
activ()
|
47 |
+
)
|
48 |
+
|
49 |
+
def __call__(self, x):
|
50 |
+
return self.conv(x)
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
|
55 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
|
56 |
+
super(Encoder, self).__init__()
|
57 |
+
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
58 |
+
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
|
59 |
+
|
60 |
+
def __call__(self, x):
|
61 |
+
skip = self.conv1(x)
|
62 |
+
h = self.conv2(skip)
|
63 |
+
|
64 |
+
return h, skip
|
65 |
+
|
66 |
+
|
67 |
+
class Decoder(nn.Module):
|
68 |
+
|
69 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
|
70 |
+
super(Decoder, self).__init__()
|
71 |
+
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
72 |
+
self.dropout = nn.Dropout2d(0.1) if dropout else None
|
73 |
+
|
74 |
+
def __call__(self, x, skip=None):
|
75 |
+
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
|
76 |
+
if skip is not None:
|
77 |
+
skip = spec_utils.crop_center(skip, x)
|
78 |
+
x = torch.cat([x, skip], dim=1)
|
79 |
+
h = self.conv(x)
|
80 |
+
|
81 |
+
if self.dropout is not None:
|
82 |
+
h = self.dropout(h)
|
83 |
+
|
84 |
+
return h
|
85 |
+
|
86 |
+
|
87 |
+
class ASPPModule(nn.Module):
|
88 |
+
|
89 |
+
def __init__(self, nin, nout, dilations=(4, 8, 16), activ=nn.ReLU):
|
90 |
+
super(ASPPModule, self).__init__()
|
91 |
+
self.conv1 = nn.Sequential(
|
92 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
93 |
+
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
94 |
+
)
|
95 |
+
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
96 |
+
self.conv3 = SeperableConv2DBNActiv(
|
97 |
+
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
|
98 |
+
self.conv4 = SeperableConv2DBNActiv(
|
99 |
+
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
|
100 |
+
self.conv5 = SeperableConv2DBNActiv(
|
101 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
102 |
+
self.bottleneck = nn.Sequential(
|
103 |
+
Conv2DBNActiv(nin * 5, nout, 1, 1, 0, activ=activ),
|
104 |
+
nn.Dropout2d(0.1)
|
105 |
+
)
|
106 |
+
|
107 |
+
def forward(self, x):
|
108 |
+
_, _, h, w = x.size()
|
109 |
+
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
|
110 |
+
feat2 = self.conv2(x)
|
111 |
+
feat3 = self.conv3(x)
|
112 |
+
feat4 = self.conv4(x)
|
113 |
+
feat5 = self.conv5(x)
|
114 |
+
out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
|
115 |
+
bottle = self.bottleneck(out)
|
116 |
+
return bottle
|
uvr5_pack/lib_v5/layers_123821KB.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
from uvr5_pack.lib_v5 import spec_utils
|
6 |
+
|
7 |
+
|
8 |
+
class Conv2DBNActiv(nn.Module):
|
9 |
+
|
10 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
11 |
+
super(Conv2DBNActiv, self).__init__()
|
12 |
+
self.conv = nn.Sequential(
|
13 |
+
nn.Conv2d(
|
14 |
+
nin, nout,
|
15 |
+
kernel_size=ksize,
|
16 |
+
stride=stride,
|
17 |
+
padding=pad,
|
18 |
+
dilation=dilation,
|
19 |
+
bias=False),
|
20 |
+
nn.BatchNorm2d(nout),
|
21 |
+
activ()
|
22 |
+
)
|
23 |
+
|
24 |
+
def __call__(self, x):
|
25 |
+
return self.conv(x)
|
26 |
+
|
27 |
+
|
28 |
+
class SeperableConv2DBNActiv(nn.Module):
|
29 |
+
|
30 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
31 |
+
super(SeperableConv2DBNActiv, self).__init__()
|
32 |
+
self.conv = nn.Sequential(
|
33 |
+
nn.Conv2d(
|
34 |
+
nin, nin,
|
35 |
+
kernel_size=ksize,
|
36 |
+
stride=stride,
|
37 |
+
padding=pad,
|
38 |
+
dilation=dilation,
|
39 |
+
groups=nin,
|
40 |
+
bias=False),
|
41 |
+
nn.Conv2d(
|
42 |
+
nin, nout,
|
43 |
+
kernel_size=1,
|
44 |
+
bias=False),
|
45 |
+
nn.BatchNorm2d(nout),
|
46 |
+
activ()
|
47 |
+
)
|
48 |
+
|
49 |
+
def __call__(self, x):
|
50 |
+
return self.conv(x)
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
|
55 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
|
56 |
+
super(Encoder, self).__init__()
|
57 |
+
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
58 |
+
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
|
59 |
+
|
60 |
+
def __call__(self, x):
|
61 |
+
skip = self.conv1(x)
|
62 |
+
h = self.conv2(skip)
|
63 |
+
|
64 |
+
return h, skip
|
65 |
+
|
66 |
+
|
67 |
+
class Decoder(nn.Module):
|
68 |
+
|
69 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
|
70 |
+
super(Decoder, self).__init__()
|
71 |
+
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
72 |
+
self.dropout = nn.Dropout2d(0.1) if dropout else None
|
73 |
+
|
74 |
+
def __call__(self, x, skip=None):
|
75 |
+
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
|
76 |
+
if skip is not None:
|
77 |
+
skip = spec_utils.crop_center(skip, x)
|
78 |
+
x = torch.cat([x, skip], dim=1)
|
79 |
+
h = self.conv(x)
|
80 |
+
|
81 |
+
if self.dropout is not None:
|
82 |
+
h = self.dropout(h)
|
83 |
+
|
84 |
+
return h
|
85 |
+
|
86 |
+
|
87 |
+
class ASPPModule(nn.Module):
|
88 |
+
|
89 |
+
def __init__(self, nin, nout, dilations=(4, 8, 16), activ=nn.ReLU):
|
90 |
+
super(ASPPModule, self).__init__()
|
91 |
+
self.conv1 = nn.Sequential(
|
92 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
93 |
+
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
94 |
+
)
|
95 |
+
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
96 |
+
self.conv3 = SeperableConv2DBNActiv(
|
97 |
+
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
|
98 |
+
self.conv4 = SeperableConv2DBNActiv(
|
99 |
+
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
|
100 |
+
self.conv5 = SeperableConv2DBNActiv(
|
101 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
102 |
+
self.bottleneck = nn.Sequential(
|
103 |
+
Conv2DBNActiv(nin * 5, nout, 1, 1, 0, activ=activ),
|
104 |
+
nn.Dropout2d(0.1)
|
105 |
+
)
|
106 |
+
|
107 |
+
def forward(self, x):
|
108 |
+
_, _, h, w = x.size()
|
109 |
+
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
|
110 |
+
feat2 = self.conv2(x)
|
111 |
+
feat3 = self.conv3(x)
|
112 |
+
feat4 = self.conv4(x)
|
113 |
+
feat5 = self.conv5(x)
|
114 |
+
out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
|
115 |
+
bottle = self.bottleneck(out)
|
116 |
+
return bottle
|
uvr5_pack/lib_v5/layers_33966KB.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
from uvr5_pack.lib_v5 import spec_utils
|
6 |
+
|
7 |
+
|
8 |
+
class Conv2DBNActiv(nn.Module):
|
9 |
+
|
10 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
11 |
+
super(Conv2DBNActiv, self).__init__()
|
12 |
+
self.conv = nn.Sequential(
|
13 |
+
nn.Conv2d(
|
14 |
+
nin, nout,
|
15 |
+
kernel_size=ksize,
|
16 |
+
stride=stride,
|
17 |
+
padding=pad,
|
18 |
+
dilation=dilation,
|
19 |
+
bias=False),
|
20 |
+
nn.BatchNorm2d(nout),
|
21 |
+
activ()
|
22 |
+
)
|
23 |
+
|
24 |
+
def __call__(self, x):
|
25 |
+
return self.conv(x)
|
26 |
+
|
27 |
+
|
28 |
+
class SeperableConv2DBNActiv(nn.Module):
|
29 |
+
|
30 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
31 |
+
super(SeperableConv2DBNActiv, self).__init__()
|
32 |
+
self.conv = nn.Sequential(
|
33 |
+
nn.Conv2d(
|
34 |
+
nin, nin,
|
35 |
+
kernel_size=ksize,
|
36 |
+
stride=stride,
|
37 |
+
padding=pad,
|
38 |
+
dilation=dilation,
|
39 |
+
groups=nin,
|
40 |
+
bias=False),
|
41 |
+
nn.Conv2d(
|
42 |
+
nin, nout,
|
43 |
+
kernel_size=1,
|
44 |
+
bias=False),
|
45 |
+
nn.BatchNorm2d(nout),
|
46 |
+
activ()
|
47 |
+
)
|
48 |
+
|
49 |
+
def __call__(self, x):
|
50 |
+
return self.conv(x)
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
|
55 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
|
56 |
+
super(Encoder, self).__init__()
|
57 |
+
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
58 |
+
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
|
59 |
+
|
60 |
+
def __call__(self, x):
|
61 |
+
skip = self.conv1(x)
|
62 |
+
h = self.conv2(skip)
|
63 |
+
|
64 |
+
return h, skip
|
65 |
+
|
66 |
+
|
67 |
+
class Decoder(nn.Module):
|
68 |
+
|
69 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
|
70 |
+
super(Decoder, self).__init__()
|
71 |
+
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
72 |
+
self.dropout = nn.Dropout2d(0.1) if dropout else None
|
73 |
+
|
74 |
+
def __call__(self, x, skip=None):
|
75 |
+
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
|
76 |
+
if skip is not None:
|
77 |
+
skip = spec_utils.crop_center(skip, x)
|
78 |
+
x = torch.cat([x, skip], dim=1)
|
79 |
+
h = self.conv(x)
|
80 |
+
|
81 |
+
if self.dropout is not None:
|
82 |
+
h = self.dropout(h)
|
83 |
+
|
84 |
+
return h
|
85 |
+
|
86 |
+
|
87 |
+
class ASPPModule(nn.Module):
|
88 |
+
|
89 |
+
def __init__(self, nin, nout, dilations=(4, 8, 16, 32, 64), activ=nn.ReLU):
|
90 |
+
super(ASPPModule, self).__init__()
|
91 |
+
self.conv1 = nn.Sequential(
|
92 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
93 |
+
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
94 |
+
)
|
95 |
+
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
96 |
+
self.conv3 = SeperableConv2DBNActiv(
|
97 |
+
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
|
98 |
+
self.conv4 = SeperableConv2DBNActiv(
|
99 |
+
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
|
100 |
+
self.conv5 = SeperableConv2DBNActiv(
|
101 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
102 |
+
self.conv6 = SeperableConv2DBNActiv(
|
103 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
104 |
+
self.conv7 = SeperableConv2DBNActiv(
|
105 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
106 |
+
self.bottleneck = nn.Sequential(
|
107 |
+
Conv2DBNActiv(nin * 7, nout, 1, 1, 0, activ=activ),
|
108 |
+
nn.Dropout2d(0.1)
|
109 |
+
)
|
110 |
+
|
111 |
+
def forward(self, x):
|
112 |
+
_, _, h, w = x.size()
|
113 |
+
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
|
114 |
+
feat2 = self.conv2(x)
|
115 |
+
feat3 = self.conv3(x)
|
116 |
+
feat4 = self.conv4(x)
|
117 |
+
feat5 = self.conv5(x)
|
118 |
+
feat6 = self.conv6(x)
|
119 |
+
feat7 = self.conv7(x)
|
120 |
+
out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1)
|
121 |
+
bottle = self.bottleneck(out)
|
122 |
+
return bottle
|
uvr5_pack/lib_v5/layers_537227KB.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
from uvr5_pack.lib_v5 import spec_utils
|
6 |
+
|
7 |
+
|
8 |
+
class Conv2DBNActiv(nn.Module):
|
9 |
+
|
10 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
11 |
+
super(Conv2DBNActiv, self).__init__()
|
12 |
+
self.conv = nn.Sequential(
|
13 |
+
nn.Conv2d(
|
14 |
+
nin, nout,
|
15 |
+
kernel_size=ksize,
|
16 |
+
stride=stride,
|
17 |
+
padding=pad,
|
18 |
+
dilation=dilation,
|
19 |
+
bias=False),
|
20 |
+
nn.BatchNorm2d(nout),
|
21 |
+
activ()
|
22 |
+
)
|
23 |
+
|
24 |
+
def __call__(self, x):
|
25 |
+
return self.conv(x)
|
26 |
+
|
27 |
+
|
28 |
+
class SeperableConv2DBNActiv(nn.Module):
|
29 |
+
|
30 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
31 |
+
super(SeperableConv2DBNActiv, self).__init__()
|
32 |
+
self.conv = nn.Sequential(
|
33 |
+
nn.Conv2d(
|
34 |
+
nin, nin,
|
35 |
+
kernel_size=ksize,
|
36 |
+
stride=stride,
|
37 |
+
padding=pad,
|
38 |
+
dilation=dilation,
|
39 |
+
groups=nin,
|
40 |
+
bias=False),
|
41 |
+
nn.Conv2d(
|
42 |
+
nin, nout,
|
43 |
+
kernel_size=1,
|
44 |
+
bias=False),
|
45 |
+
nn.BatchNorm2d(nout),
|
46 |
+
activ()
|
47 |
+
)
|
48 |
+
|
49 |
+
def __call__(self, x):
|
50 |
+
return self.conv(x)
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
|
55 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
|
56 |
+
super(Encoder, self).__init__()
|
57 |
+
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
58 |
+
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
|
59 |
+
|
60 |
+
def __call__(self, x):
|
61 |
+
skip = self.conv1(x)
|
62 |
+
h = self.conv2(skip)
|
63 |
+
|
64 |
+
return h, skip
|
65 |
+
|
66 |
+
|
67 |
+
class Decoder(nn.Module):
|
68 |
+
|
69 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
|
70 |
+
super(Decoder, self).__init__()
|
71 |
+
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
72 |
+
self.dropout = nn.Dropout2d(0.1) if dropout else None
|
73 |
+
|
74 |
+
def __call__(self, x, skip=None):
|
75 |
+
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
|
76 |
+
if skip is not None:
|
77 |
+
skip = spec_utils.crop_center(skip, x)
|
78 |
+
x = torch.cat([x, skip], dim=1)
|
79 |
+
h = self.conv(x)
|
80 |
+
|
81 |
+
if self.dropout is not None:
|
82 |
+
h = self.dropout(h)
|
83 |
+
|
84 |
+
return h
|
85 |
+
|
86 |
+
|
87 |
+
class ASPPModule(nn.Module):
|
88 |
+
|
89 |
+
def __init__(self, nin, nout, dilations=(4, 8, 16, 32, 64), activ=nn.ReLU):
|
90 |
+
super(ASPPModule, self).__init__()
|
91 |
+
self.conv1 = nn.Sequential(
|
92 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
93 |
+
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
94 |
+
)
|
95 |
+
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
96 |
+
self.conv3 = SeperableConv2DBNActiv(
|
97 |
+
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
|
98 |
+
self.conv4 = SeperableConv2DBNActiv(
|
99 |
+
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
|
100 |
+
self.conv5 = SeperableConv2DBNActiv(
|
101 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
102 |
+
self.conv6 = SeperableConv2DBNActiv(
|
103 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
104 |
+
self.conv7 = SeperableConv2DBNActiv(
|
105 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
106 |
+
self.bottleneck = nn.Sequential(
|
107 |
+
Conv2DBNActiv(nin * 7, nout, 1, 1, 0, activ=activ),
|
108 |
+
nn.Dropout2d(0.1)
|
109 |
+
)
|
110 |
+
|
111 |
+
def forward(self, x):
|
112 |
+
_, _, h, w = x.size()
|
113 |
+
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
|
114 |
+
feat2 = self.conv2(x)
|
115 |
+
feat3 = self.conv3(x)
|
116 |
+
feat4 = self.conv4(x)
|
117 |
+
feat5 = self.conv5(x)
|
118 |
+
feat6 = self.conv6(x)
|
119 |
+
feat7 = self.conv7(x)
|
120 |
+
out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1)
|
121 |
+
bottle = self.bottleneck(out)
|
122 |
+
return bottle
|
uvr5_pack/lib_v5/layers_537238KB.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
from uvr5_pack.lib_v5 import spec_utils
|
6 |
+
|
7 |
+
|
8 |
+
class Conv2DBNActiv(nn.Module):
|
9 |
+
|
10 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
11 |
+
super(Conv2DBNActiv, self).__init__()
|
12 |
+
self.conv = nn.Sequential(
|
13 |
+
nn.Conv2d(
|
14 |
+
nin, nout,
|
15 |
+
kernel_size=ksize,
|
16 |
+
stride=stride,
|
17 |
+
padding=pad,
|
18 |
+
dilation=dilation,
|
19 |
+
bias=False),
|
20 |
+
nn.BatchNorm2d(nout),
|
21 |
+
activ()
|
22 |
+
)
|
23 |
+
|
24 |
+
def __call__(self, x):
|
25 |
+
return self.conv(x)
|
26 |
+
|
27 |
+
|
28 |
+
class SeperableConv2DBNActiv(nn.Module):
|
29 |
+
|
30 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
|
31 |
+
super(SeperableConv2DBNActiv, self).__init__()
|
32 |
+
self.conv = nn.Sequential(
|
33 |
+
nn.Conv2d(
|
34 |
+
nin, nin,
|
35 |
+
kernel_size=ksize,
|
36 |
+
stride=stride,
|
37 |
+
padding=pad,
|
38 |
+
dilation=dilation,
|
39 |
+
groups=nin,
|
40 |
+
bias=False),
|
41 |
+
nn.Conv2d(
|
42 |
+
nin, nout,
|
43 |
+
kernel_size=1,
|
44 |
+
bias=False),
|
45 |
+
nn.BatchNorm2d(nout),
|
46 |
+
activ()
|
47 |
+
)
|
48 |
+
|
49 |
+
def __call__(self, x):
|
50 |
+
return self.conv(x)
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
|
55 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
|
56 |
+
super(Encoder, self).__init__()
|
57 |
+
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
58 |
+
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
|
59 |
+
|
60 |
+
def __call__(self, x):
|
61 |
+
skip = self.conv1(x)
|
62 |
+
h = self.conv2(skip)
|
63 |
+
|
64 |
+
return h, skip
|
65 |
+
|
66 |
+
|
67 |
+
class Decoder(nn.Module):
|
68 |
+
|
69 |
+
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
|
70 |
+
super(Decoder, self).__init__()
|
71 |
+
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
|
72 |
+
self.dropout = nn.Dropout2d(0.1) if dropout else None
|
73 |
+
|
74 |
+
def __call__(self, x, skip=None):
|
75 |
+
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
|
76 |
+
if skip is not None:
|
77 |
+
skip = spec_utils.crop_center(skip, x)
|
78 |
+
x = torch.cat([x, skip], dim=1)
|
79 |
+
h = self.conv(x)
|
80 |
+
|
81 |
+
if self.dropout is not None:
|
82 |
+
h = self.dropout(h)
|
83 |
+
|
84 |
+
return h
|
85 |
+
|
86 |
+
|
87 |
+
class ASPPModule(nn.Module):
|
88 |
+
|
89 |
+
def __init__(self, nin, nout, dilations=(4, 8, 16, 32, 64), activ=nn.ReLU):
|
90 |
+
super(ASPPModule, self).__init__()
|
91 |
+
self.conv1 = nn.Sequential(
|
92 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
93 |
+
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
94 |
+
)
|
95 |
+
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
|
96 |
+
self.conv3 = SeperableConv2DBNActiv(
|
97 |
+
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
|
98 |
+
self.conv4 = SeperableConv2DBNActiv(
|
99 |
+
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
|
100 |
+
self.conv5 = SeperableConv2DBNActiv(
|
101 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
102 |
+
self.conv6 = SeperableConv2DBNActiv(
|
103 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
104 |
+
self.conv7 = SeperableConv2DBNActiv(
|
105 |
+
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
|
106 |
+
self.bottleneck = nn.Sequential(
|
107 |
+
Conv2DBNActiv(nin * 7, nout, 1, 1, 0, activ=activ),
|
108 |
+
nn.Dropout2d(0.1)
|
109 |
+
)
|
110 |
+
|
111 |
+
def forward(self, x):
|
112 |
+
_, _, h, w = x.size()
|
113 |
+
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
|
114 |
+
feat2 = self.conv2(x)
|
115 |
+
feat3 = self.conv3(x)
|
116 |
+
feat4 = self.conv4(x)
|
117 |
+
feat5 = self.conv5(x)
|
118 |
+
feat6 = self.conv6(x)
|
119 |
+
feat7 = self.conv7(x)
|
120 |
+
out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1)
|
121 |
+
bottle = self.bottleneck(out)
|
122 |
+
return bottle
|
uvr5_pack/lib_v5/model_param_init.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import pathlib
|
4 |
+
|
5 |
+
default_param = {}
|
6 |
+
default_param['bins'] = 768
|
7 |
+
default_param['unstable_bins'] = 9 # training only
|
8 |
+
default_param['reduction_bins'] = 762 # training only
|
9 |
+
default_param['sr'] = 44100
|
10 |
+
default_param['pre_filter_start'] = 757
|
11 |
+
default_param['pre_filter_stop'] = 768
|
12 |
+
default_param['band'] = {}
|
13 |
+
|
14 |
+
|
15 |
+
default_param['band'][1] = {
|
16 |
+
'sr': 11025,
|
17 |
+
'hl': 128,
|
18 |
+
'n_fft': 960,
|
19 |
+
'crop_start': 0,
|
20 |
+
'crop_stop': 245,
|
21 |
+
'lpf_start': 61, # inference only
|
22 |
+
'res_type': 'polyphase'
|
23 |
+
}
|
24 |
+
|
25 |
+
default_param['band'][2] = {
|
26 |
+
'sr': 44100,
|
27 |
+
'hl': 512,
|
28 |
+
'n_fft': 1536,
|
29 |
+
'crop_start': 24,
|
30 |
+
'crop_stop': 547,
|
31 |
+
'hpf_start': 81, # inference only
|
32 |
+
'res_type': 'sinc_best'
|
33 |
+
}
|
34 |
+
|
35 |
+
|
36 |
+
def int_keys(d):
|
37 |
+
r = {}
|
38 |
+
for k, v in d:
|
39 |
+
if k.isdigit():
|
40 |
+
k = int(k)
|
41 |
+
r[k] = v
|
42 |
+
return r
|
43 |
+
|
44 |
+
|
45 |
+
class ModelParameters(object):
|
46 |
+
def __init__(self, config_path=''):
|
47 |
+
if '.pth' == pathlib.Path(config_path).suffix:
|
48 |
+
import zipfile
|
49 |
+
|
50 |
+
with zipfile.ZipFile(config_path, 'r') as zip:
|
51 |
+
self.param = json.loads(zip.read('param.json'), object_pairs_hook=int_keys)
|
52 |
+
elif '.json' == pathlib.Path(config_path).suffix:
|
53 |
+
with open(config_path, 'r') as f:
|
54 |
+
self.param = json.loads(f.read(), object_pairs_hook=int_keys)
|
55 |
+
else:
|
56 |
+
self.param = default_param
|
57 |
+
|
58 |
+
for k in ['mid_side', 'mid_side_b', 'mid_side_b2', 'stereo_w', 'stereo_n', 'reverse']:
|
59 |
+
if not k in self.param:
|
60 |
+
self.param[k] = False
|
uvr5_pack/lib_v5/modelparams/1band_sr16000_hl512.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 1024,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 16000,
|
8 |
+
"hl": 512,
|
9 |
+
"n_fft": 2048,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 1024,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "sinc_best"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 16000,
|
17 |
+
"pre_filter_start": 1023,
|
18 |
+
"pre_filter_stop": 1024
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/1band_sr32000_hl512.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 1024,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 32000,
|
8 |
+
"hl": 512,
|
9 |
+
"n_fft": 2048,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 1024,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "kaiser_fast"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 32000,
|
17 |
+
"pre_filter_start": 1000,
|
18 |
+
"pre_filter_stop": 1021
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/1band_sr33075_hl384.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 1024,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 33075,
|
8 |
+
"hl": 384,
|
9 |
+
"n_fft": 2048,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 1024,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "sinc_best"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 33075,
|
17 |
+
"pre_filter_start": 1000,
|
18 |
+
"pre_filter_stop": 1021
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/1band_sr44100_hl1024.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 1024,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 44100,
|
8 |
+
"hl": 1024,
|
9 |
+
"n_fft": 2048,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 1024,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "sinc_best"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 44100,
|
17 |
+
"pre_filter_start": 1023,
|
18 |
+
"pre_filter_stop": 1024
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/1band_sr44100_hl256.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 256,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 44100,
|
8 |
+
"hl": 256,
|
9 |
+
"n_fft": 512,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 256,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "sinc_best"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 44100,
|
17 |
+
"pre_filter_start": 256,
|
18 |
+
"pre_filter_stop": 256
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/1band_sr44100_hl512.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 1024,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 44100,
|
8 |
+
"hl": 512,
|
9 |
+
"n_fft": 2048,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 1024,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "sinc_best"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 44100,
|
17 |
+
"pre_filter_start": 1023,
|
18 |
+
"pre_filter_stop": 1024
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/1band_sr44100_hl512_cut.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 1024,
|
3 |
+
"unstable_bins": 0,
|
4 |
+
"reduction_bins": 0,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 44100,
|
8 |
+
"hl": 512,
|
9 |
+
"n_fft": 2048,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 700,
|
12 |
+
"hpf_start": -1,
|
13 |
+
"res_type": "sinc_best"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"sr": 44100,
|
17 |
+
"pre_filter_start": 1023,
|
18 |
+
"pre_filter_stop": 700
|
19 |
+
}
|
uvr5_pack/lib_v5/modelparams/2band_32000.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 768,
|
3 |
+
"unstable_bins": 7,
|
4 |
+
"reduction_bins": 705,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 6000,
|
8 |
+
"hl": 66,
|
9 |
+
"n_fft": 512,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 240,
|
12 |
+
"lpf_start": 60,
|
13 |
+
"lpf_stop": 118,
|
14 |
+
"res_type": "sinc_fastest"
|
15 |
+
},
|
16 |
+
"2": {
|
17 |
+
"sr": 32000,
|
18 |
+
"hl": 352,
|
19 |
+
"n_fft": 1024,
|
20 |
+
"crop_start": 22,
|
21 |
+
"crop_stop": 505,
|
22 |
+
"hpf_start": 44,
|
23 |
+
"hpf_stop": 23,
|
24 |
+
"res_type": "sinc_medium"
|
25 |
+
}
|
26 |
+
},
|
27 |
+
"sr": 32000,
|
28 |
+
"pre_filter_start": 710,
|
29 |
+
"pre_filter_stop": 731
|
30 |
+
}
|
uvr5_pack/lib_v5/modelparams/2band_44100_lofi.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 512,
|
3 |
+
"unstable_bins": 7,
|
4 |
+
"reduction_bins": 510,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 11025,
|
8 |
+
"hl": 160,
|
9 |
+
"n_fft": 768,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 192,
|
12 |
+
"lpf_start": 41,
|
13 |
+
"lpf_stop": 139,
|
14 |
+
"res_type": "sinc_fastest"
|
15 |
+
},
|
16 |
+
"2": {
|
17 |
+
"sr": 44100,
|
18 |
+
"hl": 640,
|
19 |
+
"n_fft": 1024,
|
20 |
+
"crop_start": 10,
|
21 |
+
"crop_stop": 320,
|
22 |
+
"hpf_start": 47,
|
23 |
+
"hpf_stop": 15,
|
24 |
+
"res_type": "sinc_medium"
|
25 |
+
}
|
26 |
+
},
|
27 |
+
"sr": 44100,
|
28 |
+
"pre_filter_start": 510,
|
29 |
+
"pre_filter_stop": 512
|
30 |
+
}
|
uvr5_pack/lib_v5/modelparams/2band_48000.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 768,
|
3 |
+
"unstable_bins": 7,
|
4 |
+
"reduction_bins": 705,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 6000,
|
8 |
+
"hl": 66,
|
9 |
+
"n_fft": 512,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 240,
|
12 |
+
"lpf_start": 60,
|
13 |
+
"lpf_stop": 240,
|
14 |
+
"res_type": "sinc_fastest"
|
15 |
+
},
|
16 |
+
"2": {
|
17 |
+
"sr": 48000,
|
18 |
+
"hl": 528,
|
19 |
+
"n_fft": 1536,
|
20 |
+
"crop_start": 22,
|
21 |
+
"crop_stop": 505,
|
22 |
+
"hpf_start": 82,
|
23 |
+
"hpf_stop": 22,
|
24 |
+
"res_type": "sinc_medium"
|
25 |
+
}
|
26 |
+
},
|
27 |
+
"sr": 48000,
|
28 |
+
"pre_filter_start": 710,
|
29 |
+
"pre_filter_stop": 731
|
30 |
+
}
|
uvr5_pack/lib_v5/modelparams/3band_44100.json
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 768,
|
3 |
+
"unstable_bins": 5,
|
4 |
+
"reduction_bins": 733,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 11025,
|
8 |
+
"hl": 128,
|
9 |
+
"n_fft": 768,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 278,
|
12 |
+
"lpf_start": 28,
|
13 |
+
"lpf_stop": 140,
|
14 |
+
"res_type": "polyphase"
|
15 |
+
},
|
16 |
+
"2": {
|
17 |
+
"sr": 22050,
|
18 |
+
"hl": 256,
|
19 |
+
"n_fft": 768,
|
20 |
+
"crop_start": 14,
|
21 |
+
"crop_stop": 322,
|
22 |
+
"hpf_start": 70,
|
23 |
+
"hpf_stop": 14,
|
24 |
+
"lpf_start": 283,
|
25 |
+
"lpf_stop": 314,
|
26 |
+
"res_type": "polyphase"
|
27 |
+
},
|
28 |
+
"3": {
|
29 |
+
"sr": 44100,
|
30 |
+
"hl": 512,
|
31 |
+
"n_fft": 768,
|
32 |
+
"crop_start": 131,
|
33 |
+
"crop_stop": 313,
|
34 |
+
"hpf_start": 154,
|
35 |
+
"hpf_stop": 141,
|
36 |
+
"res_type": "sinc_medium"
|
37 |
+
}
|
38 |
+
},
|
39 |
+
"sr": 44100,
|
40 |
+
"pre_filter_start": 757,
|
41 |
+
"pre_filter_stop": 768
|
42 |
+
}
|
uvr5_pack/lib_v5/modelparams/3band_44100_mid.json
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"mid_side": true,
|
3 |
+
"bins": 768,
|
4 |
+
"unstable_bins": 5,
|
5 |
+
"reduction_bins": 733,
|
6 |
+
"band": {
|
7 |
+
"1": {
|
8 |
+
"sr": 11025,
|
9 |
+
"hl": 128,
|
10 |
+
"n_fft": 768,
|
11 |
+
"crop_start": 0,
|
12 |
+
"crop_stop": 278,
|
13 |
+
"lpf_start": 28,
|
14 |
+
"lpf_stop": 140,
|
15 |
+
"res_type": "polyphase"
|
16 |
+
},
|
17 |
+
"2": {
|
18 |
+
"sr": 22050,
|
19 |
+
"hl": 256,
|
20 |
+
"n_fft": 768,
|
21 |
+
"crop_start": 14,
|
22 |
+
"crop_stop": 322,
|
23 |
+
"hpf_start": 70,
|
24 |
+
"hpf_stop": 14,
|
25 |
+
"lpf_start": 283,
|
26 |
+
"lpf_stop": 314,
|
27 |
+
"res_type": "polyphase"
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"sr": 44100,
|
31 |
+
"hl": 512,
|
32 |
+
"n_fft": 768,
|
33 |
+
"crop_start": 131,
|
34 |
+
"crop_stop": 313,
|
35 |
+
"hpf_start": 154,
|
36 |
+
"hpf_stop": 141,
|
37 |
+
"res_type": "sinc_medium"
|
38 |
+
}
|
39 |
+
},
|
40 |
+
"sr": 44100,
|
41 |
+
"pre_filter_start": 757,
|
42 |
+
"pre_filter_stop": 768
|
43 |
+
}
|
uvr5_pack/lib_v5/modelparams/3band_44100_msb2.json
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"mid_side_b2": true,
|
3 |
+
"bins": 640,
|
4 |
+
"unstable_bins": 7,
|
5 |
+
"reduction_bins": 565,
|
6 |
+
"band": {
|
7 |
+
"1": {
|
8 |
+
"sr": 11025,
|
9 |
+
"hl": 108,
|
10 |
+
"n_fft": 1024,
|
11 |
+
"crop_start": 0,
|
12 |
+
"crop_stop": 187,
|
13 |
+
"lpf_start": 92,
|
14 |
+
"lpf_stop": 186,
|
15 |
+
"res_type": "polyphase"
|
16 |
+
},
|
17 |
+
"2": {
|
18 |
+
"sr": 22050,
|
19 |
+
"hl": 216,
|
20 |
+
"n_fft": 768,
|
21 |
+
"crop_start": 0,
|
22 |
+
"crop_stop": 212,
|
23 |
+
"hpf_start": 68,
|
24 |
+
"hpf_stop": 34,
|
25 |
+
"lpf_start": 174,
|
26 |
+
"lpf_stop": 209,
|
27 |
+
"res_type": "polyphase"
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"sr": 44100,
|
31 |
+
"hl": 432,
|
32 |
+
"n_fft": 640,
|
33 |
+
"crop_start": 66,
|
34 |
+
"crop_stop": 307,
|
35 |
+
"hpf_start": 86,
|
36 |
+
"hpf_stop": 72,
|
37 |
+
"res_type": "kaiser_fast"
|
38 |
+
}
|
39 |
+
},
|
40 |
+
"sr": 44100,
|
41 |
+
"pre_filter_start": 639,
|
42 |
+
"pre_filter_stop": 640
|
43 |
+
}
|
uvr5_pack/lib_v5/modelparams/4band_44100.json
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 768,
|
3 |
+
"unstable_bins": 7,
|
4 |
+
"reduction_bins": 668,
|
5 |
+
"band": {
|
6 |
+
"1": {
|
7 |
+
"sr": 11025,
|
8 |
+
"hl": 128,
|
9 |
+
"n_fft": 1024,
|
10 |
+
"crop_start": 0,
|
11 |
+
"crop_stop": 186,
|
12 |
+
"lpf_start": 37,
|
13 |
+
"lpf_stop": 73,
|
14 |
+
"res_type": "polyphase"
|
15 |
+
},
|
16 |
+
"2": {
|
17 |
+
"sr": 11025,
|
18 |
+
"hl": 128,
|
19 |
+
"n_fft": 512,
|
20 |
+
"crop_start": 4,
|
21 |
+
"crop_stop": 185,
|
22 |
+
"hpf_start": 36,
|
23 |
+
"hpf_stop": 18,
|
24 |
+
"lpf_start": 93,
|
25 |
+
"lpf_stop": 185,
|
26 |
+
"res_type": "polyphase"
|
27 |
+
},
|
28 |
+
"3": {
|
29 |
+
"sr": 22050,
|
30 |
+
"hl": 256,
|
31 |
+
"n_fft": 512,
|
32 |
+
"crop_start": 46,
|
33 |
+
"crop_stop": 186,
|
34 |
+
"hpf_start": 93,
|
35 |
+
"hpf_stop": 46,
|
36 |
+
"lpf_start": 164,
|
37 |
+
"lpf_stop": 186,
|
38 |
+
"res_type": "polyphase"
|
39 |
+
},
|
40 |
+
"4": {
|
41 |
+
"sr": 44100,
|
42 |
+
"hl": 512,
|
43 |
+
"n_fft": 768,
|
44 |
+
"crop_start": 121,
|
45 |
+
"crop_stop": 382,
|
46 |
+
"hpf_start": 138,
|
47 |
+
"hpf_stop": 123,
|
48 |
+
"res_type": "sinc_medium"
|
49 |
+
}
|
50 |
+
},
|
51 |
+
"sr": 44100,
|
52 |
+
"pre_filter_start": 740,
|
53 |
+
"pre_filter_stop": 768
|
54 |
+
}
|
uvr5_pack/lib_v5/modelparams/4band_44100_mid.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bins": 768,
|
3 |
+
"unstable_bins": 7,
|
4 |
+
"mid_side": true,
|
5 |
+
"reduction_bins": 668,
|
6 |
+
"band": {
|
7 |
+
"1": {
|
8 |
+
"sr": 11025,
|
9 |
+
"hl": 128,
|
10 |
+
"n_fft": 1024,
|
11 |
+
"crop_start": 0,
|
12 |
+
"crop_stop": 186,
|
13 |
+
"lpf_start": 37,
|
14 |
+
"lpf_stop": 73,
|
15 |
+
"res_type": "polyphase"
|
16 |
+
},
|
17 |
+
"2": {
|
18 |
+
"sr": 11025,
|
19 |
+
"hl": 128,
|
20 |
+
"n_fft": 512,
|
21 |
+
"crop_start": 4,
|
22 |
+
"crop_stop": 185,
|
23 |
+
"hpf_start": 36,
|
24 |
+
"hpf_stop": 18,
|
25 |
+
"lpf_start": 93,
|
26 |
+
"lpf_stop": 185,
|
27 |
+
"res_type": "polyphase"
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"sr": 22050,
|
31 |
+
"hl": 256,
|
32 |
+
"n_fft": 512,
|
33 |
+
"crop_start": 46,
|
34 |
+
"crop_stop": 186,
|
35 |
+
"hpf_start": 93,
|
36 |
+
"hpf_stop": 46,
|
37 |
+
"lpf_start": 164,
|
38 |
+
"lpf_stop": 186,
|
39 |
+
"res_type": "polyphase"
|
40 |
+
},
|
41 |
+
"4": {
|
42 |
+
"sr": 44100,
|
43 |
+
"hl": 512,
|
44 |
+
"n_fft": 768,
|
45 |
+
"crop_start": 121,
|
46 |
+
"crop_stop": 382,
|
47 |
+
"hpf_start": 138,
|
48 |
+
"hpf_stop": 123,
|
49 |
+
"res_type": "sinc_medium"
|
50 |
+
}
|
51 |
+
},
|
52 |
+
"sr": 44100,
|
53 |
+
"pre_filter_start": 740,
|
54 |
+
"pre_filter_stop": 768
|
55 |
+
}
|
uvr5_pack/lib_v5/modelparams/4band_44100_msb.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"mid_side_b": true,
|
3 |
+
"bins": 768,
|
4 |
+
"unstable_bins": 7,
|
5 |
+
"reduction_bins": 668,
|
6 |
+
"band": {
|
7 |
+
"1": {
|
8 |
+
"sr": 11025,
|
9 |
+
"hl": 128,
|
10 |
+
"n_fft": 1024,
|
11 |
+
"crop_start": 0,
|
12 |
+
"crop_stop": 186,
|
13 |
+
"lpf_start": 37,
|
14 |
+
"lpf_stop": 73,
|
15 |
+
"res_type": "polyphase"
|
16 |
+
},
|
17 |
+
"2": {
|
18 |
+
"sr": 11025,
|
19 |
+
"hl": 128,
|
20 |
+
"n_fft": 512,
|
21 |
+
"crop_start": 4,
|
22 |
+
"crop_stop": 185,
|
23 |
+
"hpf_start": 36,
|
24 |
+
"hpf_stop": 18,
|
25 |
+
"lpf_start": 93,
|
26 |
+
"lpf_stop": 185,
|
27 |
+
"res_type": "polyphase"
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"sr": 22050,
|
31 |
+
"hl": 256,
|
32 |
+
"n_fft": 512,
|
33 |
+
"crop_start": 46,
|
34 |
+
"crop_stop": 186,
|
35 |
+
"hpf_start": 93,
|
36 |
+
"hpf_stop": 46,
|
37 |
+
"lpf_start": 164,
|
38 |
+
"lpf_stop": 186,
|
39 |
+
"res_type": "polyphase"
|
40 |
+
},
|
41 |
+
"4": {
|
42 |
+
"sr": 44100,
|
43 |
+
"hl": 512,
|
44 |
+
"n_fft": 768,
|
45 |
+
"crop_start": 121,
|
46 |
+
"crop_stop": 382,
|
47 |
+
"hpf_start": 138,
|
48 |
+
"hpf_stop": 123,
|
49 |
+
"res_type": "sinc_medium"
|
50 |
+
}
|
51 |
+
},
|
52 |
+
"sr": 44100,
|
53 |
+
"pre_filter_start": 740,
|
54 |
+
"pre_filter_stop": 768
|
55 |
+
}
|
uvr5_pack/lib_v5/modelparams/4band_44100_msb2.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"mid_side_b": true,
|
3 |
+
"bins": 768,
|
4 |
+
"unstable_bins": 7,
|
5 |
+
"reduction_bins": 668,
|
6 |
+
"band": {
|
7 |
+
"1": {
|
8 |
+
"sr": 11025,
|
9 |
+
"hl": 128,
|
10 |
+
"n_fft": 1024,
|
11 |
+
"crop_start": 0,
|
12 |
+
"crop_stop": 186,
|
13 |
+
"lpf_start": 37,
|
14 |
+
"lpf_stop": 73,
|
15 |
+
"res_type": "polyphase"
|
16 |
+
},
|
17 |
+
"2": {
|
18 |
+
"sr": 11025,
|
19 |
+
"hl": 128,
|
20 |
+
"n_fft": 512,
|
21 |
+
"crop_start": 4,
|
22 |
+
"crop_stop": 185,
|
23 |
+
"hpf_start": 36,
|
24 |
+
"hpf_stop": 18,
|
25 |
+
"lpf_start": 93,
|
26 |
+
"lpf_stop": 185,
|
27 |
+
"res_type": "polyphase"
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"sr": 22050,
|
31 |
+
"hl": 256,
|
32 |
+
"n_fft": 512,
|
33 |
+
"crop_start": 46,
|
34 |
+
"crop_stop": 186,
|
35 |
+
"hpf_start": 93,
|
36 |
+
"hpf_stop": 46,
|
37 |
+
"lpf_start": 164,
|
38 |
+
"lpf_stop": 186,
|
39 |
+
"res_type": "polyphase"
|
40 |
+
},
|
41 |
+
"4": {
|
42 |
+
"sr": 44100,
|
43 |
+
"hl": 512,
|
44 |
+
"n_fft": 768,
|
45 |
+
"crop_start": 121,
|
46 |
+
"crop_stop": 382,
|
47 |
+
"hpf_start": 138,
|
48 |
+
"hpf_stop": 123,
|
49 |
+
"res_type": "sinc_medium"
|
50 |
+
}
|
51 |
+
},
|
52 |
+
"sr": 44100,
|
53 |
+
"pre_filter_start": 740,
|
54 |
+
"pre_filter_stop": 768
|
55 |
+
}
|
uvr5_pack/lib_v5/modelparams/4band_44100_reverse.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"reverse": true,
|
3 |
+
"bins": 768,
|
4 |
+
"unstable_bins": 7,
|
5 |
+
"reduction_bins": 668,
|
6 |
+
"band": {
|
7 |
+
"1": {
|
8 |
+
"sr": 11025,
|
9 |
+
"hl": 128,
|
10 |
+
"n_fft": 1024,
|
11 |
+
"crop_start": 0,
|
12 |
+
"crop_stop": 186,
|
13 |
+
"lpf_start": 37,
|
14 |
+
"lpf_stop": 73,
|
15 |
+
"res_type": "polyphase"
|
16 |
+
},
|
17 |
+
"2": {
|
18 |
+
"sr": 11025,
|
19 |
+
"hl": 128,
|
20 |
+
"n_fft": 512,
|
21 |
+
"crop_start": 4,
|
22 |
+
"crop_stop": 185,
|
23 |
+
"hpf_start": 36,
|
24 |
+
"hpf_stop": 18,
|
25 |
+
"lpf_start": 93,
|
26 |
+
"lpf_stop": 185,
|
27 |
+
"res_type": "polyphase"
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"sr": 22050,
|
31 |
+
"hl": 256,
|
32 |
+
"n_fft": 512,
|
33 |
+
"crop_start": 46,
|
34 |
+
"crop_stop": 186,
|
35 |
+
"hpf_start": 93,
|
36 |
+
"hpf_stop": 46,
|
37 |
+
"lpf_start": 164,
|
38 |
+
"lpf_stop": 186,
|
39 |
+
"res_type": "polyphase"
|
40 |
+
},
|
41 |
+
"4": {
|
42 |
+
"sr": 44100,
|
43 |
+
"hl": 512,
|
44 |
+
"n_fft": 768,
|
45 |
+
"crop_start": 121,
|
46 |
+
"crop_stop": 382,
|
47 |
+
"hpf_start": 138,
|
48 |
+
"hpf_stop": 123,
|
49 |
+
"res_type": "sinc_medium"
|
50 |
+
}
|
51 |
+
},
|
52 |
+
"sr": 44100,
|
53 |
+
"pre_filter_start": 740,
|
54 |
+
"pre_filter_stop": 768
|
55 |
+
}
|