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

222
config.py
View File

@ -1,124 +1,108 @@
########################硬件参数########################
# 填写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
parser = argparse.ArgumentParser()
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("--colab", action="store_true", help="Launch in colab")
parser.add_argument(
"--noparallel", action="store_true", help="Disable parallel processing"
)
parser.add_argument(
"--noautoopen", action="store_true", help="Do not open in browser automatically"
)
cmd_opts = parser.parse_args()
python_cmd = cmd_opts.pycmd
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 glob
import sys
import torch
# has_mps is only available in nightly pytorch (for now) and MasOS 12.3+.
# check `getattr` and try it for compatibility
def has_mps() -> bool:
if sys.platform != "darwin":
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 (
"16" in gpu_name
or "P40" in gpu_name.upper()
or "1070" in gpu_name
or "1080" in gpu_name
):
print("16系显卡强制单精度")
is_half = False
with open("configs/32k.json", "r") as f:
strr = f.read().replace("true", "false")
with open("configs/32k.json", "w") as f:
f.write(strr)
with open("configs/40k.json", "r") as f:
strr = f.read().replace("true", "false")
with open("configs/40k.json", "w") as f:
f.write(strr)
with open("configs/48k.json", "r") as f:
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
)
if gpu_mem <= 4:
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)
from multiprocessing import cpu_count
if n_cpu == 0:
n_cpu = cpu_count()
if is_half:
# 6G显存配置
x_pad = 3
x_query = 10
x_center = 60
x_max = 65
else:
# 5G显存配置
x_pad = 1
x_query = 6
x_center = 38
x_max = 41
if gpu_mem != None and gpu_mem <= 4:
x_pad = 1
x_query = 5
x_center = 30
x_max = 32
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.add_argument("--port", type=int, default=7865, help="Listen port")
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(
"--noparallel", action="store_true", help="Disable parallel processing"
)
parser.add_argument(
"--noautoopen",
action="store_true",
help="Do not open in browser automatically",
)
cmd_opts = parser.parse_args()
cmd_opts.port = cmd_opts.port if 0 <= cmd_opts.port <= 65535 else 7865
return (
cmd_opts.pycmd,
cmd_opts.port,
cmd_opts.colab,
cmd_opts.noparallel,
cmd_opts.noautoopen,
)
def device_config(self) -> tuple:
if torch.cuda.is_available():
self.gpu_name = torch.cuda.get_device_name(int(self.device.split(":")[-1]))
i_device = int(self.device.split(":")[-1])
self.gpu_name = torch.cuda.get_device_name(i_device)
if (
"16" in self.gpu_name
or "P40" in self.gpu_name.upper()
or "1070" in self.gpu_name
or "1080" in self.gpu_name
):
print("16系显卡强制单精度")
self.is_half = False
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")
f.write(strr)
self.gpu_mem = int(
torch.cuda.get_device_properties(i_device).total_memory
/ 1024
/ 1024
/ 1024
+ 0.4
)
if self.gpu_mem <= 4:
with open("trainset_preprocess_pipeline_print.py", "a") as f:
strr = f.read().replace("3.7", "3.0")
f.write(strr)
elif torch.backends.mps.is_available():
print("没有发现支持的N卡, 使用MPS进行推理")
self.device = "mps"
else:
print("没有发现支持的N卡, 使用CPU进行推理")
self.device = "cpu"
if self.n_cpu == 0:
self.n_cpu = cpu_count()
if self.is_half:
# 6G显存配置
x_pad = 3
x_query = 10
x_center = 60
x_max = 65
else:
# 5G显存配置
x_pad = 1
x_query = 6
x_center = 38
x_max = 41
if self.gpu_name != None and self.gpu_mem <= 4:
x_pad = 1
x_query = 5
x_center = 30
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 logging
from vc_infer_pipeline import VC
from config import (
is_half,
device,
python_cmd,
listen_port,
iscolab,
noparallel,
noautoopen,
)
from config import Config
from infer_uvr5 import _audio_pre_
from my_utils import load_audio
from train.process_ckpt import show_info, change_info, merge, extract_small_model
config = Config()
# from trainset_preprocess_pipeline import PreProcess
logging.getLogger("numba").setLevel(logging.WARNING)
@ -111,8 +104,8 @@ def load_hubert():
suffix="",
)
hubert_model = models[0]
hubert_model = hubert_model.to(device)
if is_half:
hubert_model = hubert_model.to(config.device)
if config.is_half:
hubert_model = hubert_model.half()
else:
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_(
agg=int(agg),
model_path=os.path.join(weight_uvr5_root, model_name + ".pth"),
device=device,
is_half=is_half,
device=config.device,
is_half=config.is_half,
)
if 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 if_f0 == 1:
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
net_g = SynthesizerTrnMs256NSFsid(
*cpt["config"], is_half=config.is_half
)
else:
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
del net_g, cpt
@ -343,17 +338,17 @@ def get_vc(sid):
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
if_f0 = cpt.get("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:
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
del net_g.enc_q
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净, 真奇葩
net_g.eval().to(device)
if is_half:
net_g.eval().to(config.device)
if config.is_half:
net_g = net_g.half()
else:
net_g = net_g.float()
vc = VC(tgt_sr, device, is_half)
vc = VC(tgt_sr, config)
n_spk = cpt["config"][-3]
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.close()
cmd = (
python_cmd
config.python_cmd
+ " trainset_preprocess_pipeline_print.py %s %s %s %s/logs/%s "
% (trainset_dir, sr, n_p, now_dir, exp_dir)
+ str(noparallel)
+ str(config.noparallel)
)
print(cmd)
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.close()
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,
exp_dir,
n_p,
@ -498,8 +493,8 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir):
leng = len(gpus)
ps = []
for idx, n_g in enumerate(gpus):
cmd = python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % (
device,
cmd = config.python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % (
config.device,
leng,
idx,
n_g,
@ -621,7 +616,7 @@ def click_train(
print("use gpus:", gpus16)
if gpus16:
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"
% (
exp_dir1,
@ -639,7 +634,7 @@ def click_train(
)
else:
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"
% (
exp_dir1,
@ -736,10 +731,10 @@ def train1key(
#########step1:处理数据
open("%s/logs/%s/preprocess.log" % (now_dir, exp_dir1), "w").close()
cmd = (
python_cmd
config.python_cmd
+ " trainset_preprocess_pipeline_print.py %s %s %s %s/logs/%s "
% (trainset_dir4, sr_dict[sr2], ncpu, now_dir, exp_dir1)
+ str(noparallel)
+ str(config.noparallel)
)
yield get_info_str("step1:正在处理数据")
yield get_info_str(cmd)
@ -751,7 +746,7 @@ def train1key(
open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir1), "w")
if if_f0_3 == "":
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,
exp_dir1,
np7,
@ -770,8 +765,8 @@ def train1key(
leng = len(gpus)
ps = []
for idx, n_g in enumerate(gpus):
cmd = python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % (
device,
cmd = config.python_cmd + " extract_feature_print.py %s %s %s %s %s/logs/%s" % (
config.device,
leng,
idx,
n_g,
@ -852,7 +847,7 @@ def train1key(
yield get_info_str("write filelist done")
if gpus16:
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"
% (
exp_dir1,
@ -870,7 +865,7 @@ def train1key(
)
else:
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"
% (
exp_dir1,
@ -1531,12 +1526,12 @@ with gr.Blocks() as app:
# with gr.TabItem(i18n("点击查看交流、问题反馈群号")):
# gr.Markdown(value=i18n("xxxxx"))
if iscolab:
if config.iscolab:
app.queue(concurrency_count=511, max_size=1022).launch(share=True)
else:
app.queue(concurrency_count=511, max_size=1022).launch(
server_name="0.0.0.0",
inbrowser=not noautoopen,
server_port=listen_port,
inbrowser=not config.noautoopen,
server_port=config.listen_port,
quiet=True,
)

View File

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