1
0
mirror of synced 2025-01-31 20:05:20 +01:00

Config class (#192)

* update config.py

* class

* class

* fix
This commit is contained in:
Ftps 2023-04-28 21:43:02 +09:00 committed by GitHub
parent b1134d9f64
commit f391ac1763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 151 additions and 165 deletions

154
config.py
View File

@ -1,111 +1,92 @@
########################硬件参数########################
# 填写cuda:x, cpu 或 mps, x指代第几张卡只支持 N卡 / Apple Silicon 加速
device = "cuda:0"
# 9-10-20-30-40系显卡无脑True不影响质量>=20显卡开启有加速
is_half = True
# 默认0用上所有线程写数字限制CPU资源使用
n_cpu = 0
########################硬件参数########################
##################下为参数处理逻辑,勿动##################
########################命令行参数########################
import argparse import argparse
import glob
import sys
import torch
from multiprocessing import cpu_count
class Config:
def __init__(self):
self.device = "cuda:0"
self.is_half = True
self.n_cpu = 0
self.gpu_name = None
self.gpu_mem = None
(
self.python_cmd,
self.listen_port,
self.iscolab,
self.noparallel,
self.noautoopen,
) = self.arg_parse()
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
def arg_parse(self) -> tuple:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=7865, help="Listen port") parser.add_argument("--port", type=int, default=7865, help="Listen port")
parser.add_argument("--pycmd", type=str, default="python", help="Python command") parser.add_argument(
"--pycmd", type=str, default="python", help="Python command"
)
parser.add_argument("--colab", action="store_true", help="Launch in colab") parser.add_argument("--colab", action="store_true", help="Launch in colab")
parser.add_argument( parser.add_argument(
"--noparallel", action="store_true", help="Disable parallel processing" "--noparallel", action="store_true", help="Disable parallel processing"
) )
parser.add_argument( parser.add_argument(
"--noautoopen", action="store_true", help="Do not open in browser automatically" "--noautoopen",
action="store_true",
help="Do not open in browser automatically",
) )
cmd_opts = parser.parse_args() cmd_opts = parser.parse_args()
python_cmd = cmd_opts.pycmd cmd_opts.port = cmd_opts.port if 0 <= cmd_opts.port <= 65535 else 7865
listen_port = cmd_opts.port if 0 <= cmd_opts.port <= 65535 else 7865
iscolab = cmd_opts.colab
noparallel = cmd_opts.noparallel
noautoopen = cmd_opts.noautoopen
########################命令行参数########################
import sys return (
import torch cmd_opts.pycmd,
cmd_opts.port,
cmd_opts.colab,
cmd_opts.noparallel,
cmd_opts.noautoopen,
)
def device_config(self) -> tuple:
# has_mps is only available in nightly pytorch (for now) and MasOS 12.3+. if torch.cuda.is_available():
# check `getattr` and try it for compatibility self.gpu_name = torch.cuda.get_device_name(int(self.device.split(":")[-1]))
def has_mps() -> bool: i_device = int(self.device.split(":")[-1])
if sys.platform != "darwin": self.gpu_name = torch.cuda.get_device_name(i_device)
return False
else:
if not getattr(torch, "has_mps", False):
return False
try:
torch.zeros(1).to(torch.device("mps"))
return True
except Exception:
return False
if not torch.cuda.is_available():
if has_mps():
print("没有发现支持的N卡, 使用MPS进行推理")
device = "mps"
else:
print("没有发现支持的N卡, 使用CPU进行推理")
device = "cpu"
is_half = False
gpu_mem = None
if device not in ["cpu", "mps"]:
i_device = int(device.split(":")[-1])
gpu_name = torch.cuda.get_device_name(i_device)
if ( if (
"16" in gpu_name "16" in self.gpu_name
or "P40" in gpu_name.upper() or "P40" in self.gpu_name.upper()
or "1070" in gpu_name or "1070" in self.gpu_name
or "1080" in gpu_name or "1080" in self.gpu_name
): ):
print("16系显卡强制单精度") print("16系显卡强制单精度")
is_half = False self.is_half = False
with open("configs/32k.json", "r") as f: for config_file in ["32k.json", "40k.json", "48k.json"]:
with open(f"configs/{config_file}", "a") as f:
strr = f.read().replace("true", "false") strr = f.read().replace("true", "false")
with open("configs/32k.json", "w") as f:
f.write(strr) f.write(strr)
with open("configs/40k.json", "r") as f: self.gpu_mem = int(
strr = f.read().replace("true", "false") torch.cuda.get_device_properties(i_device).total_memory
with open("configs/40k.json", "w") as f: / 1024
f.write(strr) / 1024
with open("configs/48k.json", "r") as f: / 1024
strr = f.read().replace("true", "false")
with open("configs/48k.json", "w") as f:
f.write(strr)
with open("trainset_preprocess_pipeline_print.py", "r") as f:
strr = f.read().replace("3.7", "3.0")
with open("trainset_preprocess_pipeline_print.py", "w") as f:
f.write(strr)
gpu_mem = int(
torch.cuda.get_device_properties(i_device).total_memory / 1024 / 1024 / 1024
+ 0.4 + 0.4
) )
if gpu_mem <= 4: if self.gpu_mem <= 4:
with open("trainset_preprocess_pipeline_print.py", "r") as f: with open("trainset_preprocess_pipeline_print.py", "a") as f:
strr = f.read().replace("3.7", "3.0") strr = f.read().replace("3.7", "3.0")
with open("trainset_preprocess_pipeline_print.py", "w") as f:
f.write(strr) f.write(strr)
from multiprocessing import cpu_count elif torch.backends.mps.is_available():
print("没有发现支持的N卡, 使用MPS进行推理")
self.device = "mps"
else:
print("没有发现支持的N卡, 使用CPU进行推理")
self.device = "cpu"
if n_cpu == 0: if self.n_cpu == 0:
n_cpu = cpu_count() self.n_cpu = cpu_count()
if is_half:
if self.is_half:
# 6G显存配置 # 6G显存配置
x_pad = 3 x_pad = 3
x_query = 10 x_query = 10
@ -117,8 +98,11 @@ else:
x_query = 6 x_query = 6
x_center = 38 x_center = 38
x_max = 41 x_max = 41
if gpu_mem != None and gpu_mem <= 4:
if self.gpu_name != None and self.gpu_mem <= 4:
x_pad = 1 x_pad = 1
x_query = 5 x_query = 5
x_center = 30 x_center = 30
x_max = 32 x_max = 32
return x_pad, x_query, x_center, x_max

View File

@ -74,19 +74,12 @@ from fairseq import checkpoint_utils
import gradio as gr import gradio as gr
import logging import logging
from vc_infer_pipeline import VC from vc_infer_pipeline import VC
from config import ( from config import Config
is_half,
device,
python_cmd,
listen_port,
iscolab,
noparallel,
noautoopen,
)
from infer_uvr5 import _audio_pre_ from infer_uvr5 import _audio_pre_
from my_utils import load_audio from my_utils import load_audio
from train.process_ckpt import show_info, change_info, merge, extract_small_model from train.process_ckpt import show_info, change_info, merge, extract_small_model
config = Config()
# from trainset_preprocess_pipeline import PreProcess # from trainset_preprocess_pipeline import PreProcess
logging.getLogger("numba").setLevel(logging.WARNING) logging.getLogger("numba").setLevel(logging.WARNING)
@ -111,8 +104,8 @@ def load_hubert():
suffix="", suffix="",
) )
hubert_model = models[0] hubert_model = models[0]
hubert_model = hubert_model.to(device) hubert_model = hubert_model.to(config.device)
if is_half: if config.is_half:
hubert_model = hubert_model.half() hubert_model = hubert_model.half()
else: else:
hubert_model = hubert_model.float() hubert_model = hubert_model.float()
@ -259,8 +252,8 @@ def uvr(model_name, inp_root, save_root_vocal, paths, save_root_ins, agg):
pre_fun = _audio_pre_( pre_fun = _audio_pre_(
agg=int(agg), agg=int(agg),
model_path=os.path.join(weight_uvr5_root, model_name + ".pth"), model_path=os.path.join(weight_uvr5_root, model_name + ".pth"),
device=device, device=config.device,
is_half=is_half, is_half=config.is_half,
) )
if inp_root != "": if inp_root != "":
paths = [os.path.join(inp_root, name) for name in os.listdir(inp_root)] paths = [os.path.join(inp_root, name) for name in os.listdir(inp_root)]
@ -328,7 +321,9 @@ def get_vc(sid):
###楼下不这么折腾清理不干净 ###楼下不这么折腾清理不干净
if_f0 = cpt.get("f0", 1) if_f0 = cpt.get("f0", 1)
if if_f0 == 1: if if_f0 == 1:
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half) net_g = SynthesizerTrnMs256NSFsid(
*cpt["config"], is_half=config.is_half
)
else: else:
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"]) net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
del net_g, cpt del net_g, cpt
@ -343,17 +338,17 @@ def get_vc(sid):
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
if_f0 = cpt.get("f0", 1) if_f0 = cpt.get("f0", 1)
if if_f0 == 1: if if_f0 == 1:
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half) net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
else: else:
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"]) net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
del net_g.enc_q del net_g.enc_q
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净, 真奇葩 print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净, 真奇葩
net_g.eval().to(device) net_g.eval().to(config.device)
if is_half: if config.is_half:
net_g = net_g.half() net_g = net_g.half()
else: else:
net_g = net_g.float() net_g = net_g.float()
vc = VC(tgt_sr, device, is_half) vc = VC(tgt_sr, config)
n_spk = cpt["config"][-3] n_spk = cpt["config"][-3]
return {"visible": True, "maximum": n_spk, "__type__": "update"} return {"visible": True, "maximum": n_spk, "__type__": "update"}
@ -423,10 +418,10 @@ def preprocess_dataset(trainset_dir, exp_dir, sr, n_p=ncpu):
f = open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "w") f = open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir), "w")
f.close() f.close()
cmd = ( cmd = (
python_cmd config.python_cmd
+ " trainset_preprocess_pipeline_print.py %s %s %s %s/logs/%s " + " trainset_preprocess_pipeline_print.py %s %s %s %s/logs/%s "
% (trainset_dir, sr, n_p, now_dir, exp_dir) % (trainset_dir, sr, n_p, now_dir, exp_dir)
+ str(noparallel) + str(config.noparallel)
) )
print(cmd) print(cmd)
p = Popen(cmd, shell=True) # , stdin=PIPE, stdout=PIPE,stderr=PIPE,cwd=now_dir p = Popen(cmd, shell=True) # , stdin=PIPE, stdout=PIPE,stderr=PIPE,cwd=now_dir
@ -458,7 +453,7 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir):
f = open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "w") f = open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "w")
f.close() f.close()
if if_f0 == "": if if_f0 == "":
cmd = python_cmd + " extract_f0_print.py %s/logs/%s %s %s" % ( cmd = config.python_cmd + " extract_f0_print.py %s/logs/%s %s %s" % (
now_dir, now_dir,
exp_dir, exp_dir,
n_p, n_p,
@ -498,8 +493,8 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir):
leng = len(gpus) leng = len(gpus)
ps = [] ps = []
for idx, n_g in enumerate(gpus): for idx, n_g in enumerate(gpus):
cmd = python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % ( cmd = config.python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % (
device, config.device,
leng, leng,
idx, idx,
n_g, n_g,
@ -621,7 +616,7 @@ def click_train(
print("use gpus:", gpus16) print("use gpus:", gpus16)
if gpus16: if gpus16:
cmd = ( cmd = (
python_cmd config.python_cmd
+ " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -g %s -te %s -se %s -pg %s -pd %s -l %s -c %s" + " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -g %s -te %s -se %s -pg %s -pd %s -l %s -c %s"
% ( % (
exp_dir1, exp_dir1,
@ -639,7 +634,7 @@ def click_train(
) )
else: else:
cmd = ( cmd = (
python_cmd config.python_cmd
+ " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -te %s -se %s -pg %s -pd %s -l %s -c %s" + " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -te %s -se %s -pg %s -pd %s -l %s -c %s"
% ( % (
exp_dir1, exp_dir1,
@ -736,10 +731,10 @@ def train1key(
#########step1:处理数据 #########step1:处理数据
open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir1), "w").close() open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir1), "w").close()
cmd = ( cmd = (
python_cmd config.python_cmd
+ " trainset_preprocess_pipeline_print.py %s %s %s %s/logs/%s " + " trainset_preprocess_pipeline_print.py %s %s %s %s/logs/%s "
% (trainset_dir4, sr_dict[sr2], ncpu, now_dir, exp_dir1) % (trainset_dir4, sr_dict[sr2], ncpu, now_dir, exp_dir1)
+ str(noparallel) + str(config.noparallel)
) )
yield get_info_str("step1:正在处理数据") yield get_info_str("step1:正在处理数据")
yield get_info_str(cmd) yield get_info_str(cmd)
@ -751,7 +746,7 @@ def train1key(
open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir1), "w") open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir1), "w")
if if_f0_3 == "": if if_f0_3 == "":
yield get_info_str("step2a:正在提取音高") yield get_info_str("step2a:正在提取音高")
cmd = python_cmd + " extract_f0_print.py %s/logs/%s %s %s" % ( cmd = config.python_cmd + " extract_f0_print.py %s/logs/%s %s %s" % (
now_dir, now_dir,
exp_dir1, exp_dir1,
np7, np7,
@ -770,8 +765,8 @@ def train1key(
leng = len(gpus) leng = len(gpus)
ps = [] ps = []
for idx, n_g in enumerate(gpus): for idx, n_g in enumerate(gpus):
cmd = python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % ( cmd = config.python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % (
device, config.device,
leng, leng,
idx, idx,
n_g, n_g,
@ -852,7 +847,7 @@ def train1key(
yield get_info_str("write filelist done") yield get_info_str("write filelist done")
if gpus16: if gpus16:
cmd = ( cmd = (
python_cmd config.python_cmd
+ " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -g %s -te %s -se %s -pg %s -pd %s -l %s -c %s" + " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -g %s -te %s -se %s -pg %s -pd %s -l %s -c %s"
% ( % (
exp_dir1, exp_dir1,
@ -870,7 +865,7 @@ def train1key(
) )
else: else:
cmd = ( cmd = (
python_cmd config.python_cmd
+ " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -te %s -se %s -pg %s -pd %s -l %s -c %s" + " train_nsf_sim_cache_sid_load_pretrain.py -e %s -sr %s -f0 %s -bs %s -te %s -se %s -pg %s -pd %s -l %s -c %s"
% ( % (
exp_dir1, exp_dir1,
@ -1531,12 +1526,12 @@ with gr.Blocks() as app:
# with gr.TabItem(i18n("点击查看交流、问题反馈群号")): # with gr.TabItem(i18n("点击查看交流、问题反馈群号")):
# gr.Markdown(value=i18n("xxxxx")) # gr.Markdown(value=i18n("xxxxx"))
if iscolab: if config.iscolab:
app.queue(concurrency_count=511, max_size=1022).launch(share=True) app.queue(concurrency_count=511, max_size=1022).launch(share=True)
else: else:
app.queue(concurrency_count=511, max_size=1022).launch( app.queue(concurrency_count=511, max_size=1022).launch(
server_name="0.0.0.0", server_name="0.0.0.0",
inbrowser=not noautoopen, inbrowser=not config.noautoopen,
server_port=listen_port, server_port=config.listen_port,
quiet=True, quiet=True,
) )

View File

@ -1,7 +1,6 @@
import numpy as np, parselmouth, torch, pdb import numpy as np, parselmouth, torch, pdb
from time import time as ttime from time import time as ttime
import torch.nn.functional as F import torch.nn.functional as F
from config import x_pad, x_query, x_center, x_max
import scipy.signal as signal import scipy.signal as signal
import pyworld, os, traceback, faiss import pyworld, os, traceback, faiss
from scipy import signal from scipy import signal
@ -10,17 +9,23 @@ bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
class VC(object): class VC(object):
def __init__(self, tgt_sr, device, is_half): def __init__(self, tgt_sr, config):
self.x_pad, self.x_query, self.x_center, self.x_max, self.is_half = (
config.x_pad,
config.x_query,
config.x_center,
config.x_max,
config.is_half
)
self.sr = 16000 # hubert输入采样率 self.sr = 16000 # hubert输入采样率
self.window = 160 # 每帧点数 self.window = 160 # 每帧点数
self.t_pad = self.sr * x_pad # 每条前后pad时间 self.t_pad = self.sr * self.x_pad # 每条前后pad时间
self.t_pad_tgt = tgt_sr * x_pad self.t_pad_tgt = tgt_sr * self.x_pad
self.t_pad2 = self.t_pad * 2 self.t_pad2 = self.t_pad * 2
self.t_query = self.sr * x_query # 查询切点前后查询时间 self.t_query = self.sr * self.x_query # 查询切点前后查询时间
self.t_center = self.sr * x_center # 查询切点位置 self.t_center = self.sr * self.x_center # 查询切点位置
self.t_max = self.sr * x_max # 免查询时长阈值 self.t_max = self.sr * self.x_max # 免查询时长阈值
self.device = device self.device = config.device
self.is_half = is_half
def get_f0(self, x, p_len, f0_up_key, f0_method, inp_f0=None): def get_f0(self, x, p_len, f0_up_key, f0_method, inp_f0=None):
time_step = self.window / self.sr * 1000 time_step = self.window / self.sr * 1000
@ -64,8 +69,10 @@ class VC(object):
replace_f0 = np.interp( replace_f0 = np.interp(
list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1] list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1]
) )
shape = f0[x_pad * tf0 : x_pad * tf0 + len(replace_f0)].shape[0] shape = f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)].shape[0]
f0[x_pad * tf0 : x_pad * tf0 + len(replace_f0)] = replace_f0[:shape] f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)] = replace_f0[
:shape
]
# with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()])) # with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
f0bak = f0.copy() f0bak = f0.copy()
f0_mel = 1127 * np.log(1 + f0 / 700) f0_mel = 1127 * np.log(1 + f0 / 700)