Upload myinfer-v2-0528.py
Browse files- myinfer-v2-0528.py +175 -0
myinfer-v2-0528.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
v1
|
3 |
+
runtime\python.exe myinfer-v2-0528.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" "E:\codes\py39\test-20230416b\weights\mi-test.pth" 0.66 cuda:0 True 3 0 1 0.33
|
4 |
+
v2
|
5 |
+
runtime\python.exe myinfer-v2-0528.py 0 "E:\codes\py39\RVC-beta\todo-songs\1111.wav" "E:\codes\py39\test-20230416b\logs\mi-test-v2\aadded_IVF677_Flat_nprobe_1_v2.index" harvest "test_v2.wav" "E:\codes\py39\test-20230416b\weights\mi-test-v2.pth" 0.66 cuda:0 True 3 0 1 0.33
|
6 |
+
'''
|
7 |
+
import os,sys,pdb,torch
|
8 |
+
now_dir = os.getcwd()
|
9 |
+
sys.path.append(now_dir)
|
10 |
+
import argparse
|
11 |
+
import glob
|
12 |
+
import sys
|
13 |
+
import torch
|
14 |
+
from multiprocessing import cpu_count
|
15 |
+
class Config:
|
16 |
+
def __init__(self,device,is_half):
|
17 |
+
self.device = device
|
18 |
+
self.is_half = is_half
|
19 |
+
self.n_cpu = 0
|
20 |
+
self.gpu_name = None
|
21 |
+
self.gpu_mem = None
|
22 |
+
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
|
23 |
+
|
24 |
+
def device_config(self) -> tuple:
|
25 |
+
if torch.cuda.is_available():
|
26 |
+
i_device = int(self.device.split(":")[-1])
|
27 |
+
self.gpu_name = torch.cuda.get_device_name(i_device)
|
28 |
+
if (
|
29 |
+
("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
|
30 |
+
or "P40" in self.gpu_name.upper()
|
31 |
+
or "1060" in self.gpu_name
|
32 |
+
or "1070" in self.gpu_name
|
33 |
+
or "1080" in self.gpu_name
|
34 |
+
):
|
35 |
+
print("16系/10系显卡和P40强制单精度")
|
36 |
+
self.is_half = False
|
37 |
+
for config_file in ["32k.json", "40k.json", "48k.json"]:
|
38 |
+
with open(f"configs/{config_file}", "r") as f:
|
39 |
+
strr = f.read().replace("true", "false")
|
40 |
+
with open(f"configs/{config_file}", "w") as f:
|
41 |
+
f.write(strr)
|
42 |
+
with open("trainset_preprocess_pipeline_print.py", "r") as f:
|
43 |
+
strr = f.read().replace("3.7", "3.0")
|
44 |
+
with open("trainset_preprocess_pipeline_print.py", "w") as f:
|
45 |
+
f.write(strr)
|
46 |
+
else:
|
47 |
+
self.gpu_name = None
|
48 |
+
self.gpu_mem = int(
|
49 |
+
torch.cuda.get_device_properties(i_device).total_memory
|
50 |
+
/ 1024
|
51 |
+
/ 1024
|
52 |
+
/ 1024
|
53 |
+
+ 0.4
|
54 |
+
)
|
55 |
+
if self.gpu_mem <= 4:
|
56 |
+
with open("trainset_preprocess_pipeline_print.py", "r") as f:
|
57 |
+
strr = f.read().replace("3.7", "3.0")
|
58 |
+
with open("trainset_preprocess_pipeline_print.py", "w") as f:
|
59 |
+
f.write(strr)
|
60 |
+
elif torch.backends.mps.is_available():
|
61 |
+
print("没有发现支持的N卡, 使用MPS进行推理")
|
62 |
+
self.device = "mps"
|
63 |
+
else:
|
64 |
+
print("没有发现支持的N卡, 使用CPU进行推理")
|
65 |
+
self.device = "cpu"
|
66 |
+
self.is_half = True
|
67 |
+
|
68 |
+
if self.n_cpu == 0:
|
69 |
+
self.n_cpu = cpu_count()
|
70 |
+
|
71 |
+
if self.is_half:
|
72 |
+
# 6G显存配置
|
73 |
+
x_pad = 3
|
74 |
+
x_query = 10
|
75 |
+
x_center = 60
|
76 |
+
x_max = 65
|
77 |
+
else:
|
78 |
+
# 5G显存配置
|
79 |
+
x_pad = 1
|
80 |
+
x_query = 6
|
81 |
+
x_center = 38
|
82 |
+
x_max = 41
|
83 |
+
|
84 |
+
if self.gpu_mem != None and self.gpu_mem <= 4:
|
85 |
+
x_pad = 1
|
86 |
+
x_query = 5
|
87 |
+
x_center = 30
|
88 |
+
x_max = 32
|
89 |
+
|
90 |
+
return x_pad, x_query, x_center, x_max
|
91 |
+
|
92 |
+
f0up_key=sys.argv[1]
|
93 |
+
input_path=sys.argv[2]
|
94 |
+
index_path=sys.argv[3]
|
95 |
+
f0method=sys.argv[4]#harvest or pm
|
96 |
+
opt_path=sys.argv[5]
|
97 |
+
model_path=sys.argv[6]
|
98 |
+
index_rate=float(sys.argv[7])
|
99 |
+
device=sys.argv[8]
|
100 |
+
is_half=bool(sys.argv[9])
|
101 |
+
filter_radius=int(sys.argv[10])
|
102 |
+
resample_sr=int(sys.argv[11])
|
103 |
+
rms_mix_rate=float(sys.argv[12])
|
104 |
+
protect=float(sys.argv[13])
|
105 |
+
print(sys.argv)
|
106 |
+
config=Config(device,is_half)
|
107 |
+
now_dir=os.getcwd()
|
108 |
+
sys.path.append(now_dir)
|
109 |
+
from vc_infer_pipeline import VC
|
110 |
+
from infer_pack.models import (
|
111 |
+
SynthesizerTrnMs256NSFsid,
|
112 |
+
SynthesizerTrnMs256NSFsid_nono,
|
113 |
+
SynthesizerTrnMs768NSFsid,
|
114 |
+
SynthesizerTrnMs768NSFsid_nono,
|
115 |
+
)
|
116 |
+
from my_utils import load_audio
|
117 |
+
from fairseq import checkpoint_utils
|
118 |
+
from scipy.io import wavfile
|
119 |
+
|
120 |
+
hubert_model=None
|
121 |
+
def load_hubert():
|
122 |
+
global hubert_model
|
123 |
+
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)
|
124 |
+
hubert_model = models[0]
|
125 |
+
hubert_model = hubert_model.to(device)
|
126 |
+
if(is_half):hubert_model = hubert_model.half()
|
127 |
+
else:hubert_model = hubert_model.float()
|
128 |
+
hubert_model.eval()
|
129 |
+
|
130 |
+
def vc_single(sid,input_audio,f0_up_key,f0_file,f0_method,file_index,index_rate):
|
131 |
+
global tgt_sr,net_g,vc,hubert_model,version
|
132 |
+
if input_audio is None:return "You need to upload an audio", None
|
133 |
+
f0_up_key = int(f0_up_key)
|
134 |
+
audio=load_audio(input_audio,16000)
|
135 |
+
times = [0, 0, 0]
|
136 |
+
if(hubert_model==None):load_hubert()
|
137 |
+
if_f0 = cpt.get("f0", 1)
|
138 |
+
# 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)
|
139 |
+
audio_opt=vc.pipeline(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)
|
140 |
+
print(times)
|
141 |
+
return audio_opt
|
142 |
+
|
143 |
+
|
144 |
+
def get_vc(model_path):
|
145 |
+
global n_spk,tgt_sr,net_g,vc,cpt,device,is_half,version
|
146 |
+
print("loading pth %s"%model_path)
|
147 |
+
cpt = torch.load(model_path, map_location="cpu")
|
148 |
+
tgt_sr = cpt["config"][-1]
|
149 |
+
cpt["config"][-3]=cpt["weight"]["emb_g.weight"].shape[0]#n_spk
|
150 |
+
if_f0=cpt.get("f0",1)
|
151 |
+
version = cpt.get("version", "v1")
|
152 |
+
if version == "v1":
|
153 |
+
if if_f0 == 1:
|
154 |
+
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
|
155 |
+
else:
|
156 |
+
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
157 |
+
elif version == "v2":
|
158 |
+
if if_f0 == 1:#
|
159 |
+
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=is_half)
|
160 |
+
else:
|
161 |
+
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
162 |
+
del net_g.enc_q
|
163 |
+
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净,真奇葩
|
164 |
+
net_g.eval().to(device)
|
165 |
+
if (is_half):net_g = net_g.half()
|
166 |
+
else:net_g = net_g.float()
|
167 |
+
vc = VC(tgt_sr, config)
|
168 |
+
n_spk=cpt["config"][-3]
|
169 |
+
# return {"visible": True,"maximum": n_spk, "__type__": "update"}
|
170 |
+
|
171 |
+
|
172 |
+
get_vc(model_path)
|
173 |
+
wav_opt=vc_single(0,input_path,f0up_key,None,f0method,index_path,index_rate)
|
174 |
+
wavfile.write(opt_path, tgt_sr, wav_opt)
|
175 |
+
|