Delete web.py
Browse files
web.py
DELETED
@@ -1,802 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
|
5 |
-
now_dir = os.getcwd()
|
6 |
-
sys.path.append(now_dir)
|
7 |
-
load_dotenv()
|
8 |
-
load_dotenv("sha256.env")
|
9 |
-
|
10 |
-
if sys.platform == "darwin":
|
11 |
-
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
12 |
-
|
13 |
-
from infer.modules.vc import VC, show_info, hash_similarity
|
14 |
-
from infer.modules.uvr5.modules import uvr
|
15 |
-
from infer.lib.train.process_ckpt import (
|
16 |
-
change_info,
|
17 |
-
extract_small_model,
|
18 |
-
merge,
|
19 |
-
)
|
20 |
-
from i18n.i18n import I18nAuto
|
21 |
-
from configs import Config
|
22 |
-
from sklearn.cluster import MiniBatchKMeans
|
23 |
-
import torch, platform
|
24 |
-
import numpy as np
|
25 |
-
import gradio as gr
|
26 |
-
import faiss
|
27 |
-
import pathlib
|
28 |
-
import json
|
29 |
-
from time import sleep
|
30 |
-
from subprocess import Popen
|
31 |
-
from random import shuffle
|
32 |
-
import warnings
|
33 |
-
import traceback
|
34 |
-
import threading
|
35 |
-
import shutil
|
36 |
-
import logging
|
37 |
-
|
38 |
-
|
39 |
-
logging.getLogger("numba").setLevel(logging.WARNING)
|
40 |
-
logging.getLogger("httpx").setLevel(logging.WARNING)
|
41 |
-
|
42 |
-
logger = logging.getLogger(__name__)
|
43 |
-
|
44 |
-
tmp = os.path.join(now_dir, "TEMP")
|
45 |
-
shutil.rmtree(tmp, ignore_errors=True)
|
46 |
-
os.makedirs(tmp, exist_ok=True)
|
47 |
-
os.makedirs(os.path.join(now_dir, "logs"), exist_ok=True)
|
48 |
-
os.makedirs(os.path.join(now_dir, "assets/weights"), exist_ok=True)
|
49 |
-
os.environ["TEMP"] = tmp
|
50 |
-
warnings.filterwarnings("ignore")
|
51 |
-
torch.manual_seed(114514)
|
52 |
-
|
53 |
-
|
54 |
-
config = Config()
|
55 |
-
vc = VC(config)
|
56 |
-
|
57 |
-
if not config.nocheck:
|
58 |
-
from infer.lib.rvcmd import check_all_assets, download_all_assets
|
59 |
-
|
60 |
-
if not check_all_assets(update=config.update):
|
61 |
-
if config.update:
|
62 |
-
download_all_assets(tmpdir=tmp)
|
63 |
-
if not check_all_assets(update=config.update):
|
64 |
-
logging.error("counld not satisfy all assets needed.")
|
65 |
-
exit(1)
|
66 |
-
|
67 |
-
if config.dml == True:
|
68 |
-
|
69 |
-
def forward_dml(ctx, x, scale):
|
70 |
-
ctx.scale = scale
|
71 |
-
res = x.clone().detach()
|
72 |
-
return res
|
73 |
-
|
74 |
-
import fairseq
|
75 |
-
|
76 |
-
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
|
77 |
-
|
78 |
-
i18n = I18nAuto()
|
79 |
-
logger.info(i18n)
|
80 |
-
# 判断是否有能用来训练和加速推理的N卡
|
81 |
-
ngpu = torch.cuda.device_count()
|
82 |
-
gpu_infos = []
|
83 |
-
mem = []
|
84 |
-
if_gpu_ok = False
|
85 |
-
|
86 |
-
if torch.cuda.is_available() or ngpu != 0:
|
87 |
-
for i in range(ngpu):
|
88 |
-
gpu_name = torch.cuda.get_device_name(i)
|
89 |
-
if any(
|
90 |
-
value in gpu_name.upper()
|
91 |
-
for value in [
|
92 |
-
"10",
|
93 |
-
"16",
|
94 |
-
"20",
|
95 |
-
"30",
|
96 |
-
"40",
|
97 |
-
"A2",
|
98 |
-
"A3",
|
99 |
-
"A4",
|
100 |
-
"P4",
|
101 |
-
"A50",
|
102 |
-
"500",
|
103 |
-
"A60",
|
104 |
-
"70",
|
105 |
-
"80",
|
106 |
-
"90",
|
107 |
-
"M4",
|
108 |
-
"T4",
|
109 |
-
"TITAN",
|
110 |
-
"4060",
|
111 |
-
"L",
|
112 |
-
"6000",
|
113 |
-
]
|
114 |
-
):
|
115 |
-
# A10#A100#V100#A40#P40#M40#K80#A4500
|
116 |
-
if_gpu_ok = True # 至少有一张能用的N卡
|
117 |
-
gpu_infos.append("%s\t%s" % (i, gpu_name))
|
118 |
-
mem.append(
|
119 |
-
int(
|
120 |
-
torch.cuda.get_device_properties(i).total_memory
|
121 |
-
/ 1024
|
122 |
-
/ 1024
|
123 |
-
/ 1024
|
124 |
-
+ 0.4
|
125 |
-
)
|
126 |
-
)
|
127 |
-
if if_gpu_ok and len(gpu_infos) > 0:
|
128 |
-
gpu_info = "\n".join(gpu_infos)
|
129 |
-
default_batch_size = min(mem) // 2
|
130 |
-
else:
|
131 |
-
gpu_info = i18n(
|
132 |
-
"Unfortunately, there is no compatible GPU available to support your training."
|
133 |
-
)
|
134 |
-
default_batch_size = 1
|
135 |
-
gpus = "-".join([i[0] for i in gpu_infos])
|
136 |
-
|
137 |
-
|
138 |
-
weight_root = os.getenv("weight_root")
|
139 |
-
weight_uvr5_root = os.getenv("weight_uvr5_root")
|
140 |
-
index_root = os.getenv("index_root")
|
141 |
-
outside_index_root = os.getenv("outside_index_root")
|
142 |
-
|
143 |
-
names = []
|
144 |
-
for name in os.listdir(weight_root):
|
145 |
-
if name.endswith(".pth"):
|
146 |
-
names.append(name)
|
147 |
-
index_paths = []
|
148 |
-
|
149 |
-
|
150 |
-
def lookup_indices(index_root):
|
151 |
-
global index_paths
|
152 |
-
for root, dirs, files in os.walk(index_root, topdown=False):
|
153 |
-
for name in files:
|
154 |
-
if name.endswith(".index") and "trained" not in name:
|
155 |
-
index_paths.append("%s/%s" % (root, name))
|
156 |
-
|
157 |
-
|
158 |
-
lookup_indices(index_root)
|
159 |
-
lookup_indices(outside_index_root)
|
160 |
-
uvr5_names = []
|
161 |
-
for name in os.listdir(weight_uvr5_root):
|
162 |
-
if name.endswith(".pth") or "onnx" in name:
|
163 |
-
uvr5_names.append(name.replace(".pth", ""))
|
164 |
-
|
165 |
-
|
166 |
-
def change_choices():
|
167 |
-
names = []
|
168 |
-
for name in os.listdir(weight_root):
|
169 |
-
if name.endswith(".pth"):
|
170 |
-
names.append(name)
|
171 |
-
index_paths = []
|
172 |
-
for root, dirs, files in os.walk(index_root, topdown=False):
|
173 |
-
for name in files:
|
174 |
-
if name.endswith(".index") and "trained" not in name:
|
175 |
-
index_paths.append("%s/%s" % (root, name))
|
176 |
-
return {"choices": sorted(names), "__type__": "update"}, {
|
177 |
-
"choices": sorted(index_paths),
|
178 |
-
"__type__": "update",
|
179 |
-
}
|
180 |
-
|
181 |
-
|
182 |
-
def clean():
|
183 |
-
return {"value": "", "__type__": "update"}
|
184 |
-
|
185 |
-
|
186 |
-
def export_onnx(ModelPath, ExportedPath):
|
187 |
-
from rvc.onnx import export_onnx as eo
|
188 |
-
|
189 |
-
eo(ModelPath, ExportedPath)
|
190 |
-
|
191 |
-
|
192 |
-
sr_dict = {
|
193 |
-
"32k": 32000,
|
194 |
-
"40k": 40000,
|
195 |
-
"48k": 48000,
|
196 |
-
}
|
197 |
-
|
198 |
-
|
199 |
-
def if_done(done, p):
|
200 |
-
while 1:
|
201 |
-
if p.poll() is None:
|
202 |
-
sleep(0.5)
|
203 |
-
else:
|
204 |
-
break
|
205 |
-
done[0] = True
|
206 |
-
|
207 |
-
|
208 |
-
def if_done_multi(done, ps):
|
209 |
-
while 1:
|
210 |
-
# poll==None代表进程未结束
|
211 |
-
# 只要有一个进程未结束都不停
|
212 |
-
flag = 1
|
213 |
-
for p in ps:
|
214 |
-
if p.poll() is None:
|
215 |
-
flag = 0
|
216 |
-
sleep(0.5)
|
217 |
-
break
|
218 |
-
if flag == 1:
|
219 |
-
break
|
220 |
-
done[0] = True
|
221 |
-
|
222 |
-
|
223 |
-
def preprocess_dataset(trainset_dir, exp_dir, sr, n_p):
|
224 |
-
sr = sr_dict[sr]
|
225 |
-
os.makedirs("%s/logs/%s" % (now_dir, exp_dir), exist_ok=True)
|
226 |
-
f = open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "w")
|
227 |
-
f.close()
|
228 |
-
cmd = '"%s" infer/modules/train/preprocess.py "%s" %s %s "%s/logs/%s" %s %.1f' % (
|
229 |
-
config.python_cmd,
|
230 |
-
trainset_dir,
|
231 |
-
sr,
|
232 |
-
n_p,
|
233 |
-
now_dir,
|
234 |
-
exp_dir,
|
235 |
-
config.noparallel,
|
236 |
-
config.preprocess_per,
|
237 |
-
)
|
238 |
-
logger.info("Execute: " + cmd)
|
239 |
-
# , stdin=PIPE, stdout=PIPE,stderr=PIPE,cwd=now_dir
|
240 |
-
p = Popen(cmd, shell=True)
|
241 |
-
# 煞笔gr, popen read都非得全跑完了再一次性读取, 不用gr就正常读一句输出一句;只能额外弄出一个文本流定时读
|
242 |
-
done = [False]
|
243 |
-
threading.Thread(
|
244 |
-
target=if_done,
|
245 |
-
args=(
|
246 |
-
done,
|
247 |
-
p,
|
248 |
-
),
|
249 |
-
).start()
|
250 |
-
while 1:
|
251 |
-
with open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "r") as f:
|
252 |
-
yield (f.read())
|
253 |
-
sleep(1)
|
254 |
-
if done[0]:
|
255 |
-
break
|
256 |
-
with open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "r") as f:
|
257 |
-
log = f.read()
|
258 |
-
logger.info(log)
|
259 |
-
yield log
|
260 |
-
|
261 |
-
|
262 |
-
# but2.click(extract_f0,[gpus6,np7,f0method8,if_f0_3,trainset_dir4],[info2])
|
263 |
-
def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvpe):
|
264 |
-
gpus = gpus.split("-")
|
265 |
-
os.makedirs("%s/logs/%s" % (now_dir, exp_dir), exist_ok=True)
|
266 |
-
f = open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "w")
|
267 |
-
f.close()
|
268 |
-
if if_f0:
|
269 |
-
if f0method != "rmvpe_gpu":
|
270 |
-
cmd = (
|
271 |
-
'"%s" infer/modules/train/extract/extract_f0_print.py "%s/logs/%s" %s %s'
|
272 |
-
% (
|
273 |
-
config.python_cmd,
|
274 |
-
now_dir,
|
275 |
-
exp_dir,
|
276 |
-
n_p,
|
277 |
-
f0method,
|
278 |
-
)
|
279 |
-
)
|
280 |
-
logger.info("Execute: " + cmd)
|
281 |
-
p = Popen(
|
282 |
-
cmd, shell=True, cwd=now_dir
|
283 |
-
) # , stdin=PIPE, stdout=PIPE,stderr=PIPE
|
284 |
-
# 煞笔gr, popen read都非得全跑完了再一次性读取, 不用gr就正常读一句输出一句;只能额外弄出一个文本流定时读
|
285 |
-
done = [False]
|
286 |
-
threading.Thread(
|
287 |
-
target=if_done,
|
288 |
-
args=(
|
289 |
-
done,
|
290 |
-
p,
|
291 |
-
),
|
292 |
-
).start()
|
293 |
-
else:
|
294 |
-
if gpus_rmvpe != "-":
|
295 |
-
gpus_rmvpe = gpus_rmvpe.split("-")
|
296 |
-
leng = len(gpus_rmvpe)
|
297 |
-
ps = []
|
298 |
-
for idx, n_g in enumerate(gpus_rmvpe):
|
299 |
-
cmd = (
|
300 |
-
'"%s" infer/modules/train/extract/extract_f0_rmvpe.py %s %s %s "%s/logs/%s" %s '
|
301 |
-
% (
|
302 |
-
config.python_cmd,
|
303 |
-
leng,
|
304 |
-
idx,
|
305 |
-
n_g,
|
306 |
-
now_dir,
|
307 |
-
exp_dir,
|
308 |
-
config.is_half,
|
309 |
-
)
|
310 |
-
)
|
311 |
-
logger.info("Execute: " + cmd)
|
312 |
-
p = Popen(
|
313 |
-
cmd, shell=True, cwd=now_dir
|
314 |
-
) # , shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=now_dir
|
315 |
-
ps.append(p)
|
316 |
-
# 煞笔gr, popen read都非得全跑完了再一次性读取, 不用gr就正常读一句输出一句;只能额外弄出一个文本流定时读
|
317 |
-
done = [False]
|
318 |
-
threading.Thread(
|
319 |
-
target=if_done_multi, #
|
320 |
-
args=(
|
321 |
-
done,
|
322 |
-
ps,
|
323 |
-
),
|
324 |
-
).start()
|
325 |
-
else:
|
326 |
-
cmd = (
|
327 |
-
config.python_cmd
|
328 |
-
+ ' infer/modules/train/extract/extract_f0_rmvpe_dml.py "%s/logs/%s" '
|
329 |
-
% (
|
330 |
-
now_dir,
|
331 |
-
exp_dir,
|
332 |
-
)
|
333 |
-
)
|
334 |
-
logger.info("Execute: " + cmd)
|
335 |
-
p = Popen(
|
336 |
-
cmd, shell=True, cwd=now_dir
|
337 |
-
) # , shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=now_dir
|
338 |
-
p.wait()
|
339 |
-
done = [True]
|
340 |
-
while 1:
|
341 |
-
with open(
|
342 |
-
"%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r"
|
343 |
-
) as f:
|
344 |
-
yield (f.read())
|
345 |
-
sleep(1)
|
346 |
-
if done[0]:
|
347 |
-
break
|
348 |
-
with open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r") as f:
|
349 |
-
log = f.read()
|
350 |
-
logger.info(log)
|
351 |
-
yield log
|
352 |
-
# 对不同part分别开多进程
|
353 |
-
"""
|
354 |
-
n_part=int(sys.argv[1])
|
355 |
-
i_part=int(sys.argv[2])
|
356 |
-
i_gpu=sys.argv[3]
|
357 |
-
exp_dir=sys.argv[4]
|
358 |
-
os.environ["CUDA_VISIBLE_DEVICES"]=str(i_gpu)
|
359 |
-
"""
|
360 |
-
leng = len(gpus)
|
361 |
-
ps = []
|
362 |
-
for idx, n_g in enumerate(gpus):
|
363 |
-
cmd = (
|
364 |
-
'"%s" infer/modules/train/extract_feature_print.py %s %s %s %s "%s/logs/%s" %s %s'
|
365 |
-
% (
|
366 |
-
config.python_cmd,
|
367 |
-
config.device,
|
368 |
-
leng,
|
369 |
-
idx,
|
370 |
-
n_g,
|
371 |
-
now_dir,
|
372 |
-
exp_dir,
|
373 |
-
version19,
|
374 |
-
config.is_half,
|
375 |
-
)
|
376 |
-
)
|
377 |
-
logger.info("Execute: " + cmd)
|
378 |
-
p = Popen(
|
379 |
-
cmd, shell=True, cwd=now_dir
|
380 |
-
) # , shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=now_dir
|
381 |
-
ps.append(p)
|
382 |
-
# 煞笔gr, popen read都非得全跑完了再一次性读取, 不用gr就正常读一句输出一句;只能额外弄出一个文本流定时读
|
383 |
-
done = [False]
|
384 |
-
threading.Thread(
|
385 |
-
target=if_done_multi,
|
386 |
-
args=(
|
387 |
-
done,
|
388 |
-
ps,
|
389 |
-
),
|
390 |
-
).start()
|
391 |
-
while 1:
|
392 |
-
with open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r") as f:
|
393 |
-
yield (f.read())
|
394 |
-
sleep(1)
|
395 |
-
if done[0]:
|
396 |
-
break
|
397 |
-
with open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r") as f:
|
398 |
-
log = f.read()
|
399 |
-
logger.info(log)
|
400 |
-
yield log
|
401 |
-
|
402 |
-
|
403 |
-
def get_pretrained_models(path_str, f0_str, sr2):
|
404 |
-
if_pretrained_generator_exist = os.access(
|
405 |
-
"assets/pretrained%s/%sG%s.pth" % (path_str, f0_str, sr2), os.F_OK
|
406 |
-
)
|
407 |
-
if_pretrained_discriminator_exist = os.access(
|
408 |
-
"assets/pretrained%s/%sD%s.pth" % (path_str, f0_str, sr2), os.F_OK
|
409 |
-
)
|
410 |
-
if not if_pretrained_generator_exist:
|
411 |
-
logger.warning(
|
412 |
-
"assets/pretrained%s/%sG%s.pth not exist, will not use pretrained model",
|
413 |
-
path_str,
|
414 |
-
f0_str,
|
415 |
-
sr2,
|
416 |
-
)
|
417 |
-
if not if_pretrained_discriminator_exist:
|
418 |
-
logger.warning(
|
419 |
-
"assets/pretrained%s/%sD%s.pth not exist, will not use pretrained model",
|
420 |
-
path_str,
|
421 |
-
f0_str,
|
422 |
-
sr2,
|
423 |
-
)
|
424 |
-
return (
|
425 |
-
(
|
426 |
-
"assets/pretrained%s/%sG%s.pth" % (path_str, f0_str, sr2)
|
427 |
-
if if_pretrained_generator_exist
|
428 |
-
else ""
|
429 |
-
),
|
430 |
-
(
|
431 |
-
"assets/pretrained%s/%sD%s.pth" % (path_str, f0_str, sr2)
|
432 |
-
if if_pretrained_discriminator_exist
|
433 |
-
else ""
|
434 |
-
),
|
435 |
-
)
|
436 |
-
|
437 |
-
|
438 |
-
def change_sr2(sr2, if_f0_3, version19):
|
439 |
-
path_str = "" if version19 == "v1" else "_v2"
|
440 |
-
f0_str = "f0" if if_f0_3 else ""
|
441 |
-
return get_pretrained_models(path_str, f0_str, sr2)
|
442 |
-
|
443 |
-
|
444 |
-
def change_version19(sr2, if_f0_3, version19):
|
445 |
-
path_str = "" if version19 == "v1" else "_v2"
|
446 |
-
if sr2 == "32k" and version19 == "v1":
|
447 |
-
sr2 = "40k"
|
448 |
-
to_return_sr2 = (
|
449 |
-
{"choices": ["40k", "48k"], "__type__": "update", "value": sr2}
|
450 |
-
if version19 == "v1"
|
451 |
-
else {"choices": ["40k", "48k", "32k"], "__type__": "update", "value": sr2}
|
452 |
-
)
|
453 |
-
f0_str = "f0" if if_f0_3 else ""
|
454 |
-
return (
|
455 |
-
*get_pretrained_models(path_str, f0_str, sr2),
|
456 |
-
to_return_sr2,
|
457 |
-
)
|
458 |
-
|
459 |
-
|
460 |
-
def change_f0(if_f0_3, sr2, version19): # f0method8,pretrained_G14,pretrained_D15
|
461 |
-
path_str = "" if version19 == "v1" else "_v2"
|
462 |
-
return (
|
463 |
-
{"visible": if_f0_3, "__type__": "update"},
|
464 |
-
{"visible": if_f0_3, "__type__": "update"},
|
465 |
-
*get_pretrained_models(path_str, "f0" if if_f0_3 == True else "", sr2),
|
466 |
-
)
|
467 |
-
|
468 |
-
|
469 |
-
# but3.click(click_train,[exp_dir1,sr2,if_f0_3,save_epoch10,total_epoch11,batch_size12,if_save_latest13,pretrained_G14,pretrained_D15,gpus16])
|
470 |
-
def click_train(
|
471 |
-
exp_dir1,
|
472 |
-
sr2,
|
473 |
-
if_f0_3,
|
474 |
-
spk_id5,
|
475 |
-
save_epoch10,
|
476 |
-
total_epoch11,
|
477 |
-
batch_size12,
|
478 |
-
if_save_latest13,
|
479 |
-
pretrained_G14,
|
480 |
-
pretrained_D15,
|
481 |
-
gpus16,
|
482 |
-
if_cache_gpu17,
|
483 |
-
if_save_every_weights18,
|
484 |
-
version19,
|
485 |
-
author,
|
486 |
-
):
|
487 |
-
# 生成filelist
|
488 |
-
exp_dir = "%s/logs/%s" % (now_dir, exp_dir1)
|
489 |
-
os.makedirs(exp_dir, exist_ok=True)
|
490 |
-
gt_wavs_dir = "%s/0_gt_wavs" % (exp_dir)
|
491 |
-
feature_dir = (
|
492 |
-
"%s/3_feature256" % (exp_dir)
|
493 |
-
if version19 == "v1"
|
494 |
-
else "%s/3_feature768" % (exp_dir)
|
495 |
-
)
|
496 |
-
if if_f0_3:
|
497 |
-
f0_dir = "%s/2a_f0" % (exp_dir)
|
498 |
-
f0nsf_dir = "%s/2b-f0nsf" % (exp_dir)
|
499 |
-
names = (
|
500 |
-
set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)])
|
501 |
-
& set([name.split(".")[0] for name in os.listdir(feature_dir)])
|
502 |
-
& set([name.split(".")[0] for name in os.listdir(f0_dir)])
|
503 |
-
& set([name.split(".")[0] for name in os.listdir(f0nsf_dir)])
|
504 |
-
)
|
505 |
-
else:
|
506 |
-
names = set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)]) & set(
|
507 |
-
[name.split(".")[0] for name in os.listdir(feature_dir)]
|
508 |
-
)
|
509 |
-
opt = []
|
510 |
-
for name in names:
|
511 |
-
if if_f0_3:
|
512 |
-
opt.append(
|
513 |
-
"%s/%s.wav|%s/%s.npy|%s/%s.wav.npy|%s/%s.wav.npy|%s"
|
514 |
-
% (
|
515 |
-
gt_wavs_dir.replace("\\", "\\\\"),
|
516 |
-
name,
|
517 |
-
feature_dir.replace("\\", "\\\\"),
|
518 |
-
name,
|
519 |
-
f0_dir.replace("\\", "\\\\"),
|
520 |
-
name,
|
521 |
-
f0nsf_dir.replace("\\", "\\\\"),
|
522 |
-
name,
|
523 |
-
spk_id5,
|
524 |
-
)
|
525 |
-
)
|
526 |
-
else:
|
527 |
-
opt.append(
|
528 |
-
"%s/%s.wav|%s/%s.npy|%s"
|
529 |
-
% (
|
530 |
-
gt_wavs_dir.replace("\\", "\\\\"),
|
531 |
-
name,
|
532 |
-
feature_dir.replace("\\", "\\\\"),
|
533 |
-
name,
|
534 |
-
spk_id5,
|
535 |
-
)
|
536 |
-
)
|
537 |
-
fea_dim = 256 if version19 == "v1" else 768
|
538 |
-
if if_f0_3:
|
539 |
-
for _ in range(2):
|
540 |
-
opt.append(
|
541 |
-
"%s/logs/mute/0_gt_wavs/mute%s.wav|%s/logs/mute/3_feature%s/mute.npy|%s/logs/mute/2a_f0/mute.wav.npy|%s/logs/mute/2b-f0nsf/mute.wav.npy|%s"
|
542 |
-
% (now_dir, sr2, now_dir, fea_dim, now_dir, now_dir, spk_id5)
|
543 |
-
)
|
544 |
-
else:
|
545 |
-
for _ in range(2):
|
546 |
-
opt.append(
|
547 |
-
"%s/logs/mute/0_gt_wavs/mute%s.wav|%s/logs/mute/3_feature%s/mute.npy|%s"
|
548 |
-
% (now_dir, sr2, now_dir, fea_dim, spk_id5)
|
549 |
-
)
|
550 |
-
shuffle(opt)
|
551 |
-
with open("%s/filelist.txt" % exp_dir, "w") as f:
|
552 |
-
f.write("\n".join(opt))
|
553 |
-
logger.debug("Write filelist done")
|
554 |
-
logger.info("Use gpus: %s", str(gpus16))
|
555 |
-
if pretrained_G14 == "":
|
556 |
-
logger.info("No pretrained Generator")
|
557 |
-
if pretrained_D15 == "":
|
558 |
-
logger.info("No pretrained Discriminator")
|
559 |
-
if version19 == "v1" or sr2 == "40k":
|
560 |
-
config_path = "v1/%s.json" % sr2
|
561 |
-
else:
|
562 |
-
config_path = "v2/%s.json" % sr2
|
563 |
-
config_save_path = os.path.join(exp_dir, "config.json")
|
564 |
-
if not pathlib.Path(config_save_path).exists():
|
565 |
-
with open(config_save_path, "w", encoding="utf-8") as f:
|
566 |
-
json.dump(
|
567 |
-
config.json_config[config_path],
|
568 |
-
f,
|
569 |
-
ensure_ascii=False,
|
570 |
-
indent=4,
|
571 |
-
sort_keys=True,
|
572 |
-
)
|
573 |
-
f.write("\n")
|
574 |
-
cmd = (
|
575 |
-
'"%s" infer/modules/train/train.py -e "%s" -sr %s -f0 %s -bs %s -te %s -se %s %s %s -l %s -c %s -sw %s -v %s -a "%s"'
|
576 |
-
% (
|
577 |
-
config.python_cmd,
|
578 |
-
exp_dir1,
|
579 |
-
sr2,
|
580 |
-
1 if if_f0_3 else 0,
|
581 |
-
batch_size12,
|
582 |
-
total_epoch11,
|
583 |
-
save_epoch10,
|
584 |
-
'-pg "%s"' % pretrained_G14 if pretrained_G14 != "" else "",
|
585 |
-
'-pd "%s"' % pretrained_D15 if pretrained_D15 != "" else "",
|
586 |
-
1 if if_save_latest13 == i18n("Yes") else 0,
|
587 |
-
1 if if_cache_gpu17 == i18n("Yes") else 0,
|
588 |
-
1 if if_save_every_weights18 == i18n("Yes") else 0,
|
589 |
-
version19,
|
590 |
-
author,
|
591 |
-
)
|
592 |
-
)
|
593 |
-
if gpus16:
|
594 |
-
cmd += ' -g "%s"' % (gpus16)
|
595 |
-
|
596 |
-
logger.info("Execute: " + cmd)
|
597 |
-
p = Popen(cmd, shell=True, cwd=now_dir)
|
598 |
-
p.wait()
|
599 |
-
return "Training complete. You can check the training logs in the console or the 'train.log' file under the experiment folder."
|
600 |
-
|
601 |
-
|
602 |
-
# but4.click(train_index, [exp_dir1], info3)
|
603 |
-
def train_index(exp_dir1, version19):
|
604 |
-
# exp_dir = "%s/logs/%s" % (now_dir, exp_dir1)
|
605 |
-
exp_dir = "logs/%s" % (exp_dir1)
|
606 |
-
os.makedirs(exp_dir, exist_ok=True)
|
607 |
-
feature_dir = (
|
608 |
-
"%s/3_feature256" % (exp_dir)
|
609 |
-
if version19 == "v1"
|
610 |
-
else "%s/3_feature768" % (exp_dir)
|
611 |
-
)
|
612 |
-
if not os.path.exists(feature_dir):
|
613 |
-
return "请先进行特征提取!"
|
614 |
-
listdir_res = list(os.listdir(feature_dir))
|
615 |
-
if len(listdir_res) == 0:
|
616 |
-
return "请先进行特征提取!"
|
617 |
-
infos = []
|
618 |
-
npys = []
|
619 |
-
for name in sorted(listdir_res):
|
620 |
-
phone = np.load("%s/%s" % (feature_dir, name))
|
621 |
-
npys.append(phone)
|
622 |
-
big_npy = np.concatenate(npys, 0)
|
623 |
-
big_npy_idx = np.arange(big_npy.shape[0])
|
624 |
-
np.random.shuffle(big_npy_idx)
|
625 |
-
big_npy = big_npy[big_npy_idx]
|
626 |
-
if big_npy.shape[0] > 2e5:
|
627 |
-
infos.append("Trying doing kmeans %s shape to 10k centers." % big_npy.shape[0])
|
628 |
-
yield "\n".join(infos)
|
629 |
-
try:
|
630 |
-
big_npy = (
|
631 |
-
MiniBatchKMeans(
|
632 |
-
n_clusters=10000,
|
633 |
-
verbose=True,
|
634 |
-
batch_size=256 * config.n_cpu,
|
635 |
-
compute_labels=False,
|
636 |
-
init="random",
|
637 |
-
)
|
638 |
-
.fit(big_npy)
|
639 |
-
.cluster_centers_
|
640 |
-
)
|
641 |
-
except:
|
642 |
-
info = traceback.format_exc()
|
643 |
-
logger.info(info)
|
644 |
-
infos.append(info)
|
645 |
-
yield "\n".join(infos)
|
646 |
-
|
647 |
-
np.save("%s/total_fea.npy" % exp_dir, big_npy)
|
648 |
-
n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
|
649 |
-
infos.append("%s,%s" % (big_npy.shape, n_ivf))
|
650 |
-
yield "\n".join(infos)
|
651 |
-
index = faiss.index_factory(256 if version19 == "v1" else 768, "IVF%s,Flat" % n_ivf)
|
652 |
-
# index = faiss.index_factory(256if version19=="v1"else 768, "IVF%s,PQ128x4fs,RFlat"%n_ivf)
|
653 |
-
infos.append("training")
|
654 |
-
yield "\n".join(infos)
|
655 |
-
index_ivf = faiss.extract_index_ivf(index) #
|
656 |
-
index_ivf.nprobe = 1
|
657 |
-
index.train(big_npy)
|
658 |
-
faiss.write_index(
|
659 |
-
index,
|
660 |
-
"%s/trained_IVF%s_Flat_nprobe_%s_%s_%s.index"
|
661 |
-
% (exp_dir, n_ivf, index_ivf.nprobe, exp_dir1, version19),
|
662 |
-
)
|
663 |
-
infos.append("adding")
|
664 |
-
yield "\n".join(infos)
|
665 |
-
batch_size_add = 8192
|
666 |
-
for i in range(0, big_npy.shape[0], batch_size_add):
|
667 |
-
index.add(big_npy[i : i + batch_size_add])
|
668 |
-
index_save_path = "%s/added_IVF%s_Flat_nprobe_%s_%s_%s.index" % (
|
669 |
-
exp_dir,
|
670 |
-
n_ivf,
|
671 |
-
index_ivf.nprobe,
|
672 |
-
exp_dir1,
|
673 |
-
version19,
|
674 |
-
)
|
675 |
-
faiss.write_index(index, index_save_path)
|
676 |
-
infos.append(i18n("Successfully built index into") + " " + index_save_path)
|
677 |
-
link_target = "%s/%s_IVF%s_Flat_nprobe_%s_%s_%s.index" % (
|
678 |
-
outside_index_root,
|
679 |
-
exp_dir1,
|
680 |
-
n_ivf,
|
681 |
-
index_ivf.nprobe,
|
682 |
-
exp_dir1,
|
683 |
-
version19,
|
684 |
-
)
|
685 |
-
try:
|
686 |
-
link = os.link if platform.system() == "Windows" else os.symlink
|
687 |
-
link(index_save_path, link_target)
|
688 |
-
infos.append(i18n("Link index to outside folder") + " " + link_target)
|
689 |
-
except:
|
690 |
-
infos.append(
|
691 |
-
i18n("Link index to outside folder")
|
692 |
-
+ " "
|
693 |
-
+ link_target
|
694 |
-
+ " "
|
695 |
-
+ i18n("Fail")
|
696 |
-
)
|
697 |
-
|
698 |
-
# faiss.write_index(index, '%s/added_IVF%s_Flat_FastScan_%s.index'%(exp_dir,n_ivf,version19))
|
699 |
-
# infos.append("成功构建索引,added_IVF%s_Flat_FastScan_%s.index"%(n_ivf,version19))
|
700 |
-
yield "\n".join(infos)
|
701 |
-
|
702 |
-
|
703 |
-
# but5.click(train1key, [exp_dir1, sr2, if_f0_3, trainset_dir4, spk_id5, gpus6, np7, f0method8, save_epoch10, total_epoch11, batch_size12, if_save_latest13, pretrained_G14, pretrained_D15, gpus16, if_cache_gpu17], info3)
|
704 |
-
def train1key(
|
705 |
-
exp_dir1,
|
706 |
-
sr2,
|
707 |
-
if_f0_3,
|
708 |
-
trainset_dir4,
|
709 |
-
spk_id5,
|
710 |
-
np7,
|
711 |
-
f0method8,
|
712 |
-
save_epoch10,
|
713 |
-
total_epoch11,
|
714 |
-
batch_size12,
|
715 |
-
if_save_latest13,
|
716 |
-
pretrained_G14,
|
717 |
-
pretrained_D15,
|
718 |
-
gpus16,
|
719 |
-
if_cache_gpu17,
|
720 |
-
if_save_every_weights18,
|
721 |
-
version19,
|
722 |
-
gpus_rmvpe,
|
723 |
-
author,
|
724 |
-
):
|
725 |
-
infos = []
|
726 |
-
|
727 |
-
def get_info_str(strr):
|
728 |
-
infos.append(strr)
|
729 |
-
return "\n".join(infos)
|
730 |
-
|
731 |
-
# step1:Process data
|
732 |
-
yield get_info_str(i18n("Step 1: Processing data"))
|
733 |
-
[get_info_str(_) for _ in preprocess_dataset(trainset_dir4, exp_dir1, sr2, np7)]
|
734 |
-
|
735 |
-
# step2a:提取音高
|
736 |
-
yield get_info_str(i18n("step2:Pitch extraction & feature extraction"))
|
737 |
-
[
|
738 |
-
get_info_str(_)
|
739 |
-
for _ in extract_f0_feature(
|
740 |
-
gpus16, np7, f0method8, if_f0_3, exp_dir1, version19, gpus_rmvpe
|
741 |
-
)
|
742 |
-
]
|
743 |
-
|
744 |
-
# step3a:Train model
|
745 |
-
yield get_info_str(i18n("Step 3a: Model training started"))
|
746 |
-
click_train(
|
747 |
-
exp_dir1,
|
748 |
-
sr2,
|
749 |
-
if_f0_3,
|
750 |
-
spk_id5,
|
751 |
-
save_epoch10,
|
752 |
-
total_epoch11,
|
753 |
-
batch_size12,
|
754 |
-
if_save_latest13,
|
755 |
-
pretrained_G14,
|
756 |
-
pretrained_D15,
|
757 |
-
gpus16,
|
758 |
-
if_cache_gpu17,
|
759 |
-
if_save_every_weights18,
|
760 |
-
version19,
|
761 |
-
author,
|
762 |
-
)
|
763 |
-
yield get_info_str(
|
764 |
-
i18n(
|
765 |
-
"Training complete. You can check the training logs in the console or the 'train.log' file under the experiment folder."
|
766 |
-
)
|
767 |
-
)
|
768 |
-
|
769 |
-
# step3b:训练索引
|
770 |
-
[get_info_str(_) for _ in train_index(exp_dir1, version19)]
|
771 |
-
yield get_info_str(i18n("All processes have been completed!"))
|
772 |
-
|
773 |
-
|
774 |
-
# ckpt_path2.change(change_info_,[ckpt_path2],[sr__,if_f0__])
|
775 |
-
def change_info_(ckpt_path):
|
776 |
-
if not os.path.exists(ckpt_path.replace(os.path.basename(ckpt_path), "train.log")):
|
777 |
-
return {"__type__": "update"}, {"__type__": "update"}, {"__type__": "update"}
|
778 |
-
try:
|
779 |
-
with open(
|
780 |
-
ckpt_path.replace(os.path.basename(ckpt_path), "train.log"), "r"
|
781 |
-
) as f:
|
782 |
-
info = eval(f.read().strip("\n").split("\n")[0].split("\t")[-1])
|
783 |
-
sr, f0 = info["sample_rate"], info["if_f0"]
|
784 |
-
version = "v2" if ("version" in info and info["version"] == "v2") else "v1"
|
785 |
-
return sr, str(f0), version
|
786 |
-
except:
|
787 |
-
traceback.print_exc()
|
788 |
-
return {"__type__": "update"}, {"__type__": "update"}, {"__type__": "update"}
|
789 |
-
|
790 |
-
|
791 |
-
F0GPUVisible = config.dml == False
|
792 |
-
|
793 |
-
|
794 |
-
def change_f0_method(f0method8):
|
795 |
-
if f0method8 == "rmvpe_gpu":
|
796 |
-
visible = F0GPUVisible
|
797 |
-
else:
|
798 |
-
visible = False
|
799 |
-
return {"visible": visible, "__type__": "update"}
|
800 |
-
|
801 |
-
|
802 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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