1
0
mirror of synced 2024-11-28 01:10:56 +01:00

Format code (#989)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
github-actions[bot] 2023-08-13 11:52:51 +08:00 committed by GitHub
parent 7293002f53
commit 76b67842ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 218 additions and 117 deletions

View File

@ -83,12 +83,13 @@ def get_models(device, dim_f, dim_t, n_fft):
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
import sys import sys
now_dir = os.getcwd() now_dir = os.getcwd()
sys.path.append(now_dir) sys.path.append(now_dir)
from config import Config from config import Config
cpu = torch.device("cpu") cpu = torch.device("cpu")
device=Config().device device = Config().device
# if torch.cuda.is_available(): # if torch.cuda.is_available():
# device = torch.device("cuda:0") # device = torch.device("cuda:0")
# elif torch.backends.mps.is_available(): # elif torch.backends.mps.is_available():
@ -104,10 +105,15 @@ class Predictor:
device=cpu, dim_f=args.dim_f, dim_t=args.dim_t, n_fft=args.n_fft device=cpu, dim_f=args.dim_f, dim_t=args.dim_t, n_fft=args.n_fft
) )
import onnxruntime as ort import onnxruntime as ort
print(ort.get_available_providers()) print(ort.get_available_providers())
self.model = ort.InferenceSession( self.model = ort.InferenceSession(
os.path.join(args.onnx, self.model_.target_name + ".onnx"), os.path.join(args.onnx, self.model_.target_name + ".onnx"),
providers=["CUDAExecutionProvider", "DmlExecutionProvider","CPUExecutionProvider"], providers=[
"CUDAExecutionProvider",
"DmlExecutionProvider",
"CPUExecutionProvider",
],
) )
print("onnx load done") print("onnx load done")

View File

@ -36,7 +36,7 @@ class Config:
self.iscolab, self.iscolab,
self.noparallel, self.noparallel,
self.noautoopen, self.noautoopen,
self.dml self.dml,
) = self.arg_parse() ) = self.arg_parse()
self.instead = "" self.instead = ""
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config() self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
@ -71,7 +71,7 @@ class Config:
cmd_opts.colab, cmd_opts.colab,
cmd_opts.noparallel, cmd_opts.noparallel,
cmd_opts.noautoopen, cmd_opts.noautoopen,
cmd_opts.dml cmd_opts.dml,
) )
# has_mps is only available in nightly pytorch (for now) and MasOS 12.3+. # has_mps is only available in nightly pytorch (for now) and MasOS 12.3+.
@ -149,26 +149,38 @@ class Config:
if self.dml: if self.dml:
print("use DirectML instead") print("use DirectML instead")
try: try:
os.rename("runtime\Lib\site-packages\onnxruntime","runtime\Lib\site-packages\onnxruntime-cuda") os.rename(
"runtime\Lib\site-packages\onnxruntime",
"runtime\Lib\site-packages\onnxruntime-cuda",
)
except: except:
pass pass
try: try:
os.rename("runtime\Lib\site-packages\onnxruntime-dml","runtime\Lib\site-packages\onnxruntime") os.rename(
"runtime\Lib\site-packages\onnxruntime-dml",
"runtime\Lib\site-packages\onnxruntime",
)
except: except:
pass pass
import torch_directml import torch_directml
self.device = torch_directml.device(torch_directml.default_device()) self.device = torch_directml.device(torch_directml.default_device())
self.is_half = False self.is_half = False
else: else:
if self.instead: if self.instead:
print(f"use {self.instead} instead") print(f"use {self.instead} instead")
try: try:
os.rename("runtime\Lib\site-packages\onnxruntime","runtime\Lib\site-packages\onnxruntime-cuda") os.rename(
"runtime\Lib\site-packages\onnxruntime",
"runtime\Lib\site-packages\onnxruntime-cuda",
)
except: except:
pass pass
try: try:
os.rename("runtime\Lib\site-packages\onnxruntime-dml","runtime\Lib\site-packages\onnxruntime") os.rename(
"runtime\Lib\site-packages\onnxruntime-dml",
"runtime\Lib\site-packages\onnxruntime",
)
except: except:
pass pass
return x_pad, x_query, x_center, x_max return x_pad, x_query, x_center, x_max

View File

@ -10,6 +10,7 @@ logging.getLogger("numba").setLevel(logging.WARNING)
exp_dir = sys.argv[1] exp_dir = sys.argv[1]
import torch_directml import torch_directml
device = torch_directml.device(torch_directml.default_device()) device = torch_directml.device(torch_directml.default_device())
f = open("%s/extract_f0_feature.log" % exp_dir, "a+") f = open("%s/extract_f0_feature.log" % exp_dir, "a+")

View File

@ -3,7 +3,7 @@ import os, sys, traceback
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0" os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0"
device=sys.argv[1] device = sys.argv[1]
n_part = int(sys.argv[2]) n_part = int(sys.argv[2])
i_part = int(sys.argv[3]) i_part = int(sys.argv[3])
if len(sys.argv) == 6: if len(sys.argv) == 6:
@ -20,7 +20,7 @@ import soundfile as sf
import numpy as np import numpy as np
import fairseq import fairseq
if("privateuseone"not in device): if "privateuseone" not in device:
device = "cpu" device = "cpu"
if torch.cuda.is_available(): if torch.cuda.is_available():
device = "cuda" device = "cuda"
@ -28,12 +28,15 @@ if("privateuseone"not in device):
device = "mps" device = "mps"
else: else:
import torch_directml import torch_directml
device = torch_directml.device(torch_directml.default_device()) device = torch_directml.device(torch_directml.default_device())
def forward_dml(ctx, x, scale): def forward_dml(ctx, x, scale):
ctx.scale = scale ctx.scale = scale
res = x.clone().detach() res = x.clone().detach()
return res return res
fairseq.modules.grad_multiply.GradMultiply.forward=forward_dml
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
f = open("%s/extract_f0_feature.log" % exp_dir, "a+") f = open("%s/extract_f0_feature.log" % exp_dir, "a+")

View File

@ -1,5 +1,6 @@
import os, sys,pdb import os, sys, pdb
os.environ["OMP_NUM_THREADS"]="2"
os.environ["OMP_NUM_THREADS"] = "2"
if sys.platform == "darwin": if sys.platform == "darwin":
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
@ -47,8 +48,9 @@ if __name__ == "__main__":
import torchaudio.transforms as tat import torchaudio.transforms as tat
from i18n import I18nAuto from i18n import I18nAuto
import rvc_for_realtime import rvc_for_realtime
i18n = I18nAuto() i18n = I18nAuto()
device=rvc_for_realtime.config.device device = rvc_for_realtime.config.device
# device = torch.device( # device = torch.device(
# "cuda" # "cuda"
# if torch.cuda.is_available() # if torch.cuda.is_available()
@ -61,7 +63,6 @@ if __name__ == "__main__":
for _ in range(n_cpu): for _ in range(n_cpu):
Harvest(inp_q, opt_q).start() Harvest(inp_q, opt_q).start()
class GUIConfig: class GUIConfig:
def __init__(self) -> None: def __init__(self) -> None:
self.pth_path: str = "" self.pth_path: str = ""

View File

@ -43,9 +43,7 @@ logging.getLogger("numba").setLevel(logging.WARNING)
now_dir = os.getcwd() now_dir = os.getcwd()
tmp = os.path.join(now_dir, "TEMP") tmp = os.path.join(now_dir, "TEMP")
shutil.rmtree(tmp, ignore_errors=True) shutil.rmtree(tmp, ignore_errors=True)
shutil.rmtree( shutil.rmtree("%s/runtime/Lib/site-packages/infer_pack" % (now_dir), ignore_errors=True)
"%s/runtime/Lib/site-packages/infer_pack" % (now_dir), ignore_errors=True
)
shutil.rmtree("%s/runtime/Lib/site-packages/uvr5_pack" % (now_dir), ignore_errors=True) shutil.rmtree("%s/runtime/Lib/site-packages/uvr5_pack" % (now_dir), ignore_errors=True)
os.makedirs(tmp, exist_ok=True) os.makedirs(tmp, exist_ok=True)
os.makedirs(os.path.join(now_dir, "logs"), exist_ok=True) os.makedirs(os.path.join(now_dir, "logs"), exist_ok=True)
@ -56,12 +54,14 @@ torch.manual_seed(114514)
config = Config() config = Config()
if(config.dml==True): if config.dml == True:
def forward_dml(ctx, x, scale): def forward_dml(ctx, x, scale):
ctx.scale = scale ctx.scale = scale
res = x.clone().detach() res = x.clone().detach()
return res return res
fairseq.modules.grad_multiply.GradMultiply.forward=forward_dml
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
i18n = I18nAuto() i18n = I18nAuto()
i18n.print() i18n.print()
# 判断是否有能用来训练和加速推理的N卡 # 判断是否有能用来训练和加速推理的N卡
@ -451,15 +451,21 @@ def get_vc(sid, to_return_protect0, to_return_protect1):
del net_g, cpt del net_g, cpt
if torch.cuda.is_available(): if torch.cuda.is_available():
torch.cuda.empty_cache() torch.cuda.empty_cache()
return {"visible": False, "__type__": "update"},{ return (
"visible": True, {"visible": False, "__type__": "update"},
"value": to_return_protect0, {
"__type__": "update", "visible": True,
},{ "value": to_return_protect0,
"visible": True, "__type__": "update",
"value": to_return_protect1, },
"__type__": "update", {
},"","" "visible": True,
"value": to_return_protect1,
"__type__": "update",
},
"",
"",
)
person = "%s/%s" % (weight_root, sid) person = "%s/%s" % (weight_root, sid)
print("loading %s" % person) print("loading %s" % person)
@ -504,15 +510,13 @@ def get_vc(sid, to_return_protect0, to_return_protect1):
net_g = net_g.float() net_g = net_g.float()
vc = VC(tgt_sr, config) vc = VC(tgt_sr, config)
n_spk = cpt["config"][-3] n_spk = cpt["config"][-3]
index={ index = {"value": get_index_path_from_model(sid), "__type__": "update"}
"value":get_index_path_from_model(sid),
"__type__": "update"
}
return ( return (
{"visible": True, "maximum": n_spk, "__type__": "update"}, {"visible": True, "maximum": n_spk, "__type__": "update"},
to_return_protect0, to_return_protect0,
to_return_protect1, to_return_protect1,
index,index index,
index,
) )
@ -629,7 +633,7 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvp
), ),
).start() ).start()
else: else:
if(gpus_rmvpe!="-"): if gpus_rmvpe != "-":
gpus_rmvpe = gpus_rmvpe.split("-") gpus_rmvpe = gpus_rmvpe.split("-")
leng = len(gpus_rmvpe) leng = len(gpus_rmvpe)
ps = [] ps = []
@ -647,7 +651,7 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvp
###煞笔gr, popen read都非得全跑完了再一次性读取, 不用gr就正常读一句输出一句;只能额外弄出一个文本流定时读 ###煞笔gr, popen read都非得全跑完了再一次性读取, 不用gr就正常读一句输出一句;只能额外弄出一个文本流定时读
done = [False] done = [False]
threading.Thread( threading.Thread(
target=if_done_multi,# target=if_done_multi, #
args=( args=(
done, done,
ps, ps,
@ -655,7 +659,8 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvp
).start() ).start()
else: else:
cmd = config.python_cmd + ' extract_f0_rmvpe_dml.py "%s/logs/%s" ' % ( cmd = config.python_cmd + ' extract_f0_rmvpe_dml.py "%s/logs/%s" ' % (
now_dir, exp_dir now_dir,
exp_dir,
) )
print(cmd) print(cmd)
p = Popen( p = Popen(
@ -671,9 +676,7 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvp
sleep(1) sleep(1)
if done[0]: if done[0]:
break break
with open( with open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r") as f:
"%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r"
) as f:
log = f.read() log = f.read()
print(log) print(log)
yield log yield log
@ -971,7 +974,7 @@ def click_train(
# but4.click(train_index, [exp_dir1], info3) # but4.click(train_index, [exp_dir1], info3)
def train_index(exp_dir1, version19): def train_index(exp_dir1, version19):
# exp_dir = "%s/logs/%s" % (now_dir, exp_dir1) # exp_dir = "%s/logs/%s" % (now_dir, exp_dir1)
exp_dir = "logs/%s" % ( exp_dir1) exp_dir = "logs/%s" % (exp_dir1)
os.makedirs(exp_dir, exist_ok=True) os.makedirs(exp_dir, exist_ok=True)
feature_dir = ( feature_dir = (
"%s/3_feature256" % (exp_dir) "%s/3_feature256" % (exp_dir)
@ -1115,17 +1118,21 @@ def train1key(
p = Popen(cmd, shell=True, cwd=now_dir) p = Popen(cmd, shell=True, cwd=now_dir)
p.wait() p.wait()
else: else:
if(gpus_rmvpe!="-"): if gpus_rmvpe != "-":
gpus_rmvpe = gpus_rmvpe.split("-") gpus_rmvpe = gpus_rmvpe.split("-")
leng = len(gpus_rmvpe) leng = len(gpus_rmvpe)
ps = [] ps = []
for idx, n_g in enumerate(gpus_rmvpe): for idx, n_g in enumerate(gpus_rmvpe):
cmd = config.python_cmd + ' extract_f0_rmvpe.py %s %s %s "%s" %s ' % ( cmd = (
leng, config.python_cmd
idx, + ' extract_f0_rmvpe.py %s %s %s "%s" %s '
n_g, % (
model_log_dir, leng,
config.is_half, idx,
n_g,
model_log_dir,
config.is_half,
)
) )
yield get_info_str(cmd) yield get_info_str(cmd)
p = Popen( p = Popen(
@ -1134,7 +1141,7 @@ def train1key(
ps.append(p) ps.append(p)
for p in ps: for p in ps:
p.wait() p.wait()
else:#dml else: # dml
cmd = config.python_cmd + ' extract_f0_rmvpe_dml.py "%s" ' % ( cmd = config.python_cmd + ' extract_f0_rmvpe_dml.py "%s" ' % (
model_log_dir model_log_dir
) )
@ -1316,7 +1323,17 @@ def train1key(
index_ivf = faiss.extract_index_ivf(index) # index_ivf = faiss.extract_index_ivf(index) #
index_ivf.nprobe = 1 index_ivf.nprobe = 1
index.train(big_npy) index.train(big_npy)
faiss.write_index(index,"%s/trained_IVF%s_Flat_nprobe_%s_%s_%s.index"% (model_log_dir.replace(now_dir+"/",""), n_ivf, index_ivf.nprobe, exp_dir1, version19)) faiss.write_index(
index,
"%s/trained_IVF%s_Flat_nprobe_%s_%s_%s.index"
% (
model_log_dir.replace(now_dir + "/", ""),
n_ivf,
index_ivf.nprobe,
exp_dir1,
version19,
),
)
yield get_info_str("adding index") yield get_info_str("adding index")
batch_size_add = 8192 batch_size_add = 8192
for i in range(0, big_npy.shape[0], batch_size_add): for i in range(0, big_npy.shape[0], batch_size_add):
@ -1324,7 +1341,13 @@ def train1key(
faiss.write_index( faiss.write_index(
index, index,
"%s/added_IVF%s_Flat_nprobe_%s_%s_%s.index" "%s/added_IVF%s_Flat_nprobe_%s_%s_%s.index"
% (model_log_dir.replace(now_dir+"/",""), n_ivf, index_ivf.nprobe, exp_dir1, version19), % (
model_log_dir.replace(now_dir + "/", ""),
n_ivf,
index_ivf.nprobe,
exp_dir1,
version19,
),
) )
yield get_info_str( yield get_info_str(
"成功构建索引, added_IVF%s_Flat_nprobe_%s_%s_%s.index" "成功构建索引, added_IVF%s_Flat_nprobe_%s_%s_%s.index"
@ -1351,6 +1374,8 @@ def change_info_(ckpt_path):
F0GPUVisible = config.dml == False F0GPUVisible = config.dml == False
def change_f0_method(f0method8): def change_f0_method(f0method8):
if f0method8 == "rmvpe_gpu": if f0method8 == "rmvpe_gpu":
visible = F0GPUVisible visible = F0GPUVisible
@ -1450,7 +1475,9 @@ with gr.Blocks(title="RVC WebUI") as app:
label=i18n( label=i18n(
"选择音高提取算法,输入歌声可用pm提速,harvest低音好但巨慢无比,crepe效果好但吃GPU,rmvpe效果最好且微吃GPU" "选择音高提取算法,输入歌声可用pm提速,harvest低音好但巨慢无比,crepe效果好但吃GPU,rmvpe效果最好且微吃GPU"
), ),
choices=["pm", "harvest", "crepe", "rmvpe"]if config.dml==False else ["pm", "harvest", "rmvpe"], choices=["pm", "harvest", "crepe", "rmvpe"]
if config.dml == False
else ["pm", "harvest", "rmvpe"],
value="pm", value="pm",
interactive=True, interactive=True,
) )
@ -1556,7 +1583,9 @@ with gr.Blocks(title="RVC WebUI") as app:
label=i18n( label=i18n(
"选择音高提取算法,输入歌声可用pm提速,harvest低音好但巨慢无比,crepe效果好但吃GPU,rmvpe效果最好且微吃GPU" "选择音高提取算法,输入歌声可用pm提速,harvest低音好但巨慢无比,crepe效果好但吃GPU,rmvpe效果最好且微吃GPU"
), ),
choices=["pm", "harvest", "crepe", "rmvpe"]if config.dml==False else ["pm", "harvest", "rmvpe"], choices=["pm", "harvest", "crepe", "rmvpe"]
if config.dml == False
else ["pm", "harvest", "rmvpe"],
value="pm", value="pm",
interactive=True, interactive=True,
) )
@ -1790,9 +1819,12 @@ with gr.Blocks(title="RVC WebUI") as app:
gpus6 = gr.Textbox( gpus6 = gr.Textbox(
label=i18n("以-分隔输入使用的卡号, 例如 0-1-2 使用卡0和卡1和卡2"), label=i18n("以-分隔输入使用的卡号, 例如 0-1-2 使用卡0和卡1和卡2"),
value=gpus, value=gpus,
interactive=True,visible=F0GPUVisible interactive=True,
visible=F0GPUVisible,
)
gpu_info9 = gr.Textbox(
label=i18n("显卡信息"), value=gpu_info, visible=F0GPUVisible
) )
gpu_info9 = gr.Textbox(label=i18n("显卡信息"), value=gpu_info,visible=F0GPUVisible)
with gr.Column(): with gr.Column():
f0method8 = gr.Radio( f0method8 = gr.Radio(
label=i18n( label=i18n(

View File

@ -315,8 +315,8 @@ class SineGen(torch.nn.Module):
# generate uv signal # generate uv signal
uv = torch.ones_like(f0) uv = torch.ones_like(f0)
uv = uv * (f0 > self.voiced_threshold) uv = uv * (f0 > self.voiced_threshold)
if(uv.device.type=="privateuseone"):#for DirectML if uv.device.type == "privateuseone": # for DirectML
uv=uv.float() uv = uv.float()
return uv return uv
def forward(self, f0, upp): def forward(self, f0, upp):

View File

@ -1,14 +1,23 @@
import torch, numpy as np,pdb import torch, numpy as np, pdb
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
import torch,pdb import torch, pdb
import numpy as np import numpy as np
import torch.nn.functional as F import torch.nn.functional as F
from scipy.signal import get_window from scipy.signal import get_window
from librosa.util import pad_center, tiny,normalize from librosa.util import pad_center, tiny, normalize
###stft codes from https://github.com/pseeth/torch-stft/blob/master/torch_stft/util.py ###stft codes from https://github.com/pseeth/torch-stft/blob/master/torch_stft/util.py
def window_sumsquare(window, n_frames, hop_length=200, win_length=800, def window_sumsquare(
n_fft=800, dtype=np.float32, norm=None): window,
n_frames,
hop_length=200,
win_length=800,
n_fft=800,
dtype=np.float32,
norm=None,
):
""" """
# from librosa 0.6 # from librosa 0.6
Compute the sum-square envelope of a window function at a given hop length. Compute the sum-square envelope of a window function at a given hop length.
@ -41,18 +50,20 @@ def window_sumsquare(window, n_frames, hop_length=200, win_length=800,
# Compute the squared window at the desired length # Compute the squared window at the desired length
win_sq = get_window(window, win_length, fftbins=True) win_sq = get_window(window, win_length, fftbins=True)
win_sq = normalize(win_sq, norm=norm)**2 win_sq = normalize(win_sq, norm=norm) ** 2
win_sq = pad_center(win_sq, n_fft) win_sq = pad_center(win_sq, n_fft)
# Fill the envelope # Fill the envelope
for i in range(n_frames): for i in range(n_frames):
sample = i * hop_length sample = i * hop_length
x[sample:min(n, sample + n_fft)] += win_sq[:max(0, min(n_fft, n - sample))] x[sample : min(n, sample + n_fft)] += win_sq[: max(0, min(n_fft, n - sample))]
return x return x
class STFT(torch.nn.Module): class STFT(torch.nn.Module):
def __init__(self, filter_length=1024, hop_length=512, win_length=None, def __init__(
window='hann'): self, filter_length=1024, hop_length=512, win_length=None, window="hann"
):
""" """
This module implements an STFT using 1D convolution and 1D transpose convolutions. This module implements an STFT using 1D convolution and 1D transpose convolutions.
This is a bit tricky so there are some cases that probably won't work as working This is a bit tricky so there are some cases that probably won't work as working
@ -79,12 +90,15 @@ class STFT(torch.nn.Module):
fourier_basis = np.fft.fft(np.eye(self.filter_length)) fourier_basis = np.fft.fft(np.eye(self.filter_length))
cutoff = int((self.filter_length / 2 + 1)) cutoff = int((self.filter_length / 2 + 1))
fourier_basis = np.vstack([np.real(fourier_basis[:cutoff, :]),np.imag(fourier_basis[:cutoff, :])]) fourier_basis = np.vstack(
[np.real(fourier_basis[:cutoff, :]), np.imag(fourier_basis[:cutoff, :])]
)
forward_basis = torch.FloatTensor(fourier_basis[:, None, :]) forward_basis = torch.FloatTensor(fourier_basis[:, None, :])
inverse_basis = torch.FloatTensor( inverse_basis = torch.FloatTensor(
np.linalg.pinv(scale * fourier_basis).T[:, None, :]) np.linalg.pinv(scale * fourier_basis).T[:, None, :]
)
assert (filter_length >= self.win_length) assert filter_length >= self.win_length
# get window and zero center pad it to filter_length # get window and zero center pad it to filter_length
fft_window = get_window(window, self.win_length, fftbins=True) fft_window = get_window(window, self.win_length, fftbins=True)
fft_window = pad_center(fft_window, size=filter_length) fft_window = pad_center(fft_window, size=filter_length)
@ -94,8 +108,8 @@ class STFT(torch.nn.Module):
forward_basis *= fft_window forward_basis *= fft_window
inverse_basis *= fft_window inverse_basis *= fft_window
self.register_buffer('forward_basis', forward_basis.float()) self.register_buffer("forward_basis", forward_basis.float())
self.register_buffer('inverse_basis', inverse_basis.float()) self.register_buffer("inverse_basis", inverse_basis.float())
def transform(self, input_data): def transform(self, input_data):
"""Take input data (audio) to STFT domain. """Take input data (audio) to STFT domain.
@ -117,23 +131,25 @@ class STFT(torch.nn.Module):
# similar to librosa, reflect-pad the input # similar to librosa, reflect-pad the input
input_data = input_data.view(num_batches, 1, num_samples) input_data = input_data.view(num_batches, 1, num_samples)
# print(1234,input_data.shape) # print(1234,input_data.shape)
input_data = F.pad(input_data.unsqueeze(1),(self.pad_amount, self.pad_amount, 0, 0,0,0),mode='reflect').squeeze(1) input_data = F.pad(
input_data.unsqueeze(1),
(self.pad_amount, self.pad_amount, 0, 0, 0, 0),
mode="reflect",
).squeeze(1)
# print(2333,input_data.shape,self.forward_basis.shape,self.hop_length) # print(2333,input_data.shape,self.forward_basis.shape,self.hop_length)
# pdb.set_trace() # pdb.set_trace()
forward_transform = F.conv1d( forward_transform = F.conv1d(
input_data, input_data, self.forward_basis, stride=self.hop_length, padding=0
self.forward_basis, )
stride=self.hop_length,
padding=0)
cutoff = int((self.filter_length / 2) + 1) cutoff = int((self.filter_length / 2) + 1)
real_part = forward_transform[:, :cutoff, :] real_part = forward_transform[:, :cutoff, :]
imag_part = forward_transform[:, cutoff:, :] imag_part = forward_transform[:, cutoff:, :]
magnitude = torch.sqrt(real_part ** 2 + imag_part ** 2) magnitude = torch.sqrt(real_part**2 + imag_part**2)
# phase = torch.atan2(imag_part.data, real_part.data) # phase = torch.atan2(imag_part.data, real_part.data)
return magnitude#, phase return magnitude # , phase
def inverse(self, magnitude, phase): def inverse(self, magnitude, phase):
"""Call the inverse STFT (iSTFT), given magnitude and phase tensors produced """Call the inverse STFT (iSTFT), given magnitude and phase tensors produced
@ -150,30 +166,39 @@ class STFT(torch.nn.Module):
shape (num_batch, num_samples) shape (num_batch, num_samples)
""" """
recombine_magnitude_phase = torch.cat( recombine_magnitude_phase = torch.cat(
[magnitude * torch.cos(phase), magnitude * torch.sin(phase)], dim=1) [magnitude * torch.cos(phase), magnitude * torch.sin(phase)], dim=1
)
inverse_transform = F.conv_transpose1d( inverse_transform = F.conv_transpose1d(
recombine_magnitude_phase, recombine_magnitude_phase,
self.inverse_basis, self.inverse_basis,
stride=self.hop_length, stride=self.hop_length,
padding=0) padding=0,
)
if self.window is not None: if self.window is not None:
window_sum = window_sumsquare( window_sum = window_sumsquare(
self.window, magnitude.size(-1), hop_length=self.hop_length, self.window,
win_length=self.win_length, n_fft=self.filter_length, magnitude.size(-1),
dtype=np.float32) hop_length=self.hop_length,
win_length=self.win_length,
n_fft=self.filter_length,
dtype=np.float32,
)
# remove modulation effects # remove modulation effects
approx_nonzero_indices = torch.from_numpy( approx_nonzero_indices = torch.from_numpy(
np.where(window_sum > tiny(window_sum))[0]) np.where(window_sum > tiny(window_sum))[0]
)
window_sum = torch.from_numpy(window_sum).to(inverse_transform.device) window_sum = torch.from_numpy(window_sum).to(inverse_transform.device)
inverse_transform[:, :, approx_nonzero_indices] /= window_sum[approx_nonzero_indices] inverse_transform[:, :, approx_nonzero_indices] /= window_sum[
approx_nonzero_indices
]
# scale by hop ratio # scale by hop ratio
inverse_transform *= float(self.filter_length) / self.hop_length inverse_transform *= float(self.filter_length) / self.hop_length
inverse_transform = inverse_transform[..., self.pad_amount:] inverse_transform = inverse_transform[..., self.pad_amount :]
inverse_transform = inverse_transform[..., :self.num_samples] inverse_transform = inverse_transform[..., : self.num_samples]
inverse_transform = inverse_transform.squeeze(1) inverse_transform = inverse_transform.squeeze(1)
return inverse_transform return inverse_transform
@ -191,7 +216,11 @@ class STFT(torch.nn.Module):
self.magnitude, self.phase = self.transform(input_data) self.magnitude, self.phase = self.transform(input_data)
reconstruction = self.inverse(self.magnitude, self.phase) reconstruction = self.inverse(self.magnitude, self.phase)
return reconstruction return reconstruction
from time import time as ttime from time import time as ttime
class BiGRU(nn.Module): class BiGRU(nn.Module):
def __init__(self, input_features, hidden_features, num_layers): def __init__(self, input_features, hidden_features, num_layers):
super(BiGRU, self).__init__() super(BiGRU, self).__init__()
@ -509,14 +538,14 @@ class MelSpectrogram(torch.nn.Module):
# print(1111111111) # print(1111111111)
# print(222222222222222,audio.device,self.is_half) # print(222222222222222,audio.device,self.is_half)
if hasattr(self, "stft") == False: if hasattr(self, "stft") == False:
# print(n_fft_new,hop_length_new,win_length_new,audio.shape) # print(n_fft_new,hop_length_new,win_length_new,audio.shape)
self.stft=STFT( self.stft = STFT(
filter_length=n_fft_new, filter_length=n_fft_new,
hop_length=hop_length_new, hop_length=hop_length_new,
win_length=win_length_new, win_length=win_length_new,
window='hann' window="hann",
).to(audio.device) ).to(audio.device)
magnitude = self.stft.transform(audio)#phase magnitude = self.stft.transform(audio) # phase
# if (audio.device.type == "privateuseone"): # if (audio.device.type == "privateuseone"):
# magnitude=magnitude.to(audio.device) # magnitude=magnitude.to(audio.device)
if keyshift != 0: if keyshift != 0:
@ -544,10 +573,13 @@ class RMVPE:
self.mel_extractor = MelSpectrogram( self.mel_extractor = MelSpectrogram(
is_half, 128, 16000, 1024, 160, None, 30, 8000 is_half, 128, 16000, 1024, 160, None, 30, 8000
).to(device) ).to(device)
if ("privateuseone" in str(device)): if "privateuseone" in str(device):
import onnxruntime as ort import onnxruntime as ort
ort_session = ort.InferenceSession("rmvpe.onnx", providers=["DmlExecutionProvider"])
self.model=ort_session ort_session = ort.InferenceSession(
"rmvpe.onnx", providers=["DmlExecutionProvider"]
)
self.model = ort_session
else: else:
model = E2E(4, 1, (2, 2)) model = E2E(4, 1, (2, 2))
ckpt = torch.load(model_path, map_location="cpu") ckpt = torch.load(model_path, map_location="cpu")
@ -566,10 +598,13 @@ class RMVPE:
mel = F.pad( mel = F.pad(
mel, (0, 32 * ((n_frames - 1) // 32 + 1) - n_frames), mode="reflect" mel, (0, 32 * ((n_frames - 1) // 32 + 1) - n_frames), mode="reflect"
) )
if("privateuseone" in str(self.device) ): if "privateuseone" in str(self.device):
onnx_input_name = self.model.get_inputs()[0].name onnx_input_name = self.model.get_inputs()[0].name
onnx_outputs_names = self.model.get_outputs()[0].name onnx_outputs_names = self.model.get_outputs()[0].name
hidden = self.model.run([onnx_outputs_names], input_feed={onnx_input_name: mel.cpu().numpy()})[0] hidden = self.model.run(
[onnx_outputs_names],
input_feed={onnx_input_name: mel.cpu().numpy()},
)[0]
else: else:
hidden = self.model(mel) hidden = self.model(mel)
return hidden[:, :n_frames] return hidden[:, :n_frames]
@ -583,25 +618,27 @@ class RMVPE:
def infer_from_audio(self, audio, thred=0.03): def infer_from_audio(self, audio, thred=0.03):
# torch.cuda.synchronize() # torch.cuda.synchronize()
t0=ttime() t0 = ttime()
mel = self.mel_extractor(torch.from_numpy(audio).float().to(self.device).unsqueeze(0), center=True) mel = self.mel_extractor(
torch.from_numpy(audio).float().to(self.device).unsqueeze(0), center=True
)
# print(123123123,mel.device.type) # print(123123123,mel.device.type)
# torch.cuda.synchronize() # torch.cuda.synchronize()
t1=ttime() t1 = ttime()
hidden = self.mel2hidden(mel) hidden = self.mel2hidden(mel)
# torch.cuda.synchronize() # torch.cuda.synchronize()
t2=ttime() t2 = ttime()
# print(234234,hidden.device.type) # print(234234,hidden.device.type)
if("privateuseone" not in str(self.device)): if "privateuseone" not in str(self.device):
hidden = hidden.squeeze(0).cpu().numpy() hidden = hidden.squeeze(0).cpu().numpy()
else: else:
hidden=hidden[0] hidden = hidden[0]
if self.is_half == True: if self.is_half == True:
hidden = hidden.astype("float32") hidden = hidden.astype("float32")
f0 = self.decode(hidden, thred=thred) f0 = self.decode(hidden, thred=thred)
# torch.cuda.synchronize() # torch.cuda.synchronize()
t3=ttime() t3 = ttime()
# print("hmvpe:%s\t%s\t%s\t%s"%(t1-t0,t2-t1,t3-t2,t3-t0)) # print("hmvpe:%s\t%s\t%s\t%s"%(t1-t0,t2-t1,t3-t2,t3-t0))
return f0 return f0
@ -632,8 +669,9 @@ class RMVPE:
return devided return devided
if __name__ == '__main__': if __name__ == "__main__":
import soundfile as sf, librosa import soundfile as sf, librosa
audio, sampling_rate = sf.read(r"C:\Users\liujing04\Desktop\Z\冬之花clip1.wav") audio, sampling_rate = sf.read(r"C:\Users\liujing04\Desktop\Z\冬之花clip1.wav")
if len(audio.shape) > 1: if len(audio.shape) > 1:
audio = librosa.to_mono(audio.transpose(1, 0)) audio = librosa.to_mono(audio.transpose(1, 0))
@ -642,13 +680,13 @@ if __name__ == '__main__':
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000) audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
model_path = r"D:\BaiduNetdiskDownload\RVC-beta-v2-0727AMD_realtime\rmvpe.pt" model_path = r"D:\BaiduNetdiskDownload\RVC-beta-v2-0727AMD_realtime\rmvpe.pt"
thred = 0.03 # 0.01 thred = 0.03 # 0.01
device = 'cuda' if torch.cuda.is_available() else 'cpu' device = "cuda" if torch.cuda.is_available() else "cpu"
rmvpe = RMVPE(model_path,is_half=False, device=device) rmvpe = RMVPE(model_path, is_half=False, device=device)
t0=ttime() t0 = ttime()
f0 = rmvpe.infer_from_audio(audio, thred=thred) f0 = rmvpe.infer_from_audio(audio, thred=thred)
# f0 = rmvpe.infer_from_audio(audio, thred=thred) # f0 = rmvpe.infer_from_audio(audio, thred=thred)
# f0 = rmvpe.infer_from_audio(audio, thred=thred) # f0 = rmvpe.infer_from_audio(audio, thred=thred)
# f0 = rmvpe.infer_from_audio(audio, thred=thred) # f0 = rmvpe.infer_from_audio(audio, thred=thred)
# f0 = rmvpe.infer_from_audio(audio, thred=thred) # f0 = rmvpe.infer_from_audio(audio, thred=thred)
t1=ttime() t1 = ttime()
print(f0.shape,t1-t0) print(f0.shape, t1 - t0)

View File

@ -18,12 +18,16 @@ from multiprocessing import Manager as M
mm = M() mm = M()
config = Config() config = Config()
if(config.dml==True): if config.dml == True:
def forward_dml(ctx, x, scale): def forward_dml(ctx, x, scale):
ctx.scale = scale ctx.scale = scale
res = x.clone().detach() res = x.clone().detach()
return res return res
fairseq.modules.grad_multiply.GradMultiply.forward=forward_dml
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
# config.device=torch.device("cpu")########强制cpu测试 # config.device=torch.device("cpu")########强制cpu测试
# config.is_half=False########强制cpu测试 # config.is_half=False########强制cpu测试
class RVC: class RVC:
@ -183,8 +187,8 @@ class RVC:
return self.get_f0_post(f0bak) return self.get_f0_post(f0bak)
def get_f0_crepe(self, x, f0_up_key): def get_f0_crepe(self, x, f0_up_key):
if self.device.type == "privateuseone":###不支持dmlcpu又太慢用不成拿pm顶替 if self.device.type == "privateuseone": ###不支持dmlcpu又太慢用不成拿pm顶替
return self.get_f0(x, f0_up_key,1,"pm") return self.get_f0(x, f0_up_key, 1, "pm")
audio = torch.tensor(np.copy(x))[None].float() audio = torch.tensor(np.copy(x))[None].float()
# print("using crepe,device:%s"%self.device) # print("using crepe,device:%s"%self.device)
f0, pd = torchcrepe.predict( f0, pd = torchcrepe.predict(
@ -209,12 +213,15 @@ class RVC:
def get_f0_rmvpe(self, x, f0_up_key): def get_f0_rmvpe(self, x, f0_up_key):
if hasattr(self, "model_rmvpe") == False: if hasattr(self, "model_rmvpe") == False:
from lib.rmvpe import RMVPE from lib.rmvpe import RMVPE
print("loading rmvpe model") print("loading rmvpe model")
self.model_rmvpe = RMVPE( self.model_rmvpe = RMVPE(
# "rmvpe.pt", is_half=self.is_half if self.device.type!="privateuseone" else False, device=self.device if self.device.type!="privateuseone"else "cpu"####dml时强制对rmvpe用cpu跑 # "rmvpe.pt", is_half=self.is_half if self.device.type!="privateuseone" else False, device=self.device if self.device.type!="privateuseone"else "cpu"####dml时强制对rmvpe用cpu跑
# "rmvpe.pt", is_half=False, device=self.device####dml配置 # "rmvpe.pt", is_half=False, device=self.device####dml配置
# "rmvpe.pt", is_half=False, device="cpu"####锁定cpu配置 # "rmvpe.pt", is_half=False, device="cpu"####锁定cpu配置
"rmvpe.pt", is_half=self.is_half, device=self.device####正常逻辑 "rmvpe.pt",
is_half=self.is_half,
device=self.device, ####正常逻辑
) )
# self.model_rmvpe = RMVPE("aug2_58000_half.pt", is_half=self.is_half, device=self.device) # self.model_rmvpe = RMVPE("aug2_58000_half.pt", is_half=self.is_half, device=self.device)
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03) f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)

View File

@ -130,13 +130,14 @@ class VC(object):
elif f0_method == "rmvpe": elif f0_method == "rmvpe":
if hasattr(self, "model_rmvpe") == False: if hasattr(self, "model_rmvpe") == False:
from lib.rmvpe import RMVPE from lib.rmvpe import RMVPE
print("loading rmvpe model") print("loading rmvpe model")
self.model_rmvpe = RMVPE( self.model_rmvpe = RMVPE(
"rmvpe.pt", is_half=self.is_half, device=self.device "rmvpe.pt", is_half=self.is_half, device=self.device
) )
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03) f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
if("privateuseone"in str(self.device)):#clean ortruntime memory if "privateuseone" in str(self.device): # clean ortruntime memory
del self.model_rmvpe.model del self.model_rmvpe.model
del self.model_rmvpe del self.model_rmvpe
print("cleaning ortruntime memory") print("cleaning ortruntime memory")