Skip to content

Commit 03e7c68

Browse files
authored
Add files via upload
1 parent 7f78dce commit 03e7c68

11 files changed

+479
-271
lines changed

MDXNet.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import soundfile as sf
22
import torch, pdb, os, warnings, librosa
33
import numpy as np
4-
import onnxruntime as ort
54
from tqdm import tqdm
65
import torch
76

@@ -83,13 +82,19 @@ def get_models(device, dim_f, dim_t, n_fft):
8382

8483

8584
warnings.filterwarnings("ignore")
85+
import sys
86+
now_dir = os.getcwd()
87+
sys.path.append(now_dir)
88+
from config import Config
89+
8690
cpu = torch.device("cpu")
87-
if torch.cuda.is_available():
88-
device = torch.device("cuda:0")
89-
elif torch.backends.mps.is_available():
90-
device = torch.device("mps")
91-
else:
92-
device = torch.device("cpu")
91+
device=Config().device
92+
# if torch.cuda.is_available():
93+
# device = torch.device("cuda:0")
94+
# elif torch.backends.mps.is_available():
95+
# device = torch.device("mps")
96+
# else:
97+
# device = torch.device("cpu")
9398

9499

95100
class Predictor:
@@ -98,9 +103,11 @@ def __init__(self, args):
98103
self.model_ = get_models(
99104
device=cpu, dim_f=args.dim_f, dim_t=args.dim_t, n_fft=args.n_fft
100105
)
106+
import onnxruntime as ort
107+
print(ort.get_available_providers())
101108
self.model = ort.InferenceSession(
102109
os.path.join(args.onnx, self.model_.target_name + ".onnx"),
103-
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
110+
providers=["CUDAExecutionProvider", "DmlExecutionProvider","CPUExecutionProvider"],
104111
)
105112
print("onnx load done")
106113

config.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def __init__(self):
3636
self.noparallel,
3737
self.noautoopen,
3838
) = self.arg_parse()
39+
self.instead=""
3940
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
4041

41-
@staticmethod
42-
def arg_parse() -> tuple:
42+
def arg_parse(self) -> tuple:
4343
exe = sys.executable or "python"
4444
parser = argparse.ArgumentParser()
4545
parser.add_argument("--port", type=int, default=7865, help="Listen port")
@@ -53,10 +53,15 @@ def arg_parse() -> tuple:
5353
action="store_true",
5454
help="Do not open in browser automatically",
5555
)
56+
parser.add_argument(
57+
"--dml",
58+
action="store_true",
59+
help="torch_dml",
60+
)
5661
cmd_opts = parser.parse_args()
5762

5863
cmd_opts.port = cmd_opts.port if 0 <= cmd_opts.port <= 65535 else 7865
59-
64+
self.dml=cmd_opts.dml
6065
return (
6166
cmd_opts.pycmd,
6267
cmd_opts.port,
@@ -106,13 +111,13 @@ def device_config(self) -> tuple:
106111
with open("trainset_preprocess_pipeline_print.py", "w") as f:
107112
f.write(strr)
108113
elif self.has_mps():
109-
print("No supported Nvidia GPU found, use MPS instead")
110-
self.device = "mps"
114+
print("No supported Nvidia GPU found")
115+
self.device = self.instead="mps"
111116
self.is_half = False
112117
use_fp32_config()
113118
else:
114-
print("No supported Nvidia GPU found, use CPU instead")
115-
self.device = "cpu"
119+
print("No supported Nvidia GPU found")
120+
self.device = self.instead="cpu"
116121
self.is_half = False
117122
use_fp32_config()
118123

@@ -137,5 +142,20 @@ def device_config(self) -> tuple:
137142
x_query = 5
138143
x_center = 30
139144
x_max = 32
140-
145+
if(self.dml==True):
146+
print("use DirectML instead")
147+
try:os.rename("runtime\Lib\site-packages\onnxruntime","runtime\Lib\site-packages\onnxruntime-cuda")
148+
except:pass
149+
try:os.rename("runtime\Lib\site-packages\onnxruntime-dml","runtime\Lib\site-packages\onnxruntime")
150+
except:pass
151+
import torch_directml
152+
self.device= torch_directml.device(torch_directml.default_device())
153+
self.is_half=False
154+
else:
155+
if(self.instead):
156+
print("use %s instead"%self.instead)
157+
try:os.rename("runtime\Lib\site-packages\onnxruntime","runtime\Lib\site-packages\onnxruntime-cuda")
158+
except:pass
159+
try:os.rename("runtime\Lib\site-packages\onnxruntime-dml","runtime\Lib\site-packages\onnxruntime")
160+
except:pass
141161
return x_pad, x_query, x_center, x_max

extract_f0_rmvpe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ def __init__(self, samplerate=16000, hop_size=160):
3636

3737
def compute_f0(self, path, f0_method):
3838
x = load_audio(path, self.fs)
39-
p_len = x.shape[0] // self.hop
39+
# p_len = x.shape[0] // self.hop
4040
if f0_method == "rmvpe":
4141
if hasattr(self, "model_rmvpe") == False:
4242
from lib.rmvpe import RMVPE
4343

4444
print("loading rmvpe model")
45-
self.model_rmvpe = RMVPE("rmvpe.pt", is_half=True, device="cuda")
45+
self.model_rmvpe = RMVPE("rmvpe.pt", is_half=is_half, device="cuda")
4646
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
4747
return f0
4848

extract_f0_rmvpe_dml.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import os, traceback, sys, parselmouth
2+
3+
now_dir = os.getcwd()
4+
sys.path.append(now_dir)
5+
from lib.audio import load_audio
6+
import pyworld
7+
import numpy as np, logging
8+
9+
logging.getLogger("numba").setLevel(logging.WARNING)
10+
11+
exp_dir = sys.argv[1]
12+
import torch_directml
13+
device = torch_directml.device(torch_directml.default_device())
14+
f = open("%s/extract_f0_feature.log" % exp_dir, "a+")
15+
16+
17+
def printt(strr):
18+
print(strr)
19+
f.write("%s\n" % strr)
20+
f.flush()
21+
22+
23+
class FeatureInput(object):
24+
def __init__(self, samplerate=16000, hop_size=160):
25+
self.fs = samplerate
26+
self.hop = hop_size
27+
28+
self.f0_bin = 256
29+
self.f0_max = 1100.0
30+
self.f0_min = 50.0
31+
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
32+
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
33+
34+
def compute_f0(self, path, f0_method):
35+
x = load_audio(path, self.fs)
36+
# p_len = x.shape[0] // self.hop
37+
if f0_method == "rmvpe":
38+
if hasattr(self, "model_rmvpe") == False:
39+
from lib.rmvpe import RMVPE
40+
41+
print("loading rmvpe model")
42+
self.model_rmvpe = RMVPE("rmvpe.pt", is_half=False, device=device)
43+
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
44+
return f0
45+
46+
def coarse_f0(self, f0):
47+
f0_mel = 1127 * np.log(1 + f0 / 700)
48+
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * (
49+
self.f0_bin - 2
50+
) / (self.f0_mel_max - self.f0_mel_min) + 1
51+
52+
# use 0 or 1
53+
f0_mel[f0_mel <= 1] = 1
54+
f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1
55+
f0_coarse = np.rint(f0_mel).astype(int)
56+
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (
57+
f0_coarse.max(),
58+
f0_coarse.min(),
59+
)
60+
return f0_coarse
61+
62+
def go(self, paths, f0_method):
63+
if len(paths) == 0:
64+
printt("no-f0-todo")
65+
else:
66+
printt("todo-f0-%s" % len(paths))
67+
n = max(len(paths) // 5, 1) # 每个进程最多打印5条
68+
for idx, (inp_path, opt_path1, opt_path2) in enumerate(paths):
69+
try:
70+
if idx % n == 0:
71+
printt("f0ing,now-%s,all-%s,-%s" % (idx, len(paths), inp_path))
72+
if (
73+
os.path.exists(opt_path1 + ".npy") == True
74+
and os.path.exists(opt_path2 + ".npy") == True
75+
):
76+
continue
77+
featur_pit = self.compute_f0(inp_path, f0_method)
78+
np.save(
79+
opt_path2,
80+
featur_pit,
81+
allow_pickle=False,
82+
) # nsf
83+
coarse_pit = self.coarse_f0(featur_pit)
84+
np.save(
85+
opt_path1,
86+
coarse_pit,
87+
allow_pickle=False,
88+
) # ori
89+
except:
90+
printt("f0fail-%s-%s-%s" % (idx, inp_path, traceback.format_exc()))
91+
92+
93+
if __name__ == "__main__":
94+
# exp_dir=r"E:\codes\py39\dataset\mi-test"
95+
# n_p=16
96+
# f = open("%s/log_extract_f0.log"%exp_dir, "w")
97+
printt(sys.argv)
98+
featureInput = FeatureInput()
99+
paths = []
100+
inp_root = "%s/1_16k_wavs" % (exp_dir)
101+
opt_root1 = "%s/2a_f0" % (exp_dir)
102+
opt_root2 = "%s/2b-f0nsf" % (exp_dir)
103+
104+
os.makedirs(opt_root1, exist_ok=True)
105+
os.makedirs(opt_root2, exist_ok=True)
106+
for name in sorted(list(os.listdir(inp_root))):
107+
inp_path = "%s/%s" % (inp_root, name)
108+
if "spec" in inp_path:
109+
continue
110+
opt_path1 = "%s/%s" % (opt_root1, name)
111+
opt_path2 = "%s/%s" % (opt_root2, name)
112+
paths.append([inp_path, opt_path1, opt_path2])
113+
try:
114+
featureInput.go(paths, "rmvpe")
115+
except:
116+
printt("f0_all_fail-%s" % (traceback.format_exc()))
117+
# ps = []
118+
# for i in range(n_p):
119+
# p = Process(
120+
# target=featureInput.go,
121+
# args=(
122+
# paths[i::n_p],
123+
# f0method,
124+
# ),
125+
# )
126+
# ps.append(p)
127+
# p.start()
128+
# for i in range(n_p):
129+
# ps[i].join()

extract_feature_print.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
44
os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0"
55

6-
# device=sys.argv[1]
6+
device=sys.argv[1]
77
n_part = int(sys.argv[2])
88
i_part = int(sys.argv[3])
99
if len(sys.argv) == 6:
@@ -18,13 +18,22 @@
1818
import torch.nn.functional as F
1919
import soundfile as sf
2020
import numpy as np
21-
from fairseq import checkpoint_utils
22-
23-
device = "cpu"
24-
if torch.cuda.is_available():
25-
device = "cuda"
26-
elif torch.backends.mps.is_available():
27-
device = "mps"
21+
import fairseq
22+
23+
if("privateuseone"not in device):
24+
device = "cpu"
25+
if torch.cuda.is_available():
26+
device = "cuda"
27+
elif torch.backends.mps.is_available():
28+
device = "mps"
29+
else:
30+
import torch_directml
31+
device = torch_directml.device(torch_directml.default_device())
32+
def forward_dml(ctx, x, scale):
33+
ctx.scale = scale
34+
res = x.clone().detach()
35+
return res
36+
fairseq.modules.grad_multiply.GradMultiply.forward=forward_dml
2837

2938
f = open("%s/extract_f0_feature.log" % exp_dir, "a+")
3039

@@ -70,7 +79,7 @@ def readwave(wav_path, normalize=False):
7079
% model_path
7180
)
7281
exit(0)
73-
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
82+
models, saved_cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task(
7483
[model_path],
7584
suffix="",
7685
)

gui_v1.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import os, sys
2-
1+
import os, sys,pdb
2+
os.environ["OMP_NUM_THREADS"]="2"
33
if sys.platform == "darwin":
44
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
55

@@ -46,20 +46,21 @@ def run(self):
4646
import torch.nn.functional as F
4747
import torchaudio.transforms as tat
4848
from i18n import I18nAuto
49-
49+
import rvc_for_realtime
5050
i18n = I18nAuto()
51-
device = torch.device(
52-
"cuda"
53-
if torch.cuda.is_available()
54-
else ("mps" if torch.backends.mps.is_available() else "cpu")
55-
)
51+
device=rvc_for_realtime.config.device
52+
# device = torch.device(
53+
# "cuda"
54+
# if torch.cuda.is_available()
55+
# else ("mps" if torch.backends.mps.is_available() else "cpu")
56+
# )
5657
current_dir = os.getcwd()
5758
inp_q = Queue()
5859
opt_q = Queue()
5960
n_cpu = min(cpu_count(), 8)
6061
for _ in range(n_cpu):
6162
Harvest(inp_q, opt_q).start()
62-
from rvc_for_realtime import RVC
63+
6364

6465
class GUIConfig:
6566
def __init__(self) -> None:
@@ -75,7 +76,7 @@ def __init__(self) -> None:
7576
self.I_noise_reduce = False
7677
self.O_noise_reduce = False
7778
self.index_rate = 0.3
78-
self.n_cpu = min(n_cpu, 8)
79+
self.n_cpu = min(n_cpu, 6)
7980
self.f0method = "harvest"
8081
self.sg_input_device = ""
8182
self.sg_output_device = ""
@@ -239,7 +240,7 @@ def launcher(self):
239240
[
240241
sg.Text(i18n("采样长度")),
241242
sg.Slider(
242-
range=(0.12, 2.4),
243+
range=(0.09, 2.4),
243244
key="block_time",
244245
resolution=0.03,
245246
orientation="h",
@@ -271,7 +272,7 @@ def launcher(self):
271272
[
272273
sg.Text(i18n("额外推理时长")),
273274
sg.Slider(
274-
range=(0.05, 3.00),
275+
range=(0.05, 5.00),
275276
key="extra_time",
276277
resolution=0.01,
277278
orientation="h",
@@ -391,7 +392,7 @@ def set_values(self, values):
391392
def start_vc(self):
392393
torch.cuda.empty_cache()
393394
self.flag_vc = True
394-
self.rvc = RVC(
395+
self.rvc = rvc_for_realtime.RVC(
395396
self.config.pitch,
396397
self.config.pth_path,
397398
self.config.index_path,
@@ -510,7 +511,6 @@ def audio_callback(
510511
self.input_wav[:] = np.append(self.input_wav[self.block_frame :], indata)
511512
# infer
512513
inp = torch.from_numpy(self.input_wav).to(device)
513-
##0
514514
res1 = self.resampler(inp)
515515
###55%
516516
rate1 = self.block_frame / (

0 commit comments

Comments
 (0)