Skip to content

Commit c8261b2

Browse files
Tps-Ffumiama
andauthored
Reformat and rewrite _get_name_params (#57)
* Reformat * rewrite _get_name_params * Add workflow for automatic formatting * Revert "Add workflow for automatic formatting" This reverts commit 9111c5d. * revert Retrieval_based_Voice_Conversion_WebUI.ipynb --------- Co-authored-by: 源文雨 <41315874+fumiama@users.noreply.github.com>
1 parent aaa893c commit c8261b2

45 files changed

Lines changed: 4780 additions & 2358 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

config.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
########################硬件参数########################
22

3-
#填写cuda:x, cpu 或 mps, x指代第几张卡,只支持 N卡 / Apple Silicon 加速
4-
device = "cuda:0"
3+
# 填写cuda:x, cpu 或 mps, x指代第几张卡,只支持 N卡 / Apple Silicon 加速
4+
device = "cuda:0"
55

6-
#9-10-20-30-40系显卡无脑True,不影响质量,>=20显卡开启有加速
7-
is_half = True
6+
# 9-10-20-30-40系显卡无脑True,不影响质量,>=20显卡开启有加速
7+
is_half = True
88

9-
#默认0用上所有线程,写数字限制CPU资源使用
10-
n_cpu = 0
9+
# 默认0用上所有线程,写数字限制CPU资源使用
10+
n_cpu = 0
1111

1212
########################硬件参数########################
1313

@@ -16,64 +16,73 @@
1616

1717
########################命令行参数########################
1818
import argparse
19+
1920
parser = argparse.ArgumentParser()
2021
parser.add_argument("--port", type=int, default=7865, help="Listen port")
2122
parser.add_argument("--pycmd", type=str, default="python", help="Python command")
22-
parser.add_argument("--colab", action='store_true', help="Launch in colab")
23-
parser.add_argument("--noparallel", action='store_true', help="Disable parallel processing")
24-
parser.add_argument("--noautoopen", action='store_true', help="Do not open in browser automatically")
23+
parser.add_argument("--colab", action="store_true", help="Launch in colab")
24+
parser.add_argument(
25+
"--noparallel", action="store_true", help="Disable parallel processing"
26+
)
27+
parser.add_argument(
28+
"--noautoopen", action="store_true", help="Do not open in browser automatically"
29+
)
2530
cmd_opts = parser.parse_args()
2631

27-
python_cmd=cmd_opts.pycmd
28-
listen_port=cmd_opts.port
29-
iscolab=cmd_opts.colab
30-
noparallel=cmd_opts.noparallel
31-
noautoopen=cmd_opts.noautoopen
32+
python_cmd = cmd_opts.pycmd
33+
listen_port = cmd_opts.port
34+
iscolab = cmd_opts.colab
35+
noparallel = cmd_opts.noparallel
36+
noautoopen = cmd_opts.noautoopen
3237
########################命令行参数########################
3338

3439
import sys
3540
import torch
3641

42+
3743
# has_mps is only available in nightly pytorch (for now) and MasOS 12.3+.
3844
# check `getattr` and try it for compatibility
3945
def has_mps() -> bool:
4046
if sys.platform != "darwin":
4147
return False
4248
else:
43-
if not getattr(torch, 'has_mps', False): return False
49+
if not getattr(torch, "has_mps", False):
50+
return False
4451
try:
4552
torch.zeros(1).to(torch.device("mps"))
4653
return True
4754
except Exception:
4855
return False
4956

5057

51-
if(not torch.cuda.is_available()):
58+
if not torch.cuda.is_available():
5259
if has_mps():
5360
print("没有发现支持的N卡, 使用MPS进行推理")
54-
device = "mps"
61+
device = "mps"
5562
else:
5663
print("没有发现支持的N卡, 使用CPU进行推理")
57-
device = "cpu"
64+
device = "cpu"
5865
is_half = False
5966

60-
if(device not in ["cpu", "mps"]):
67+
if device not in ["cpu", "mps"]:
6168
gpu_name = torch.cuda.get_device_name(int(device.split(":")[-1]))
62-
if("16" in gpu_name or "MX" in gpu_name):
69+
if "16" in gpu_name or "MX" in gpu_name:
6370
print("16系显卡/MX系显卡强制单精度")
6471
is_half = False
6572

6673
from multiprocessing import cpu_count
67-
if(n_cpu==0): n_cpu=cpu_count()
68-
if(is_half):
69-
#6G显存配置
70-
x_pad = 3
71-
x_query = 10
72-
x_center = 60
73-
x_max = 65
74+
75+
if n_cpu == 0:
76+
n_cpu = cpu_count()
77+
if is_half:
78+
# 6G显存配置
79+
x_pad = 3
80+
x_query = 10
81+
x_center = 60
82+
x_max = 65
7483
else:
75-
#5G显存配置
76-
x_pad = 1
77-
x_query = 6
78-
x_center = 38
79-
x_max = 41
84+
# 5G显存配置
85+
x_pad = 1
86+
x_query = 6
87+
x_center = 38
88+
x_max = 41

export_onnx.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,43 @@
55
exported_path = "model.onnx"
66

77

8-
98
cpt = torch.load(person, map_location="cpu")
10-
cpt["config"][-3]=cpt["weight"]["emb_g.weight"].shape[0]#n_spk
9+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
1110
print(*cpt["config"])
1211
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=False)
1312
net_g.load_state_dict(cpt["weight"], strict=False)
1413

1514
test_phone = torch.rand(1, 200, 256)
1615
test_phone_lengths = torch.tensor([200]).long()
17-
test_pitch = torch.randint(size=(1 ,200),low=5,high=255)
16+
test_pitch = torch.randint(size=(1, 200), low=5, high=255)
1817
test_pitchf = torch.rand(1, 200)
1918
test_ds = torch.LongTensor([0])
2019
test_rnd = torch.rand(1, 192, 200)
2120
input_names = ["phone", "phone_lengths", "pitch", "pitchf", "ds", "rnd"]
22-
output_names = ["audio", ]
23-
device="cpu"
24-
torch.onnx.export(net_g,
25-
(
26-
test_phone.to(device),
27-
test_phone_lengths.to(device),
28-
test_pitch.to(device),
29-
test_pitchf.to(device),
30-
test_ds.to(device),
31-
test_rnd.to(device)
32-
),
33-
exported_path,
34-
dynamic_axes={
35-
"phone": [1],
36-
"pitch": [1],
37-
"pitchf": [1],
38-
"rnd": [2],
39-
},
40-
do_constant_folding=False,
41-
opset_version=16,
42-
verbose=False,
43-
input_names=input_names,
44-
output_names=output_names)
21+
output_names = [
22+
"audio",
23+
]
24+
device = "cpu"
25+
torch.onnx.export(
26+
net_g,
27+
(
28+
test_phone.to(device),
29+
test_phone_lengths.to(device),
30+
test_pitch.to(device),
31+
test_pitchf.to(device),
32+
test_ds.to(device),
33+
test_rnd.to(device),
34+
),
35+
exported_path,
36+
dynamic_axes={
37+
"phone": [1],
38+
"pitch": [1],
39+
"pitchf": [1],
40+
"rnd": [2],
41+
},
42+
do_constant_folding=False,
43+
opset_version=16,
44+
verbose=False,
45+
input_names=input_names,
46+
output_names=output_names,
47+
)

extract_f0_print.py

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import os,traceback,sys,parselmouth
1+
import os, traceback, sys, parselmouth
22
import librosa
33
import pyworld
44
from scipy.io import wavfile
5-
import numpy as np,logging
6-
logging.getLogger('numba').setLevel(logging.WARNING)
5+
import numpy as np, logging
6+
7+
logging.getLogger("numba").setLevel(logging.WARNING)
78
from multiprocessing import Process
89

910
exp_dir = sys.argv[1]
10-
f = open("%s/extract_f0_feature.log"%exp_dir, "a+")
11+
f = open("%s/extract_f0_feature.log" % exp_dir, "a+")
12+
13+
1114
def printt(strr):
1215
print(strr)
1316
f.write("%s\n" % strr)
1417
f.flush()
1518

19+
1620
n_p = int(sys.argv[2])
1721
f0method = sys.argv[3]
1822

23+
1924
class FeatureInput(object):
2025
def __init__(self, samplerate=16000, hop_size=160):
2126
self.fs = samplerate
@@ -27,21 +32,30 @@ def __init__(self, samplerate=16000, hop_size=160):
2732
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
2833
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
2934

30-
def compute_f0(self, path,f0_method):
35+
def compute_f0(self, path, f0_method):
3136
x, sr = librosa.load(path, self.fs)
32-
p_len=x.shape[0]//self.hop
37+
p_len = x.shape[0] // self.hop
3338
assert sr == self.fs
34-
if(f0_method=="pm"):
39+
if f0_method == "pm":
3540
time_step = 160 / 16000 * 1000
3641
f0_min = 50
3742
f0_max = 1100
38-
f0 = parselmouth.Sound(x, sr).to_pitch_ac(
39-
time_step=time_step / 1000, voicing_threshold=0.6,
40-
pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency']
41-
pad_size=(p_len - len(f0) + 1) // 2
42-
if(pad_size>0 or p_len - len(f0) - pad_size>0):
43-
f0 = np.pad(f0,[[pad_size,p_len - len(f0) - pad_size]], mode='constant')
44-
elif(f0_method=="harvest"):
43+
f0 = (
44+
parselmouth.Sound(x, sr)
45+
.to_pitch_ac(
46+
time_step=time_step / 1000,
47+
voicing_threshold=0.6,
48+
pitch_floor=f0_min,
49+
pitch_ceiling=f0_max,
50+
)
51+
.selected_array["frequency"]
52+
)
53+
pad_size = (p_len - len(f0) + 1) // 2
54+
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
55+
f0 = np.pad(
56+
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
57+
)
58+
elif f0_method == "harvest":
4559
f0, t = pyworld.harvest(
4660
x.astype(np.double),
4761
fs=sr,
@@ -50,7 +64,7 @@ def compute_f0(self, path,f0_method):
5064
frame_period=1000 * self.hop / sr,
5165
)
5266
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs)
53-
elif(f0_method=="dio"):
67+
elif f0_method == "dio":
5468
f0, t = pyworld.dio(
5569
x.astype(np.double),
5670
fs=sr,
@@ -77,45 +91,67 @@ def coarse_f0(self, f0):
7791
)
7892
return f0_coarse
7993

80-
def go(self,paths,f0_method):
81-
if (len(paths) == 0): printt("no-f0-todo")
94+
def go(self, paths, f0_method):
95+
if len(paths) == 0:
96+
printt("no-f0-todo")
8297
else:
83-
printt("todo-f0-%s"%len(paths))
84-
n=max(len(paths)//5,1)#每个进程最多打印5条
85-
for idx,(inp_path,opt_path1,opt_path2) in enumerate(paths):
98+
printt("todo-f0-%s" % len(paths))
99+
n = max(len(paths) // 5, 1) # 每个进程最多打印5条
100+
for idx, (inp_path, opt_path1, opt_path2) in enumerate(paths):
86101
try:
87-
if(idx%n==0):printt("f0ing,now-%s,all-%s,-%s"%(idx,len(paths),inp_path))
88-
if(os.path.exists(opt_path1+".npy")==True and os.path.exists(opt_path2+".npy")==True):continue
89-
featur_pit = self.compute_f0(inp_path,f0_method)
90-
np.save(opt_path2,featur_pit,allow_pickle=False,)#nsf
102+
if idx % n == 0:
103+
printt("f0ing,now-%s,all-%s,-%s" % (idx, len(paths), inp_path))
104+
if (
105+
os.path.exists(opt_path1 + ".npy") == True
106+
and os.path.exists(opt_path2 + ".npy") == True
107+
):
108+
continue
109+
featur_pit = self.compute_f0(inp_path, f0_method)
110+
np.save(
111+
opt_path2,
112+
featur_pit,
113+
allow_pickle=False,
114+
) # nsf
91115
coarse_pit = self.coarse_f0(featur_pit)
92-
np.save(opt_path1,coarse_pit,allow_pickle=False,)#ori
116+
np.save(
117+
opt_path1,
118+
coarse_pit,
119+
allow_pickle=False,
120+
) # ori
93121
except:
94-
printt("f0fail-%s-%s-%s" % (idx, inp_path,traceback.format_exc()))
122+
printt("f0fail-%s-%s-%s" % (idx, inp_path, traceback.format_exc()))
95123

96-
if __name__=='__main__':
124+
125+
if __name__ == "__main__":
97126
# exp_dir=r"E:\codes\py39\dataset\mi-test"
98127
# n_p=16
99128
# f = open("%s/log_extract_f0.log"%exp_dir, "w")
100129
printt(sys.argv)
101130
featureInput = FeatureInput()
102-
paths=[]
103-
inp_root= "%s/1_16k_wavs"%(exp_dir)
104-
opt_root1="%s/2a_f0"%(exp_dir)
105-
opt_root2="%s/2b-f0nsf"%(exp_dir)
131+
paths = []
132+
inp_root = "%s/1_16k_wavs" % (exp_dir)
133+
opt_root1 = "%s/2a_f0" % (exp_dir)
134+
opt_root2 = "%s/2b-f0nsf" % (exp_dir)
106135

107-
os.makedirs(opt_root1,exist_ok=True)
108-
os.makedirs(opt_root2,exist_ok=True)
136+
os.makedirs(opt_root1, exist_ok=True)
137+
os.makedirs(opt_root2, exist_ok=True)
109138
for name in sorted(list(os.listdir(inp_root))):
110-
inp_path="%s/%s"%(inp_root,name)
111-
if ("spec" in inp_path): continue
112-
opt_path1="%s/%s"%(opt_root1,name)
113-
opt_path2="%s/%s"%(opt_root2,name)
114-
paths.append([inp_path,opt_path1,opt_path2])
139+
inp_path = "%s/%s" % (inp_root, name)
140+
if "spec" in inp_path:
141+
continue
142+
opt_path1 = "%s/%s" % (opt_root1, name)
143+
opt_path2 = "%s/%s" % (opt_root2, name)
144+
paths.append([inp_path, opt_path1, opt_path2])
115145

116-
ps=[]
146+
ps = []
117147
for i in range(n_p):
118-
p=Process(target=featureInput.go,args=(paths[i::n_p],f0method,))
148+
p = Process(
149+
target=featureInput.go,
150+
args=(
151+
paths[i::n_p],
152+
f0method,
153+
),
154+
)
119155
p.start()
120156
ps.append(p)
121157
for p in ps:

0 commit comments

Comments
 (0)