voice-changer/server/voice_changer/DiffusionSVC/pipeline/Pipeline.py

218 lines
8.2 KiB
Python
Raw Normal View History

2023-07-12 18:59:48 +03:00
from typing import Any
import torch
import torch.nn.functional as F
from torch.cuda.amp import autocast
from Exceptions import (
DeviceCannotSupportHalfPrecisionException,
DeviceChangingException,
HalfPrecisionChangingException,
NotEnoughDataExtimateF0,
)
2023-07-13 21:33:04 +03:00
from voice_changer.DiffusionSVC.inferencer.Inferencer import Inferencer
2023-07-14 07:54:08 +03:00
from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor
2023-07-12 18:59:48 +03:00
from voice_changer.RVC.embedder.Embedder import Embedder
2023-07-13 21:33:04 +03:00
from voice_changer.common.VolumeExtractor import VolumeExtractor
2023-07-14 22:45:27 +03:00
from torchaudio.transforms import Resample
2023-07-17 01:21:06 +03:00
from voice_changer.utils.Timer import Timer
2023-07-12 18:59:48 +03:00
class Pipeline(object):
embedder: Embedder
inferencer: Inferencer
pitchExtractor: PitchExtractor
index: Any | None
big_npy: Any | None
# feature: Any | None
targetSR: int
device: torch.device
isHalf: bool
def __init__(
self,
embedder: Embedder,
inferencer: Inferencer,
pitchExtractor: PitchExtractor,
2023-07-13 21:33:04 +03:00
# index: Any | None,
2023-07-12 18:59:48 +03:00
targetSR,
device,
isHalf,
2023-07-14 22:45:27 +03:00
resamplerIn: Resample,
resamplerOut: Resample
2023-07-12 18:59:48 +03:00
):
2023-07-13 22:28:03 +03:00
self.inferencer = inferencer
inferencer_block_size, inferencer_sampling_rate = inferencer.getConfig()
2023-07-14 22:45:27 +03:00
self.hop_size = inferencer_block_size * 16000 / inferencer_sampling_rate # 16000はオーディオのサンプルレート。16Kで処理
2023-07-13 22:28:03 +03:00
self.inferencer_block_size = inferencer_block_size
self.inferencer_sampling_rate = inferencer_sampling_rate
2023-07-13 21:33:04 +03:00
2023-07-13 22:28:03 +03:00
self.volumeExtractor = VolumeExtractor(self.hop_size)
2023-07-12 18:59:48 +03:00
self.embedder = embedder
self.pitchExtractor = pitchExtractor
2023-07-14 22:45:27 +03:00
self.resamplerIn = resamplerIn
self.resamplerOut = resamplerOut
2023-07-13 22:28:03 +03:00
print("VOLUME EXTRACTOR", self.volumeExtractor)
2023-07-12 18:59:48 +03:00
print("GENERATE INFERENCER", self.inferencer)
print("GENERATE EMBEDDER", self.embedder)
print("GENERATE PITCH EXTRACTOR", self.pitchExtractor)
self.targetSR = targetSR
self.device = device
2023-07-13 21:33:04 +03:00
self.isHalf = False
2023-07-12 18:59:48 +03:00
def getPipelineInfo(self):
2023-07-13 22:28:03 +03:00
volumeExtractorInfo = self.volumeExtractor.getVolumeExtractorInfo()
2023-07-12 18:59:48 +03:00
inferencerInfo = self.inferencer.getInferencerInfo() if self.inferencer else {}
embedderInfo = self.embedder.getEmbedderInfo()
pitchExtractorInfo = self.pitchExtractor.getPitchExtractorInfo()
2023-07-13 22:28:03 +03:00
return {"volumeExtractor": volumeExtractorInfo, "inferencer": inferencerInfo, "embedder": embedderInfo, "pitchExtractor": pitchExtractorInfo, "isHalf": self.isHalf}
2023-07-12 18:59:48 +03:00
def setPitchExtractor(self, pitchExtractor: PitchExtractor):
self.pitchExtractor = pitchExtractor
2023-07-13 21:33:04 +03:00
@torch.no_grad()
2023-07-14 22:45:27 +03:00
def extract_volume_and_mask(self, audio: torch.Tensor, threshold: float):
'''
with Timer("[VolumeExt np]") as t:
for i in range(100):
volume = self.volumeExtractor.extract(audio)
time_np = t.secs
with Timer("[VolumeExt pt]") as t:
for i in range(100):
volume_t = self.volumeExtractor.extract_t(audio)
time_pt = t.secs
print("[Volume np]:", volume)
print("[Volume pt]:", volume_t)
print("[Perform]:", time_np, time_pt)
# -> [Perform]: 0.030178070068359375 0.005780220031738281 (RTX4090)
# -> [Perform]: 0.029046058654785156 0.0025115013122558594 (CPU i9 13900KF)
# ---> これくらいの処理ならCPU上のTorchでやった方が早い
'''
volume_t = self.volumeExtractor.extract_t(audio)
mask = self.volumeExtractor.get_mask_from_volume_t(volume_t, self.inferencer_block_size, threshold=threshold)
volume = volume_t.unsqueeze(-1).unsqueeze(0)
2023-07-13 21:33:04 +03:00
return volume, mask
2023-07-12 18:59:48 +03:00
def exec(
self,
sid,
audio, # torch.tensor [n]
sr,
2023-07-12 18:59:48 +03:00
pitchf, # np.array [m]
feature, # np.array [m, feat]
f0_up_key,
2023-07-15 12:35:11 +03:00
k_step,
infer_speedup,
2023-07-12 18:59:48 +03:00
silence_front,
embOutputLayer,
useFinalProj,
2023-07-13 22:28:03 +03:00
protect=0.5
2023-07-12 18:59:48 +03:00
):
2023-07-17 01:21:06 +03:00
# print("---------- pipe line --------------------")
with Timer("pre-process") as t:
audio_t = torch.from_numpy(audio).float().unsqueeze(0).to(self.device)
audio16k = self.resamplerIn(audio_t)
volume, mask = self.extract_volume_and_mask(audio16k, threshold=-60.0)
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
n_frames = int(audio16k.size(-1) // self.hop_size + 1)
# print("[Timer::1: ]", t.secs)
with Timer("pre-process") as t:
# ピッチ検出
try:
# pitch = self.pitchExtractor.extract(
# audio16k.squeeze(),
# pitchf,
# f0_up_key,
# int(self.hop_size), # 処理のwindowサイズ (44100における512)
# silence_front=silence_front,
# )
2023-07-17 01:21:06 +03:00
pitch = self.pitchExtractor.extract(
audio,
sr,
self.inferencer_block_size,
self.inferencer_sampling_rate,
2023-07-17 01:21:06 +03:00
pitchf,
f0_up_key,
silence_front=silence_front,
)
pitch = torch.tensor(pitch[-n_frames:], device=self.device).unsqueeze(0).long()
except IndexError as e: # NOQA
raise NotEnoughDataExtimateF0()
# tensor型調整
feats = audio16k.squeeze()
if feats.dim() == 2: # double channels
feats = feats.mean(-1)
feats = feats.view(1, -1)
# print("[Timer::2: ]", t.secs)
with Timer("pre-process") as t:
# embedding
with autocast(enabled=self.isHalf):
try:
feats = self.embedder.extractFeatures(feats, embOutputLayer, useFinalProj)
if torch.isnan(feats).all():
raise DeviceCannotSupportHalfPrecisionException()
except RuntimeError as e:
if "HALF" in e.__str__().upper():
raise HalfPrecisionChangingException()
elif "same device" in e.__str__():
raise DeviceChangingException()
else:
raise e
feats = F.interpolate(feats.permute(0, 2, 1), size=int(n_frames), mode='nearest').permute(0, 2, 1)
# print("[Timer::3: ]", t.secs)
with Timer("pre-process") as t:
# 推論実行
2023-07-12 18:59:48 +03:00
try:
2023-07-17 01:21:06 +03:00
with torch.no_grad():
with autocast(enabled=self.isHalf):
audio1 = (
torch.clip(
self.inferencer.infer(
audio16k,
feats,
pitch.unsqueeze(-1),
volume,
mask,
sid,
k_step,
infer_speedup,
silence_front=silence_front
).to(dtype=torch.float32),
-1.0,
1.0,
)
* 32767.5
).data.to(dtype=torch.int16)
2023-07-12 18:59:48 +03:00
except RuntimeError as e:
if "HALF" in e.__str__().upper():
2023-07-17 01:21:06 +03:00
print("11", e)
2023-07-12 18:59:48 +03:00
raise HalfPrecisionChangingException()
else:
raise e
2023-07-17 01:21:06 +03:00
# print("[Timer::4: ]", t.secs)
2023-07-12 18:59:48 +03:00
2023-07-17 01:21:06 +03:00
with Timer("pre-process") as t: # NOQA
feats_buffer = feats.squeeze(0).detach().cpu()
if pitch is not None:
pitch_buffer = pitch.squeeze(0).detach().cpu()
else:
pitch_buffer = None
2023-07-12 18:59:48 +03:00
2023-07-17 01:21:06 +03:00
del pitch, pitchf, feats, sid
torch.cuda.empty_cache()
audio1 = self.resamplerOut(audio1.float())
# print("[Timer::5: ]", t.secs)
2023-07-14 22:53:38 +03:00
return audio1, pitch_buffer, feats_buffer