Apply Code Formatter Change

This commit is contained in:
Tps-F 2023-08-19 11:01:49 +00:00 committed by github-actions[bot]
parent 055864cc90
commit de0c1399c8
2 changed files with 90 additions and 53 deletions

View File

@ -6,9 +6,18 @@ 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.
@ -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))] 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,14 +131,16 @@ 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, :]
@ -150,24 +166,33 @@ 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
@ -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__()
@ -514,7 +543,7 @@ class MelSpectrogram(torch.nn.Module):
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"):
@ -544,9 +573,12 @@ 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"])
ort_session = ort.InferenceSession(
"rmvpe.onnx", providers=["DmlExecutionProvider"]
)
self.model = ort_session self.model = ort_session
else: else:
model = E2E(4, 1, (2, 2)) model = E2E(4, 1, (2, 2))
@ -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]
@ -584,7 +619,9 @@ 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()
@ -592,7 +629,7 @@ class RMVPE:
# 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]
@ -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,7 +680,7 @@ 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)

View File

@ -31,4 +31,3 @@ def load_hubert(config):
else: else:
hubert_model = hubert_model.float() hubert_model = hubert_model.float()
return hubert_model.eval() return hubert_model.eval()