1
0
mirror of https://github.com/fumiama/Retrieval-based-Voice-Conversion-WebUI.git synced 2026-06-05 01:10:22 +08:00

optimize(f0): move some f0s into rvc.f0

This commit is contained in:
源文雨
2024-06-13 00:10:22 +09:00
parent d44a942882
commit 77b371d615
15 changed files with 91 additions and 185 deletions

View File

@@ -2,7 +2,7 @@ import torch
def get_rmvpe(model_path="assets/rmvpe/rmvpe.pt", device=torch.device("cpu")):
from infer.lib.rmvpe import E2E
from rvc.f0.e2e import E2E
model = E2E(4, 1, (2, 2))
ckpt = torch.load(model_path, map_location=device)

View File

@@ -6,17 +6,6 @@ import torch
from infer.lib import jit
try:
# Fix "Torch not compiled with CUDA enabled"
import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import
if torch.xpu.is_available():
from infer.modules.ipex import ipex_init
ipex_init()
except Exception: # pylint: disable=broad-exception-caught
pass
import torch.nn as nn
import torch.nn.functional as F
import logging
@@ -127,13 +116,13 @@ class RMVPE:
return hidden[:, :n_frames]
def decode(self, hidden, thred=0.03):
cents_pred = self.to_local_average_cents(hidden, thred=thred)
cents_pred = self.to_local_average_cents(hidden, threshold=thred)
f0 = 10 * (2 ** (cents_pred / 1200))
f0[f0 == 10] = 0
# f0 = np.array([10 * (2 ** (cent_pred / 1200)) if cent_pred else 0 for cent_pred in cents_pred])
return f0
def infer_from_audio(self, audio, thred=0.03):
def infer_from_audio(self, audio, threshold=0.03):
# torch.cuda.synchronize()
# t0 = ttime()
if not torch.is_tensor(audio):
@@ -155,17 +144,15 @@ class RMVPE:
if self.is_half == True:
hidden = hidden.astype("float32")
f0 = self.decode(hidden, thred=thred)
f0 = self.decode(hidden, thred=threshold)
# torch.cuda.synchronize()
# t3 = ttime()
# print("hmvpe:%s\t%s\t%s\t%s"%(t1-t0,t2-t1,t3-t2,t3-t0))
return f0
def to_local_average_cents(self, salience, thred=0.05):
# t0 = ttime()
def to_local_average_cents(self, salience, threshold=0.05):
center = np.argmax(salience, axis=1) # 帧长#index
salience = np.pad(salience, ((0, 0), (4, 4))) # 帧长,368
# t1 = ttime()
center += 4
todo_salience = []
todo_cents_mapping = []
@@ -174,15 +161,11 @@ class RMVPE:
for idx in range(salience.shape[0]):
todo_salience.append(salience[:, starts[idx] : ends[idx]][idx])
todo_cents_mapping.append(self.cents_mapping[starts[idx] : ends[idx]])
# t2 = ttime()
todo_salience = np.array(todo_salience) # 帧长9
todo_cents_mapping = np.array(todo_cents_mapping) # 帧长9
product_sum = np.sum(todo_salience * todo_cents_mapping, 1)
weight_sum = np.sum(todo_salience, 1) # 帧长
devided = product_sum / weight_sum # 帧长
# t3 = ttime()
maxx = np.max(salience, axis=1) # 帧长
devided[maxx <= thred] = 0
# t4 = ttime()
# print("decode:%s\t%s\t%s\t%s" % (t1 - t0, t2 - t1, t3 - t2, t4 - t3))
devided[maxx <= threshold] = 0
return devided

View File

@@ -89,7 +89,7 @@ class FeatureInput(object):
self.model_rmvpe = RMVPE(
"assets/rmvpe/rmvpe.pt", is_half=False, device="cpu"
)
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
f0 = self.model_rmvpe.infer_from_audio(x, threshold=0.03)
return f0
def coarse_f0(self, f0):

View File

@@ -52,7 +52,7 @@ class FeatureInput(object):
self.model_rmvpe = RMVPE(
"assets/rmvpe/rmvpe.pt", is_half=is_half, device="cuda"
)
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
f0 = self.model_rmvpe.infer_from_audio(x, threshold=0.03)
return f0
def coarse_f0(self, f0):

View File

@@ -50,7 +50,7 @@ class FeatureInput(object):
self.model_rmvpe = RMVPE(
"assets/rmvpe/rmvpe.pt", is_half=False, device=device
)
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
f0 = self.model_rmvpe.infer_from_audio(x, threshold=0.03)
return f0
def coarse_f0(self, f0):

View File

@@ -47,7 +47,7 @@ from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from rvc import utils
from rvc.layers import utils
from infer.lib.train.data_utils import (
DistributedBucketSampler,
TextAudioCollate,

View File

@@ -5,40 +5,24 @@ import logging
logger = logging.getLogger(__name__)
from functools import lru_cache
from time import time
import faiss
import librosa
import numpy as np
import parselmouth
import pyworld
import torch
import torch.nn.functional as F
import torchcrepe
from scipy import signal
from rvc.f0 import PM, Harvest
now_dir = os.getcwd()
sys.path.append(now_dir)
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
input_audio_path2wav = {}
@lru_cache
def cache_harvest_f0(f0_cache_key, fs, f0max, f0min, frame_period):
audio = input_audio_path2wav[f0_cache_key]
f0, t = pyworld.harvest(
audio,
fs=fs,
f0_ceil=f0max,
f0_floor=f0min,
frame_period=frame_period,
)
f0 = pyworld.stonemask(audio, f0, t, fs)
return f0
def change_rms(data1, sr1, data2, sr2, rate): # 1是输入音频2是输出音频,rate是2的占比
# print(data1.max(),data2.max())
@@ -90,37 +74,18 @@ class Pipeline(object):
filter_radius,
inp_f0=None,
):
global input_audio_path2wav
time_step = self.window / self.sr * 1000
f0_min = 50
f0_max = 1100
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
if f0_method == "pm":
f0 = (
parselmouth.Sound(x, self.sr)
.to_pitch_ac(
time_step=time_step / 1000,
voicing_threshold=0.6,
pitch_floor=f0_min,
pitch_ceiling=f0_max,
)
.selected_array["frequency"]
)
pad_size = (p_len - len(f0) + 1) // 2
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
f0 = np.pad(
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
)
if not hasattr(self, "pm"):
self.pm = PM(self.window, f0_min, f0_max, self.sr)
f0 = self.pm.compute_f0(x, p_len=p_len)
elif f0_method == "harvest":
from hashlib import md5
f0_cache_key = md5(x.tobytes()).digest()
input_audio_path2wav[f0_cache_key] = x.astype(np.double)
f0 = cache_harvest_f0(f0_cache_key, self.sr, f0_max, f0_min, 10)
del input_audio_path2wav[f0_cache_key]
if filter_radius > 2:
f0 = signal.medfilt(f0, 3)
if not hasattr(self, "harvest"):
self.harvest = Harvest(self.window, f0_min, f0_max, self.sr)
f0 = self.harvest.compute_f0(x, p_len=p_len, filter_radius=filter_radius)
elif f0_method == "crepe":
model = "full"
# Pick a batch size that doesn't cause memory errors on your gpu
@@ -155,7 +120,7 @@ class Pipeline(object):
device=self.device,
# use_jit=self.config.use_jit,
)
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
f0 = self.model_rmvpe.infer_from_audio(x, threshold=0.03)
if "privateuseone" in str(self.device): # clean ortruntime memory
del self.model_rmvpe.model