Upload myinfer.py
Browse files1234567891011122 unmodified lines151617181920212223242526272829303132333 unmodified lines37383940414243441 unmodified line464748495051525354555657585960616 unmodified lines686970717273747576777879import os,sys,pdb,torchf0up_key=sys.argv[1]input_path=sys.argv[2]index_path=sys.argv[3]npy_path=sys.argv[4]opt_path=sys.argv[5]model_path=sys.argv[6]print(sys.argv)sys.argv=['myinfer.py']now_dir=os.getcwd()sys.path.append(now_dir)from vc_infer_pipeline import VC2 unmodified linesfrom fairseq import checkpoint_utilsfrom scipy.io import wavfile# f0up_key=0# input_path=r"E:\codes\py39\RVC-beta\todo-songs\1111.wav"# index_path=r"E:\codes\py39\logs\mi-test\added_IVF677_Flat_nprobe_7.index"# npy_path =r"E:\codes\py39\logs\mi-test\total_fea.npy"# opt_path ="test.wav"# model_path="mi-test.pth"hubert_model=Noneis_half=Falsedevice="cuda"def load_hubert():global hubert_modelmodels, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)3 unmodified lineselse:hubert_model = hubert_model.float()hubert_model.eval()def vc_single(sid,input_audio,f0_up_key,f0_file,f0_method,file_index,file_big_npy,index_rate):#spk_item, input_audio0, vc_transform0,f0_file,f0method0global tgt_sr,net_g,vc,hubert_modelif input_audio is None:return "You need to upload an audio", Nonef0_up_key = int(f0_up_key)1 unmodified linetimes = [0, 0, 0]if(hubert_model==None):load_hubert()if_f0 = cpt.get("f0", 1)audio_opt=vc.pipeline(hubert_model,net_g,sid,audio,times,f0_up_key,f0_method,file_index,file_big_npy,index_rate,if_f0,f0_file=f0_file)print(times)return audio_optdef get_vc(sid):global n_spk,tgt_sr,net_g,vc,cptperson = "weights/%s" % (sid)print("loading %s"%person)cpt = torch.load(person, map_location="cpu")tgt_sr = cpt["config"][-1]cpt["config"][-3]=cpt["weight"]["emb_g.weight"].shape[0]#n_spkif_f0=cpt.get("f0",1)6 unmodified linesnet_g.eval().to(device)if (is_half):net_g = net_g.half()else:net_g = net_g.float()vc = VC(tgt_sr, device, is_half)n_spk=cpt["config"][-3]# return {"visible": True,"maximum": n_spk, "__type__": "update"}get_vc(model_path)wav_opt=vc_single(0,input_path,f0up_key,None,"harvest",index_path,npy_path,0.6)wavfile.write(opt_path, tgt_sr, wav_opt)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011022 unmodified lines1051061071081091101113 unmodified lines1151161171181191201211 unmodified line1231241251261271281291301311321331341351361371386 unmodified lines145146147148149150151152153154155156'''runtime\python.exe myinfer.py 0 "E:\codes\py39\RVC-beta\todo-songs\1111.wav" "E:\codes\py39\logs\mi-test\added_IVF677_Flat_nprobe_7.index" harvest "test.wav" "weights/mi-test.pth" 0.6 cuda:0 True'''import os,sys,pdb,torchnow_dir = os.getcwd()sys.path.append(now_dir)import argparseimport globimport sysimport torchfrom multiprocessing import cpu_countclass Config:def __init__(self,device,is_half):self.device = deviceself.is_half = is_halfself.n_cpu = 0self.gpu_name = Noneself.gpu_mem = Noneself.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()def device_config(self) -> tuple: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_nameor "1070" in self.gpu_nameor "1080" in self.gpu_name):print("16系/10系显卡和P40强制单精度")self.is_half = Falsefor config_file in ["32k.json", "40k.json", "48k.json"]:with open(f"configs/{config_file}", "r") as f:strr = f.read().replace("true", "false")with open(f"configs/{config_file}", "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)else:self.gpu_name = Noneself.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", "r") as f:strr = f.read().replace("3.7", "3.0")with open("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 = Trueif self.n_cpu == 0:self.n_cpu = cpu_count()if self.is_half:# 6G显存配置x_pad = 3x_query = 10x_center = 60x_max = 65else:# 5G显存配置x_pad = 1x_query = 6x_center = 38x_max = 41if self.gpu_mem != None and self.gpu_mem <= 4:x_pad = 1x_query = 5x_center = 30x_max = 32return x_pad, x_query, x_center, x_maxf0up_key=sys.argv[1]input_path=sys.argv[2]index_path=sys.argv[3]f0method=sys.argv[4]#harvest or pmopt_path=sys.argv[5]model_path=sys.argv[6]index_rate=float(sys.argv[7])device=sys.argv[8]is_half=bool(sys.argv[9])print(sys.argv)config=Config(device,is_half)now_dir=os.getcwd()sys.path.append(now_dir)from vc_infer_pipeline import VC2 unmodified linesfrom fairseq import checkpoint_utilsfrom scipy.io import wavfilehubert_model=Nonedef load_hubert():global hubert_modelmodels, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)3 unmodified lineselse:hubert_model = hubert_model.float()hubert_model.eval()def vc_single(sid,input_audio,f0_up_key,f0_file,f0_method,file_index,index_rate):global tgt_sr,net_g,vc,hubert_modelif input_audio is None:return "You need to upload an audio", Nonef0_up_key = int(f0_up_key)1 unmodified linetimes = [0, 0, 0]if(hubert_model==None):load_hubert()if_f0 = cpt.get("f0", 1)# audio_opt=vc.pipeline(hubert_model,net_g,sid,audio,times,f0_up_key,f0_method,file_index,file_big_npy,index_rate,if_f0,f0_file=f0_file)audio_opt=vc.pipeline(hubert_model,net_g,sid,audio,times,f0_up_key,f0_method,file_index,index_rate,if_f0,f0_file=f0_file)print(times)return audio_optdef get_vc(model_path):global n_spk,tgt_sr,net_g,vc,cpt,device,is_halfprint("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]#n_spkif_f0=cpt.get("f0",1)6 unmodified linesnet_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"}get_vc(model_path)wav_opt=vc_single(0,input_path,f0up_key,None,f0method,index_path,index_rate)wavfile.write(opt_path, tgt_sr, wav_opt)
myinfer.py+101-24