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