1
0
mirror of synced 2024-11-24 07:30:16 +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,6 +83,7 @@ def get_models(device, dim_f, dim_t, n_fft):
warnings.filterwarnings("ignore")
import sys
now_dir = os.getcwd()
sys.path.append(now_dir)
from config import Config
@ -104,10 +105,15 @@ class Predictor:
device=cpu, dim_f=args.dim_f, dim_t=args.dim_t, n_fft=args.n_fft
)
import onnxruntime as ort
print(ort.get_available_providers())
self.model = ort.InferenceSession(
os.path.join(args.onnx, self.model_.target_name + ".onnx"),
providers=["CUDAExecutionProvider", "DmlExecutionProvider","CPUExecutionProvider"],
providers=[
"CUDAExecutionProvider",
"DmlExecutionProvider",
"CPUExecutionProvider",
],
)
print("onnx load done")

View File

@ -36,7 +36,7 @@ class Config:
self.iscolab,
self.noparallel,
self.noautoopen,
self.dml
self.dml,
) = self.arg_parse()
self.instead = ""
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.noparallel,
cmd_opts.noautoopen,
cmd_opts.dml
cmd_opts.dml,
)
# has_mps is only available in nightly pytorch (for now) and MasOS 12.3+.
@ -149,26 +149,38 @@ class Config:
if self.dml:
print("use DirectML instead")
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:
pass
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:
pass
import torch_directml
self.device = torch_directml.device(torch_directml.default_device())
self.is_half = False
else:
if self.instead:
print(f"use {self.instead} instead")
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:
pass
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:
pass
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]
import torch_directml
device = torch_directml.device(torch_directml.default_device())
f = open("%s/extract_f0_feature.log" % exp_dir, "a+")

View File

@ -20,7 +20,7 @@ import soundfile as sf
import numpy as np
import fairseq
if("privateuseone"not in device):
if "privateuseone" not in device:
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
@ -28,11 +28,14 @@ if("privateuseone"not in device):
device = "mps"
else:
import torch_directml
device = torch_directml.device(torch_directml.default_device())
def forward_dml(ctx, x, scale):
ctx.scale = scale
res = x.clone().detach()
return res
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
f = open("%s/extract_f0_feature.log" % exp_dir, "a+")

View File

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

View File

@ -43,9 +43,7 @@ logging.getLogger("numba").setLevel(logging.WARNING)
now_dir = os.getcwd()
tmp = os.path.join(now_dir, "TEMP")
shutil.rmtree(tmp, ignore_errors=True)
shutil.rmtree(
"%s/runtime/Lib/site-packages/infer_pack" % (now_dir), ignore_errors=True
)
shutil.rmtree("%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)
os.makedirs(tmp, exist_ok=True)
os.makedirs(os.path.join(now_dir, "logs"), exist_ok=True)
@ -56,11 +54,13 @@ torch.manual_seed(114514)
config = Config()
if(config.dml==True):
if config.dml == True:
def forward_dml(ctx, x, scale):
ctx.scale = scale
res = x.clone().detach()
return res
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
i18n = I18nAuto()
i18n.print()
@ -451,15 +451,21 @@ def get_vc(sid, to_return_protect0, to_return_protect1):
del net_g, cpt
if torch.cuda.is_available():
torch.cuda.empty_cache()
return {"visible": False, "__type__": "update"},{
return (
{"visible": False, "__type__": "update"},
{
"visible": True,
"value": to_return_protect0,
"__type__": "update",
},{
},
{
"visible": True,
"value": to_return_protect1,
"__type__": "update",
},"",""
},
"",
"",
)
person = "%s/%s" % (weight_root, sid)
print("loading %s" % person)
@ -504,15 +510,13 @@ def get_vc(sid, to_return_protect0, to_return_protect1):
net_g = net_g.float()
vc = VC(tgt_sr, config)
n_spk = cpt["config"][-3]
index={
"value":get_index_path_from_model(sid),
"__type__": "update"
}
index = {"value": get_index_path_from_model(sid), "__type__": "update"}
return (
{"visible": True, "maximum": n_spk, "__type__": "update"},
to_return_protect0,
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()
else:
if(gpus_rmvpe!="-"):
if gpus_rmvpe != "-":
gpus_rmvpe = gpus_rmvpe.split("-")
leng = len(gpus_rmvpe)
ps = []
@ -655,7 +659,8 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvp
).start()
else:
cmd = config.python_cmd + ' extract_f0_rmvpe_dml.py "%s/logs/%s" ' % (
now_dir, exp_dir
now_dir,
exp_dir,
)
print(cmd)
p = Popen(
@ -671,9 +676,7 @@ def extract_f0_feature(gpus, n_p, f0method, if_f0, exp_dir, version19, gpus_rmvp
sleep(1)
if done[0]:
break
with open(
"%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r"
) as f:
with open("%s/logs/%s/extract_f0_feature.log" % (now_dir, exp_dir), "r") as f:
log = f.read()
print(log)
yield log
@ -1115,18 +1118,22 @@ def train1key(
p = Popen(cmd, shell=True, cwd=now_dir)
p.wait()
else:
if(gpus_rmvpe!="-"):
if gpus_rmvpe != "-":
gpus_rmvpe = gpus_rmvpe.split("-")
leng = len(gpus_rmvpe)
ps = []
for idx, n_g in enumerate(gpus_rmvpe):
cmd = config.python_cmd + ' extract_f0_rmvpe.py %s %s %s "%s" %s ' % (
cmd = (
config.python_cmd
+ ' extract_f0_rmvpe.py %s %s %s "%s" %s '
% (
leng,
idx,
n_g,
model_log_dir,
config.is_half,
)
)
yield get_info_str(cmd)
p = Popen(
cmd, shell=True, cwd=now_dir
@ -1316,7 +1323,17 @@ def train1key(
index_ivf = faiss.extract_index_ivf(index) #
index_ivf.nprobe = 1
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")
batch_size_add = 8192
for i in range(0, big_npy.shape[0], batch_size_add):
@ -1324,7 +1341,13 @@ def train1key(
faiss.write_index(
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(
"成功构建索引, added_IVF%s_Flat_nprobe_%s_%s_%s.index"
@ -1351,6 +1374,8 @@ def change_info_(ckpt_path):
F0GPUVisible = config.dml == False
def change_f0_method(f0method8):
if f0method8 == "rmvpe_gpu":
visible = F0GPUVisible
@ -1450,7 +1475,9 @@ with gr.Blocks(title="RVC WebUI") as app:
label=i18n(
"选择音高提取算法,输入歌声可用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",
interactive=True,
)
@ -1556,7 +1583,9 @@ with gr.Blocks(title="RVC WebUI") as app:
label=i18n(
"选择音高提取算法,输入歌声可用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",
interactive=True,
)
@ -1790,9 +1819,12 @@ with gr.Blocks(title="RVC WebUI") as app:
gpus6 = gr.Textbox(
label=i18n("以-分隔输入使用的卡号, 例如 0-1-2 使用卡0和卡1和卡2"),
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():
f0method8 = gr.Radio(
label=i18n(

View File

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

View File

@ -6,9 +6,18 @@ import numpy as np
import torch.nn.functional as F
from scipy.signal import get_window
from librosa.util import pad_center, tiny, normalize
###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,
n_fft=800, dtype=np.float32, norm=None):
def window_sumsquare(
window,
n_frames,
hop_length=200,
win_length=800,
n_fft=800,
dtype=np.float32,
norm=None,
):
"""
# from librosa 0.6
Compute the sum-square envelope of a window function at a given hop length.
@ -50,9 +59,11 @@ def window_sumsquare(window, n_frames, hop_length=200, win_length=800,
x[sample : min(n, sample + n_fft)] += win_sq[: max(0, min(n_fft, n - sample))]
return x
class STFT(torch.nn.Module):
def __init__(self, filter_length=1024, hop_length=512, win_length=None,
window='hann'):
def __init__(
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 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))
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, :])
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
fft_window = get_window(window, self.win_length, fftbins=True)
fft_window = pad_center(fft_window, size=filter_length)
@ -94,8 +108,8 @@ class STFT(torch.nn.Module):
forward_basis *= fft_window
inverse_basis *= fft_window
self.register_buffer('forward_basis', forward_basis.float())
self.register_buffer('inverse_basis', inverse_basis.float())
self.register_buffer("forward_basis", forward_basis.float())
self.register_buffer("inverse_basis", inverse_basis.float())
def transform(self, input_data):
"""Take input data (audio) to STFT domain.
@ -117,14 +131,16 @@ class STFT(torch.nn.Module):
# similar to librosa, reflect-pad the input
input_data = input_data.view(num_batches, 1, num_samples)
# 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)
# pdb.set_trace()
forward_transform = F.conv1d(
input_data,
self.forward_basis,
stride=self.hop_length,
padding=0)
input_data, self.forward_basis, stride=self.hop_length, padding=0
)
cutoff = int((self.filter_length / 2) + 1)
real_part = forward_transform[:, :cutoff, :]
@ -150,24 +166,33 @@ class STFT(torch.nn.Module):
shape (num_batch, num_samples)
"""
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(
recombine_magnitude_phase,
self.inverse_basis,
stride=self.hop_length,
padding=0)
padding=0,
)
if self.window is not None:
window_sum = window_sumsquare(
self.window, magnitude.size(-1), hop_length=self.hop_length,
win_length=self.win_length, n_fft=self.filter_length,
dtype=np.float32)
self.window,
magnitude.size(-1),
hop_length=self.hop_length,
win_length=self.win_length,
n_fft=self.filter_length,
dtype=np.float32,
)
# remove modulation effects
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)
inverse_transform[:, :, approx_nonzero_indices] /= window_sum[approx_nonzero_indices]
inverse_transform[:, :, approx_nonzero_indices] /= window_sum[
approx_nonzero_indices
]
# scale by hop ratio
inverse_transform *= float(self.filter_length) / self.hop_length
@ -191,7 +216,11 @@ class STFT(torch.nn.Module):
self.magnitude, self.phase = self.transform(input_data)
reconstruction = self.inverse(self.magnitude, self.phase)
return reconstruction
from time import time as ttime
class BiGRU(nn.Module):
def __init__(self, input_features, hidden_features, num_layers):
super(BiGRU, self).__init__()
@ -514,7 +543,7 @@ class MelSpectrogram(torch.nn.Module):
filter_length=n_fft_new,
hop_length=hop_length_new,
win_length=win_length_new,
window='hann'
window="hann",
).to(audio.device)
magnitude = self.stft.transform(audio) # phase
# if (audio.device.type == "privateuseone"):
@ -544,9 +573,12 @@ class RMVPE:
self.mel_extractor = MelSpectrogram(
is_half, 128, 16000, 1024, 160, None, 30, 8000
).to(device)
if ("privateuseone" in str(device)):
if "privateuseone" in str(device):
import onnxruntime as ort
ort_session = ort.InferenceSession("rmvpe.onnx", providers=["DmlExecutionProvider"])
ort_session = ort.InferenceSession(
"rmvpe.onnx", providers=["DmlExecutionProvider"]
)
self.model = ort_session
else:
model = E2E(4, 1, (2, 2))
@ -566,10 +598,13 @@ class RMVPE:
mel = F.pad(
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_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:
hidden = self.model(mel)
return hidden[:, :n_frames]
@ -584,7 +619,9 @@ class RMVPE:
def infer_from_audio(self, audio, thred=0.03):
# torch.cuda.synchronize()
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)
# torch.cuda.synchronize()
t1 = ttime()
@ -592,7 +629,7 @@ class RMVPE:
# torch.cuda.synchronize()
t2 = ttime()
# 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()
else:
hidden = hidden[0]
@ -632,8 +669,9 @@ class RMVPE:
return devided
if __name__ == '__main__':
if __name__ == "__main__":
import soundfile as sf, librosa
audio, sampling_rate = sf.read(r"C:\Users\liujing04\Desktop\Z\冬之花clip1.wav")
if len(audio.shape) > 1:
audio = librosa.to_mono(audio.transpose(1, 0))
@ -642,7 +680,7 @@ if __name__ == '__main__':
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
model_path = r"D:\BaiduNetdiskDownload\RVC-beta-v2-0727AMD_realtime\rmvpe.pt"
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)
t0 = ttime()
f0 = rmvpe.infer_from_audio(audio, thred=thred)

View File

@ -18,12 +18,16 @@ from multiprocessing import Manager as M
mm = M()
config = Config()
if(config.dml==True):
if config.dml == True:
def forward_dml(ctx, x, scale):
ctx.scale = scale
res = x.clone().detach()
return res
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
# config.device=torch.device("cpu")########强制cpu测试
# config.is_half=False########强制cpu测试
class RVC:
@ -209,12 +213,15 @@ class RVC:
def get_f0_rmvpe(self, x, f0_up_key):
if hasattr(self, "model_rmvpe") == False:
from lib.rmvpe import RMVPE
print("loading rmvpe model")
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=False, device=self.device####dml配置
# "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)
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)

View File

@ -130,13 +130,14 @@ class VC(object):
elif f0_method == "rmvpe":
if hasattr(self, "model_rmvpe") == False:
from lib.rmvpe import RMVPE
print("loading rmvpe model")
self.model_rmvpe = RMVPE(
"rmvpe.pt", is_half=self.is_half, device=self.device
)
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
print("cleaning ortruntime memory")