Page MenuHomePhabricator

myinfer_multi_spk_embed_in_dec_diff_fi_meisheng.py
No OneTemporary

myinfer_multi_spk_embed_in_dec_diff_fi_meisheng.py

import os,sys,pdb,torch
now_dir = os.getcwd()
sys.path.append(now_dir)
import argparse
import glob
import sys
import torch
from multiprocessing import cpu_count
class Config:
def __init__(self,device,is_half):
self.device = device
self.is_half = is_half
self.n_cpu = 0
self.gpu_name = None
self.gpu_mem = None
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
def device_config(self) -> tuple:
current_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(current_dir, "configs")
if torch.cuda.is_available():
i_device = int(self.device.split(":")[-1])
self.gpu_name = torch.cuda.get_device_name(i_device)
if (
("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
or "P40" in self.gpu_name.upper()
or "1060" in self.gpu_name
or "1070" in self.gpu_name
or "1080" in self.gpu_name
):
print("16系/10系显卡和P40强制单精度")
self.is_half = False
for config_file in ["32k.json", "40k.json", "48k.json"]:
with open(f"{config_path}/{config_file}", "r") as f:
strr = f.read().replace("true", "false")
with open(f"{config_path}/{config_file}", "w") as f:
f.write(strr)
with open(f"{current_dir}/trainset_preprocess_pipeline_print.py", "r") as f:
strr = f.read().replace("3.7", "3.0")
with open(f"{current_dir}/trainset_preprocess_pipeline_print.py", "w") as f:
f.write(strr)
else:
self.gpu_name = None
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(f"{current_dir}/trainset_preprocess_pipeline_print.py", "r") as f:
strr = f.read().replace("3.7", "3.0")
with open(f"{current_dir}/trainset_preprocess_pipeline_print.py", "w") as f:
f.write(strr)
elif torch.backends.mps.is_available():
print("没有发现支持的N卡, 使用MPS进行推理")
self.device = "mps"
else:
print("没有发现支持的N卡, 使用CPU进行推理")
self.device = "cpu"
self.is_half = True
if self.n_cpu == 0:
self.n_cpu = cpu_count()
if self.is_half:
# 6G显存配置
x_pad = 3
x_query = 10
x_center = 80 #60
x_max = 85#65
else:
# 5G显存配置
x_pad = 1
x_query = 6
x_center = 38
x_max = 41
if self.gpu_mem != 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
index_path="./logs/xusong_v2_org_version_multispk_charlie_puth_embed_in_dec_muloss_show/added_IVF614_Flat_nprobe_1_xusong_v2_org_version_multispk_charlie_puth_embed_in_dec_show_v2.index"
# f0method="rmvpe" #harvest or pm
index_rate=float("0.0") #index rate
device="cuda:0"
is_half=True
filter_radius=int(3) ##3
resample_sr=int(0) # 0
rms_mix_rate=float(1) # rms混合比例 1,不等于1混合
protect=float(0.33 )## ??? 0.33 fang
#print(sys.argv)
config=Config(device,is_half)
now_dir=os.getcwd()
sys.path.append(now_dir)
from vc_infer_pipeline_org_embed import VC
from lib.infer_pack.models_embed_in_dec_diff_fi import (
SynthesizerTrnMs256NSFsid,
SynthesizerTrnMs256NSFsid_nono,
SynthesizerTrnMs768NSFsid,
SynthesizerTrnMs768NSFsid_nono,
)
from lib.audio import load_audio
from fairseq import checkpoint_utils
from scipy.io import wavfile
from AIMeiSheng.docker_demo.common import gs_hubert_model_path
# hubert_model=None
def load_hubert():
# global hubert_model
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task([gs_hubert_model_path],suffix="",)
#models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["checkpoint_best_legacy_500.pt"],suffix="",)
hubert_model = models[0]
hubert_model = hubert_model.to(device)
if(is_half):hubert_model = hubert_model.half()
else:hubert_model = hubert_model.float()
hubert_model.eval()
return hubert_model
def vc_single(sid,input_audio,f0_up_key,f0_file,f0_method,file_index,index_rate,hubert_model,paras):
global tgt_sr,net_g,vc,version
if input_audio is None:return "You need to upload an audio", None
f0_up_key = int(f0_up_key)
# print("@@xxxf0_up_key:",f0_up_key)
audio = load_audio(input_audio,16000)
if paras != None:
st = int(paras['tst'] * 16000/1000)
en = len(audio)
if paras['tnd'] != None:
en = min(en,int(paras['tnd'] * 16000/1000))
audio = audio[st:en]
times = [0, 0, 0]
if(hubert_model==None):
hubert_model = load_hubert()
if_f0 = cpt.get("f0", 1)
audio_opt=vc.pipeline_mulprocess(hubert_model,net_g,sid,audio,input_audio,times,f0_up_key,f0_method,file_index,index_rate,if_f0,filter_radius,tgt_sr,resample_sr,rms_mix_rate,version,protect,f0_file=f0_file)
#print(times)
#print("@@using multi process")
return audio_opt
def get_vc_core(model_path,is_half):
#print("loading pth %s" % model_path)
cpt = torch.load(model_path, map_location="cpu")
tgt_sr = cpt["config"][-1]
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
if_f0 = cpt.get("f0", 1)
version = cpt.get("version", "v1")
if version == "v1":
if if_f0 == 1:
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
else:
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
elif version == "v2":
if if_f0 == 1: #
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=is_half)
else:
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
#print("load model finished")
del net_g.enc_q
net_g.load_state_dict(cpt["weight"], strict=False)
#print("load net_g finished")
return tgt_sr,net_g,cpt,version
def get_vc1(model_path,is_half):
tgt_sr, net_g, cpt, version = get_vc_core(model_path, is_half)
net_g.eval().to(device)
if (is_half):net_g = net_g.half()
else:net_g = net_g.float()
vc = VC(tgt_sr, config)
n_spk=cpt["config"][-3]
return
def get_rmvpe(model_path="rmvpe.pt"):
from lib.rmvpe import RMVPE
global f0_method
#print("loading rmvpe model")
f0_method = RMVPE(model_path, is_half=True, device='cuda')
return f0_method
def get_vc(model_path):
global n_spk,tgt_sr,net_g,vc,cpt,device,is_half,version
tgt_sr, net_g, cpt, version = get_vc_core(model_path, is_half)
net_g.eval().to(device)
if (is_half):net_g = net_g.half()
else:net_g = net_g.float()
vc = VC(tgt_sr, config)
n_spk=cpt["config"][-3]
# return {"visible": True,"maximum": n_spk, "__type__": "update"}
# return net_g
def svc_main(input_path,opt_path,sid_embed,f0up_key=0,hubert_model=None, paras=None):
#print("sid_embed: ",sid_embed)
wav_opt = vc_single(sid_embed,input_path,f0up_key,None,f0_method,index_path,index_rate,hubert_model,paras)
#print("out_path: ",opt_path)
wavfile.write(opt_path, tgt_sr, wav_opt)

File Metadata

Mime Type
text/x-python
Expires
Mon, May 19, 02:14 (19 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1433511
Default Alt Text
myinfer_multi_spk_embed_in_dec_diff_fi_meisheng.py (7 KB)

Event Timeline