mirror of
https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git
synced 2026-08-29 10:09:32 +02:00
Import RVC 20260716 Nvidia 50x0 v2bb
This commit is contained in:
51
infer/audio.py
Normal file
51
infer/audio.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import platform, os
|
||||
import ffmpeg
|
||||
import numpy as np
|
||||
import av
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
def wav2(i, o, format):
|
||||
inp = av.open(i, "r")
|
||||
if format == "m4a":
|
||||
format = "mp4"
|
||||
out = av.open(o, "w", format=format)
|
||||
if format == "ogg":
|
||||
format = "libvorbis"
|
||||
if format == "mp4":
|
||||
format = "aac"
|
||||
|
||||
ostream = out.add_stream(format)
|
||||
|
||||
for frame in inp.decode(audio=0):
|
||||
for p in ostream.encode(frame):
|
||||
out.mux(p)
|
||||
|
||||
for p in ostream.encode(None):
|
||||
out.mux(p)
|
||||
|
||||
out.close()
|
||||
inp.close()
|
||||
|
||||
|
||||
def load_audio(file, sr):
|
||||
try:
|
||||
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
|
||||
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
|
||||
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
|
||||
file = clean_path(file) # 防止小白拷路径头尾带了空格和"和回车
|
||||
out, _ = (
|
||||
ffmpeg.input(file, threads=0)
|
||||
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
||||
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
||||
)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to load audio: {e}")
|
||||
|
||||
return np.frombuffer(out, np.float32).flatten()
|
||||
|
||||
|
||||
def clean_path(path_str):
|
||||
if platform.system() == "Windows":
|
||||
path_str = path_str.replace("/", "\\")
|
||||
return path_str.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
||||
97
infer/fcpe.py
Normal file
97
infer/fcpe.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import torch
|
||||
|
||||
|
||||
def _is_directml_device(device):
|
||||
"""Return whether *device* is the PrivateUse1 device registered by DirectML."""
|
||||
return getattr(device, "type", None) == "privateuseone" or "privateuseone" in str(
|
||||
device
|
||||
).lower()
|
||||
|
||||
|
||||
class FCPEInfer:
|
||||
"""Project-local FCPE inference adapter with a DirectML execution path.
|
||||
|
||||
DirectML does not support the complex tensor produced by ``torch.stft`` in
|
||||
torchfcpe's wav2mel stage. Keep preprocessing and the small indexed
|
||||
decoder on CPU while the FCPE neural network runs on DirectML. Other
|
||||
devices retain torchfcpe's original end-to-end inference path.
|
||||
"""
|
||||
|
||||
def __init__(self, device):
|
||||
from torchfcpe import spawn_bundled_infer_model
|
||||
|
||||
self.device = device
|
||||
self.is_directml = _is_directml_device(device)
|
||||
if self.is_directml:
|
||||
# Loading a checkpoint directly with map_location=privateuseone is
|
||||
# not supported consistently. Load on CPU, leave wav2mel there,
|
||||
# and move only the real-valued FCPE network to DirectML.
|
||||
self.infer_model = spawn_bundled_infer_model("cpu")
|
||||
self.infer_model.wav2mel.eval()
|
||||
self.cent_table_cpu = (
|
||||
self.infer_model.model.cent_table.detach().float().cpu().clone()
|
||||
)
|
||||
self.out_dims = int(self.infer_model.model.out_dims)
|
||||
self.infer_model.model.to(device).eval()
|
||||
else:
|
||||
self.infer_model = spawn_bundled_infer_model(device)
|
||||
|
||||
def _decode_on_cpu(self, latent, decoder_mode, threshold):
|
||||
"""Decode DML network logits on CPU with torchfcpe's exact formulas.
|
||||
|
||||
The current DirectML backend's ``aten::gather`` returns incorrect FCPE
|
||||
bin values even though its indices and the neural-network logits match
|
||||
CPU. Decoding is tiny compared with the model, so keep this
|
||||
compatibility boundary on CPU as well as the complex STFT.
|
||||
"""
|
||||
latent = latent.detach().float().cpu()
|
||||
batch, frames, _ = latent.shape
|
||||
cents = self.cent_table_cpu[None, None, :].expand(batch, frames, -1)
|
||||
|
||||
if decoder_mode == "argmax":
|
||||
confidence = torch.max(latent, dim=-1, keepdim=True).values
|
||||
decoded = torch.sum(cents * latent, dim=-1, keepdim=True) / torch.sum(
|
||||
latent, dim=-1, keepdim=True
|
||||
)
|
||||
elif decoder_mode == "local_argmax":
|
||||
confidence, max_index = torch.max(latent, dim=-1, keepdim=True)
|
||||
local_index = torch.arange(9, dtype=torch.long) + (max_index - 4)
|
||||
local_index.clamp_(0, self.out_dims - 1)
|
||||
local_cents = torch.gather(cents, -1, local_index)
|
||||
local_latent = torch.gather(latent, -1, local_index)
|
||||
decoded = torch.sum(
|
||||
local_cents * local_latent, dim=-1, keepdim=True
|
||||
) / torch.sum(local_latent, dim=-1, keepdim=True)
|
||||
else:
|
||||
raise ValueError(f"Unknown FCPE decoder mode: {decoder_mode}")
|
||||
|
||||
decoded = decoded.masked_fill(confidence <= threshold, float("-inf"))
|
||||
return 10.0 * torch.pow(2.0, decoded / 1200.0)
|
||||
|
||||
# torch.no_grad is used instead of inference_mode because DirectML's
|
||||
# PrivateUse1 backend still updates version counters in a few operators.
|
||||
@torch.no_grad()
|
||||
def infer(
|
||||
self,
|
||||
wav,
|
||||
sr,
|
||||
decoder_mode="local_argmax",
|
||||
threshold=0.006,
|
||||
):
|
||||
if not self.is_directml:
|
||||
return self.infer_model.infer(
|
||||
wav,
|
||||
sr=sr,
|
||||
decoder_mode=decoder_mode,
|
||||
threshold=threshold,
|
||||
)
|
||||
|
||||
wav_cpu = wav.detach().to(device="cpu", dtype=torch.float32)
|
||||
mel_cpu = self.infer_model.wav2mel(wav_cpu, sr)
|
||||
mel_dml = mel_cpu.to(device=self.device, dtype=torch.float32)
|
||||
latent_dml = self.infer_model.model(mel_dml)
|
||||
return self._decode_on_cpu(
|
||||
latent_dml,
|
||||
decoder_mode=decoder_mode,
|
||||
threshold=threshold,
|
||||
)
|
||||
96
infer/hubert.py
Normal file
96
infer/hubert.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import logging
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
from transformers import AutoFeatureExtractor, HubertModel
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
class HubertModelWithFinalProj(HubertModel):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
self.final_proj = nn.Linear(config.hidden_size, config.classifier_proj_size)
|
||||
|
||||
HUBERT_MODEL_PATH = (PROJECT_ROOT / "assets" / "hubert_base").resolve()
|
||||
|
||||
|
||||
def _device_type(device):
|
||||
if isinstance(device, torch.device):
|
||||
return device.type
|
||||
return str(device).split(":", 1)[0]
|
||||
|
||||
|
||||
def load_hubert_model(device, is_half=False):
|
||||
"""Load the local Transformers HuBERT/ContentVec model for RVC."""
|
||||
if not (HUBERT_MODEL_PATH / "config.json").is_file():
|
||||
raise FileNotFoundError(
|
||||
f"Transformers HuBERT model not found: {HUBERT_MODEL_PATH}"
|
||||
)
|
||||
|
||||
dtype = torch.float16 if is_half else torch.float32
|
||||
load_options = {
|
||||
"local_files_only": True,
|
||||
"torch_dtype": dtype,
|
||||
}
|
||||
# DirectML does not implement every SDPA kernel used by Transformers.
|
||||
if _device_type(device) == "privateuseone":
|
||||
load_options["attn_implementation"] = "eager"
|
||||
|
||||
logger.info(
|
||||
"Loading Transformers HuBERT from %s (%s on %s)",
|
||||
HUBERT_MODEL_PATH,
|
||||
dtype,
|
||||
device,
|
||||
)
|
||||
model = HubertModelWithFinalProj.from_pretrained(
|
||||
str(HUBERT_MODEL_PATH), **load_options
|
||||
)
|
||||
model = model.to(device)
|
||||
return model.eval()
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def hubert_audio_requires_normalization():
|
||||
feature_extractor = AutoFeatureExtractor.from_pretrained(
|
||||
str(HUBERT_MODEL_PATH), local_files_only=True
|
||||
)
|
||||
return bool(feature_extractor.do_normalize)
|
||||
|
||||
|
||||
def extract_hubert_features(model, source, version, padding_mask=None):
|
||||
"""Return the RVC v1 (256-D) or v2 (768-D) HuBERT representation.
|
||||
|
||||
Transformers hidden_states[N] is numerically equivalent to the source checkpoint's
|
||||
output_layer=N for this converted checkpoint. RVC v1 uses layer 9 followed
|
||||
by final_proj; RVC v2 uses the final (12th) encoder layer directly.
|
||||
"""
|
||||
if version not in {"v1", "v2"}:
|
||||
raise ValueError(f"Unsupported RVC feature version: {version!r}")
|
||||
|
||||
attention_mask = None
|
||||
if padding_mask is not None and bool(torch.any(padding_mask).item()):
|
||||
attention_mask = (~padding_mask.bool()).long()
|
||||
|
||||
if version == "v1":
|
||||
outputs = model(
|
||||
input_values=source,
|
||||
attention_mask=attention_mask,
|
||||
output_hidden_states=True,
|
||||
return_dict=True,
|
||||
)
|
||||
features = outputs.hidden_states[9]
|
||||
return model.final_proj(features)
|
||||
|
||||
outputs = model(
|
||||
input_values=source,
|
||||
attention_mask=attention_mask,
|
||||
output_hidden_states=False,
|
||||
return_dict=True,
|
||||
)
|
||||
return outputs.last_hidden_state
|
||||
459
infer/module/attentions.py
Normal file
459
infer/module/attentions.py
Normal file
@@ -0,0 +1,459 @@
|
||||
import copy
|
||||
import math
|
||||
from typing import Optional
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
|
||||
from infer.module import commons, modules
|
||||
from infer.module.modules import LayerNorm
|
||||
|
||||
|
||||
class Encoder(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
hidden_channels,
|
||||
filter_channels,
|
||||
n_heads,
|
||||
n_layers,
|
||||
kernel_size=1,
|
||||
p_dropout=0.0,
|
||||
window_size=10,
|
||||
**kwargs
|
||||
):
|
||||
super(Encoder, self).__init__()
|
||||
self.hidden_channels = hidden_channels
|
||||
self.filter_channels = filter_channels
|
||||
self.n_heads = n_heads
|
||||
self.n_layers = int(n_layers)
|
||||
self.kernel_size = kernel_size
|
||||
self.p_dropout = p_dropout
|
||||
self.window_size = window_size
|
||||
|
||||
self.drop = nn.Dropout(p_dropout)
|
||||
self.attn_layers = nn.ModuleList()
|
||||
self.norm_layers_1 = nn.ModuleList()
|
||||
self.ffn_layers = nn.ModuleList()
|
||||
self.norm_layers_2 = nn.ModuleList()
|
||||
for i in range(self.n_layers):
|
||||
self.attn_layers.append(
|
||||
MultiHeadAttention(
|
||||
hidden_channels,
|
||||
hidden_channels,
|
||||
n_heads,
|
||||
p_dropout=p_dropout,
|
||||
window_size=window_size,
|
||||
)
|
||||
)
|
||||
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
||||
self.ffn_layers.append(
|
||||
FFN(
|
||||
hidden_channels,
|
||||
hidden_channels,
|
||||
filter_channels,
|
||||
kernel_size,
|
||||
p_dropout=p_dropout,
|
||||
)
|
||||
)
|
||||
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
||||
|
||||
def forward(self, x, x_mask):
|
||||
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
||||
x = x * x_mask
|
||||
zippep = zip(
|
||||
self.attn_layers, self.norm_layers_1, self.ffn_layers, self.norm_layers_2
|
||||
)
|
||||
for attn_layers, norm_layers_1, ffn_layers, norm_layers_2 in zippep:
|
||||
y = attn_layers(x, x, attn_mask)
|
||||
y = self.drop(y)
|
||||
x = norm_layers_1(x + y)
|
||||
|
||||
y = ffn_layers(x, x_mask)
|
||||
y = self.drop(y)
|
||||
x = norm_layers_2(x + y)
|
||||
x = x * x_mask
|
||||
return x
|
||||
|
||||
|
||||
class Decoder(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
hidden_channels,
|
||||
filter_channels,
|
||||
n_heads,
|
||||
n_layers,
|
||||
kernel_size=1,
|
||||
p_dropout=0.0,
|
||||
proximal_bias=False,
|
||||
proximal_init=True,
|
||||
**kwargs
|
||||
):
|
||||
super(Decoder, self).__init__()
|
||||
self.hidden_channels = hidden_channels
|
||||
self.filter_channels = filter_channels
|
||||
self.n_heads = n_heads
|
||||
self.n_layers = n_layers
|
||||
self.kernel_size = kernel_size
|
||||
self.p_dropout = p_dropout
|
||||
self.proximal_bias = proximal_bias
|
||||
self.proximal_init = proximal_init
|
||||
|
||||
self.drop = nn.Dropout(p_dropout)
|
||||
self.self_attn_layers = nn.ModuleList()
|
||||
self.norm_layers_0 = nn.ModuleList()
|
||||
self.encdec_attn_layers = nn.ModuleList()
|
||||
self.norm_layers_1 = nn.ModuleList()
|
||||
self.ffn_layers = nn.ModuleList()
|
||||
self.norm_layers_2 = nn.ModuleList()
|
||||
for i in range(self.n_layers):
|
||||
self.self_attn_layers.append(
|
||||
MultiHeadAttention(
|
||||
hidden_channels,
|
||||
hidden_channels,
|
||||
n_heads,
|
||||
p_dropout=p_dropout,
|
||||
proximal_bias=proximal_bias,
|
||||
proximal_init=proximal_init,
|
||||
)
|
||||
)
|
||||
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
||||
self.encdec_attn_layers.append(
|
||||
MultiHeadAttention(
|
||||
hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
|
||||
)
|
||||
)
|
||||
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
||||
self.ffn_layers.append(
|
||||
FFN(
|
||||
hidden_channels,
|
||||
hidden_channels,
|
||||
filter_channels,
|
||||
kernel_size,
|
||||
p_dropout=p_dropout,
|
||||
causal=True,
|
||||
)
|
||||
)
|
||||
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
||||
|
||||
def forward(self, x, x_mask, h, h_mask):
|
||||
"""
|
||||
x: decoder input
|
||||
h: encoder output
|
||||
"""
|
||||
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
||||
device=x.device, dtype=x.dtype
|
||||
)
|
||||
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
||||
x = x * x_mask
|
||||
for i in range(self.n_layers):
|
||||
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
||||
y = self.drop(y)
|
||||
x = self.norm_layers_0[i](x + y)
|
||||
|
||||
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
||||
y = self.drop(y)
|
||||
x = self.norm_layers_1[i](x + y)
|
||||
|
||||
y = self.ffn_layers[i](x, x_mask)
|
||||
y = self.drop(y)
|
||||
x = self.norm_layers_2[i](x + y)
|
||||
x = x * x_mask
|
||||
return x
|
||||
|
||||
|
||||
class MultiHeadAttention(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
channels,
|
||||
out_channels,
|
||||
n_heads,
|
||||
p_dropout=0.0,
|
||||
window_size=None,
|
||||
heads_share=True,
|
||||
block_length=None,
|
||||
proximal_bias=False,
|
||||
proximal_init=False,
|
||||
):
|
||||
super(MultiHeadAttention, self).__init__()
|
||||
assert channels % n_heads == 0
|
||||
|
||||
self.channels = channels
|
||||
self.out_channels = out_channels
|
||||
self.n_heads = n_heads
|
||||
self.p_dropout = p_dropout
|
||||
self.window_size = window_size
|
||||
self.heads_share = heads_share
|
||||
self.block_length = block_length
|
||||
self.proximal_bias = proximal_bias
|
||||
self.proximal_init = proximal_init
|
||||
self.attn = None
|
||||
|
||||
self.k_channels = channels // n_heads
|
||||
self.conv_q = nn.Conv1d(channels, channels, 1)
|
||||
self.conv_k = nn.Conv1d(channels, channels, 1)
|
||||
self.conv_v = nn.Conv1d(channels, channels, 1)
|
||||
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
||||
self.drop = nn.Dropout(p_dropout)
|
||||
|
||||
if window_size is not None:
|
||||
n_heads_rel = 1 if heads_share else n_heads
|
||||
rel_stddev = self.k_channels**-0.5
|
||||
self.emb_rel_k = nn.Parameter(
|
||||
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
||||
* rel_stddev
|
||||
)
|
||||
self.emb_rel_v = nn.Parameter(
|
||||
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
||||
* rel_stddev
|
||||
)
|
||||
|
||||
nn.init.xavier_uniform_(self.conv_q.weight)
|
||||
nn.init.xavier_uniform_(self.conv_k.weight)
|
||||
nn.init.xavier_uniform_(self.conv_v.weight)
|
||||
if proximal_init:
|
||||
with torch.no_grad():
|
||||
self.conv_k.weight.copy_(self.conv_q.weight)
|
||||
self.conv_k.bias.copy_(self.conv_q.bias)
|
||||
|
||||
def forward(
|
||||
self, x, c, attn_mask = None
|
||||
):
|
||||
q = self.conv_q(x)
|
||||
k = self.conv_k(c)
|
||||
v = self.conv_v(c)
|
||||
|
||||
x, _ = self.attention(q, k, v, mask=attn_mask)
|
||||
|
||||
x = self.conv_o(x)
|
||||
return x
|
||||
|
||||
def attention(
|
||||
self,
|
||||
query,
|
||||
key,
|
||||
value,
|
||||
mask = None,
|
||||
):
|
||||
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
||||
b, d, t_s = key.size()
|
||||
t_t = query.size(2)
|
||||
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
||||
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
||||
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
||||
|
||||
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
||||
if self.window_size is not None:
|
||||
assert (
|
||||
t_s == t_t
|
||||
), "Relative attention is only available for self-attention."
|
||||
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
||||
rel_logits = self._matmul_with_relative_keys(
|
||||
query / math.sqrt(self.k_channels), key_relative_embeddings
|
||||
)
|
||||
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
||||
scores = scores + scores_local
|
||||
if self.proximal_bias:
|
||||
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
||||
scores = scores + self._attention_bias_proximal(t_s).to(
|
||||
device=scores.device, dtype=scores.dtype
|
||||
)
|
||||
if mask is not None:
|
||||
scores = scores.masked_fill(mask == 0, -1e4)
|
||||
if self.block_length is not None:
|
||||
assert (
|
||||
t_s == t_t
|
||||
), "Local attention is only available for self-attention."
|
||||
block_mask = (
|
||||
torch.ones_like(scores)
|
||||
.triu(-self.block_length)
|
||||
.tril(self.block_length)
|
||||
)
|
||||
scores = scores.masked_fill(block_mask == 0, -1e4)
|
||||
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
||||
p_attn = self.drop(p_attn)
|
||||
output = torch.matmul(p_attn, value)
|
||||
if self.window_size is not None:
|
||||
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
||||
value_relative_embeddings = self._get_relative_embeddings(
|
||||
self.emb_rel_v, t_s
|
||||
)
|
||||
output = output + self._matmul_with_relative_values(
|
||||
relative_weights, value_relative_embeddings
|
||||
)
|
||||
output = (
|
||||
output.transpose(2, 3).contiguous().view(b, d, t_t)
|
||||
) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
||||
return output, p_attn
|
||||
|
||||
def _matmul_with_relative_values(self, x, y):
|
||||
"""
|
||||
x: [b, h, l, m]
|
||||
y: [h or 1, m, d]
|
||||
ret: [b, h, l, d]
|
||||
"""
|
||||
ret = torch.matmul(x, y.unsqueeze(0))
|
||||
return ret
|
||||
|
||||
def _matmul_with_relative_keys(self, x, y):
|
||||
"""
|
||||
x: [b, h, l, d]
|
||||
y: [h or 1, m, d]
|
||||
ret: [b, h, l, m]
|
||||
"""
|
||||
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
||||
return ret
|
||||
|
||||
def _get_relative_embeddings(self, relative_embeddings, length):
|
||||
max_relative_position = 2 * self.window_size + 1
|
||||
# Pad first before slice to avoid using cond ops.
|
||||
pad_length = max(length - (self.window_size + 1), 0)
|
||||
slice_start_position = max((self.window_size + 1) - length, 0)
|
||||
slice_end_position = slice_start_position + 2 * length - 1
|
||||
if pad_length > 0:
|
||||
padded_relative_embeddings = F.pad(
|
||||
relative_embeddings,
|
||||
# commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
|
||||
[0, 0, pad_length, pad_length, 0, 0],
|
||||
)
|
||||
else:
|
||||
padded_relative_embeddings = relative_embeddings
|
||||
used_relative_embeddings = padded_relative_embeddings[
|
||||
:, slice_start_position:slice_end_position
|
||||
]
|
||||
return used_relative_embeddings
|
||||
|
||||
def _relative_position_to_absolute_position(self, x):
|
||||
"""
|
||||
x: [b, h, l, 2*l-1]
|
||||
ret: [b, h, l, l]
|
||||
"""
|
||||
batch, heads, length, _ = x.size()
|
||||
# Concat columns of pad to shift from relative to absolute indexing.
|
||||
x = F.pad(
|
||||
x,
|
||||
# commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]])
|
||||
[0, 1, 0, 0, 0, 0, 0, 0],
|
||||
)
|
||||
|
||||
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
||||
x_flat = x.view([batch, heads, length * 2 * length])
|
||||
x_flat = F.pad(
|
||||
x_flat,
|
||||
# commons.convert_pad_shape([[0, 0], [0, 0], [0, int(length) - 1]])
|
||||
[0, int(length) - 1, 0, 0, 0, 0],
|
||||
)
|
||||
|
||||
# Reshape and slice out the padded elements.
|
||||
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
|
||||
:, :, :length, length - 1 :
|
||||
]
|
||||
return x_final
|
||||
|
||||
def _absolute_position_to_relative_position(self, x):
|
||||
"""
|
||||
x: [b, h, l, l]
|
||||
ret: [b, h, l, 2*l-1]
|
||||
"""
|
||||
batch, heads, length, _ = x.size()
|
||||
# padd along column
|
||||
x = F.pad(
|
||||
x,
|
||||
# commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, int(length) - 1]])
|
||||
[0, int(length) - 1, 0, 0, 0, 0, 0, 0],
|
||||
)
|
||||
x_flat = x.view([batch, heads, int(length**2) + int(length * (length - 1))])
|
||||
# add 0's in the beginning that will skew the elements after reshape
|
||||
x_flat = F.pad(
|
||||
x_flat,
|
||||
# commons.convert_pad_shape([[0, 0], [0, 0], [int(length), 0]])
|
||||
[length, 0, 0, 0, 0, 0],
|
||||
)
|
||||
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
||||
return x_final
|
||||
|
||||
def _attention_bias_proximal(self, length):
|
||||
"""Bias for self-attention to encourage attention to close positions.
|
||||
Args:
|
||||
length: an integer scalar.
|
||||
Returns:
|
||||
a Tensor with shape [1, 1, length, length]
|
||||
"""
|
||||
r = torch.arange(length, dtype=torch.float32)
|
||||
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
||||
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
||||
|
||||
|
||||
class FFN(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
out_channels,
|
||||
filter_channels,
|
||||
kernel_size,
|
||||
p_dropout=0.0,
|
||||
activation = None,
|
||||
causal=False,
|
||||
):
|
||||
super(FFN, self).__init__()
|
||||
self.in_channels = in_channels
|
||||
self.out_channels = out_channels
|
||||
self.filter_channels = filter_channels
|
||||
self.kernel_size = kernel_size
|
||||
self.p_dropout = p_dropout
|
||||
self.activation = activation
|
||||
self.causal = causal
|
||||
self.is_activation = True if activation == "gelu" else False
|
||||
# if causal:
|
||||
# self.padding = self._causal_padding
|
||||
# else:
|
||||
# self.padding = self._same_padding
|
||||
|
||||
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
||||
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
||||
self.drop = nn.Dropout(p_dropout)
|
||||
|
||||
def padding(self, x, x_mask) :
|
||||
if self.causal:
|
||||
padding = self._causal_padding(x * x_mask)
|
||||
else:
|
||||
padding = self._same_padding(x * x_mask)
|
||||
return padding
|
||||
|
||||
def forward(self, x, x_mask):
|
||||
x = self.conv_1(self.padding(x, x_mask))
|
||||
if self.is_activation:
|
||||
x = x * torch.sigmoid(1.702 * x)
|
||||
else:
|
||||
x = torch.relu(x)
|
||||
x = self.drop(x)
|
||||
|
||||
x = self.conv_2(self.padding(x, x_mask))
|
||||
return x * x_mask
|
||||
|
||||
def _causal_padding(self, x):
|
||||
if self.kernel_size == 1:
|
||||
return x
|
||||
pad_l = self.kernel_size - 1
|
||||
pad_r = 0
|
||||
# padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
||||
x = F.pad(
|
||||
x,
|
||||
# commons.convert_pad_shape(padding)
|
||||
[pad_l, pad_r, 0, 0, 0, 0],
|
||||
)
|
||||
return x
|
||||
|
||||
def _same_padding(self, x):
|
||||
if self.kernel_size == 1:
|
||||
return x
|
||||
pad_l = (self.kernel_size - 1) // 2
|
||||
pad_r = self.kernel_size // 2
|
||||
# padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
||||
x = F.pad(
|
||||
x,
|
||||
# commons.convert_pad_shape(padding)
|
||||
[pad_l, pad_r, 0, 0, 0, 0],
|
||||
)
|
||||
return x
|
||||
171
infer/module/commons.py
Normal file
171
infer/module/commons.py
Normal file
@@ -0,0 +1,171 @@
|
||||
from typing import List, Optional
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
|
||||
|
||||
def init_weights(m, mean=0.0, std=0.01):
|
||||
classname = m.__class__.__name__
|
||||
if classname.find("Conv") != -1:
|
||||
m.weight.data.normal_(mean, std)
|
||||
|
||||
|
||||
def get_padding(kernel_size, dilation=1):
|
||||
return int((kernel_size * dilation - dilation) / 2)
|
||||
|
||||
|
||||
# def convert_pad_shape(pad_shape):
|
||||
# l = pad_shape[::-1]
|
||||
# pad_shape = [item for sublist in l for item in sublist]
|
||||
# return pad_shape
|
||||
|
||||
|
||||
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
||||
"""KL(P||Q)"""
|
||||
kl = (logs_q - logs_p) - 0.5
|
||||
kl += (
|
||||
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
|
||||
)
|
||||
return kl
|
||||
|
||||
|
||||
def rand_gumbel(shape):
|
||||
"""Sample from the Gumbel distribution, protect from overflows."""
|
||||
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
||||
return -torch.log(-torch.log(uniform_samples))
|
||||
|
||||
|
||||
def rand_gumbel_like(x):
|
||||
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
||||
return g
|
||||
|
||||
|
||||
def slice_segments(x, ids_str, segment_size=4):
|
||||
ret = torch.zeros_like(x[:, :, :segment_size])
|
||||
for i in range(x.size(0)):
|
||||
idx_str = ids_str[i]
|
||||
idx_end = idx_str + segment_size
|
||||
ret[i] = x[i, :, idx_str:idx_end]
|
||||
return ret
|
||||
|
||||
|
||||
def slice_segments2(x, ids_str, segment_size=4):
|
||||
ret = torch.zeros_like(x[:, :segment_size])
|
||||
for i in range(x.size(0)):
|
||||
idx_str = ids_str[i]
|
||||
idx_end = idx_str + segment_size
|
||||
ret[i] = x[i, idx_str:idx_end]
|
||||
return ret
|
||||
|
||||
|
||||
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
||||
b, d, t = x.size()
|
||||
if x_lengths is None:
|
||||
x_lengths = t
|
||||
ids_str_max = x_lengths - segment_size + 1
|
||||
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
||||
ret = slice_segments(x, ids_str, segment_size)
|
||||
return ret, ids_str
|
||||
|
||||
|
||||
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
||||
position = torch.arange(length, dtype=torch.float)
|
||||
num_timescales = channels // 2
|
||||
log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
|
||||
num_timescales - 1
|
||||
)
|
||||
inv_timescales = min_timescale * torch.exp(
|
||||
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
|
||||
)
|
||||
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
||||
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
||||
signal = F.pad(signal, [0, 0, 0, channels % 2])
|
||||
signal = signal.view(1, channels, length)
|
||||
return signal
|
||||
|
||||
|
||||
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
||||
b, channels, length = x.size()
|
||||
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
||||
return x + signal.to(dtype=x.dtype, device=x.device)
|
||||
|
||||
|
||||
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
||||
b, channels, length = x.size()
|
||||
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
||||
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
||||
|
||||
|
||||
def subsequent_mask(length):
|
||||
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
||||
return mask
|
||||
|
||||
|
||||
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
||||
n_channels_int = n_channels[0]
|
||||
in_act = input_a + input_b
|
||||
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
||||
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
||||
acts = t_act * s_act
|
||||
return acts
|
||||
|
||||
|
||||
# def convert_pad_shape(pad_shape):
|
||||
# l = pad_shape[::-1]
|
||||
# pad_shape = [item for sublist in l for item in sublist]
|
||||
# return pad_shape
|
||||
|
||||
|
||||
def convert_pad_shape(pad_shape) :
|
||||
return torch.tensor(pad_shape).flip(0).reshape(-1).int().tolist()
|
||||
|
||||
|
||||
def shift_1d(x):
|
||||
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
||||
return x
|
||||
|
||||
|
||||
def sequence_mask(length, max_length = None):
|
||||
if max_length is None:
|
||||
max_length = length.max()
|
||||
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
||||
return x.unsqueeze(0) < length.unsqueeze(1)
|
||||
|
||||
|
||||
def generate_path(duration, mask):
|
||||
"""
|
||||
duration: [b, 1, t_x]
|
||||
mask: [b, 1, t_y, t_x]
|
||||
"""
|
||||
device = duration.device
|
||||
|
||||
b, _, t_y, t_x = mask.shape
|
||||
cum_duration = torch.cumsum(duration, -1)
|
||||
|
||||
cum_duration_flat = cum_duration.view(b * t_x)
|
||||
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
||||
path = path.view(b, t_x, t_y)
|
||||
path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
||||
path = path.unsqueeze(1).transpose(2, 3) * mask
|
||||
return path
|
||||
|
||||
|
||||
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
||||
if isinstance(parameters, torch.Tensor):
|
||||
parameters = [parameters]
|
||||
parameters = list(filter(lambda p: p.grad is not None, parameters))
|
||||
norm_type = float(norm_type)
|
||||
if clip_value is not None:
|
||||
clip_value = float(clip_value)
|
||||
|
||||
total_norm = 0
|
||||
for p in parameters:
|
||||
param_norm = p.grad.data.norm(norm_type)
|
||||
total_norm += param_norm.item() ** norm_type
|
||||
if clip_value is not None:
|
||||
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
||||
total_norm = total_norm ** (1.0 / norm_type)
|
||||
return total_norm
|
||||
1108
infer/module/models.py
Normal file
1108
infer/module/models.py
Normal file
File diff suppressed because it is too large
Load Diff
548
infer/module/modules.py
Normal file
548
infer/module/modules.py
Normal file
@@ -0,0 +1,548 @@
|
||||
import copy
|
||||
import math
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import numpy as np
|
||||
import scipy
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import AvgPool1d, Conv1d, Conv2d, ConvTranspose1d
|
||||
from torch.nn import functional as F
|
||||
from torch.nn.utils import remove_weight_norm, weight_norm
|
||||
|
||||
from infer.module import commons
|
||||
from infer.module.commons import get_padding, init_weights
|
||||
from infer.module.transforms import piecewise_rational_quadratic_transform
|
||||
|
||||
LRELU_SLOPE = 0.1
|
||||
|
||||
|
||||
class LayerNorm(nn.Module):
|
||||
def __init__(self, channels, eps=1e-5):
|
||||
super(LayerNorm, self).__init__()
|
||||
self.channels = channels
|
||||
self.eps = eps
|
||||
|
||||
self.gamma = nn.Parameter(torch.ones(channels))
|
||||
self.beta = nn.Parameter(torch.zeros(channels))
|
||||
|
||||
def forward(self, x):
|
||||
x = x.transpose(1, -1)
|
||||
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
||||
return x.transpose(1, -1)
|
||||
|
||||
|
||||
class ConvReluNorm(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
hidden_channels,
|
||||
out_channels,
|
||||
kernel_size,
|
||||
n_layers,
|
||||
p_dropout,
|
||||
):
|
||||
super(ConvReluNorm, self).__init__()
|
||||
self.in_channels = in_channels
|
||||
self.hidden_channels = hidden_channels
|
||||
self.out_channels = out_channels
|
||||
self.kernel_size = kernel_size
|
||||
self.n_layers = n_layers
|
||||
self.p_dropout = float(p_dropout)
|
||||
assert n_layers > 1, "Number of layers should be larger than 0."
|
||||
|
||||
self.conv_layers = nn.ModuleList()
|
||||
self.norm_layers = nn.ModuleList()
|
||||
self.conv_layers.append(
|
||||
nn.Conv1d(
|
||||
in_channels, hidden_channels, kernel_size, padding=kernel_size // 2
|
||||
)
|
||||
)
|
||||
self.norm_layers.append(LayerNorm(hidden_channels))
|
||||
self.relu_drop = nn.Sequential(nn.ReLU(), nn.Dropout(float(p_dropout)))
|
||||
for _ in range(n_layers - 1):
|
||||
self.conv_layers.append(
|
||||
nn.Conv1d(
|
||||
hidden_channels,
|
||||
hidden_channels,
|
||||
kernel_size,
|
||||
padding=kernel_size // 2,
|
||||
)
|
||||
)
|
||||
self.norm_layers.append(LayerNorm(hidden_channels))
|
||||
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
||||
self.proj.weight.data.zero_()
|
||||
self.proj.bias.data.zero_()
|
||||
|
||||
def forward(self, x, x_mask):
|
||||
x_org = x
|
||||
for i in range(self.n_layers):
|
||||
x = self.conv_layers[i](x * x_mask)
|
||||
x = self.norm_layers[i](x)
|
||||
x = self.relu_drop(x)
|
||||
x = x_org + self.proj(x)
|
||||
return x * x_mask
|
||||
|
||||
|
||||
class DDSConv(nn.Module):
|
||||
"""
|
||||
Dialted and Depth-Separable Convolution
|
||||
"""
|
||||
|
||||
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0):
|
||||
super(DDSConv, self).__init__()
|
||||
self.channels = channels
|
||||
self.kernel_size = kernel_size
|
||||
self.n_layers = n_layers
|
||||
self.p_dropout = float(p_dropout)
|
||||
|
||||
self.drop = nn.Dropout(float(p_dropout))
|
||||
self.convs_sep = nn.ModuleList()
|
||||
self.convs_1x1 = nn.ModuleList()
|
||||
self.norms_1 = nn.ModuleList()
|
||||
self.norms_2 = nn.ModuleList()
|
||||
for i in range(n_layers):
|
||||
dilation = kernel_size**i
|
||||
padding = (kernel_size * dilation - dilation) // 2
|
||||
self.convs_sep.append(
|
||||
nn.Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
groups=channels,
|
||||
dilation=dilation,
|
||||
padding=padding,
|
||||
)
|
||||
)
|
||||
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
||||
self.norms_1.append(LayerNorm(channels))
|
||||
self.norms_2.append(LayerNorm(channels))
|
||||
|
||||
def forward(self, x, x_mask, g = None):
|
||||
if g is not None:
|
||||
x = x + g
|
||||
for i in range(self.n_layers):
|
||||
y = self.convs_sep[i](x * x_mask)
|
||||
y = self.norms_1[i](y)
|
||||
y = F.gelu(y)
|
||||
y = self.convs_1x1[i](y)
|
||||
y = self.norms_2[i](y)
|
||||
y = F.gelu(y)
|
||||
y = self.drop(y)
|
||||
x = x + y
|
||||
return x * x_mask
|
||||
|
||||
|
||||
class WN(torch.nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
hidden_channels,
|
||||
kernel_size,
|
||||
dilation_rate,
|
||||
n_layers,
|
||||
gin_channels=0,
|
||||
p_dropout=0,
|
||||
):
|
||||
super(WN, self).__init__()
|
||||
assert kernel_size % 2 == 1
|
||||
self.hidden_channels = hidden_channels
|
||||
self.kernel_size = (kernel_size,)
|
||||
self.dilation_rate = dilation_rate
|
||||
self.n_layers = n_layers
|
||||
self.gin_channels = gin_channels
|
||||
self.p_dropout = float(p_dropout)
|
||||
|
||||
self.in_layers = torch.nn.ModuleList()
|
||||
self.res_skip_layers = torch.nn.ModuleList()
|
||||
self.drop = nn.Dropout(float(p_dropout))
|
||||
|
||||
if gin_channels != 0:
|
||||
cond_layer = torch.nn.Conv1d(
|
||||
gin_channels, 2 * hidden_channels * n_layers, 1
|
||||
)
|
||||
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name="weight")
|
||||
|
||||
for i in range(n_layers):
|
||||
dilation = dilation_rate**i
|
||||
padding = int((kernel_size * dilation - dilation) / 2)
|
||||
in_layer = torch.nn.Conv1d(
|
||||
hidden_channels,
|
||||
2 * hidden_channels,
|
||||
kernel_size,
|
||||
dilation=dilation,
|
||||
padding=padding,
|
||||
)
|
||||
in_layer = torch.nn.utils.weight_norm(in_layer, name="weight")
|
||||
self.in_layers.append(in_layer)
|
||||
|
||||
# last one is not necessary
|
||||
if i < n_layers - 1:
|
||||
res_skip_channels = 2 * hidden_channels
|
||||
else:
|
||||
res_skip_channels = hidden_channels
|
||||
|
||||
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
||||
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name="weight")
|
||||
self.res_skip_layers.append(res_skip_layer)
|
||||
|
||||
def forward(
|
||||
self, x, x_mask, g = None
|
||||
):
|
||||
output = torch.zeros_like(x)
|
||||
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
||||
|
||||
if g is not None:
|
||||
g = self.cond_layer(g)
|
||||
|
||||
for i, (in_layer, res_skip_layer) in enumerate(
|
||||
zip(self.in_layers, self.res_skip_layers)
|
||||
):
|
||||
x_in = in_layer(x)
|
||||
if g is not None:
|
||||
cond_offset = i * 2 * self.hidden_channels
|
||||
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
||||
else:
|
||||
g_l = torch.zeros_like(x_in)
|
||||
|
||||
acts = commons.fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor)
|
||||
acts = self.drop(acts)
|
||||
|
||||
res_skip_acts = res_skip_layer(acts)
|
||||
if i < self.n_layers - 1:
|
||||
res_acts = res_skip_acts[:, : self.hidden_channels, :]
|
||||
x = (x + res_acts) * x_mask
|
||||
output = output + res_skip_acts[:, self.hidden_channels :, :]
|
||||
else:
|
||||
output = output + res_skip_acts
|
||||
return output * x_mask
|
||||
|
||||
def remove_weight_norm(self):
|
||||
if self.gin_channels != 0:
|
||||
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
||||
for l in self.in_layers:
|
||||
torch.nn.utils.remove_weight_norm(l)
|
||||
for l in self.res_skip_layers:
|
||||
torch.nn.utils.remove_weight_norm(l)
|
||||
|
||||
class ResBlock1(torch.nn.Module):
|
||||
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
||||
super(ResBlock1, self).__init__()
|
||||
self.convs1 = nn.ModuleList(
|
||||
[
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=dilation[0],
|
||||
padding=get_padding(kernel_size, dilation[0]),
|
||||
)
|
||||
),
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=dilation[1],
|
||||
padding=get_padding(kernel_size, dilation[1]),
|
||||
)
|
||||
),
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=dilation[2],
|
||||
padding=get_padding(kernel_size, dilation[2]),
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
self.convs1.apply(init_weights)
|
||||
|
||||
self.convs2 = nn.ModuleList(
|
||||
[
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=1,
|
||||
padding=get_padding(kernel_size, 1),
|
||||
)
|
||||
),
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=1,
|
||||
padding=get_padding(kernel_size, 1),
|
||||
)
|
||||
),
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=1,
|
||||
padding=get_padding(kernel_size, 1),
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
self.convs2.apply(init_weights)
|
||||
self.lrelu_slope = LRELU_SLOPE
|
||||
|
||||
def forward(self, x, x_mask = None):
|
||||
for c1, c2 in zip(self.convs1, self.convs2):
|
||||
xt = F.leaky_relu(x, self.lrelu_slope)
|
||||
if x_mask is not None:
|
||||
xt = xt * x_mask
|
||||
xt = c1(xt)
|
||||
xt = F.leaky_relu(xt, self.lrelu_slope)
|
||||
if x_mask is not None:
|
||||
xt = xt * x_mask
|
||||
xt = c2(xt)
|
||||
x = xt + x
|
||||
if x_mask is not None:
|
||||
x = x * x_mask
|
||||
return x
|
||||
|
||||
def remove_weight_norm(self):
|
||||
for l in self.convs1:
|
||||
remove_weight_norm(l)
|
||||
for l in self.convs2:
|
||||
remove_weight_norm(l)
|
||||
|
||||
class ResBlock2(torch.nn.Module):
|
||||
def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
|
||||
super(ResBlock2, self).__init__()
|
||||
self.convs = nn.ModuleList(
|
||||
[
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=dilation[0],
|
||||
padding=get_padding(kernel_size, dilation[0]),
|
||||
)
|
||||
),
|
||||
weight_norm(
|
||||
Conv1d(
|
||||
channels,
|
||||
channels,
|
||||
kernel_size,
|
||||
1,
|
||||
dilation=dilation[1],
|
||||
padding=get_padding(kernel_size, dilation[1]),
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
self.convs.apply(init_weights)
|
||||
self.lrelu_slope = LRELU_SLOPE
|
||||
|
||||
def forward(self, x, x_mask = None):
|
||||
for c in self.convs:
|
||||
xt = F.leaky_relu(x, self.lrelu_slope)
|
||||
if x_mask is not None:
|
||||
xt = xt * x_mask
|
||||
xt = c(xt)
|
||||
x = xt + x
|
||||
if x_mask is not None:
|
||||
x = x * x_mask
|
||||
return x
|
||||
|
||||
def remove_weight_norm(self):
|
||||
for l in self.convs:
|
||||
remove_weight_norm(l)
|
||||
|
||||
class Log(nn.Module):
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
x_mask,
|
||||
g = None,
|
||||
reverse = False,
|
||||
) :
|
||||
if not reverse:
|
||||
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
||||
logdet = torch.sum(-y, [1, 2])
|
||||
return y, logdet
|
||||
else:
|
||||
x = torch.exp(x) * x_mask
|
||||
return x
|
||||
|
||||
|
||||
class Flip(nn.Module):
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
x_mask,
|
||||
g = None,
|
||||
reverse = False,
|
||||
) :
|
||||
x = torch.flip(x, [1])
|
||||
if not reverse:
|
||||
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
||||
return x, logdet
|
||||
else:
|
||||
return x, torch.zeros([1], device=x.device)
|
||||
|
||||
|
||||
class ElementwiseAffine(nn.Module):
|
||||
def __init__(self, channels):
|
||||
super(ElementwiseAffine, self).__init__()
|
||||
self.channels = channels
|
||||
self.m = nn.Parameter(torch.zeros(channels, 1))
|
||||
self.logs = nn.Parameter(torch.zeros(channels, 1))
|
||||
|
||||
def forward(self, x, x_mask, reverse=False, **kwargs):
|
||||
if not reverse:
|
||||
y = self.m + torch.exp(self.logs) * x
|
||||
y = y * x_mask
|
||||
logdet = torch.sum(self.logs * x_mask, [1, 2])
|
||||
return y, logdet
|
||||
else:
|
||||
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
||||
return x
|
||||
|
||||
|
||||
class ResidualCouplingLayer(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
channels,
|
||||
hidden_channels,
|
||||
kernel_size,
|
||||
dilation_rate,
|
||||
n_layers,
|
||||
p_dropout=0,
|
||||
gin_channels=0,
|
||||
mean_only=False,
|
||||
):
|
||||
assert channels % 2 == 0, "channels should be divisible by 2"
|
||||
super(ResidualCouplingLayer, self).__init__()
|
||||
self.channels = channels
|
||||
self.hidden_channels = hidden_channels
|
||||
self.kernel_size = kernel_size
|
||||
self.dilation_rate = dilation_rate
|
||||
self.n_layers = n_layers
|
||||
self.half_channels = channels // 2
|
||||
self.mean_only = mean_only
|
||||
|
||||
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
||||
self.enc = WN(
|
||||
hidden_channels,
|
||||
kernel_size,
|
||||
dilation_rate,
|
||||
n_layers,
|
||||
p_dropout=float(p_dropout),
|
||||
gin_channels=gin_channels,
|
||||
)
|
||||
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
||||
self.post.weight.data.zero_()
|
||||
self.post.bias.data.zero_()
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
x_mask,
|
||||
g = None,
|
||||
reverse = False,
|
||||
):
|
||||
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
||||
h = self.pre(x0) * x_mask
|
||||
h = self.enc(h, x_mask, g=g)
|
||||
stats = self.post(h) * x_mask
|
||||
if not self.mean_only:
|
||||
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
||||
else:
|
||||
m = stats
|
||||
logs = torch.zeros_like(m)
|
||||
|
||||
if not reverse:
|
||||
x1 = m + x1 * torch.exp(logs) * x_mask
|
||||
x = torch.cat([x0, x1], 1)
|
||||
logdet = torch.sum(logs, [1, 2])
|
||||
return x, logdet
|
||||
else:
|
||||
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
||||
x = torch.cat([x0, x1], 1)
|
||||
return x, torch.zeros([1])
|
||||
|
||||
def remove_weight_norm(self):
|
||||
self.enc.remove_weight_norm()
|
||||
|
||||
class ConvFlow(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
filter_channels,
|
||||
kernel_size,
|
||||
n_layers,
|
||||
num_bins=10,
|
||||
tail_bound=5.0,
|
||||
):
|
||||
super(ConvFlow, self).__init__()
|
||||
self.in_channels = in_channels
|
||||
self.filter_channels = filter_channels
|
||||
self.kernel_size = kernel_size
|
||||
self.n_layers = n_layers
|
||||
self.num_bins = num_bins
|
||||
self.tail_bound = tail_bound
|
||||
self.half_channels = in_channels // 2
|
||||
|
||||
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
||||
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.0)
|
||||
self.proj = nn.Conv1d(
|
||||
filter_channels, self.half_channels * (num_bins * 3 - 1), 1
|
||||
)
|
||||
self.proj.weight.data.zero_()
|
||||
self.proj.bias.data.zero_()
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
x_mask,
|
||||
g = None,
|
||||
reverse=False,
|
||||
):
|
||||
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
||||
h = self.pre(x0)
|
||||
h = self.convs(h, x_mask, g=g)
|
||||
h = self.proj(h) * x_mask
|
||||
|
||||
b, c, t = x0.shape
|
||||
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
||||
|
||||
unnormalized_widths = h[..., : self.num_bins] / math.sqrt(self.filter_channels)
|
||||
unnormalized_heights = h[..., self.num_bins : 2 * self.num_bins] / math.sqrt(
|
||||
self.filter_channels
|
||||
)
|
||||
unnormalized_derivatives = h[..., 2 * self.num_bins :]
|
||||
|
||||
x1, logabsdet = piecewise_rational_quadratic_transform(
|
||||
x1,
|
||||
unnormalized_widths,
|
||||
unnormalized_heights,
|
||||
unnormalized_derivatives,
|
||||
inverse=reverse,
|
||||
tails="linear",
|
||||
tail_bound=self.tail_bound,
|
||||
)
|
||||
|
||||
x = torch.cat([x0, x1], 1) * x_mask
|
||||
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
||||
if not reverse:
|
||||
return x, logdet
|
||||
else:
|
||||
return x
|
||||
207
infer/module/transforms.py
Normal file
207
infer/module/transforms.py
Normal file
@@ -0,0 +1,207 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch.nn import functional as F
|
||||
|
||||
DEFAULT_MIN_BIN_WIDTH = 1e-3
|
||||
DEFAULT_MIN_BIN_HEIGHT = 1e-3
|
||||
DEFAULT_MIN_DERIVATIVE = 1e-3
|
||||
|
||||
|
||||
def piecewise_rational_quadratic_transform(
|
||||
inputs,
|
||||
unnormalized_widths,
|
||||
unnormalized_heights,
|
||||
unnormalized_derivatives,
|
||||
inverse=False,
|
||||
tails=None,
|
||||
tail_bound=1.0,
|
||||
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
||||
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
||||
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
||||
):
|
||||
if tails is None:
|
||||
spline_fn = rational_quadratic_spline
|
||||
spline_kwargs = {}
|
||||
else:
|
||||
spline_fn = unconstrained_rational_quadratic_spline
|
||||
spline_kwargs = {"tails": tails, "tail_bound": tail_bound}
|
||||
|
||||
outputs, logabsdet = spline_fn(
|
||||
inputs=inputs,
|
||||
unnormalized_widths=unnormalized_widths,
|
||||
unnormalized_heights=unnormalized_heights,
|
||||
unnormalized_derivatives=unnormalized_derivatives,
|
||||
inverse=inverse,
|
||||
min_bin_width=min_bin_width,
|
||||
min_bin_height=min_bin_height,
|
||||
min_derivative=min_derivative,
|
||||
**spline_kwargs
|
||||
)
|
||||
return outputs, logabsdet
|
||||
|
||||
|
||||
def searchsorted(bin_locations, inputs, eps=1e-6):
|
||||
bin_locations[..., -1] += eps
|
||||
return torch.sum(inputs[..., None] >= bin_locations, dim=-1) - 1
|
||||
|
||||
|
||||
def unconstrained_rational_quadratic_spline(
|
||||
inputs,
|
||||
unnormalized_widths,
|
||||
unnormalized_heights,
|
||||
unnormalized_derivatives,
|
||||
inverse=False,
|
||||
tails="linear",
|
||||
tail_bound=1.0,
|
||||
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
||||
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
||||
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
||||
):
|
||||
inside_interval_mask = (inputs >= -tail_bound) & (inputs <= tail_bound)
|
||||
outside_interval_mask = ~inside_interval_mask
|
||||
|
||||
outputs = torch.zeros_like(inputs)
|
||||
logabsdet = torch.zeros_like(inputs)
|
||||
|
||||
if tails == "linear":
|
||||
unnormalized_derivatives = F.pad(unnormalized_derivatives, pad=(1, 1))
|
||||
constant = np.log(np.exp(1 - min_derivative) - 1)
|
||||
unnormalized_derivatives[..., 0] = constant
|
||||
unnormalized_derivatives[..., -1] = constant
|
||||
|
||||
outputs[outside_interval_mask] = inputs[outside_interval_mask]
|
||||
logabsdet[outside_interval_mask] = 0
|
||||
else:
|
||||
raise RuntimeError("{} tails are not implemented.".format(tails))
|
||||
|
||||
(
|
||||
outputs[inside_interval_mask],
|
||||
logabsdet[inside_interval_mask],
|
||||
) = rational_quadratic_spline(
|
||||
inputs=inputs[inside_interval_mask],
|
||||
unnormalized_widths=unnormalized_widths[inside_interval_mask, :],
|
||||
unnormalized_heights=unnormalized_heights[inside_interval_mask, :],
|
||||
unnormalized_derivatives=unnormalized_derivatives[inside_interval_mask, :],
|
||||
inverse=inverse,
|
||||
left=-tail_bound,
|
||||
right=tail_bound,
|
||||
bottom=-tail_bound,
|
||||
top=tail_bound,
|
||||
min_bin_width=min_bin_width,
|
||||
min_bin_height=min_bin_height,
|
||||
min_derivative=min_derivative,
|
||||
)
|
||||
|
||||
return outputs, logabsdet
|
||||
|
||||
|
||||
def rational_quadratic_spline(
|
||||
inputs,
|
||||
unnormalized_widths,
|
||||
unnormalized_heights,
|
||||
unnormalized_derivatives,
|
||||
inverse=False,
|
||||
left=0.0,
|
||||
right=1.0,
|
||||
bottom=0.0,
|
||||
top=1.0,
|
||||
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
||||
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
||||
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
||||
):
|
||||
if torch.min(inputs) < left or torch.max(inputs) > right:
|
||||
raise ValueError("Input to a transform is not within its domain")
|
||||
|
||||
num_bins = unnormalized_widths.shape[-1]
|
||||
|
||||
if min_bin_width * num_bins > 1.0:
|
||||
raise ValueError("Minimal bin width too large for the number of bins")
|
||||
if min_bin_height * num_bins > 1.0:
|
||||
raise ValueError("Minimal bin height too large for the number of bins")
|
||||
|
||||
widths = F.softmax(unnormalized_widths, dim=-1)
|
||||
widths = min_bin_width + (1 - min_bin_width * num_bins) * widths
|
||||
cumwidths = torch.cumsum(widths, dim=-1)
|
||||
cumwidths = F.pad(cumwidths, pad=(1, 0), mode="constant", value=0.0)
|
||||
cumwidths = (right - left) * cumwidths + left
|
||||
cumwidths[..., 0] = left
|
||||
cumwidths[..., -1] = right
|
||||
widths = cumwidths[..., 1:] - cumwidths[..., :-1]
|
||||
|
||||
derivatives = min_derivative + F.softplus(unnormalized_derivatives)
|
||||
|
||||
heights = F.softmax(unnormalized_heights, dim=-1)
|
||||
heights = min_bin_height + (1 - min_bin_height * num_bins) * heights
|
||||
cumheights = torch.cumsum(heights, dim=-1)
|
||||
cumheights = F.pad(cumheights, pad=(1, 0), mode="constant", value=0.0)
|
||||
cumheights = (top - bottom) * cumheights + bottom
|
||||
cumheights[..., 0] = bottom
|
||||
cumheights[..., -1] = top
|
||||
heights = cumheights[..., 1:] - cumheights[..., :-1]
|
||||
|
||||
if inverse:
|
||||
bin_idx = searchsorted(cumheights, inputs)[..., None]
|
||||
else:
|
||||
bin_idx = searchsorted(cumwidths, inputs)[..., None]
|
||||
|
||||
input_cumwidths = cumwidths.gather(-1, bin_idx)[..., 0]
|
||||
input_bin_widths = widths.gather(-1, bin_idx)[..., 0]
|
||||
|
||||
input_cumheights = cumheights.gather(-1, bin_idx)[..., 0]
|
||||
delta = heights / widths
|
||||
input_delta = delta.gather(-1, bin_idx)[..., 0]
|
||||
|
||||
input_derivatives = derivatives.gather(-1, bin_idx)[..., 0]
|
||||
input_derivatives_plus_one = derivatives[..., 1:].gather(-1, bin_idx)[..., 0]
|
||||
|
||||
input_heights = heights.gather(-1, bin_idx)[..., 0]
|
||||
|
||||
if inverse:
|
||||
a = (inputs - input_cumheights) * (
|
||||
input_derivatives + input_derivatives_plus_one - 2 * input_delta
|
||||
) + input_heights * (input_delta - input_derivatives)
|
||||
b = input_heights * input_derivatives - (inputs - input_cumheights) * (
|
||||
input_derivatives + input_derivatives_plus_one - 2 * input_delta
|
||||
)
|
||||
c = -input_delta * (inputs - input_cumheights)
|
||||
|
||||
discriminant = b.pow(2) - 4 * a * c
|
||||
assert (discriminant >= 0).all()
|
||||
|
||||
root = (2 * c) / (-b - torch.sqrt(discriminant))
|
||||
outputs = root * input_bin_widths + input_cumwidths
|
||||
|
||||
theta_one_minus_theta = root * (1 - root)
|
||||
denominator = input_delta + (
|
||||
(input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
||||
* theta_one_minus_theta
|
||||
)
|
||||
derivative_numerator = input_delta.pow(2) * (
|
||||
input_derivatives_plus_one * root.pow(2)
|
||||
+ 2 * input_delta * theta_one_minus_theta
|
||||
+ input_derivatives * (1 - root).pow(2)
|
||||
)
|
||||
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
||||
|
||||
return outputs, -logabsdet
|
||||
else:
|
||||
theta = (inputs - input_cumwidths) / input_bin_widths
|
||||
theta_one_minus_theta = theta * (1 - theta)
|
||||
|
||||
numerator = input_heights * (
|
||||
input_delta * theta.pow(2) + input_derivatives * theta_one_minus_theta
|
||||
)
|
||||
denominator = input_delta + (
|
||||
(input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
||||
* theta_one_minus_theta
|
||||
)
|
||||
outputs = input_cumheights + numerator / denominator
|
||||
|
||||
derivative_numerator = input_delta.pow(2) * (
|
||||
input_derivatives_plus_one * theta.pow(2)
|
||||
+ 2 * input_delta * theta_one_minus_theta
|
||||
+ input_derivatives * (1 - theta).pow(2)
|
||||
)
|
||||
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
||||
|
||||
return outputs, logabsdet
|
||||
640
infer/rmvpe.py
Normal file
640
infer/rmvpe.py
Normal file
@@ -0,0 +1,640 @@
|
||||
import os
|
||||
from typing import List, Optional, Tuple
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from librosa.util import normalize, pad_center, tiny
|
||||
from scipy.signal import get_window
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class STFT(torch.nn.Module):
|
||||
def __init__(
|
||||
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 is a bit tricky so there are some cases that probably won't work as working
|
||||
out the same sizes before and after in all overlap add setups is tough. Right now,
|
||||
this code should work with hop lengths that are half the filter length (50% overlap
|
||||
between frames).
|
||||
|
||||
Keyword Arguments:
|
||||
filter_length {int} -- Length of filters used (default: {1024})
|
||||
hop_length {int} -- Hop length of STFT (restrict to 50% overlap between frames) (default: {512})
|
||||
win_length {[type]} -- Length of the window function applied to each frame (if not specified, it
|
||||
equals the filter length). (default: {None})
|
||||
window {str} -- Type of window to use (options are bartlett, hann, hamming, blackman, blackmanharris)
|
||||
(default: {'hann'})
|
||||
"""
|
||||
super(STFT, self).__init__()
|
||||
self.filter_length = filter_length
|
||||
self.hop_length = hop_length
|
||||
self.win_length = win_length if win_length else filter_length
|
||||
self.window = window
|
||||
self.forward_transform = None
|
||||
self.pad_amount = int(self.filter_length / 2)
|
||||
fourier_basis = np.fft.fft(np.eye(self.filter_length))
|
||||
|
||||
cutoff = int((self.filter_length / 2 + 1))
|
||||
fourier_basis = np.vstack(
|
||||
[np.real(fourier_basis[:cutoff, :]), np.imag(fourier_basis[:cutoff, :])]
|
||||
)
|
||||
forward_basis = torch.FloatTensor(fourier_basis)
|
||||
inverse_basis = torch.FloatTensor(np.linalg.pinv(fourier_basis))
|
||||
|
||||
assert filter_length >= self.win_length
|
||||
# get window and zero center pad it to filter_length
|
||||
fft_window = get_window(window, self.win_length, fftbins=True)
|
||||
fft_window = pad_center(fft_window, size=filter_length)
|
||||
fft_window = torch.from_numpy(fft_window).float()
|
||||
|
||||
# window the bases
|
||||
forward_basis *= fft_window
|
||||
inverse_basis = (inverse_basis.T * fft_window).T
|
||||
|
||||
self.register_buffer("forward_basis", forward_basis.float())
|
||||
self.register_buffer("inverse_basis", inverse_basis.float())
|
||||
self.register_buffer("fft_window", fft_window.float())
|
||||
|
||||
def transform(self, input_data, return_phase=False):
|
||||
"""Take input data (audio) to STFT domain.
|
||||
|
||||
Arguments:
|
||||
input_data {tensor} -- Tensor of floats, with shape (num_batch, num_samples)
|
||||
|
||||
Returns:
|
||||
magnitude {tensor} -- Magnitude of STFT with shape (num_batch,
|
||||
num_frequencies, num_frames)
|
||||
phase {tensor} -- Phase of STFT with shape (num_batch,
|
||||
num_frequencies, num_frames)
|
||||
"""
|
||||
input_data = F.pad(
|
||||
input_data,
|
||||
(self.pad_amount, self.pad_amount),
|
||||
mode="reflect",
|
||||
)
|
||||
forward_transform = input_data.unfold(
|
||||
1, self.filter_length, self.hop_length
|
||||
).permute(0, 2, 1)
|
||||
forward_transform = torch.matmul(self.forward_basis, forward_transform)
|
||||
cutoff = int((self.filter_length / 2) + 1)
|
||||
real_part = forward_transform[:, :cutoff, :]
|
||||
imag_part = forward_transform[:, cutoff:, :]
|
||||
magnitude = torch.sqrt(real_part**2 + imag_part**2)
|
||||
if return_phase:
|
||||
phase = torch.atan2(imag_part.data, real_part.data)
|
||||
return magnitude, phase
|
||||
else:
|
||||
return magnitude
|
||||
|
||||
def inverse(self, magnitude, phase):
|
||||
"""Call the inverse STFT (iSTFT), given magnitude and phase tensors produced
|
||||
by the ```transform``` function.
|
||||
|
||||
Arguments:
|
||||
magnitude {tensor} -- Magnitude of STFT with shape (num_batch,
|
||||
num_frequencies, num_frames)
|
||||
phase {tensor} -- Phase of STFT with shape (num_batch,
|
||||
num_frequencies, num_frames)
|
||||
|
||||
Returns:
|
||||
inverse_transform {tensor} -- Reconstructed audio given magnitude and phase. Of
|
||||
shape (num_batch, num_samples)
|
||||
"""
|
||||
cat = torch.cat(
|
||||
[magnitude * torch.cos(phase), magnitude * torch.sin(phase)], dim=1
|
||||
)
|
||||
fold = torch.nn.Fold(
|
||||
output_size=(1, (cat.size(-1) - 1) * self.hop_length + self.filter_length),
|
||||
kernel_size=(1, self.filter_length),
|
||||
stride=(1, self.hop_length),
|
||||
)
|
||||
inverse_transform = torch.matmul(self.inverse_basis, cat)
|
||||
inverse_transform = fold(inverse_transform)[
|
||||
:, 0, 0, self.pad_amount : -self.pad_amount
|
||||
]
|
||||
window_square_sum = (
|
||||
self.fft_window.pow(2).repeat(cat.size(-1), 1).T.unsqueeze(0)
|
||||
)
|
||||
window_square_sum = fold(window_square_sum)[
|
||||
:, 0, 0, self.pad_amount : -self.pad_amount
|
||||
]
|
||||
inverse_transform /= window_square_sum
|
||||
return inverse_transform
|
||||
|
||||
def forward(self, input_data):
|
||||
"""Take input data (audio) to STFT domain and then back to audio.
|
||||
|
||||
Arguments:
|
||||
input_data {tensor} -- Tensor of floats, with shape (num_batch, num_samples)
|
||||
|
||||
Returns:
|
||||
reconstruction {tensor} -- Reconstructed audio given magnitude and phase. Of
|
||||
shape (num_batch, num_samples)
|
||||
"""
|
||||
self.magnitude, self.phase = self.transform(input_data, return_phase=True)
|
||||
reconstruction = self.inverse(self.magnitude, self.phase)
|
||||
return reconstruction
|
||||
|
||||
|
||||
from time import time as ttime
|
||||
|
||||
|
||||
class BiGRU(nn.Module):
|
||||
def __init__(self, input_features, hidden_features, num_layers):
|
||||
super(BiGRU, self).__init__()
|
||||
self.gru = nn.GRU(
|
||||
input_features,
|
||||
hidden_features,
|
||||
num_layers=num_layers,
|
||||
batch_first=True,
|
||||
bidirectional=True,
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
return self.gru(x)[0]
|
||||
|
||||
|
||||
class ConvBlockRes(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, momentum=0.01):
|
||||
super(ConvBlockRes, self).__init__()
|
||||
self.conv = nn.Sequential(
|
||||
nn.Conv2d(
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=(3, 3),
|
||||
stride=(1, 1),
|
||||
padding=(1, 1),
|
||||
bias=False,
|
||||
),
|
||||
nn.BatchNorm2d(out_channels, momentum=momentum),
|
||||
nn.ReLU(),
|
||||
nn.Conv2d(
|
||||
in_channels=out_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=(3, 3),
|
||||
stride=(1, 1),
|
||||
padding=(1, 1),
|
||||
bias=False,
|
||||
),
|
||||
nn.BatchNorm2d(out_channels, momentum=momentum),
|
||||
nn.ReLU(),
|
||||
)
|
||||
# self.shortcut:Optional[nn.Module] = None
|
||||
if in_channels != out_channels:
|
||||
self.shortcut = nn.Conv2d(in_channels, out_channels, (1, 1))
|
||||
|
||||
def forward(self, x):
|
||||
if not hasattr(self, "shortcut"):
|
||||
return self.conv(x) + x
|
||||
else:
|
||||
return self.conv(x) + self.shortcut(x)
|
||||
|
||||
|
||||
class Encoder(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
in_size,
|
||||
n_encoders,
|
||||
kernel_size,
|
||||
n_blocks,
|
||||
out_channels=16,
|
||||
momentum=0.01,
|
||||
):
|
||||
super(Encoder, self).__init__()
|
||||
self.n_encoders = n_encoders
|
||||
self.bn = nn.BatchNorm2d(in_channels, momentum=momentum)
|
||||
self.layers = nn.ModuleList()
|
||||
self.latent_channels = []
|
||||
for i in range(self.n_encoders):
|
||||
self.layers.append(
|
||||
ResEncoderBlock(
|
||||
in_channels, out_channels, kernel_size, n_blocks, momentum=momentum
|
||||
)
|
||||
)
|
||||
self.latent_channels.append([out_channels, in_size])
|
||||
in_channels = out_channels
|
||||
out_channels *= 2
|
||||
in_size //= 2
|
||||
self.out_size = in_size
|
||||
self.out_channel = out_channels
|
||||
|
||||
def forward(self, x):
|
||||
concat_tensors = []
|
||||
x = self.bn(x)
|
||||
for i, layer in enumerate(self.layers):
|
||||
t, x = layer(x)
|
||||
concat_tensors.append(t)
|
||||
return x, concat_tensors
|
||||
|
||||
|
||||
class ResEncoderBlock(nn.Module):
|
||||
def __init__(
|
||||
self, in_channels, out_channels, kernel_size, n_blocks=1, momentum=0.01
|
||||
):
|
||||
super(ResEncoderBlock, self).__init__()
|
||||
self.n_blocks = n_blocks
|
||||
self.conv = nn.ModuleList()
|
||||
self.conv.append(ConvBlockRes(in_channels, out_channels, momentum))
|
||||
for i in range(n_blocks - 1):
|
||||
self.conv.append(ConvBlockRes(out_channels, out_channels, momentum))
|
||||
self.kernel_size = kernel_size
|
||||
if self.kernel_size is not None:
|
||||
self.pool = nn.AvgPool2d(kernel_size=kernel_size)
|
||||
|
||||
def forward(self, x):
|
||||
for i, conv in enumerate(self.conv):
|
||||
x = conv(x)
|
||||
if self.kernel_size is not None:
|
||||
return x, self.pool(x)
|
||||
else:
|
||||
return x
|
||||
|
||||
|
||||
class Intermediate(nn.Module): #
|
||||
def __init__(self, in_channels, out_channels, n_inters, n_blocks, momentum=0.01):
|
||||
super(Intermediate, self).__init__()
|
||||
self.n_inters = n_inters
|
||||
self.layers = nn.ModuleList()
|
||||
self.layers.append(
|
||||
ResEncoderBlock(in_channels, out_channels, None, n_blocks, momentum)
|
||||
)
|
||||
for i in range(self.n_inters - 1):
|
||||
self.layers.append(
|
||||
ResEncoderBlock(out_channels, out_channels, None, n_blocks, momentum)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
for i, layer in enumerate(self.layers):
|
||||
x = layer(x)
|
||||
return x
|
||||
|
||||
|
||||
class ResDecoderBlock(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, stride, n_blocks=1, momentum=0.01):
|
||||
super(ResDecoderBlock, self).__init__()
|
||||
out_padding = (0, 1) if stride == (1, 2) else (1, 1)
|
||||
self.n_blocks = n_blocks
|
||||
self.conv1 = nn.Sequential(
|
||||
nn.ConvTranspose2d(
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=(3, 3),
|
||||
stride=stride,
|
||||
padding=(1, 1),
|
||||
output_padding=out_padding,
|
||||
bias=False,
|
||||
),
|
||||
nn.BatchNorm2d(out_channels, momentum=momentum),
|
||||
nn.ReLU(),
|
||||
)
|
||||
self.conv2 = nn.ModuleList()
|
||||
self.conv2.append(ConvBlockRes(out_channels * 2, out_channels, momentum))
|
||||
for i in range(n_blocks - 1):
|
||||
self.conv2.append(ConvBlockRes(out_channels, out_channels, momentum))
|
||||
|
||||
def forward(self, x, concat_tensor):
|
||||
x = self.conv1(x)
|
||||
x = torch.cat((x, concat_tensor), dim=1)
|
||||
for i, conv2 in enumerate(self.conv2):
|
||||
x = conv2(x)
|
||||
return x
|
||||
|
||||
|
||||
class Decoder(nn.Module):
|
||||
def __init__(self, in_channels, n_decoders, stride, n_blocks, momentum=0.01):
|
||||
super(Decoder, self).__init__()
|
||||
self.layers = nn.ModuleList()
|
||||
self.n_decoders = n_decoders
|
||||
for i in range(self.n_decoders):
|
||||
out_channels = in_channels // 2
|
||||
self.layers.append(
|
||||
ResDecoderBlock(in_channels, out_channels, stride, n_blocks, momentum)
|
||||
)
|
||||
in_channels = out_channels
|
||||
|
||||
def forward(self, x, concat_tensors):
|
||||
for i, layer in enumerate(self.layers):
|
||||
x = layer(x, concat_tensors[-1 - i])
|
||||
return x
|
||||
|
||||
|
||||
class DeepUnet(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
kernel_size,
|
||||
n_blocks,
|
||||
en_de_layers=5,
|
||||
inter_layers=4,
|
||||
in_channels=1,
|
||||
en_out_channels=16,
|
||||
):
|
||||
super(DeepUnet, self).__init__()
|
||||
self.encoder = Encoder(
|
||||
in_channels, 128, en_de_layers, kernel_size, n_blocks, en_out_channels
|
||||
)
|
||||
self.intermediate = Intermediate(
|
||||
self.encoder.out_channel // 2,
|
||||
self.encoder.out_channel,
|
||||
inter_layers,
|
||||
n_blocks,
|
||||
)
|
||||
self.decoder = Decoder(
|
||||
self.encoder.out_channel, en_de_layers, kernel_size, n_blocks
|
||||
)
|
||||
|
||||
def forward(self, x) :
|
||||
x, concat_tensors = self.encoder(x)
|
||||
x = self.intermediate(x)
|
||||
x = self.decoder(x, concat_tensors)
|
||||
return x
|
||||
|
||||
|
||||
class E2E(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
n_blocks,
|
||||
n_gru,
|
||||
kernel_size,
|
||||
en_de_layers=5,
|
||||
inter_layers=4,
|
||||
in_channels=1,
|
||||
en_out_channels=16,
|
||||
):
|
||||
super(E2E, self).__init__()
|
||||
self.unet = DeepUnet(
|
||||
kernel_size,
|
||||
n_blocks,
|
||||
en_de_layers,
|
||||
inter_layers,
|
||||
in_channels,
|
||||
en_out_channels,
|
||||
)
|
||||
self.cnn = nn.Conv2d(en_out_channels, 3, (3, 3), padding=(1, 1))
|
||||
if n_gru:
|
||||
self.fc = nn.Sequential(
|
||||
BiGRU(3 * 128, 256, n_gru),
|
||||
nn.Linear(512, 360),
|
||||
nn.Dropout(0.25),
|
||||
nn.Sigmoid(),
|
||||
)
|
||||
else:
|
||||
self.fc = nn.Sequential(
|
||||
nn.Linear(3 * nn.N_MELS, nn.N_CLASS), nn.Dropout(0.25), nn.Sigmoid()
|
||||
)
|
||||
|
||||
def forward(self, mel):
|
||||
# print(mel.shape)
|
||||
mel = mel.transpose(-1, -2).unsqueeze(1)
|
||||
x = self.cnn(self.unet(mel)).transpose(1, 2).flatten(-2)
|
||||
x = self.fc(x)
|
||||
# print(x.shape)
|
||||
return x
|
||||
|
||||
|
||||
from librosa.filters import mel
|
||||
|
||||
|
||||
class MelSpectrogram(torch.nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
is_half,
|
||||
n_mel_channels,
|
||||
sampling_rate,
|
||||
win_length,
|
||||
hop_length,
|
||||
n_fft=None,
|
||||
mel_fmin=0,
|
||||
mel_fmax=None,
|
||||
clamp=1e-5,
|
||||
):
|
||||
super().__init__()
|
||||
n_fft = win_length if n_fft is None else n_fft
|
||||
self.hann_window = {}
|
||||
mel_basis = mel(
|
||||
sr=sampling_rate,
|
||||
n_fft=n_fft,
|
||||
n_mels=n_mel_channels,
|
||||
fmin=mel_fmin,
|
||||
fmax=mel_fmax,
|
||||
htk=True,
|
||||
)
|
||||
mel_basis = torch.from_numpy(mel_basis).float()
|
||||
self.register_buffer("mel_basis", mel_basis)
|
||||
self.n_fft = win_length if n_fft is None else n_fft
|
||||
self.hop_length = hop_length
|
||||
self.win_length = win_length
|
||||
self.sampling_rate = sampling_rate
|
||||
self.n_mel_channels = n_mel_channels
|
||||
self.clamp = clamp
|
||||
self.is_half = is_half
|
||||
|
||||
def forward(self, audio, keyshift=0, speed=1, center=True):
|
||||
factor = 2 ** (keyshift / 12)
|
||||
n_fft_new = int(np.round(self.n_fft * factor))
|
||||
win_length_new = int(np.round(self.win_length * factor))
|
||||
hop_length_new = int(np.round(self.hop_length * speed))
|
||||
keyshift_key = str(keyshift) + "_" + str(audio.device)
|
||||
if keyshift_key not in self.hann_window:
|
||||
self.hann_window[keyshift_key] = torch.hann_window(win_length_new).to(
|
||||
audio.device
|
||||
)
|
||||
if "privateuseone" in str(audio.device):
|
||||
if not hasattr(self, "stft"):
|
||||
self.stft = STFT(
|
||||
filter_length=n_fft_new,
|
||||
hop_length=hop_length_new,
|
||||
win_length=win_length_new,
|
||||
window="hann",
|
||||
).to(audio.device)
|
||||
magnitude = self.stft.transform(audio)
|
||||
else:
|
||||
fft = torch.stft(
|
||||
audio,
|
||||
n_fft=n_fft_new,
|
||||
hop_length=hop_length_new,
|
||||
win_length=win_length_new,
|
||||
window=self.hann_window[keyshift_key],
|
||||
center=center,
|
||||
return_complex=True,
|
||||
)
|
||||
magnitude = torch.sqrt(fft.real.pow(2) + fft.imag.pow(2))
|
||||
if keyshift != 0:
|
||||
size = self.n_fft // 2 + 1
|
||||
resize = magnitude.size(1)
|
||||
if resize < size:
|
||||
magnitude = F.pad(magnitude, (0, 0, 0, size - resize))
|
||||
magnitude = magnitude[:, :size, :] * self.win_length / win_length_new
|
||||
mel_output = torch.matmul(self.mel_basis, magnitude)
|
||||
if self.is_half == True:
|
||||
mel_output = mel_output.half()
|
||||
log_mel_spec = torch.log(torch.clamp(mel_output, min=self.clamp))
|
||||
return log_mel_spec
|
||||
|
||||
|
||||
class RMVPE:
|
||||
def __init__(self, model_path, is_half, device=None):
|
||||
self.resample_kernel = {}
|
||||
self.resample_kernel = {}
|
||||
if isinstance(is_half, str):
|
||||
is_half = is_half.lower() == "true"
|
||||
if device is None:
|
||||
from configs.config import infer_device, infer_dtype
|
||||
|
||||
device = str(infer_device)
|
||||
is_half = infer_dtype == torch.float16
|
||||
elif str(device).startswith("cuda"):
|
||||
from configs.config import get_device_dtype_sm
|
||||
|
||||
parsed_device = torch.device(device)
|
||||
device_index = parsed_device.index
|
||||
if device_index is None:
|
||||
device_index = torch.cuda.current_device()
|
||||
selected_device, selected_dtype, _, _ = get_device_dtype_sm(device_index)
|
||||
device = str(selected_device)
|
||||
is_half = selected_dtype == torch.float16
|
||||
else:
|
||||
is_half = False
|
||||
self.is_half = is_half
|
||||
self.device = device
|
||||
self.mel_extractor = MelSpectrogram(
|
||||
is_half, 128, 16000, 1024, 160, None, 30, 8000
|
||||
).to(device)
|
||||
if "privateuseone" in str(device):
|
||||
import onnxruntime as ort
|
||||
|
||||
ort_session = ort.InferenceSession(
|
||||
os.path.splitext(model_path)[0] + ".onnx",
|
||||
providers=["DmlExecutionProvider"],
|
||||
)
|
||||
self.model = ort_session
|
||||
else:
|
||||
if str(self.device) == "cuda":
|
||||
self.device = torch.device("cuda:0")
|
||||
|
||||
def get_default_model():
|
||||
model = E2E(4, 1, (2, 2))
|
||||
ckpt = torch.load(model_path, map_location="cpu")
|
||||
model.load_state_dict(ckpt)
|
||||
model.eval()
|
||||
if is_half:
|
||||
model = model.half()
|
||||
else:
|
||||
model = model.float()
|
||||
return model
|
||||
|
||||
self.model = get_default_model()
|
||||
|
||||
self.model = self.model.to(device)
|
||||
cents_mapping = 20 * np.arange(360) + 1997.3794084376191
|
||||
self.cents_mapping = np.pad(cents_mapping, (4, 4)) # 368
|
||||
|
||||
def mel2hidden(self, mel):
|
||||
with torch.no_grad():
|
||||
n_frames = mel.shape[-1]
|
||||
n_pad = 32 * ((n_frames - 1) // 32 + 1) - n_frames
|
||||
if n_pad > 0:
|
||||
mel = F.pad(mel, (0, n_pad), mode="constant")
|
||||
if "privateuseone" in str(self.device):
|
||||
onnx_input_name = self.model.get_inputs()[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]
|
||||
else:
|
||||
mel = mel.half() if self.is_half else mel.float()
|
||||
hidden = self.model(mel)
|
||||
return hidden[:, :n_frames]
|
||||
|
||||
def decode(self, hidden, thred=0.03):
|
||||
cents_pred = self.to_local_average_cents(hidden, thred=thred)
|
||||
f0 = 10 * (2 ** (cents_pred / 1200))
|
||||
f0[f0 == 10] = 0
|
||||
# f0 = np.array([10 * (2 ** (cent_pred / 1200)) if cent_pred else 0 for cent_pred in cents_pred])
|
||||
return f0
|
||||
|
||||
def infer_from_audio(self, audio, thred=0.03):
|
||||
# torch.cuda.synchronize()
|
||||
# t0 = ttime()
|
||||
if not torch.is_tensor(audio):
|
||||
audio = torch.from_numpy(audio)
|
||||
mel = self.mel_extractor(
|
||||
audio.float().to(self.device).unsqueeze(0), center=True
|
||||
)
|
||||
# print(123123123,mel.device.type)
|
||||
# torch.cuda.synchronize()
|
||||
# t1 = ttime()
|
||||
hidden = self.mel2hidden(mel)
|
||||
# torch.cuda.synchronize()
|
||||
# t2 = ttime()
|
||||
# print(234234,hidden.device.type)
|
||||
if "privateuseone" not in str(self.device):
|
||||
hidden = hidden.squeeze(0).cpu().numpy()
|
||||
else:
|
||||
hidden = hidden[0]
|
||||
if self.is_half == True:
|
||||
hidden = hidden.astype("float32")
|
||||
|
||||
f0 = self.decode(hidden, thred=thred)
|
||||
# torch.cuda.synchronize()
|
||||
# t3 = ttime()
|
||||
# print("hmvpe:%s\t%s\t%s\t%s"%(t1-t0,t2-t1,t3-t2,t3-t0))
|
||||
return f0
|
||||
|
||||
def to_local_average_cents(self, salience, thred=0.05):
|
||||
# t0 = ttime()
|
||||
center = np.argmax(salience, axis=1) # 帧长#index
|
||||
salience = np.pad(salience, ((0, 0), (4, 4))) # 帧长,368
|
||||
# t1 = ttime()
|
||||
center += 4
|
||||
todo_salience = []
|
||||
todo_cents_mapping = []
|
||||
starts = center - 4
|
||||
ends = center + 5
|
||||
for idx in range(salience.shape[0]):
|
||||
todo_salience.append(salience[:, starts[idx] : ends[idx]][idx])
|
||||
todo_cents_mapping.append(self.cents_mapping[starts[idx] : ends[idx]])
|
||||
# t2 = ttime()
|
||||
todo_salience = np.array(todo_salience) # 帧长,9
|
||||
todo_cents_mapping = np.array(todo_cents_mapping) # 帧长,9
|
||||
product_sum = np.sum(todo_salience * todo_cents_mapping, 1)
|
||||
weight_sum = np.sum(todo_salience, 1) # 帧长
|
||||
devided = product_sum / weight_sum # 帧长
|
||||
# t3 = ttime()
|
||||
maxx = np.max(salience, axis=1) # 帧长
|
||||
devided[maxx <= thred] = 0
|
||||
# t4 = ttime()
|
||||
# print("decode:%s\t%s\t%s\t%s" % (t1 - t0, t2 - t1, t3 - t2, t4 - t3))
|
||||
return devided
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import librosa
|
||||
import soundfile as sf
|
||||
|
||||
audio, sampling_rate = sf.read(r"C:\Users\liujing04\Desktop\Z\冬之花clip1.wav")
|
||||
if len(audio.shape) > 1:
|
||||
audio = librosa.to_mono(audio.transpose(1, 0))
|
||||
audio_bak = audio.copy()
|
||||
if sampling_rate != 16000:
|
||||
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
|
||||
model_path = r"D:\BaiduNetdiskDownload\RVC-beta-v2-0727AMD_realtime\rmvpe.pt"
|
||||
thred = 0.03 # 0.01
|
||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||
rmvpe = RMVPE(model_path, is_half=False, device=device)
|
||||
t0 = ttime()
|
||||
f0 = rmvpe.infer_from_audio(audio, thred=thred)
|
||||
# f0 = rmvpe.infer_from_audio(audio, thred=thred)
|
||||
# f0 = rmvpe.infer_from_audio(audio, thred=thred)
|
||||
# f0 = rmvpe.infer_from_audio(audio, thred=thred)
|
||||
# f0 = rmvpe.infer_from_audio(audio, thred=thred)
|
||||
t1 = ttime()
|
||||
logger.info("%s %.2f", f0.shape, t1 - t0)
|
||||
342
infer/rtrvc.py
Normal file
342
infer/rtrvc.py
Normal file
@@ -0,0 +1,342 @@
|
||||
import traceback
|
||||
from time import time as ttime
|
||||
import faiss
|
||||
import numpy as np
|
||||
import parselmouth
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from torchaudio.transforms import Resample
|
||||
|
||||
from infer.hubert import extract_hubert_features, load_hubert_model
|
||||
from i18n.i18n import I18nAuto
|
||||
|
||||
|
||||
i18n = I18nAuto()
|
||||
|
||||
|
||||
def printt(strr, *args):
|
||||
if len(args) == 0:
|
||||
print(strr)
|
||||
else:
|
||||
print(strr % args)
|
||||
|
||||
|
||||
def get_synthesizer(pth_path, device=torch.device("cpu")):
|
||||
from infer.module.models import (
|
||||
SynthesizerTrnMs256NSFsid,
|
||||
SynthesizerTrnMs256NSFsid_nono,
|
||||
SynthesizerTrnMs768NSFsid,
|
||||
SynthesizerTrnMs768NSFsid_nono,
|
||||
)
|
||||
|
||||
cpt = torch.load(pth_path, map_location=torch.device("cpu"))
|
||||
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
||||
if_f0 = cpt.get("f0", 1)
|
||||
version = cpt.get("version", "v1")
|
||||
if version == "v1":
|
||||
if if_f0 == 1:
|
||||
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=False)
|
||||
else:
|
||||
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
||||
elif version == "v2":
|
||||
if if_f0 == 1:
|
||||
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=False)
|
||||
else:
|
||||
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
||||
del net_g.enc_q
|
||||
net_g.load_state_dict(cpt["weight"], strict=False)
|
||||
net_g = net_g.float()
|
||||
net_g.eval().to(device)
|
||||
net_g.remove_weight_norm()
|
||||
return net_g, cpt
|
||||
|
||||
|
||||
# config.device=torch.device("cpu")########强制cpu测试
|
||||
# config.is_half=False########强制cpu测试
|
||||
class RVC:
|
||||
def __init__(
|
||||
self,
|
||||
key,
|
||||
formant,
|
||||
pth_path,
|
||||
index_path,
|
||||
index_rate,
|
||||
config,
|
||||
last_rvc=None,
|
||||
) :
|
||||
"""
|
||||
初始化
|
||||
"""
|
||||
try:
|
||||
# global config
|
||||
self.config = config
|
||||
# device="cpu"########强制cpu测试
|
||||
self.device = config.device
|
||||
self.f0_up_key = key
|
||||
self.formant_shift = formant
|
||||
self.f0_min = 50
|
||||
self.f0_max = 1100
|
||||
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
|
||||
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
|
||||
self.is_half = config.is_half
|
||||
if index_rate != 0:
|
||||
self.index = faiss.read_index(index_path)
|
||||
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
||||
printt(i18n("已启用索引检索"))
|
||||
self.pth_path = pth_path
|
||||
self.index_path = index_path
|
||||
self.index_rate = index_rate
|
||||
self.cache_pitch = torch.zeros(
|
||||
1024, device=self.device, dtype=torch.long
|
||||
)
|
||||
self.cache_pitchf = torch.zeros(
|
||||
1024, device=self.device, dtype=torch.float32
|
||||
)
|
||||
|
||||
self.resample_kernel = {}
|
||||
|
||||
if last_rvc is None:
|
||||
self.model = load_hubert_model(self.device, self.is_half)
|
||||
else:
|
||||
self.model = last_rvc.model
|
||||
|
||||
self.net_g = None
|
||||
|
||||
def set_synthesizer():
|
||||
self.net_g, cpt = get_synthesizer(self.pth_path, self.device)
|
||||
self.tgt_sr = cpt["config"][-1]
|
||||
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
||||
self.if_f0 = cpt.get("f0", 1)
|
||||
self.version = cpt.get("version", "v1")
|
||||
if self.is_half:
|
||||
self.net_g = self.net_g.half()
|
||||
else:
|
||||
self.net_g = self.net_g.float()
|
||||
|
||||
if last_rvc is None or last_rvc.pth_path != self.pth_path:
|
||||
set_synthesizer()
|
||||
else:
|
||||
self.tgt_sr = last_rvc.tgt_sr
|
||||
self.if_f0 = last_rvc.if_f0
|
||||
self.version = last_rvc.version
|
||||
self.is_half = last_rvc.is_half
|
||||
self.net_g = last_rvc.net_g
|
||||
|
||||
if last_rvc is not None and hasattr(last_rvc, "model_rmvpe"):
|
||||
self.model_rmvpe = last_rvc.model_rmvpe
|
||||
if last_rvc is not None and hasattr(last_rvc, "model_fcpe"):
|
||||
self.model_fcpe = last_rvc.model_fcpe
|
||||
except:
|
||||
printt(traceback.format_exc())
|
||||
|
||||
def change_key(self, new_key):
|
||||
self.f0_up_key = new_key
|
||||
|
||||
def change_formant(self, new_formant):
|
||||
self.formant_shift = new_formant
|
||||
|
||||
def change_index_rate(self, new_index_rate):
|
||||
if new_index_rate != 0 and self.index_rate == 0:
|
||||
self.index = faiss.read_index(self.index_path)
|
||||
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
||||
printt(i18n("已启用索引检索"))
|
||||
self.index_rate = new_index_rate
|
||||
|
||||
def get_f0_post(self, f0):
|
||||
if not torch.is_tensor(f0):
|
||||
f0 = torch.from_numpy(f0)
|
||||
f0 = f0.float().to(self.device).squeeze()
|
||||
f0_mel = 1127 * torch.log(1 + f0 / 700)
|
||||
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * 254 / (
|
||||
self.f0_mel_max - self.f0_mel_min
|
||||
) + 1
|
||||
f0_mel[f0_mel <= 1] = 1
|
||||
f0_mel[f0_mel > 255] = 255
|
||||
f0_coarse = torch.round(f0_mel).long()
|
||||
return f0_coarse, f0
|
||||
|
||||
def get_f0(self, x, f0_up_key, method="rmvpe"):
|
||||
if method == "rmvpe":
|
||||
return self.get_f0_rmvpe(x, f0_up_key)
|
||||
if method == "fcpe":
|
||||
return self.get_f0_fcpe(x, f0_up_key)
|
||||
if method != "pm":
|
||||
raise ValueError(f"Unsupported F0 method: {method}")
|
||||
x = x.cpu().numpy()
|
||||
p_len = x.shape[0] // 160 + 1
|
||||
f0_min = 65
|
||||
l_pad = int(np.ceil(1.5 / f0_min * 16000))
|
||||
r_pad = l_pad + 1
|
||||
s = parselmouth.Sound(np.pad(x, (l_pad, r_pad)), 16000).to_pitch_ac(
|
||||
time_step=0.01,
|
||||
voicing_threshold=0.6,
|
||||
pitch_floor=f0_min,
|
||||
pitch_ceiling=1100,
|
||||
)
|
||||
assert np.abs(s.t1 - 1.5 / f0_min) < 0.001
|
||||
f0 = s.selected_array["frequency"]
|
||||
if len(f0) < p_len:
|
||||
f0 = np.pad(f0, (0, p_len - len(f0)))
|
||||
f0 = f0[:p_len]
|
||||
try:
|
||||
uv = f0 == 0
|
||||
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
f0 *= pow(2, f0_up_key / 12)
|
||||
return self.get_f0_post(f0)
|
||||
|
||||
def get_f0_rmvpe(self, x, f0_up_key):
|
||||
if hasattr(self, "model_rmvpe") == False:
|
||||
from infer.rmvpe import RMVPE
|
||||
|
||||
printt(i18n("正在加载RMVPE模型"))
|
||||
self.model_rmvpe = RMVPE(
|
||||
"assets/rmvpe/rmvpe.pt",
|
||||
is_half=self.is_half,
|
||||
device=self.device,
|
||||
)
|
||||
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
||||
try:
|
||||
uv = f0 == 0
|
||||
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
f0 *= pow(2, f0_up_key / 12)
|
||||
return self.get_f0_post(f0)
|
||||
|
||||
def get_f0_fcpe(self, x, f0_up_key):
|
||||
if hasattr(self, "model_fcpe") == False:
|
||||
from infer.fcpe import FCPEInfer
|
||||
|
||||
printt("Loading fcpe model")
|
||||
self.model_fcpe = FCPEInfer(self.device)
|
||||
f0 = self.model_fcpe.infer(
|
||||
x.unsqueeze(0).float(),
|
||||
sr=16000,
|
||||
decoder_mode="local_argmax",
|
||||
threshold=0.006,
|
||||
).squeeze().detach().cpu().numpy()
|
||||
try:
|
||||
uv = f0 == 0
|
||||
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
f0 *= pow(2, f0_up_key / 12)
|
||||
return self.get_f0_post(f0)
|
||||
|
||||
def infer(
|
||||
self,
|
||||
input_wav,
|
||||
block_frame_16k,
|
||||
skip_head,
|
||||
return_length,
|
||||
f0method,
|
||||
) :
|
||||
t1 = ttime()
|
||||
with torch.no_grad():
|
||||
if self.config.is_half:
|
||||
feats = input_wav.half().view(1, -1)
|
||||
else:
|
||||
feats = input_wav.float().view(1, -1)
|
||||
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
||||
feats = extract_hubert_features(
|
||||
self.model,
|
||||
feats,
|
||||
self.version,
|
||||
padding_mask=padding_mask,
|
||||
)
|
||||
feats = torch.cat((feats, feats[:, -1:, :]), 1)
|
||||
t2 = ttime()
|
||||
try:
|
||||
if hasattr(self, "index") and self.index_rate != 0:
|
||||
npy = feats[0][skip_head // 2 :].cpu().numpy().astype("float32")
|
||||
score, ix = self.index.search(npy, k=8)
|
||||
if (ix >= 0).all():
|
||||
weight = np.square(1 / score)
|
||||
weight /= weight.sum(axis=1, keepdims=True)
|
||||
npy = np.sum(
|
||||
self.big_npy[ix] * np.expand_dims(weight, axis=2), axis=1
|
||||
)
|
||||
if self.config.is_half:
|
||||
npy = npy.astype("float16")
|
||||
feats[0][skip_head // 2 :] = (
|
||||
torch.from_numpy(npy).unsqueeze(0).to(self.device)
|
||||
* self.index_rate
|
||||
+ (1 - self.index_rate) * feats[0][skip_head // 2 :]
|
||||
)
|
||||
else:
|
||||
printt(
|
||||
i18n("索引无效:必须使用added_xxxx.index,不能使用trained_xxxx.index")
|
||||
)
|
||||
else:
|
||||
printt(i18n("索引检索失败或未启用"))
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
printt(i18n("索引检索失败"))
|
||||
t3 = ttime()
|
||||
p_len = input_wav.shape[0] // 160
|
||||
factor = pow(2, self.formant_shift / 12)
|
||||
return_length2 = int(np.ceil(return_length * factor))
|
||||
if self.if_f0 == 1:
|
||||
f0_extractor_frame = block_frame_16k + 800
|
||||
if f0method == "rmvpe":
|
||||
f0_extractor_frame = 5120 * ((f0_extractor_frame - 1) // 5120 + 1) - 160
|
||||
pitch, pitchf = self.get_f0(
|
||||
input_wav[-f0_extractor_frame:],
|
||||
self.f0_up_key - self.formant_shift,
|
||||
f0method,
|
||||
)
|
||||
shift = block_frame_16k // 160
|
||||
self.cache_pitch[:-shift] = self.cache_pitch[shift:].clone()
|
||||
self.cache_pitchf[:-shift] = self.cache_pitchf[shift:].clone()
|
||||
self.cache_pitch[4 - pitch.shape[0] :] = pitch[3:-1]
|
||||
self.cache_pitchf[4 - pitch.shape[0] :] = pitchf[3:-1]
|
||||
cache_pitch = self.cache_pitch[None, -p_len:]
|
||||
cache_pitchf = self.cache_pitchf[None, -p_len:] * return_length2 / return_length
|
||||
t4 = ttime()
|
||||
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
||||
feats = feats[:, :p_len, :]
|
||||
p_len = torch.LongTensor([p_len]).to(self.device)
|
||||
sid = torch.LongTensor([0]).to(self.device)
|
||||
skip_head = torch.LongTensor([skip_head])
|
||||
return_length2 = torch.LongTensor([return_length2])
|
||||
return_length = torch.LongTensor([return_length])
|
||||
with torch.no_grad():
|
||||
if self.if_f0 == 1:
|
||||
infered_audio, _, _ = self.net_g.infer(
|
||||
feats,
|
||||
p_len,
|
||||
cache_pitch,
|
||||
cache_pitchf,
|
||||
sid,
|
||||
skip_head,
|
||||
return_length,
|
||||
return_length2,
|
||||
)
|
||||
else:
|
||||
infered_audio, _, _ = self.net_g.infer(
|
||||
feats, p_len, sid, skip_head, return_length, return_length2
|
||||
)
|
||||
infered_audio = infered_audio.squeeze(1).float()
|
||||
upp_res = int(np.floor(factor * self.tgt_sr // 100))
|
||||
if upp_res != self.tgt_sr // 100:
|
||||
if upp_res not in self.resample_kernel:
|
||||
self.resample_kernel[upp_res] = Resample(
|
||||
orig_freq=upp_res,
|
||||
new_freq=self.tgt_sr // 100,
|
||||
dtype=torch.float32,
|
||||
).to(self.device)
|
||||
infered_audio = self.resample_kernel[upp_res](
|
||||
infered_audio[:, : return_length * upp_res]
|
||||
)
|
||||
t5 = ttime()
|
||||
printt(
|
||||
i18n("耗时:特征=%.3f秒,索引=%.3f秒,音高=%.3f秒,模型=%.3f秒"),
|
||||
t2 - t1,
|
||||
t3 - t2,
|
||||
t4 - t3,
|
||||
t5 - t4,
|
||||
)
|
||||
return infered_audio.squeeze()
|
||||
0
infer/vc/__init__.py
Normal file
0
infer/vc/__init__.py
Normal file
359
infer/vc/modules.py
Normal file
359
infer/vc/modules.py
Normal file
@@ -0,0 +1,359 @@
|
||||
import traceback
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
import numpy as np
|
||||
import soundfile as sf
|
||||
import torch
|
||||
from io import BytesIO
|
||||
|
||||
from infer.audio import load_audio, wav2
|
||||
from infer.module.models import (
|
||||
SynthesizerTrnMs256NSFsid,
|
||||
SynthesizerTrnMs256NSFsid_nono,
|
||||
SynthesizerTrnMs768NSFsid,
|
||||
SynthesizerTrnMs768NSFsid_nono,
|
||||
)
|
||||
from infer.vc.pipeline import Pipeline
|
||||
from infer.vc.utils import *
|
||||
from i18n.i18n import I18nAuto
|
||||
from tools.progress import batch_status, should_report
|
||||
|
||||
|
||||
i18n = I18nAuto()
|
||||
|
||||
|
||||
def inference_status(title, state, detail=""):
|
||||
lines = ["【%s】" % i18n(title), "%s:%s" % (i18n("状态"), i18n(state))]
|
||||
if detail:
|
||||
lines.extend(["", str(detail).strip()])
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
class VC:
|
||||
def __init__(self, config):
|
||||
self.n_spk = None
|
||||
self.tgt_sr = None
|
||||
self.net_g = None
|
||||
self.pipeline = None
|
||||
self.cpt = None
|
||||
self.version = None
|
||||
self.if_f0 = None
|
||||
self.version = None
|
||||
self.hubert_model = None
|
||||
|
||||
self.config = config
|
||||
|
||||
def get_vc(self, sid, *to_return_protect):
|
||||
logger.info("%s: %s", i18n("选择模型"), sid)
|
||||
|
||||
to_return_protect0 = {
|
||||
"visible": self.if_f0 != 0,
|
||||
"value": (
|
||||
to_return_protect[0] if self.if_f0 != 0 and to_return_protect else 0.5
|
||||
),
|
||||
"__type__": "update",
|
||||
}
|
||||
to_return_protect1 = {
|
||||
"visible": self.if_f0 != 0,
|
||||
"value": (
|
||||
to_return_protect[1] if self.if_f0 != 0 and to_return_protect else 0.33
|
||||
),
|
||||
"__type__": "update",
|
||||
}
|
||||
|
||||
if sid == "" or sid == []:
|
||||
if (
|
||||
self.hubert_model is not None
|
||||
): # 考虑到轮询, 需要加个判断看是否 sid 是由有模型切换到无模型的
|
||||
logger.info(i18n("清理模型缓存"))
|
||||
del (self.net_g, self.n_spk, self.hubert_model, self.tgt_sr) # ,cpt
|
||||
self.hubert_model = self.net_g = self.n_spk = self.hubert_model = (
|
||||
self.tgt_sr
|
||||
) = None
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
###楼下不这么折腾清理不干净
|
||||
self.if_f0 = self.cpt.get("f0", 1)
|
||||
self.version = self.cpt.get("version", "v1")
|
||||
if self.version == "v1":
|
||||
if self.if_f0 == 1:
|
||||
self.net_g = SynthesizerTrnMs256NSFsid(
|
||||
*self.cpt["config"], is_half=self.config.is_half
|
||||
)
|
||||
else:
|
||||
self.net_g = SynthesizerTrnMs256NSFsid_nono(*self.cpt["config"])
|
||||
elif self.version == "v2":
|
||||
if self.if_f0 == 1:
|
||||
self.net_g = SynthesizerTrnMs768NSFsid(
|
||||
*self.cpt["config"], is_half=self.config.is_half
|
||||
)
|
||||
else:
|
||||
self.net_g = SynthesizerTrnMs768NSFsid_nono(*self.cpt["config"])
|
||||
del self.net_g, self.cpt
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
return (
|
||||
{"visible": False, "__type__": "update"},
|
||||
{
|
||||
"visible": True,
|
||||
"value": to_return_protect0,
|
||||
"__type__": "update",
|
||||
},
|
||||
{
|
||||
"visible": True,
|
||||
"value": to_return_protect1,
|
||||
"__type__": "update",
|
||||
},
|
||||
"",
|
||||
"",
|
||||
)
|
||||
person = f'{os.getenv("weight_root")}/{sid}'
|
||||
logger.info("%s: %s", i18n("正在加载模型"), person)
|
||||
|
||||
self.cpt = torch.load(person, map_location="cpu")
|
||||
self.tgt_sr = self.cpt["config"][-1]
|
||||
self.cpt["config"][-3] = self.cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
||||
self.if_f0 = self.cpt.get("f0", 1)
|
||||
self.version = self.cpt.get("version", "v1")
|
||||
|
||||
synthesizer_class = {
|
||||
("v1", 1): SynthesizerTrnMs256NSFsid,
|
||||
("v1", 0): SynthesizerTrnMs256NSFsid_nono,
|
||||
("v2", 1): SynthesizerTrnMs768NSFsid,
|
||||
("v2", 0): SynthesizerTrnMs768NSFsid_nono,
|
||||
}
|
||||
|
||||
self.net_g = synthesizer_class.get(
|
||||
(self.version, self.if_f0), SynthesizerTrnMs256NSFsid
|
||||
)(*self.cpt["config"], is_half=self.config.is_half)
|
||||
|
||||
del self.net_g.enc_q
|
||||
|
||||
self.net_g.load_state_dict(self.cpt["weight"], strict=False)
|
||||
self.net_g.eval().to(self.config.device)
|
||||
if self.config.is_half:
|
||||
self.net_g = self.net_g.half()
|
||||
else:
|
||||
self.net_g = self.net_g.float()
|
||||
|
||||
self.pipeline = Pipeline(self.tgt_sr, self.config)
|
||||
n_spk = self.cpt["config"][-3]
|
||||
index = {"value": get_index_path_from_model(sid), "__type__": "update"}
|
||||
logger.info("%s: %s", i18n("选择索引"), index["value"])
|
||||
|
||||
return (
|
||||
(
|
||||
{"visible": True, "maximum": n_spk, "__type__": "update"},
|
||||
to_return_protect0,
|
||||
to_return_protect1,
|
||||
index,
|
||||
index,
|
||||
)
|
||||
if to_return_protect
|
||||
else {"visible": True, "maximum": n_spk, "__type__": "update"}
|
||||
)
|
||||
|
||||
def vc_single(
|
||||
self,
|
||||
sid,
|
||||
input_audio_path,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
index_rate,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
protect,
|
||||
):
|
||||
if input_audio_path is None:
|
||||
return inference_status("单次推理", "等待输入", i18n("请上传音频文件")), None
|
||||
f0_up_key = int(f0_up_key)
|
||||
try:
|
||||
audio = load_audio(input_audio_path, 16000)
|
||||
audio_max = np.abs(audio).max() / 0.95
|
||||
if audio_max > 1:
|
||||
audio /= audio_max
|
||||
times = [0, 0, 0]
|
||||
|
||||
if self.hubert_model is None:
|
||||
self.hubert_model = load_hubert(self.config)
|
||||
|
||||
if file_index:
|
||||
file_index = (
|
||||
file_index.strip(" ")
|
||||
.strip('"')
|
||||
.strip("\n")
|
||||
.strip('"')
|
||||
.strip(" ")
|
||||
.replace("trained", "added")
|
||||
)
|
||||
else:
|
||||
file_index = "" # 防止小白写错,自动帮他替换掉
|
||||
|
||||
audio_opt = self.pipeline.pipeline(
|
||||
self.hubert_model,
|
||||
self.net_g,
|
||||
sid,
|
||||
audio,
|
||||
times,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
index_rate,
|
||||
self.if_f0,
|
||||
self.tgt_sr,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
self.version,
|
||||
protect,
|
||||
)
|
||||
if self.tgt_sr != resample_sr >= 16000:
|
||||
tgt_sr = resample_sr
|
||||
else:
|
||||
tgt_sr = self.tgt_sr
|
||||
index_info = (
|
||||
"%s:%s" % (i18n("索引"), file_index)
|
||||
if os.path.exists(file_index)
|
||||
else "%s:%s" % (i18n("索引"), i18n("未使用"))
|
||||
)
|
||||
return (
|
||||
inference_status(
|
||||
"单次推理",
|
||||
"成功",
|
||||
"%s\n%s:%s %.2fs | F0 %.2fs | %s %.2fs"
|
||||
% (
|
||||
index_info,
|
||||
i18n("耗时"),
|
||||
i18n("特征"),
|
||||
times[0],
|
||||
times[1],
|
||||
i18n("合成"),
|
||||
times[2],
|
||||
),
|
||||
),
|
||||
(tgt_sr, audio_opt),
|
||||
)
|
||||
except Exception:
|
||||
info = traceback.format_exc()
|
||||
logger.warning(info)
|
||||
return inference_status("单次推理", "失败", info), (None, None)
|
||||
|
||||
def vc_multi(
|
||||
self,
|
||||
sid,
|
||||
dir_path,
|
||||
opt_root,
|
||||
paths,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
index_rate,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
protect,
|
||||
format1,
|
||||
):
|
||||
try:
|
||||
dir_path = (
|
||||
(dir_path or "")
|
||||
.strip(" ")
|
||||
.strip('"')
|
||||
.strip("\n")
|
||||
.strip('"')
|
||||
.strip(" ")
|
||||
) # 防止小白拷路径头尾带了空格和"和回车
|
||||
opt_root = (
|
||||
(opt_root or "")
|
||||
.strip(" ")
|
||||
.strip('"')
|
||||
.strip("\n")
|
||||
.strip('"')
|
||||
.strip(" ")
|
||||
)
|
||||
if not opt_root:
|
||||
yield inference_status(
|
||||
"批量推理", "等待输入", i18n("请填写输出文件夹路径")
|
||||
)
|
||||
return
|
||||
os.makedirs(opt_root, exist_ok=True)
|
||||
try:
|
||||
if dir_path != "":
|
||||
paths = [
|
||||
os.path.join(dir_path, name) for name in os.listdir(dir_path)
|
||||
]
|
||||
else:
|
||||
paths = [path if isinstance(path, str) else path.name for path in (paths or [])]
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
paths = [
|
||||
path if isinstance(path, str) else path.name for path in (paths or [])
|
||||
]
|
||||
total = len(paths)
|
||||
if total == 0:
|
||||
yield batch_status(i18n("批量推理"), 0, 0, 0, 0)
|
||||
return
|
||||
success = 0
|
||||
failed = 0
|
||||
failures = []
|
||||
for idx, path in enumerate(paths):
|
||||
item_failed = False
|
||||
info, opt = self.vc_single(
|
||||
sid,
|
||||
path,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
index_rate,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
protect,
|
||||
)
|
||||
if opt and opt[0] is not None and opt[1] is not None:
|
||||
try:
|
||||
tgt_sr, audio_opt = opt
|
||||
if format1 in ["wav", "flac"]:
|
||||
sf.write(
|
||||
"%s/%s.%s"
|
||||
% (
|
||||
opt_root,
|
||||
os.path.splitext(os.path.basename(path))[0],
|
||||
format1,
|
||||
),
|
||||
audio_opt,
|
||||
tgt_sr,
|
||||
)
|
||||
else:
|
||||
path = "%s/%s.%s" % (
|
||||
opt_root,
|
||||
os.path.splitext(os.path.basename(path))[0],
|
||||
format1,
|
||||
)
|
||||
with BytesIO() as wavf:
|
||||
sf.write(wavf, audio_opt, tgt_sr, format="wav")
|
||||
wavf.seek(0, 0)
|
||||
with open(path, "wb") as outf:
|
||||
wav2(wavf, outf, format1)
|
||||
success += 1
|
||||
except Exception:
|
||||
info = "%s\n%s" % (info, traceback.format_exc())
|
||||
failed += 1
|
||||
item_failed = True
|
||||
failures.append("%s:%s" % (os.path.basename(path), info))
|
||||
else:
|
||||
failed += 1
|
||||
item_failed = True
|
||||
failures.append("%s:%s" % (os.path.basename(path), info))
|
||||
if should_report(idx, total) or item_failed:
|
||||
yield batch_status(
|
||||
i18n("批量推理"),
|
||||
idx + 1,
|
||||
total,
|
||||
success,
|
||||
failed,
|
||||
os.path.basename(path),
|
||||
failures,
|
||||
)
|
||||
except Exception:
|
||||
yield inference_status("批量推理", "失败", traceback.format_exc())
|
||||
386
infer/vc/pipeline.py
Normal file
386
infer/vc/pipeline.py
Normal file
@@ -0,0 +1,386 @@
|
||||
import os
|
||||
import traceback
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from time import time as ttime
|
||||
|
||||
import faiss
|
||||
import librosa
|
||||
import numpy as np
|
||||
import parselmouth
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from scipy import signal
|
||||
|
||||
from infer.hubert import extract_hubert_features
|
||||
|
||||
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
|
||||
|
||||
|
||||
def change_rms(data1, sr1, data2, sr2, rate): # 1是输入音频,2是输出音频,rate是2的占比
|
||||
# print(data1.max(),data2.max())
|
||||
rms1 = librosa.feature.rms(
|
||||
y=data1, frame_length=sr1 // 2 * 2, hop_length=sr1 // 2
|
||||
) # 每半秒一个点
|
||||
rms2 = librosa.feature.rms(y=data2, frame_length=sr2 // 2 * 2, hop_length=sr2 // 2)
|
||||
rms1 = torch.from_numpy(rms1)
|
||||
rms1 = F.interpolate(
|
||||
rms1.unsqueeze(0), size=data2.shape[0], mode="linear"
|
||||
).squeeze()
|
||||
rms2 = torch.from_numpy(rms2)
|
||||
rms2 = F.interpolate(
|
||||
rms2.unsqueeze(0), size=data2.shape[0], mode="linear"
|
||||
).squeeze()
|
||||
rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-6)
|
||||
data2 *= (
|
||||
torch.pow(rms1, torch.tensor(1 - rate))
|
||||
* torch.pow(rms2, torch.tensor(rate - 1))
|
||||
).numpy()
|
||||
return data2
|
||||
|
||||
|
||||
class Pipeline(object):
|
||||
def __init__(self, tgt_sr, config):
|
||||
self.x_pad, self.x_query, self.x_center, self.x_max, self.is_half = (
|
||||
config.x_pad,
|
||||
config.x_query,
|
||||
config.x_center,
|
||||
config.x_max,
|
||||
config.is_half,
|
||||
)
|
||||
self.sr = 16000 # hubert输入采样率
|
||||
self.window = 160 # 每帧点数
|
||||
self.t_pad = self.sr * self.x_pad # 每条前后pad时间
|
||||
self.t_pad_tgt = tgt_sr * self.x_pad
|
||||
self.t_pad2 = self.t_pad * 2
|
||||
self.t_query = self.sr * self.x_query # 查询切点前后查询时间
|
||||
self.t_center = self.sr * self.x_center # 查询切点位置
|
||||
self.t_max = self.sr * self.x_max # 免查询时长阈值
|
||||
self.device = config.device
|
||||
|
||||
def get_f0(
|
||||
self,
|
||||
x,
|
||||
p_len,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
):
|
||||
if f0_method not in ("pm", "rmvpe", "fcpe"):
|
||||
raise ValueError(f"Unsupported F0 method: {f0_method}")
|
||||
time_step = self.window / self.sr * 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)
|
||||
if f0_method == "pm":
|
||||
f0 = (
|
||||
parselmouth.Sound(x, self.sr)
|
||||
.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"
|
||||
)
|
||||
elif f0_method == "rmvpe":
|
||||
if not hasattr(self, "model_rmvpe"):
|
||||
from infer.rmvpe import RMVPE
|
||||
|
||||
logger.info(
|
||||
"Loading rmvpe model,%s" % "%s/rmvpe.pt" % os.environ["rmvpe_root"]
|
||||
)
|
||||
self.model_rmvpe = RMVPE(
|
||||
"%s/rmvpe.pt" % os.environ["rmvpe_root"],
|
||||
is_half=self.is_half,
|
||||
device=self.device,
|
||||
)
|
||||
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
||||
|
||||
if "privateuseone" in str(self.device): # clean ortruntime memory
|
||||
del self.model_rmvpe.model
|
||||
del self.model_rmvpe
|
||||
logger.info("Cleaning ortruntime memory")
|
||||
elif f0_method == "fcpe":
|
||||
if not hasattr(self, "model_fcpe"):
|
||||
from infer.fcpe import FCPEInfer
|
||||
|
||||
logger.info("Loading fcpe model")
|
||||
self.model_fcpe = FCPEInfer(self.device)
|
||||
f0 = self.model_fcpe.infer(
|
||||
torch.from_numpy(x).unsqueeze(0).float(),
|
||||
sr=self.sr,
|
||||
decoder_mode="local_argmax",
|
||||
threshold=0.006,
|
||||
).squeeze().detach().cpu().numpy()
|
||||
|
||||
try:
|
||||
uv = f0 == 0
|
||||
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
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_coarse = np.rint(f0_mel).astype(np.int32)
|
||||
return f0_coarse, f0bak # 1-0
|
||||
|
||||
def vc(
|
||||
self,
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio0,
|
||||
pitch,
|
||||
pitchf,
|
||||
times,
|
||||
index,
|
||||
index_vectors,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
):
|
||||
feats = torch.from_numpy(audio0)
|
||||
if self.is_half:
|
||||
feats = feats.half()
|
||||
else:
|
||||
feats = feats.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).to(self.device).fill_(False)
|
||||
|
||||
t0 = ttime()
|
||||
with torch.no_grad():
|
||||
feats = extract_hubert_features(
|
||||
model,
|
||||
feats.to(self.device),
|
||||
version,
|
||||
padding_mask=padding_mask,
|
||||
)
|
||||
if protect < 0.5 and pitch is not None and pitchf is not None:
|
||||
feats0 = feats.clone()
|
||||
if (
|
||||
not isinstance(index, type(None))
|
||||
and not isinstance(index_vectors, type(None))
|
||||
and index_rate != 0
|
||||
):
|
||||
npy = feats[0].cpu().numpy()
|
||||
if self.is_half:
|
||||
npy = npy.astype("float32")
|
||||
|
||||
score, ix = index.search(npy, k=8)
|
||||
weight = np.square(1 / score)
|
||||
weight /= weight.sum(axis=1, keepdims=True)
|
||||
npy = np.sum(index_vectors[ix] * np.expand_dims(weight, axis=2), axis=1)
|
||||
|
||||
if self.is_half:
|
||||
npy = npy.astype("float16")
|
||||
feats = (
|
||||
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
|
||||
+ (1 - index_rate) * feats
|
||||
)
|
||||
|
||||
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
||||
if protect < 0.5 and pitch is not None and pitchf is not None:
|
||||
feats0 = F.interpolate(feats0.permute(0, 2, 1), scale_factor=2).permute(
|
||||
0, 2, 1
|
||||
)
|
||||
t1 = ttime()
|
||||
p_len = audio0.shape[0] // self.window
|
||||
if feats.shape[1] < p_len:
|
||||
p_len = feats.shape[1]
|
||||
if pitch is not None and pitchf is not None:
|
||||
pitch = pitch[:, :p_len]
|
||||
pitchf = pitchf[:, :p_len]
|
||||
|
||||
if protect < 0.5 and pitch is not None and pitchf is not None:
|
||||
pitchff = pitchf.clone()
|
||||
pitchff[pitchf > 0] = 1
|
||||
pitchff[pitchf < 1] = protect
|
||||
pitchff = pitchff.unsqueeze(-1)
|
||||
feats = feats * pitchff + feats0 * (1 - pitchff)
|
||||
feats = feats.to(feats0.dtype)
|
||||
p_len = torch.tensor([p_len], device=self.device).long()
|
||||
with torch.no_grad():
|
||||
hasp = pitch is not None and pitchf is not None
|
||||
arg = (feats, p_len, pitch, pitchf, sid) if hasp else (feats, p_len, sid)
|
||||
audio1 = (net_g.infer(*arg)[0][0, 0]).data.cpu().float().numpy()
|
||||
del hasp, arg
|
||||
del feats, p_len, padding_mask
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
t2 = ttime()
|
||||
times[0] += t1 - t0
|
||||
times[2] += t2 - t1
|
||||
return audio1
|
||||
|
||||
def pipeline(
|
||||
self,
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio,
|
||||
times,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
index_rate,
|
||||
if_f0,
|
||||
tgt_sr,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
version,
|
||||
protect,
|
||||
):
|
||||
if (
|
||||
file_index != ""
|
||||
and os.path.exists(file_index)
|
||||
and index_rate != 0
|
||||
):
|
||||
try:
|
||||
index = faiss.read_index(file_index)
|
||||
index_vectors = index.reconstruct_n(0, index.ntotal)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
index = index_vectors = None
|
||||
else:
|
||||
index = index_vectors = None
|
||||
audio = signal.filtfilt(bh, ah, audio)
|
||||
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
|
||||
opt_ts = []
|
||||
if audio_pad.shape[0] > self.t_max:
|
||||
audio_sum = np.zeros_like(audio)
|
||||
for i in range(self.window):
|
||||
audio_sum += np.abs(audio_pad[i : i - self.window])
|
||||
for t in range(self.t_center, audio.shape[0], self.t_center):
|
||||
opt_ts.append(
|
||||
t
|
||||
- self.t_query
|
||||
+ np.where(
|
||||
audio_sum[t - self.t_query : t + self.t_query]
|
||||
== audio_sum[t - self.t_query : t + self.t_query].min()
|
||||
)[0][0]
|
||||
)
|
||||
s = 0
|
||||
audio_opt = []
|
||||
t = None
|
||||
t1 = ttime()
|
||||
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
|
||||
p_len = audio_pad.shape[0] // self.window
|
||||
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
||||
pitch, pitchf = None, None
|
||||
if if_f0 == 1:
|
||||
pitch, pitchf = self.get_f0(
|
||||
audio_pad,
|
||||
p_len,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
)
|
||||
pitch = pitch[:p_len]
|
||||
pitchf = pitchf[:p_len]
|
||||
pitchf = pitchf.astype(np.float32)
|
||||
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
|
||||
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
|
||||
t2 = ttime()
|
||||
times[1] += t2 - t1
|
||||
for t in opt_ts:
|
||||
t = t // self.window * self.window
|
||||
if if_f0 == 1:
|
||||
audio_opt.append(
|
||||
self.vc(
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio_pad[s : t + self.t_pad2 + self.window],
|
||||
pitch[:, s // self.window : (t + self.t_pad2) // self.window],
|
||||
pitchf[:, s // self.window : (t + self.t_pad2) // self.window],
|
||||
times,
|
||||
index,
|
||||
index_vectors,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt : -self.t_pad_tgt]
|
||||
)
|
||||
else:
|
||||
audio_opt.append(
|
||||
self.vc(
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio_pad[s : t + self.t_pad2 + self.window],
|
||||
None,
|
||||
None,
|
||||
times,
|
||||
index,
|
||||
index_vectors,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt : -self.t_pad_tgt]
|
||||
)
|
||||
s = t
|
||||
if if_f0 == 1:
|
||||
audio_opt.append(
|
||||
self.vc(
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio_pad[t:],
|
||||
pitch[:, t // self.window :] if t is not None else pitch,
|
||||
pitchf[:, t // self.window :] if t is not None else pitchf,
|
||||
times,
|
||||
index,
|
||||
index_vectors,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt : -self.t_pad_tgt]
|
||||
)
|
||||
else:
|
||||
audio_opt.append(
|
||||
self.vc(
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio_pad[t:],
|
||||
None,
|
||||
None,
|
||||
times,
|
||||
index,
|
||||
index_vectors,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt : -self.t_pad_tgt]
|
||||
)
|
||||
audio_opt = np.concatenate(audio_opt)
|
||||
if rms_mix_rate != 1:
|
||||
audio_opt = change_rms(audio, 16000, audio_opt, tgt_sr, rms_mix_rate)
|
||||
if tgt_sr != resample_sr >= 16000:
|
||||
audio_opt = librosa.resample(
|
||||
audio_opt, orig_sr=tgt_sr, target_sr=resample_sr
|
||||
)
|
||||
audio_max = np.abs(audio_opt).max() / 0.99
|
||||
max_int16 = 32768
|
||||
if audio_max > 1:
|
||||
max_int16 /= audio_max
|
||||
audio_opt = (audio_opt * max_int16).astype(np.int16)
|
||||
del pitch, pitchf, sid
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
return audio_opt
|
||||
44
infer/vc/utils.py
Normal file
44
infer/vc/utils.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
from infer.hubert import load_hubert_model
|
||||
|
||||
|
||||
def get_index_path_from_model(sid):
|
||||
model_stem = os.path.splitext(os.path.basename(str(sid or "")))[0]
|
||||
experiment_name = re.sub(r"_e\d+_s\d+$", "", model_stem, flags=re.IGNORECASE)
|
||||
if not experiment_name:
|
||||
return ""
|
||||
|
||||
candidates = []
|
||||
roots = [os.getenv("outside_index_root"), os.getenv("index_root")]
|
||||
for index_root in roots:
|
||||
if not index_root or not os.path.isdir(index_root):
|
||||
continue
|
||||
for root, _, files in os.walk(index_root, topdown=False):
|
||||
for name in files:
|
||||
if not name.lower().endswith(".index") or "trained" in name.lower():
|
||||
continue
|
||||
index_stem = os.path.splitext(name)[0]
|
||||
lower_index = index_stem.lower()
|
||||
lower_experiment = experiment_name.lower()
|
||||
standard_match = (
|
||||
lower_index.startswith(lower_experiment + "_added_")
|
||||
or ("_" + lower_experiment + "_v1") in lower_index
|
||||
or ("_" + lower_experiment + "_v2") in lower_index
|
||||
)
|
||||
exact_model_match = model_stem.lower() in lower_index
|
||||
if standard_match or exact_model_match:
|
||||
path = os.path.abspath(os.path.join(root, name))
|
||||
score = (
|
||||
0 if standard_match else 1,
|
||||
0 if os.path.abspath(index_root) == os.path.abspath(roots[0]) else 1,
|
||||
-os.path.getmtime(path),
|
||||
path.lower(),
|
||||
)
|
||||
candidates.append((score, path))
|
||||
return min(candidates, default=(None, ""), key=lambda item: item[0])[1]
|
||||
|
||||
|
||||
def load_hubert(config):
|
||||
return load_hubert_model(config.device, config.is_half)
|
||||
Reference in New Issue
Block a user