voice-changer/server/voice_changer/SoVitsSvc40/SoVitsSvc40.py

501 lines
16 KiB
Python
Raw Permalink Normal View History

2023-03-18 19:43:36 +03:00
import sys
import os
2023-06-20 00:39:39 +03:00
from data.ModelSlot import SoVitsSvc40ModelSlot
2023-08-05 06:33:31 +03:00
from voice_changer.VoiceChangerParamsManager import VoiceChangerParamsManager
2023-04-28 08:12:19 +03:00
2023-11-08 13:54:13 +03:00
from voice_changer.utils.VoiceChangerModel import AudioInOut, VoiceChangerModel
2023-04-28 08:12:19 +03:00
from voice_changer.utils.VoiceChangerParams import VoiceChangerParams
if sys.platform.startswith("darwin"):
2023-03-18 19:43:36 +03:00
baseDir = [x for x in sys.path if x.endswith("Contents/MacOS")]
if len(baseDir) != 1:
print("baseDir should be only one ", baseDir)
sys.exit()
modulePath = os.path.join(baseDir[0], "so-vits-svc-40")
sys.path.append(modulePath)
else:
sys.path.append("so-vits-svc-40")
from dataclasses import dataclass, asdict, field
import numpy as np
import torch
import onnxruntime
2023-04-28 08:12:19 +03:00
2023-03-30 05:11:41 +03:00
# onnxruntime.set_default_logger_severity(3)
2023-03-18 19:43:36 +03:00
import pyworld as pw
2023-06-22 03:21:39 +03:00
# from models import SynthesizerTrn # type:ignore
from .models.models import SynthesizerTrn
2023-11-08 13:54:13 +03:00
from .models.utils import (
interpolate_f0,
get_hparams_from_file,
load_checkpoint,
repeat_expand_2d,
get_hubert_content,
)
2023-06-22 03:21:39 +03:00
from .models.cluster import get_cluster_model, get_cluster_center_result
2023-03-18 19:43:36 +03:00
from fairseq import checkpoint_utils
import librosa
2023-04-17 03:45:12 +03:00
from Exceptions import NoModeLoadedException
2023-04-28 08:12:19 +03:00
providers = [
"OpenVINOExecutionProvider",
"CUDAExecutionProvider",
"DmlExecutionProvider",
"CPUExecutionProvider",
]
2023-03-18 19:43:36 +03:00
@dataclass
2023-04-28 08:12:19 +03:00
class SoVitsSvc40Settings:
2023-07-23 13:39:52 +03:00
gpu: int = -9999
2023-03-18 19:43:36 +03:00
dstId: int = 0
2023-04-28 16:22:42 +03:00
f0Detector: str = "harvest" # dio or harvest
2023-03-18 19:43:36 +03:00
tran: int = 20
2023-04-20 11:17:43 +03:00
noiseScale: float = 0.3
2023-03-18 19:43:36 +03:00
predictF0: int = 0 # 0:False, 1:True
silentThreshold: float = 0.00001
extraConvertSize: int = 1024 * 32
clusterInferRatio: float = 0.1
2023-04-28 08:12:19 +03:00
speakers: dict[str, int] = field(default_factory=lambda: {})
2023-03-18 19:43:36 +03:00
# ↓mutableな物だけ列挙
2023-06-22 01:40:06 +03:00
intData = ["gpu", "dstId", "tran", "predictF0"]
2023-04-20 11:17:43 +03:00
floatData = ["noiseScale", "silentThreshold", "clusterInferRatio"]
2023-06-22 01:40:06 +03:00
strData = ["f0Detector"]
2023-03-18 19:43:36 +03:00
2023-11-08 13:54:13 +03:00
class SoVitsSvc40(VoiceChangerModel):
2023-06-22 01:40:06 +03:00
def __init__(self, params: VoiceChangerParams, slotInfo: SoVitsSvc40ModelSlot):
print("[Voice Changer] [so-vits-svc40] Creating instance ")
2023-11-08 13:54:13 +03:00
self.voiceChangerType = "so-vits-svc-40"
2023-03-18 19:43:36 +03:00
self.settings = SoVitsSvc40Settings()
self.net_g = None
self.onnx_session = None
self.params = params
2023-04-18 21:06:45 +03:00
2023-03-18 19:43:36 +03:00
# hubert model
try:
2023-06-22 01:40:06 +03:00
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
[self.params.hubert_base],
suffix="",
)
model = models[0]
model.eval()
self.hubert_model = model.cpu()
2023-03-18 19:43:36 +03:00
except Exception as e:
print("EXCEPTION during loading hubert/contentvec model", e)
2023-06-22 01:40:06 +03:00
self.gpu_num = torch.cuda.device_count()
self.audio_buffer: AudioInOut | None = None
self.prevVol = 0
self.slotInfo = slotInfo
self.initialize()
def initialize(self):
print("[Voice Changer] [so-vits-svc40] Initializing... ")
2023-08-05 06:33:31 +03:00
vcparams = VoiceChangerParamsManager.get_instance().params
2023-11-08 13:54:13 +03:00
configPath = os.path.join(
vcparams.model_dir, str(self.slotInfo.slotIndex), self.slotInfo.configFile
)
modelPath = os.path.join(
vcparams.model_dir, str(self.slotInfo.slotIndex), self.slotInfo.modelFile
)
2023-08-05 06:33:31 +03:00
self.hps = get_hparams_from_file(configPath)
2023-06-22 01:40:06 +03:00
self.settings.speakers = self.hps.spk
2023-03-18 19:43:36 +03:00
# cluster
try:
2023-06-22 01:40:06 +03:00
if self.slotInfo.clusterFile is not None:
2023-11-08 13:54:13 +03:00
clusterPath = os.path.join(
vcparams.model_dir,
str(self.slotInfo.slotIndex),
self.slotInfo.clusterFile,
)
2023-08-05 06:33:31 +03:00
self.cluster_model = get_cluster_model(clusterPath)
2023-03-18 19:43:36 +03:00
else:
self.cluster_model = None
except Exception as e:
2023-11-08 13:54:13 +03:00
print(
"[Voice Changer] [so-vits-svc40] EXCEPTION during loading cluster model ",
e,
)
2023-06-22 01:40:06 +03:00
print("[Voice Changer] [so-vits-svc40] fallback to without cluster")
self.cluster_model = None
2023-03-18 19:43:36 +03:00
2023-06-22 01:40:06 +03:00
# model
if self.slotInfo.isONNX:
providers, options = self.getOnnxExecutionProvider()
self.onnx_session = onnxruntime.InferenceSession(
2023-08-05 06:33:31 +03:00
modelPath,
2023-06-22 01:40:06 +03:00
providers=providers,
provider_options=options,
)
else:
2023-04-28 08:12:19 +03:00
net_g = SynthesizerTrn(
2023-03-18 19:43:36 +03:00
self.hps.data.filter_length // 2 + 1,
self.hps.train.segment_size // self.hps.data.hop_length,
2023-04-28 08:12:19 +03:00
**self.hps.model,
2023-03-18 19:43:36 +03:00
)
2023-04-28 08:12:19 +03:00
net_g.eval()
self.net_g = net_g
2023-08-05 06:33:31 +03:00
load_checkpoint(modelPath, self.net_g, None)
2023-03-18 19:43:36 +03:00
def getOnnxExecutionProvider(self):
2023-05-29 11:34:35 +03:00
availableProviders = onnxruntime.get_available_providers()
2023-06-22 01:40:06 +03:00
devNum = torch.cuda.device_count()
2023-11-08 13:54:13 +03:00
if (
self.settings.gpu >= 0
and "CUDAExecutionProvider" in availableProviders
and devNum > 0
):
return ["CUDAExecutionProvider"], [{"device_id": self.settings.gpu}]
2023-05-29 11:34:35 +03:00
elif self.settings.gpu >= 0 and "DmlExecutionProvider" in availableProviders:
return ["DmlExecutionProvider"], [{}]
else:
return ["CPUExecutionProvider"], [
{
"intra_op_num_threads": 8,
"execution_mode": onnxruntime.ExecutionMode.ORT_PARALLEL,
"inter_op_num_threads": 8,
}
]
def update_settings(self, key: str, val: int | float | str):
if key in self.settings.intData:
2023-04-28 08:12:19 +03:00
val = int(val)
setattr(self.settings, key, val)
2023-06-22 01:40:06 +03:00
if key == "gpu" and self.slotInfo.isONNX:
providers, options = self.getOnnxExecutionProvider()
if self.onnx_session is not None:
2023-04-28 08:12:19 +03:00
self.onnx_session.set_providers(
providers=providers,
provider_options=options,
)
2023-03-18 19:43:36 +03:00
elif key in self.settings.floatData:
setattr(self.settings, key, float(val))
elif key in self.settings.strData:
setattr(self.settings, key, str(val))
else:
return False
return True
def get_info(self):
data = asdict(self.settings)
2023-11-08 13:54:13 +03:00
data["onnxExecutionProviders"] = (
self.onnx_session.get_providers() if self.onnx_session is not None else []
)
2023-03-18 19:43:36 +03:00
return data
def get_processing_sampling_rate(self):
2023-04-28 08:12:19 +03:00
if hasattr(self, "hps") is False:
2023-04-17 03:45:12 +03:00
raise NoModeLoadedException("config")
2023-03-18 19:43:36 +03:00
return self.hps.data.sampling_rate
def get_unit_f0(self, audio_buffer, tran):
wav_44k = audio_buffer
2023-03-21 20:59:43 +03:00
if self.settings.f0Detector == "dio":
2023-04-28 08:12:19 +03:00
f0 = compute_f0_dio(
wav_44k,
sampling_rate=self.hps.data.sampling_rate,
hop_length=self.hps.data.hop_length,
)
2023-03-21 20:59:43 +03:00
else:
2023-04-28 08:12:19 +03:00
f0 = compute_f0_harvest(
wav_44k,
sampling_rate=self.hps.data.sampling_rate,
hop_length=self.hps.data.hop_length,
)
2023-03-21 20:59:43 +03:00
2023-03-18 19:43:36 +03:00
if wav_44k.shape[0] % self.hps.data.hop_length != 0:
2023-11-08 13:54:13 +03:00
print(
f" !!! !!! !!! wav size not multiple of hopsize: {wav_44k.shape[0] / self.hps.data.hop_length}"
)
2023-03-18 19:43:36 +03:00
2023-06-22 03:21:39 +03:00
f0, uv = interpolate_f0(f0)
2023-03-18 19:43:36 +03:00
f0 = torch.FloatTensor(f0)
uv = torch.FloatTensor(uv)
f0 = f0 * 2 ** (tran / 12)
f0 = f0.unsqueeze(0)
uv = uv.unsqueeze(0)
2023-11-08 13:54:13 +03:00
wav16k_numpy = librosa.resample(
audio_buffer, orig_sr=self.hps.data.sampling_rate, target_sr=16000
)
2023-03-29 17:11:35 +03:00
wav16k_tensor = torch.from_numpy(wav16k_numpy)
2023-03-18 19:43:36 +03:00
2023-06-22 01:40:06 +03:00
if (self.settings.gpu < 0 or self.gpu_num == 0) or self.slotInfo.isONNX:
2023-03-18 19:43:36 +03:00
dev = torch.device("cpu")
else:
dev = torch.device("cuda", index=self.settings.gpu)
2023-04-18 21:35:04 +03:00
if hasattr(self, "content_vec_onnx"):
c = self.content_vec_onnx.run(
2023-03-29 18:16:26 +03:00
["units"],
{
"audio": wav16k_numpy.reshape(1, -1),
2023-04-28 08:12:19 +03:00
},
)
2023-03-29 18:16:26 +03:00
c = torch.from_numpy(np.array(c)).squeeze(0).transpose(1, 2)
2023-04-18 21:35:04 +03:00
# print("onnx hubert:", self.content_vec_onnx.get_providers())
2023-03-29 18:16:26 +03:00
else:
2023-04-03 11:33:28 +03:00
if self.hps.model.ssl_dim == 768:
self.hubert_model = self.hubert_model.to(dev)
wav16k_tensor = wav16k_tensor.to(dev)
2023-11-08 13:54:13 +03:00
c = get_hubert_content_layer9(
self.hubert_model, wav_16k_tensor=wav16k_tensor
)
2023-04-03 11:33:28 +03:00
else:
self.hubert_model = self.hubert_model.to(dev)
wav16k_tensor = wav16k_tensor.to(dev)
2023-06-22 03:21:39 +03:00
c = get_hubert_content(self.hubert_model, wav_16k_tensor=wav16k_tensor)
2023-03-29 17:11:35 +03:00
2023-03-29 18:16:26 +03:00
uv = uv.to(dev)
f0 = f0.to(dev)
2023-03-29 17:11:35 +03:00
2023-06-22 03:21:39 +03:00
c = repeat_expand_2d(c.squeeze(0), f0.shape[1])
2023-03-18 19:43:36 +03:00
2023-11-08 13:54:13 +03:00
if (
self.settings.clusterInferRatio != 0
and hasattr(self, "cluster_model")
and self.cluster_model is not None
):
speaker = [
key
for key, value in self.settings.speakers.items()
if value == self.settings.dstId
]
2023-03-18 19:43:36 +03:00
if len(speaker) != 1:
2023-04-25 12:53:24 +03:00
pass
# print("not only one speaker found.", speaker)
2023-03-18 19:43:36 +03:00
else:
2023-11-08 13:54:13 +03:00
cluster_c = get_cluster_center_result(
self.cluster_model, c.cpu().numpy().T, speaker[0]
).T
2023-03-20 05:13:19 +03:00
cluster_c = torch.FloatTensor(cluster_c).to(dev)
2023-04-18 21:35:04 +03:00
c = c.to(dev)
2023-11-08 13:54:13 +03:00
c = (
self.settings.clusterInferRatio * cluster_c
+ (1 - self.settings.clusterInferRatio) * c
)
2023-03-18 19:43:36 +03:00
c = c.unsqueeze(0)
return c, f0, uv
2023-04-28 08:12:19 +03:00
def generate_input(
self,
newData: AudioInOut,
inputSize: int,
crossfadeSize: int,
solaSearchFrame: int = 0,
):
2023-03-18 19:43:36 +03:00
newData = newData.astype(np.float32) / self.hps.data.max_wav_value
2023-04-28 08:12:19 +03:00
if self.audio_buffer is not None:
2023-11-08 13:54:13 +03:00
self.audio_buffer = np.concatenate(
[self.audio_buffer, newData], 0
) # 過去のデータに連結
2023-03-18 19:43:36 +03:00
else:
self.audio_buffer = newData
2023-11-08 13:54:13 +03:00
convertSize = (
inputSize + crossfadeSize + solaSearchFrame + self.settings.extraConvertSize
)
2023-03-18 19:43:36 +03:00
if convertSize % self.hps.data.hop_length != 0: # モデルの出力のホップサイズで切り捨てが発生するので補う。
2023-11-08 13:54:13 +03:00
convertSize = convertSize + (
self.hps.data.hop_length - (convertSize % self.hps.data.hop_length)
)
2023-03-18 19:43:36 +03:00
2023-04-28 08:12:19 +03:00
convertOffset = -1 * convertSize
self.audio_buffer = self.audio_buffer[convertOffset:] # 変換対象の部分だけ抽出
2023-03-18 19:43:36 +03:00
2023-04-28 08:12:19 +03:00
cropOffset = -1 * (inputSize + crossfadeSize)
cropEnd = -1 * (crossfadeSize)
crop = self.audio_buffer[cropOffset:cropEnd]
2023-03-18 19:43:36 +03:00
rms = np.sqrt(np.square(crop).mean(axis=0))
vol = max(rms, self.prevVol * 0.0)
self.prevVol = vol
c, f0, uv = self.get_unit_f0(self.audio_buffer, self.settings.tran)
return (c, f0, uv, convertSize, vol)
def _onnx_inference(self, data):
convertSize = data[3]
vol = data[4]
2023-04-28 08:12:19 +03:00
data = (
data[0],
data[1],
data[2],
)
2023-03-18 19:43:36 +03:00
if vol < self.settings.silentThreshold:
return np.zeros(convertSize).astype(np.int16)
c, f0, uv = [x.numpy() for x in data]
2023-03-29 17:11:35 +03:00
sid_target = torch.LongTensor([self.settings.dstId]).unsqueeze(0).numpy()
2023-04-28 08:12:19 +03:00
audio1 = (
self.onnx_session.run(
["audio"],
{
"c": c.astype(np.float32),
"f0": f0.astype(np.float32),
"uv": uv.astype(np.float32),
"g": sid_target.astype(np.int64),
2023-11-08 13:54:13 +03:00
"noise_scale": np.array([self.settings.noiseScale]).astype(
np.float32
),
2023-04-28 08:12:19 +03:00
# "predict_f0": np.array([self.settings.dstId]).astype(np.int64),
},
)[0][0, 0]
* self.hps.data.max_wav_value
)
2023-03-18 19:43:36 +03:00
audio1 = audio1 * vol
result = audio1
return result
def _pyTorch_inference(self, data):
if self.settings.gpu < 0 or self.gpu_num == 0:
dev = torch.device("cpu")
else:
dev = torch.device("cuda", index=self.settings.gpu)
convertSize = data[3]
vol = data[4]
2023-04-28 08:12:19 +03:00
data = (
data[0],
data[1],
data[2],
)
2023-03-18 19:43:36 +03:00
if vol < self.settings.silentThreshold:
return np.zeros(convertSize).astype(np.int16)
with torch.no_grad():
2023-04-28 08:12:19 +03:00
c, f0, uv = [x.to(dev) for x in data]
2023-03-18 19:43:36 +03:00
sid_target = torch.LongTensor([self.settings.dstId]).to(dev).unsqueeze(0)
self.net_g.to(dev)
# audio1 = self.net_g.infer(c, f0=f0, g=sid_target, uv=uv, predict_f0=True, noice_scale=0.1)[0][0, 0].data.float()
predict_f0_flag = True if self.settings.predictF0 == 1 else False
2023-04-28 08:12:19 +03:00
audio1 = self.net_g.infer(
c,
f0=f0,
g=sid_target,
uv=uv,
predict_f0=predict_f0_flag,
noice_scale=self.settings.noiseScale,
)
2023-03-18 19:43:36 +03:00
audio1 = audio1[0][0].data.float()
# audio1 = self.net_g.infer(c, f0=f0, g=sid_target, uv=uv, predict_f0=predict_f0_flag,
# noice_scale=self.settings.noiceScale)[0][0, 0].data.float()
audio1 = audio1 * self.hps.data.max_wav_value
audio1 = audio1 * vol
result = audio1.float().cpu().numpy()
# result = infer_tool.pad_array(result, length)
return result
def inference(self, data):
2023-06-22 01:40:06 +03:00
if self.slotInfo.isONNX:
2023-03-18 19:43:36 +03:00
audio = self._onnx_inference(data)
else:
audio = self._pyTorch_inference(data)
2023-04-14 22:42:54 +03:00
2023-03-18 19:43:36 +03:00
return audio
2023-04-10 18:21:17 +03:00
def __del__(self):
2023-03-18 19:43:36 +03:00
del self.net_g
del self.onnx_session
2023-04-10 18:21:17 +03:00
remove_path = os.path.join("so-vits-svc-40")
2023-04-28 08:12:19 +03:00
sys.path = [x for x in sys.path if x.endswith(remove_path) is False]
2023-04-10 18:21:17 +03:00
for key in list(sys.modules):
val = sys.modules.get(key)
try:
file_path = val.__file__
2023-04-11 01:37:39 +03:00
if file_path.find("so-vits-svc-40" + os.path.sep) >= 0:
2023-05-30 20:26:16 +03:00
# print("remove", key, file_path)
2023-04-10 18:21:17 +03:00
sys.modules.pop(key)
2023-04-28 08:12:19 +03:00
except Exception: # type:ignore
2023-04-10 18:21:17 +03:00
pass
2023-03-21 20:59:43 +03:00
2023-08-05 06:33:31 +03:00
def get_model_current(self):
2023-11-08 13:54:13 +03:00
return []
2023-08-05 06:33:31 +03:00
2023-03-21 20:59:43 +03:00
def resize_f0(x, target_len):
source = np.array(x)
source[source < 0.001] = np.nan
2023-04-28 08:12:19 +03:00
target = np.interp(
np.arange(0, len(source) * target_len, len(source)) / target_len,
np.arange(0, len(source)),
source,
)
2023-03-21 20:59:43 +03:00
res = np.nan_to_num(target)
return res
def compute_f0_dio(wav_numpy, p_len=None, sampling_rate=44100, hop_length=512):
if p_len is None:
p_len = wav_numpy.shape[0] // hop_length
f0, t = pw.dio(
wav_numpy.astype(np.double),
fs=sampling_rate,
f0_ceil=800,
frame_period=1000 * hop_length / sampling_rate,
)
f0 = pw.stonemask(wav_numpy.astype(np.double), f0, t, sampling_rate)
for index, pitch in enumerate(f0):
f0[index] = round(pitch, 1)
return resize_f0(f0, p_len)
def compute_f0_harvest(wav_numpy, p_len=None, sampling_rate=44100, hop_length=512):
if p_len is None:
p_len = wav_numpy.shape[0] // hop_length
2023-04-28 08:12:19 +03:00
f0, t = pw.harvest(
wav_numpy.astype(np.double),
fs=sampling_rate,
frame_period=5.5,
f0_floor=71.0,
f0_ceil=1000.0,
)
2023-03-21 20:59:43 +03:00
for index, pitch in enumerate(f0):
f0[index] = round(pitch, 1)
return resize_f0(f0, p_len)
2023-04-03 11:33:28 +03:00
def get_hubert_content_layer9(hmodel, wav_16k_tensor):
feats = wav_16k_tensor
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.to(wav_16k_tensor.device),
"padding_mask": padding_mask.to(wav_16k_tensor.device),
"output_layer": 9, # layer 9
}
with torch.no_grad():
logits = hmodel.extract_features(**inputs)
return logits[0].transpose(1, 2)