mirror of
https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git
synced 2026-01-20 11:00:23 +00:00
优化代码结构
This commit is contained in:
@@ -1,199 +0,0 @@
|
||||
"""
|
||||
|
||||
对源特征进行检索
|
||||
"""
|
||||
import torch, pdb, os, parselmouth
|
||||
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
||||
import numpy as np
|
||||
import soundfile as sf
|
||||
|
||||
# from models import SynthesizerTrn256#hifigan_nonsf
|
||||
# from infer_pack.models import SynthesizerTrn256NSF as SynthesizerTrn256#hifigan_nsf
|
||||
from infer_pack.models import (
|
||||
SynthesizerTrnMs256NSFsid as SynthesizerTrn256,
|
||||
) # hifigan_nsf
|
||||
|
||||
# from infer_pack.models import SynthesizerTrnMs256NSFsid_sim as SynthesizerTrn256#hifigan_nsf
|
||||
# from models import SynthesizerTrn256NSFsim as SynthesizerTrn256#hifigan_nsf
|
||||
# from models import SynthesizerTrn256NSFsimFlow as SynthesizerTrn256#hifigan_nsf
|
||||
|
||||
|
||||
from scipy.io import wavfile
|
||||
from fairseq import checkpoint_utils
|
||||
|
||||
# import pyworld
|
||||
import librosa
|
||||
import torch.nn.functional as F
|
||||
import scipy.signal as signal
|
||||
|
||||
# import torchcrepe
|
||||
from time import time as ttime
|
||||
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
model_path = r"E:\codes\py39\vits_vc_gpu_train\hubert_base.pt" #
|
||||
print("load model(s) from {}".format(model_path))
|
||||
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
|
||||
[model_path],
|
||||
suffix="",
|
||||
)
|
||||
model = models[0]
|
||||
model = model.to(device)
|
||||
model = model.half()
|
||||
model.eval()
|
||||
|
||||
# net_g = SynthesizerTrn256(1025,32,192,192,768,2,6,3,0.1,"1", [3,7,11],[[1,3,5], [1,3,5], [1,3,5]],[10,10,2,2],512,[16,16,4,4],183,256,is_half=True)#hifigan#512#256
|
||||
# net_g = SynthesizerTrn256(1025,32,192,192,768,2,6,3,0.1,"1", [3,7,11],[[1,3,5], [1,3,5], [1,3,5]],[10,10,2,2],512,[16,16,4,4],109,256,is_half=True)#hifigan#512#256
|
||||
net_g = SynthesizerTrn256(
|
||||
1025,
|
||||
32,
|
||||
192,
|
||||
192,
|
||||
768,
|
||||
2,
|
||||
6,
|
||||
3,
|
||||
0,
|
||||
"1",
|
||||
[3, 7, 11],
|
||||
[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
|
||||
[10, 10, 2, 2],
|
||||
512,
|
||||
[16, 16, 4, 4],
|
||||
183,
|
||||
256,
|
||||
is_half=True,
|
||||
) # hifigan#512#256#no_dropout
|
||||
# net_g = SynthesizerTrn256(1025,32,192,192,768,2,3,3,0.1,"1", [3,7,11],[[1,3,5], [1,3,5], [1,3,5]],[10,10,2,2],512,[16,16,4,4],0)#ts3
|
||||
# net_g = SynthesizerTrn256(1025,32,192,192,768,2,6,3,0.1,"1", [3,7,11],[[1,3,5], [1,3,5], [1,3,5]],[10,10,2],512,[16,16,4],0)#hifigan-ps-sr
|
||||
#
|
||||
# net_g = SynthesizerTrn(1025, 32, 192, 192, 768, 2, 6, 3, 0.1, "1", [3, 7, 11], [[1, 3, 5], [1, 3, 5], [1, 3, 5]], [5,5], 512, [15,15], 0)#ms
|
||||
# net_g = SynthesizerTrn(1025, 32, 192, 192, 768, 2, 6, 3, 0.1, "1", [3, 7, 11], [[1, 3, 5], [1, 3, 5], [1, 3, 5]], [10,10], 512, [16,16], 0)#idwt2
|
||||
|
||||
# weights=torch.load("infer/ft-mi_1k-noD.pt")
|
||||
# weights=torch.load("infer/ft-mi-freeze-vocoder-flow-enc_q_1k.pt")
|
||||
# weights=torch.load("infer/ft-mi-freeze-vocoder_true_1k.pt")
|
||||
# weights=torch.load("infer/ft-mi-sim1k.pt")
|
||||
weights = torch.load("infer/ft-mi-no_opt-no_dropout.pt")
|
||||
print(net_g.load_state_dict(weights, strict=True))
|
||||
|
||||
net_g.eval().to(device)
|
||||
net_g.half()
|
||||
|
||||
|
||||
def get_f0(x, p_len, f0_up_key=0):
|
||||
time_step = 160 / 16000 * 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)
|
||||
|
||||
f0 = (
|
||||
parselmouth.Sound(x, 16000)
|
||||
.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")
|
||||
f0 *= pow(2, f0_up_key / 12)
|
||||
f0bak = f0.copy()
|
||||
|
||||
f0_mel = 1127 * np.log(1 + f0 / 700)
|
||||
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
||||
f0_mel_max - f0_mel_min
|
||||
) + 1
|
||||
f0_mel[f0_mel <= 1] = 1
|
||||
f0_mel[f0_mel > 255] = 255
|
||||
# f0_mel[f0_mel > 188] = 188
|
||||
f0_coarse = np.rint(f0_mel).astype(np.int)
|
||||
return f0_coarse, f0bak
|
||||
|
||||
|
||||
import faiss
|
||||
|
||||
index = faiss.read_index("infer/added_IVF512_Flat_mi_baseline_src_feat.index")
|
||||
big_npy = np.load("infer/big_src_feature_mi.npy")
|
||||
ta0 = ta1 = ta2 = 0
|
||||
for idx, name in enumerate(
|
||||
[
|
||||
"冬之花clip1.wav",
|
||||
]
|
||||
): ##
|
||||
wav_path = "todo-songs/%s" % name #
|
||||
f0_up_key = -2 #
|
||||
audio, sampling_rate = sf.read(wav_path)
|
||||
if len(audio.shape) > 1:
|
||||
audio = librosa.to_mono(audio.transpose(1, 0))
|
||||
if sampling_rate != 16000:
|
||||
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
|
||||
|
||||
feats = torch.from_numpy(audio).float()
|
||||
if feats.dim() == 2: # double channels
|
||||
feats = feats.mean(-1)
|
||||
assert feats.dim() == 1, feats.dim()
|
||||
feats = feats.view(1, -1)
|
||||
padding_mask = torch.BoolTensor(feats.shape).fill_(False)
|
||||
inputs = {
|
||||
"source": feats.half().to(device),
|
||||
"padding_mask": padding_mask.to(device),
|
||||
"output_layer": 9, # layer 9
|
||||
}
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.synchronize()
|
||||
t0 = ttime()
|
||||
with torch.no_grad():
|
||||
logits = model.extract_features(**inputs)
|
||||
feats = model.final_proj(logits[0])
|
||||
|
||||
####索引优化
|
||||
npy = feats[0].cpu().numpy().astype("float32")
|
||||
D, I = index.search(npy, 1)
|
||||
feats = (
|
||||
torch.from_numpy(big_npy[I.squeeze()].astype("float16")).unsqueeze(0).to(device)
|
||||
)
|
||||
|
||||
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.synchronize()
|
||||
t1 = ttime()
|
||||
# p_len = min(feats.shape[1],10000,pitch.shape[0])#太大了爆显存
|
||||
p_len = min(feats.shape[1], 10000) #
|
||||
pitch, pitchf = get_f0(audio, p_len, f0_up_key)
|
||||
p_len = min(feats.shape[1], 10000, pitch.shape[0]) # 太大了爆显存
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.synchronize()
|
||||
t2 = ttime()
|
||||
feats = feats[:, :p_len, :]
|
||||
pitch = pitch[:p_len]
|
||||
pitchf = pitchf[:p_len]
|
||||
p_len = torch.LongTensor([p_len]).to(device)
|
||||
pitch = torch.LongTensor(pitch).unsqueeze(0).to(device)
|
||||
sid = torch.LongTensor([0]).to(device)
|
||||
pitchf = torch.FloatTensor(pitchf).unsqueeze(0).to(device)
|
||||
with torch.no_grad():
|
||||
audio = (
|
||||
net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0]
|
||||
.data.cpu()
|
||||
.float()
|
||||
.numpy()
|
||||
) # nsf
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.synchronize()
|
||||
t3 = ttime()
|
||||
ta0 += t1 - t0
|
||||
ta1 += t2 - t1
|
||||
ta2 += t3 - t2
|
||||
# wavfile.write("ft-mi_1k-index256-noD-%s.wav"%name, 40000, audio)##
|
||||
# wavfile.write("ft-mi-freeze-vocoder-flow-enc_q_1k-%s.wav"%name, 40000, audio)##
|
||||
# wavfile.write("ft-mi-sim1k-%s.wav"%name, 40000, audio)##
|
||||
wavfile.write("ft-mi-no_opt-no_dropout-%s.wav" % name, 40000, audio) ##
|
||||
|
||||
|
||||
print(ta0, ta1, ta2) #
|
||||
@@ -1,44 +0,0 @@
|
||||
"""
|
||||
格式:直接cid为自带的index位;aid放不下了,通过字典来查,反正就5w个
|
||||
"""
|
||||
import faiss, numpy as np, os
|
||||
|
||||
# ###########如果是原始特征要先写save
|
||||
inp_root = r"./logs/nene/3_feature768"
|
||||
npys = []
|
||||
listdir_res = list(os.listdir(inp_root))
|
||||
for name in sorted(listdir_res):
|
||||
phone = np.load("%s/%s" % (inp_root, name))
|
||||
npys.append(phone)
|
||||
big_npy = np.concatenate(npys, 0)
|
||||
big_npy_idx = np.arange(big_npy.shape[0])
|
||||
np.random.shuffle(big_npy_idx)
|
||||
big_npy = big_npy[big_npy_idx]
|
||||
print(big_npy.shape) # (6196072, 192)#fp32#4.43G
|
||||
np.save("infer/big_src_feature_mi.npy", big_npy)
|
||||
|
||||
##################train+add
|
||||
# big_npy=np.load("/bili-coeus/jupyter/jupyterhub-liujing04/vits_ch/inference_f0/big_src_feature_mi.npy")
|
||||
n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
|
||||
index = faiss.index_factory(768, "IVF%s,Flat" % n_ivf) # mi
|
||||
print("training")
|
||||
index_ivf = faiss.extract_index_ivf(index) #
|
||||
index_ivf.nprobe = 1
|
||||
index.train(big_npy)
|
||||
faiss.write_index(
|
||||
index, "infer/trained_IVF%s_Flat_baseline_src_feat_v2.index" % (n_ivf)
|
||||
)
|
||||
print("adding")
|
||||
batch_size_add = 8192
|
||||
for i in range(0, big_npy.shape[0], batch_size_add):
|
||||
index.add(big_npy[i : i + batch_size_add])
|
||||
faiss.write_index(index, "infer/added_IVF%s_Flat_mi_baseline_src_feat.index" % (n_ivf))
|
||||
"""
|
||||
大小(都是FP32)
|
||||
big_src_feature 2.95G
|
||||
(3098036, 256)
|
||||
big_emb 4.43G
|
||||
(6196072, 192)
|
||||
big_emb双倍是因为求特征要repeat后再加pitch
|
||||
|
||||
"""
|
||||
@@ -1,36 +0,0 @@
|
||||
"""
|
||||
格式:直接cid为自带的index位;aid放不下了,通过字典来查,反正就5w个
|
||||
"""
|
||||
import faiss, numpy as np, os
|
||||
|
||||
# ###########如果是原始特征要先写save
|
||||
inp_root = r"E:\codes\py39\dataset\mi\2-co256"
|
||||
npys = []
|
||||
for name in sorted(list(os.listdir(inp_root))):
|
||||
phone = np.load("%s/%s" % (inp_root, name))
|
||||
npys.append(phone)
|
||||
big_npy = np.concatenate(npys, 0)
|
||||
print(big_npy.shape) # (6196072, 192)#fp32#4.43G
|
||||
np.save("infer/big_src_feature_mi.npy", big_npy)
|
||||
|
||||
##################train+add
|
||||
# big_npy=np.load("/bili-coeus/jupyter/jupyterhub-liujing04/vits_ch/inference_f0/big_src_feature_mi.npy")
|
||||
print(big_npy.shape)
|
||||
index = faiss.index_factory(256, "IVF512,Flat") # mi
|
||||
print("training")
|
||||
index_ivf = faiss.extract_index_ivf(index) #
|
||||
index_ivf.nprobe = 9
|
||||
index.train(big_npy)
|
||||
faiss.write_index(index, "infer/trained_IVF512_Flat_mi_baseline_src_feat.index")
|
||||
print("adding")
|
||||
index.add(big_npy)
|
||||
faiss.write_index(index, "infer/added_IVF512_Flat_mi_baseline_src_feat.index")
|
||||
"""
|
||||
大小(都是FP32)
|
||||
big_src_feature 2.95G
|
||||
(3098036, 256)
|
||||
big_emb 4.43G
|
||||
(6196072, 192)
|
||||
big_emb双倍是因为求特征要repeat后再加pitch
|
||||
|
||||
"""
|
||||
@@ -1,16 +0,0 @@
|
||||
import torch, pdb
|
||||
|
||||
# a=torch.load(r"E:\codes\py39\vits_vc_gpu_train\logs\ft-mi-suc\G_1000.pth")["model"]#sim_nsf#
|
||||
# a=torch.load(r"E:\codes\py39\vits_vc_gpu_train\logs\ft-mi-freeze-vocoder-flow-enc_q\G_1000.pth")["model"]#sim_nsf#
|
||||
# a=torch.load(r"E:\codes\py39\vits_vc_gpu_train\logs\ft-mi-freeze-vocoder\G_1000.pth")["model"]#sim_nsf#
|
||||
# a=torch.load(r"E:\codes\py39\vits_vc_gpu_train\logs\ft-mi-test\G_1000.pth")["model"]#sim_nsf#
|
||||
a = torch.load(
|
||||
r"E:\codes\py39\vits_vc_gpu_train\logs\ft-mi-no_opt-no_dropout\G_1000.pth"
|
||||
)[
|
||||
"model"
|
||||
] # sim_nsf#
|
||||
for key in a.keys():
|
||||
a[key] = a[key].half()
|
||||
# torch.save(a,"ft-mi-freeze-vocoder_true_1k.pt")#
|
||||
# torch.save(a,"ft-mi-sim1k.pt")#
|
||||
torch.save(a, "ft-mi-no_opt-no_dropout.pt") #
|
||||
Reference in New Issue
Block a user