Mirror lj1995/VoiceConversionWebUI @ b2c8cae96e3b — infer_uvr5.py
Browse files- infer_uvr5.py +108 -0
infer_uvr5.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os,sys,torch,warnings,pdb
|
| 2 |
+
warnings.filterwarnings("ignore")
|
| 3 |
+
import librosa
|
| 4 |
+
import importlib
|
| 5 |
+
import numpy as np
|
| 6 |
+
import hashlib , math
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
from uvr5_pack.lib_v5 import spec_utils
|
| 9 |
+
from uvr5_pack.utils import _get_name_params,inference
|
| 10 |
+
from uvr5_pack.lib_v5.model_param_init import ModelParameters
|
| 11 |
+
from scipy.io import wavfile
|
| 12 |
+
|
| 13 |
+
class _audio_pre_():
|
| 14 |
+
def __init__(self, model_path,device,is_half):
|
| 15 |
+
self.model_path = model_path
|
| 16 |
+
self.device = device
|
| 17 |
+
self.data = {
|
| 18 |
+
# Processing Options
|
| 19 |
+
'postprocess': False,
|
| 20 |
+
'tta': False,
|
| 21 |
+
# Constants
|
| 22 |
+
'window_size': 512,
|
| 23 |
+
'agg': 10,
|
| 24 |
+
'high_end_process': 'mirroring',
|
| 25 |
+
}
|
| 26 |
+
nn_arch_sizes = [
|
| 27 |
+
31191, # default
|
| 28 |
+
33966,61968, 123821, 123812, 537238 # custom
|
| 29 |
+
]
|
| 30 |
+
self.nn_architecture = list('{}KB'.format(s) for s in nn_arch_sizes)
|
| 31 |
+
model_size = math.ceil(os.stat(model_path ).st_size / 1024)
|
| 32 |
+
nn_architecture = '{}KB'.format(min(nn_arch_sizes, key=lambda x:abs(x-model_size)))
|
| 33 |
+
nets = importlib.import_module('uvr5_pack.lib_v5.nets' + f'_{nn_architecture}'.replace('_{}KB'.format(nn_arch_sizes[0]), ''), package=None)
|
| 34 |
+
model_hash = hashlib.md5(open(model_path,'rb').read()).hexdigest()
|
| 35 |
+
param_name ,model_params_d = _get_name_params(model_path , model_hash)
|
| 36 |
+
|
| 37 |
+
mp = ModelParameters(model_params_d)
|
| 38 |
+
model = nets.CascadedASPPNet(mp.param['bins'] * 2)
|
| 39 |
+
cpk = torch.load( model_path , map_location='cpu')
|
| 40 |
+
model.load_state_dict(cpk)
|
| 41 |
+
model.eval()
|
| 42 |
+
if(is_half==True):model = model.half().to(device)
|
| 43 |
+
else:model = model.to(device)
|
| 44 |
+
|
| 45 |
+
self.mp = mp
|
| 46 |
+
self.model = model
|
| 47 |
+
|
| 48 |
+
def _path_audio_(self, music_file ,ins_root=None,vocal_root=None):
|
| 49 |
+
if(ins_root is None and vocal_root is None):return "No save root."
|
| 50 |
+
name=os.path.basename(music_file)
|
| 51 |
+
if(ins_root is not None):os.makedirs(ins_root, exist_ok=True)
|
| 52 |
+
if(vocal_root is not None):os.makedirs(vocal_root , exist_ok=True)
|
| 53 |
+
X_wave, y_wave, X_spec_s, y_spec_s = {}, {}, {}, {}
|
| 54 |
+
bands_n = len(self.mp.param['band'])
|
| 55 |
+
# print(bands_n)
|
| 56 |
+
for d in range(bands_n, 0, -1):
|
| 57 |
+
bp = self.mp.param['band'][d]
|
| 58 |
+
if d == bands_n: # high-end band
|
| 59 |
+
X_wave[d], _ = librosa.core.load(
|
| 60 |
+
music_file, bp['sr'], False, dtype=np.float32, res_type=bp['res_type'])
|
| 61 |
+
if X_wave[d].ndim == 1:
|
| 62 |
+
X_wave[d] = np.asfortranarray([X_wave[d], X_wave[d]])
|
| 63 |
+
else: # lower bands
|
| 64 |
+
X_wave[d] = librosa.core.resample(X_wave[d+1], self.mp.param['band'][d+1]['sr'], bp['sr'], res_type=bp['res_type'])
|
| 65 |
+
# Stft of wave source
|
| 66 |
+
X_spec_s[d] = spec_utils.wave_to_spectrogram_mt(X_wave[d], bp['hl'], bp['n_fft'], self.mp.param['mid_side'], self.mp.param['mid_side_b2'], self.mp.param['reverse'])
|
| 67 |
+
# pdb.set_trace()
|
| 68 |
+
if d == bands_n and self.data['high_end_process'] != 'none':
|
| 69 |
+
input_high_end_h = (bp['n_fft']//2 - bp['crop_stop']) + ( self.mp.param['pre_filter_stop'] - self.mp.param['pre_filter_start'])
|
| 70 |
+
input_high_end = X_spec_s[d][:, bp['n_fft']//2-input_high_end_h:bp['n_fft']//2, :]
|
| 71 |
+
|
| 72 |
+
X_spec_m = spec_utils.combine_spectrograms(X_spec_s, self.mp)
|
| 73 |
+
aggresive_set = float(self.data['agg']/100)
|
| 74 |
+
aggressiveness = {'value': aggresive_set, 'split_bin': self.mp.param['band'][1]['crop_stop']}
|
| 75 |
+
with torch.no_grad():
|
| 76 |
+
pred, X_mag, X_phase = inference(X_spec_m,self.device,self.model, aggressiveness,self.data)
|
| 77 |
+
# Postprocess
|
| 78 |
+
if self.data['postprocess']:
|
| 79 |
+
pred_inv = np.clip(X_mag - pred, 0, np.inf)
|
| 80 |
+
pred = spec_utils.mask_silence(pred, pred_inv)
|
| 81 |
+
y_spec_m = pred * X_phase
|
| 82 |
+
v_spec_m = X_spec_m - y_spec_m
|
| 83 |
+
|
| 84 |
+
if (ins_root is not None):
|
| 85 |
+
if self.data['high_end_process'].startswith('mirroring'):
|
| 86 |
+
input_high_end_ = spec_utils.mirroring(self.data['high_end_process'], y_spec_m, input_high_end, self.mp)
|
| 87 |
+
wav_instrument = spec_utils.cmb_spectrogram_to_wave(y_spec_m, self.mp,input_high_end_h, input_high_end_)
|
| 88 |
+
else:
|
| 89 |
+
wav_instrument = spec_utils.cmb_spectrogram_to_wave(y_spec_m, self.mp)
|
| 90 |
+
print ('%s instruments done'%name)
|
| 91 |
+
wavfile.write(os.path.join(ins_root, 'instrument_{}.wav'.format(name) ), self.mp.param['sr'], (np.array(wav_instrument)*32768).astype("int16")) #
|
| 92 |
+
if (vocal_root is not None):
|
| 93 |
+
if self.data['high_end_process'].startswith('mirroring'):
|
| 94 |
+
input_high_end_ = spec_utils.mirroring(self.data['high_end_process'], v_spec_m, input_high_end, self.mp)
|
| 95 |
+
wav_vocals = spec_utils.cmb_spectrogram_to_wave(v_spec_m, self.mp, input_high_end_h, input_high_end_)
|
| 96 |
+
else:
|
| 97 |
+
wav_vocals = spec_utils.cmb_spectrogram_to_wave(v_spec_m, self.mp)
|
| 98 |
+
print ('%s vocals done'%name)
|
| 99 |
+
wavfile.write(os.path.join(vocal_root , 'vocal_{}.wav'.format(name) ), self.mp.param['sr'], (np.array(wav_vocals)*32768).astype("int16"))
|
| 100 |
+
|
| 101 |
+
if __name__ == '__main__':
|
| 102 |
+
device = 'cuda'
|
| 103 |
+
is_half=True
|
| 104 |
+
model_path='uvr5_weights/2_HP-UVR.pth'
|
| 105 |
+
pre_fun = _audio_pre_(model_path=model_path,device=device,is_half=True)
|
| 106 |
+
audio_path = '神女劈观.aac'
|
| 107 |
+
save_path = 'opt'
|
| 108 |
+
pre_fun._path_audio_(audio_path , save_path,save_path)
|