mirror of
https://github.com/storytold/storyteller-ml.git
synced 2026-10-09 00:09:55 +00:00
898 lines
33 KiB
Python
898 lines
33 KiB
Python
import copy
|
|
import math
|
|
import torch
|
|
from torch import nn
|
|
from torch.nn import functional as F
|
|
|
|
import commons
|
|
import modules
|
|
import attentions
|
|
import monotonic_align
|
|
|
|
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
|
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
|
from commons import init_weights, get_padding
|
|
from modules import PQMF, CoMBD, SubBandDiscriminator, TMEncoder
|
|
from stft import TorchSTFT
|
|
|
|
class StochasticDurationPredictor(nn.Module):
|
|
def __init__(self, in_channels, filter_channels, kernel_size, p_dropout, n_flows=4, gin_channels=0):
|
|
super().__init__()
|
|
filter_channels = in_channels # it needs to be removed from future version.
|
|
self.in_channels = in_channels
|
|
self.filter_channels = filter_channels
|
|
self.kernel_size = kernel_size
|
|
self.p_dropout = p_dropout
|
|
self.n_flows = n_flows
|
|
self.gin_channels = gin_channels
|
|
|
|
self.log_flow = modules.Log()
|
|
self.flows = nn.ModuleList()
|
|
self.flows.append(modules.ElementwiseAffine(2))
|
|
for i in range(n_flows):
|
|
self.flows.append(modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3))
|
|
self.flows.append(modules.Flip())
|
|
|
|
self.post_pre = nn.Conv1d(1, filter_channels, 1)
|
|
self.post_proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
|
self.post_convs = modules.DDSConv(filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout)
|
|
self.post_flows = nn.ModuleList()
|
|
self.post_flows.append(modules.ElementwiseAffine(2))
|
|
for i in range(4):
|
|
self.post_flows.append(modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3))
|
|
self.post_flows.append(modules.Flip())
|
|
|
|
self.pre = nn.Conv1d(in_channels, filter_channels, 1)
|
|
self.proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
|
self.convs = modules.DDSConv(filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout)
|
|
if gin_channels != 0:
|
|
self.cond = nn.Conv1d(gin_channels, filter_channels, 1)
|
|
|
|
def forward(self, x, x_mask, w=None, g=None, reverse=False, noise_scale=1.0):
|
|
x = torch.detach(x)
|
|
x = self.pre(x)
|
|
if g is not None:
|
|
g = torch.detach(g)
|
|
x = x + self.cond(g)
|
|
x = self.convs(x, x_mask)
|
|
x = self.proj(x) * x_mask
|
|
|
|
if not reverse:
|
|
flows = self.flows
|
|
assert w is not None
|
|
|
|
logdet_tot_q = 0
|
|
h_w = self.post_pre(w)
|
|
h_w = self.post_convs(h_w, x_mask)
|
|
h_w = self.post_proj(h_w) * x_mask
|
|
e_q = torch.randn(w.size(0), 2, w.size(2)).to(device=x.device, dtype=x.dtype) * x_mask
|
|
z_q = e_q
|
|
for flow in self.post_flows:
|
|
z_q, logdet_q = flow(z_q, x_mask, g=(x + h_w))
|
|
logdet_tot_q += logdet_q
|
|
z_u, z1 = torch.split(z_q, [1, 1], 1)
|
|
u = torch.sigmoid(z_u) * x_mask
|
|
z0 = (w - u) * x_mask
|
|
logdet_tot_q += torch.sum((F.logsigmoid(z_u) + F.logsigmoid(-z_u)) * x_mask, [1,2])
|
|
logq = torch.sum(-0.5 * (math.log(2*math.pi) + (e_q**2)) * x_mask, [1,2]) - logdet_tot_q
|
|
|
|
logdet_tot = 0
|
|
z0, logdet = self.log_flow(z0, x_mask)
|
|
logdet_tot += logdet
|
|
z = torch.cat([z0, z1], 1)
|
|
for flow in flows:
|
|
z, logdet = flow(z, x_mask, g=x, reverse=reverse)
|
|
logdet_tot = logdet_tot + logdet
|
|
nll = torch.sum(0.5 * (math.log(2*math.pi) + (z**2)) * x_mask, [1,2]) - logdet_tot
|
|
return nll + logq # [b]
|
|
else:
|
|
flows = list(reversed(self.flows))
|
|
flows = flows[:-2] + [flows[-1]] # remove a useless vflow
|
|
z = torch.randn(x.size(0), 2, x.size(2)).to(device=x.device, dtype=x.dtype) * noise_scale
|
|
for flow in flows:
|
|
z = flow(z, x_mask, g=x, reverse=reverse)
|
|
z0, z1 = torch.split(z, [1, 1], 1)
|
|
logw = z0
|
|
return logw
|
|
|
|
|
|
class DurationPredictor(nn.Module):
|
|
def __init__(self, in_channels, filter_channels, kernel_size, p_dropout, gin_channels=0):
|
|
super().__init__()
|
|
|
|
self.in_channels = in_channels
|
|
self.filter_channels = filter_channels
|
|
self.kernel_size = kernel_size
|
|
self.p_dropout = p_dropout
|
|
self.gin_channels = gin_channels
|
|
|
|
self.drop = nn.Dropout(p_dropout)
|
|
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size, padding=kernel_size//2)
|
|
self.norm_1 = modules.LayerNorm(filter_channels)
|
|
self.conv_2 = nn.Conv1d(filter_channels, filter_channels, kernel_size, padding=kernel_size//2)
|
|
self.norm_2 = modules.LayerNorm(filter_channels)
|
|
self.proj = nn.Conv1d(filter_channels, 1, 1)
|
|
|
|
if gin_channels != 0:
|
|
self.cond = nn.Conv1d(gin_channels, in_channels, 1)
|
|
|
|
def forward(self, x, x_mask, g=None):
|
|
x = torch.detach(x)
|
|
if g is not None:
|
|
g = torch.detach(g)
|
|
x = x + self.cond(g)
|
|
x = self.conv_1(x * x_mask)
|
|
x = torch.relu(x)
|
|
x = self.norm_1(x)
|
|
x = self.drop(x)
|
|
x = self.conv_2(x * x_mask)
|
|
x = torch.relu(x)
|
|
x = self.norm_2(x)
|
|
x = self.drop(x)
|
|
x = self.proj(x * x_mask)
|
|
return x * x_mask
|
|
|
|
|
|
class TextEncoder(nn.Module):
|
|
def __init__(self,
|
|
n_vocab,
|
|
out_channels,
|
|
hidden_channels,
|
|
filter_channels,
|
|
n_heads,
|
|
n_layers,
|
|
kernel_size,
|
|
p_dropout,
|
|
tm_last):
|
|
super().__init__()
|
|
self.n_vocab = n_vocab
|
|
self.out_channels = out_channels
|
|
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.emb = nn.Embedding(n_vocab, hidden_channels)
|
|
nn.init.normal_(self.emb.weight, 0.0, hidden_channels**-0.5)
|
|
|
|
|
|
|
|
self.encoder = attentions.Encoder(
|
|
hidden_channels + tm_last,
|
|
filter_channels,
|
|
n_heads,
|
|
n_layers,
|
|
kernel_size,
|
|
p_dropout)
|
|
self.proj= nn.Conv1d(hidden_channels + tm_last, out_channels * 2, 1)
|
|
|
|
def forward(self, x, x_lengths, torchmoji_hidden):
|
|
x = self.emb(x) * math.sqrt(self.hidden_channels) # [b, t, h]
|
|
|
|
torchmoji_hidden = torchmoji_hidden[:, None].repeat(1, x.size(1), 1)
|
|
x = torch.cat((x, torchmoji_hidden), dim=-1)
|
|
|
|
|
|
x = torch.transpose(x, 1, -1) # [b, h, t]
|
|
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
|
|
|
x = self.encoder(x * x_mask, x_mask)
|
|
stats = self.proj(x) * x_mask
|
|
|
|
m, logs = torch.split(stats, self.out_channels, dim=1)
|
|
return x, m, logs, x_mask
|
|
|
|
|
|
class ResidualCouplingBlock(nn.Module):
|
|
def __init__(self,
|
|
channels,
|
|
hidden_channels,
|
|
kernel_size,
|
|
dilation_rate,
|
|
n_layers,
|
|
n_flows=4,
|
|
gin_channels=0):
|
|
super().__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.n_flows = n_flows
|
|
self.gin_channels = gin_channels
|
|
|
|
self.flows = nn.ModuleList()
|
|
for i in range(n_flows):
|
|
self.flows.append(modules.ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels, mean_only=True))
|
|
self.flows.append(modules.Flip())
|
|
|
|
def forward(self, x, x_mask, g=None, reverse=False):
|
|
if not reverse:
|
|
for flow in self.flows:
|
|
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
|
else:
|
|
for flow in reversed(self.flows):
|
|
x = flow(x, x_mask, g=g, reverse=reverse)
|
|
return x
|
|
|
|
|
|
class PosteriorEncoder(nn.Module):
|
|
def __init__(self,
|
|
in_channels,
|
|
out_channels,
|
|
hidden_channels,
|
|
kernel_size,
|
|
dilation_rate,
|
|
n_layers,
|
|
gin_channels=0):
|
|
super().__init__()
|
|
self.in_channels = in_channels
|
|
self.out_channels = out_channels
|
|
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.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
|
self.enc = modules.WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
|
|
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
|
|
|
def forward(self, x, x_lengths, g=None):
|
|
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
|
x = self.pre(x) * x_mask
|
|
x = self.enc(x, x_mask, g=g)
|
|
stats = self.proj(x) * x_mask
|
|
m, logs = torch.split(stats, self.out_channels, dim=1)
|
|
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
|
return z, m, logs, x_mask
|
|
|
|
|
|
class Generator(torch.nn.Module):
|
|
def __init__(self, initial_channel, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gen_istft_n_fft, gin_channels=0):
|
|
super(Generator, self).__init__()
|
|
self.num_kernels = len(resblock_kernel_sizes)
|
|
self.num_upsamples = len(upsample_rates)
|
|
self.conv_pre = Conv1d(initial_channel, upsample_initial_channel, 7, 1, padding=3)
|
|
resblock = modules.ResBlock1 if resblock == '1' else modules.ResBlock2
|
|
|
|
self.ups = nn.ModuleList()
|
|
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
|
self.ups.append(weight_norm(
|
|
ConvTranspose1d(upsample_initial_channel//(2**i), upsample_initial_channel//(2**(i+1)),
|
|
k, u, padding=(k-u)//2)))
|
|
|
|
self.resblocks = nn.ModuleList()
|
|
for i in range(len(self.ups)):
|
|
ch = upsample_initial_channel//(2**(i+1))
|
|
for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)):
|
|
self.resblocks.append(resblock(ch, k, d))
|
|
|
|
|
|
self.post_n_fft = gen_istft_n_fft
|
|
self.conv_post = Conv1d(ch, self.post_n_fft + 2, 7, 1, padding=3)
|
|
self.ups.apply(init_weights)
|
|
self.reflection_pad = torch.nn.ReflectionPad1d((1, 0))
|
|
|
|
self.out_proj_x1 = Conv1d(upsample_initial_channel // 4, 1, 7, 1, padding=3)
|
|
|
|
if gin_channels != 0:
|
|
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
|
|
|
def forward(self, x, g=None):
|
|
x = self.conv_pre(x)
|
|
if g is not None:
|
|
x = x + self.cond(g)
|
|
|
|
for i in range(self.num_upsamples):
|
|
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
|
x = self.ups[i](x)
|
|
xs = None
|
|
for j in range(self.num_kernels):
|
|
if xs is None:
|
|
xs = self.resblocks[i*self.num_kernels+j](x)
|
|
else:
|
|
xs += self.resblocks[i*self.num_kernels+j](x)
|
|
x = xs / self.num_kernels
|
|
if i == 1:
|
|
x1 = self.out_proj_x1(x)
|
|
# elif i == 2:
|
|
# x2 = self.out_proj_x2(x)
|
|
x = F.leaky_relu(x)
|
|
x = self.reflection_pad(x)
|
|
x = self.conv_post(x)
|
|
spec = torch.exp(x[:,:self.post_n_fft // 2 + 1, :])
|
|
phase = torch.sin(x[:, self.post_n_fft // 2 + 1:, :])
|
|
|
|
return spec, phase, x1 #, x2
|
|
|
|
def remove_weight_norm(self):
|
|
print('Removing weight norm...')
|
|
for l in self.ups:
|
|
remove_weight_norm(l)
|
|
for l in self.resblocks:
|
|
l.remove_weight_norm()
|
|
|
|
|
|
class DiscriminatorP(torch.nn.Module):
|
|
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
|
super(DiscriminatorP, self).__init__()
|
|
self.period = period
|
|
self.use_spectral_norm = use_spectral_norm
|
|
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
self.convs = nn.ModuleList([
|
|
norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
|
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
|
|
])
|
|
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
|
|
|
def forward(self, x):
|
|
fmap = []
|
|
|
|
# 1d to 2d
|
|
b, c, t = x.shape
|
|
if t % self.period != 0: # pad first
|
|
n_pad = self.period - (t % self.period)
|
|
x = F.pad(x, (0, n_pad), "reflect")
|
|
t = t + n_pad
|
|
x = x.view(b, c, t // self.period, self.period)
|
|
|
|
for l in self.convs:
|
|
x = l(x)
|
|
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
|
fmap.append(x)
|
|
x = self.conv_post(x)
|
|
fmap.append(x)
|
|
x = torch.flatten(x, 1, -1)
|
|
|
|
return x, fmap
|
|
|
|
|
|
class DiscriminatorS(torch.nn.Module):
|
|
def __init__(self, use_spectral_norm=False):
|
|
super(DiscriminatorS, self).__init__()
|
|
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
|
self.convs = nn.ModuleList([
|
|
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
|
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
|
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
|
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
|
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
|
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
|
])
|
|
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
|
|
|
def forward(self, x):
|
|
fmap = []
|
|
|
|
for l in self.convs:
|
|
x = l(x)
|
|
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
|
fmap.append(x)
|
|
x = self.conv_post(x)
|
|
fmap.append(x)
|
|
x = torch.flatten(x, 1, -1)
|
|
|
|
return x, fmap
|
|
|
|
|
|
class MultiPeriodDiscriminator(torch.nn.Module):
|
|
def __init__(self, use_spectral_norm=False):
|
|
super(MultiPeriodDiscriminator, self).__init__()
|
|
periods = [2,3,5,7,11]
|
|
|
|
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
|
discs = discs + [DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods]
|
|
self.discriminators = nn.ModuleList(discs)
|
|
|
|
def forward(self, y, y_hat):
|
|
y_d_rs = []
|
|
y_d_gs = []
|
|
fmap_rs = []
|
|
fmap_gs = []
|
|
for i, d in enumerate(self.discriminators):
|
|
y_d_r, fmap_r = d(y)
|
|
y_d_g, fmap_g = d(y_hat)
|
|
y_d_rs.append(y_d_r)
|
|
y_d_gs.append(y_d_g)
|
|
fmap_rs.append(fmap_r)
|
|
fmap_gs.append(fmap_g)
|
|
|
|
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
|
|
|
|
|
class MultiScaleDiscriminator(torch.nn.Module):
|
|
def __init__(self):
|
|
super(MultiScaleDiscriminator, self).__init__()
|
|
self.discriminators = nn.ModuleList([
|
|
DiscriminatorS(use_spectral_norm=True),
|
|
DiscriminatorS(),
|
|
DiscriminatorS(),
|
|
])
|
|
self.meanpools = nn.ModuleList([
|
|
AvgPool1d(4, 2, padding=2),
|
|
AvgPool1d(4, 2, padding=2)
|
|
])
|
|
|
|
def forward(self, y, y_hat):
|
|
y_d_rs = []
|
|
y_d_gs = []
|
|
fmap_rs = []
|
|
fmap_gs = []
|
|
for i, d in enumerate(self.discriminators):
|
|
if i != 0:
|
|
y = self.meanpools[i-1](y)
|
|
y_hat = self.meanpools[i-1](y_hat)
|
|
y_d_r, fmap_r = d(y)
|
|
y_d_g, fmap_g = d(y_hat)
|
|
y_d_rs.append(y_d_r)
|
|
fmap_rs.append(fmap_r)
|
|
y_d_gs.append(y_d_g)
|
|
fmap_gs.append(fmap_g)
|
|
|
|
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
|
|
|
|
|
class MultiCoMBDiscriminator(torch.nn.Module):
|
|
|
|
def __init__(self, kernels, channels, groups, strides):
|
|
super(MultiCoMBDiscriminator, self).__init__()
|
|
self.combd_1 = CoMBD(filters=channels, kernels=kernels[0], groups=groups, strides=strides)
|
|
self.combd_2 = CoMBD(filters=channels, kernels=kernels[1], groups=groups, strides=strides)
|
|
self.combd_3 = CoMBD(filters=channels, kernels=kernels[2], groups=groups, strides=strides)
|
|
|
|
self.pqmf_2 = PQMF(N=2, taps=256, cutoff=0.25, beta=10.0)
|
|
self.pqmf_4 = PQMF(N=4, taps=192, cutoff=0.13, beta=10.0)
|
|
|
|
def forward(self, x, x_hat, x1_hat):
|
|
y = []
|
|
y_hat = []
|
|
fmap = []
|
|
fmap_hat = []
|
|
|
|
p3, p3_fmap = self.combd_3(x)
|
|
y.append(p3)
|
|
fmap.append(p3_fmap)
|
|
|
|
p3_hat, p3_fmap_hat = self.combd_3(x_hat)
|
|
y_hat.append(p3_hat)
|
|
fmap_hat.append(p3_fmap_hat)
|
|
|
|
#x2_ = self.pqmf_2(x)[:, :1, :] # Select first band
|
|
x1_ = self.pqmf_4(x)[:, :1, :] # Select first band
|
|
|
|
#x2_hat_ = self.pqmf_2(x_hat)[:, :1, :]
|
|
x1_hat_ = self.pqmf_4(x_hat)[:, :1, :]
|
|
|
|
# p2_, p2_fmap_ = self.combd_2(x2_)
|
|
# y.append(p2_)
|
|
# fmap.append(p2_fmap_)
|
|
|
|
# p2_hat_, p2_fmap_hat_ = self.combd_2(x2_hat)
|
|
# y_hat.append(p2_hat_)
|
|
# fmap_hat.append(p2_fmap_hat_)
|
|
|
|
p1_, p1_fmap_ = self.combd_1(x1_)
|
|
y.append(p1_)
|
|
fmap.append(p1_fmap_)
|
|
|
|
p1_hat_, p1_fmap_hat_ = self.combd_1(x1_hat)
|
|
y_hat.append(p1_hat_)
|
|
fmap_hat.append(p1_fmap_hat_)
|
|
|
|
|
|
# p2, p2_fmap = self.combd_2(x2_)
|
|
# y.append(p2)
|
|
# fmap.append(p2_fmap)
|
|
#
|
|
# p2_hat, p2_fmap_hat = self.combd_2(x2_hat_)
|
|
# y_hat.append(p2_hat)
|
|
# fmap_hat.append(p2_fmap_hat)
|
|
|
|
p1, p1_fmap = self.combd_1(x1_)
|
|
y.append(p1)
|
|
fmap.append(p1_fmap)
|
|
|
|
p1_hat, p1_fmap_hat = self.combd_1(x1_hat_)
|
|
y_hat.append(p1_hat)
|
|
fmap_hat.append(p1_fmap_hat)
|
|
|
|
return y, y_hat, fmap, fmap_hat
|
|
|
|
class MultiSubBandDiscriminator(torch.nn.Module):
|
|
|
|
def __init__(self, tkernels, fkernel, tchannels, fchannels, tstrides, fstride, tdilations, fdilations, tsubband,
|
|
n, m, freq_init_ch):
|
|
|
|
super(MultiSubBandDiscriminator, self).__init__()
|
|
|
|
self.fsbd = SubBandDiscriminator(init_channel=freq_init_ch, channels=fchannels, kernel=fkernel,
|
|
strides=fstride, dilations=fdilations)
|
|
|
|
self.tsubband1 = tsubband[0]
|
|
self.tsbd1 = SubBandDiscriminator(init_channel=self.tsubband1, channels=tchannels, kernel=tkernels[0],
|
|
strides=tstrides[0], dilations=tdilations[0])
|
|
|
|
self.tsubband2 = tsubband[1]
|
|
self.tsbd2 = SubBandDiscriminator(init_channel=self.tsubband2, channels=tchannels, kernel=tkernels[1],
|
|
strides=tstrides[1], dilations=tdilations[1])
|
|
|
|
self.tsubband3 = tsubband[2]
|
|
self.tsbd3 = SubBandDiscriminator(init_channel=self.tsubband3, channels=tchannels, kernel=tkernels[2],
|
|
strides=tstrides[2], dilations=tdilations[2])
|
|
|
|
|
|
self.pqmf_n = PQMF(N=n, taps=256, cutoff=0.03, beta=10.0)
|
|
self.pqmf_m = PQMF(N=m, taps=256, cutoff=0.1, beta=9.0)
|
|
|
|
def forward(self, x, x_hat):
|
|
fmap = []
|
|
fmap_hat = []
|
|
y = []
|
|
y_hat = []
|
|
|
|
# Time analysis
|
|
xn = self.pqmf_n(x)
|
|
xn_hat = self.pqmf_n(x_hat)
|
|
|
|
q3, feat_q3 = self.tsbd3(xn[:, :self.tsubband3, :])
|
|
q3_hat, feat_q3_hat = self.tsbd3(xn_hat[:, :self.tsubband3, :])
|
|
y.append(q3)
|
|
y_hat.append(q3_hat)
|
|
fmap.append(feat_q3)
|
|
fmap_hat.append(feat_q3_hat)
|
|
|
|
q2, feat_q2 = self.tsbd2(xn[:, :self.tsubband2, :])
|
|
q2_hat, feat_q2_hat = self.tsbd2(xn_hat[:, :self.tsubband2, :])
|
|
y.append(q2)
|
|
y_hat.append(q2_hat)
|
|
fmap.append(feat_q2)
|
|
fmap_hat.append(feat_q2_hat)
|
|
|
|
q1, feat_q1 = self.tsbd1(xn[:, :self.tsubband1, :])
|
|
q1_hat, feat_q1_hat = self.tsbd1(xn_hat[:, :self.tsubband1, :])
|
|
y.append(q1)
|
|
y_hat.append(q1_hat)
|
|
fmap.append(feat_q1)
|
|
fmap_hat.append(feat_q1_hat)
|
|
|
|
# Frequency analysis
|
|
xm = self.pqmf_m(x)
|
|
xm_hat = self.pqmf_m(x_hat)
|
|
|
|
xm = xm.transpose(-2, -1)
|
|
xm_hat = xm_hat.transpose(-2, -1)
|
|
|
|
q4, feat_q4 = self.fsbd(xm)
|
|
q4_hat, feat_q4_hat = self.fsbd(xm_hat)
|
|
y.append(q4)
|
|
y_hat.append(q4_hat)
|
|
fmap.append(feat_q4)
|
|
fmap_hat.append(feat_q4_hat)
|
|
|
|
return y, y_hat, fmap, fmap_hat
|
|
|
|
class SynthesizerTrn(nn.Module):
|
|
"""
|
|
Synthesizer for Training
|
|
"""
|
|
|
|
def __init__(self,
|
|
n_vocab,
|
|
spec_channels,
|
|
segment_size,
|
|
inter_channels,
|
|
hidden_channels,
|
|
filter_channels,
|
|
n_heads,
|
|
n_layers,
|
|
kernel_size,
|
|
p_dropout,
|
|
resblock,
|
|
resblock_kernel_sizes,
|
|
resblock_dilation_sizes,
|
|
upsample_rates,
|
|
upsample_initial_channel,
|
|
upsample_kernel_sizes,
|
|
moji_start_size,
|
|
moji_enc_sizes,
|
|
bert_size,
|
|
bert_final,
|
|
n_speakers=0,
|
|
gin_channels=0,
|
|
use_sdp=True,
|
|
gen_istft_n_fft=16,
|
|
gen_istft_hop_size=4,
|
|
**kwargs):
|
|
|
|
super().__init__()
|
|
self.n_vocab = n_vocab
|
|
self.spec_channels = spec_channels
|
|
self.inter_channels = inter_channels
|
|
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.resblock = resblock
|
|
self.resblock_kernel_sizes = resblock_kernel_sizes
|
|
self.resblock_dilation_sizes = resblock_dilation_sizes
|
|
self.upsample_rates = upsample_rates
|
|
self.upsample_initial_channel = upsample_initial_channel
|
|
self.upsample_kernel_sizes = upsample_kernel_sizes
|
|
self.segment_size = segment_size
|
|
self.n_speakers = n_speakers
|
|
self.gin_channels = gin_channels
|
|
self.gen_istft_n_fft = gen_istft_n_fft
|
|
self.gen_istft_hop_size = gen_istft_hop_size
|
|
self.bert_size = bert_size
|
|
self.bert_final = bert_final
|
|
|
|
self.use_sdp = use_sdp
|
|
self.moji_last = moji_enc_sizes[-1]
|
|
self.final_exp = self.moji_last + self.bert_final
|
|
|
|
self.enc_p = TextEncoder(n_vocab,
|
|
inter_channels,
|
|
hidden_channels,
|
|
filter_channels,
|
|
n_heads,
|
|
n_layers,
|
|
kernel_size,
|
|
p_dropout,
|
|
self.moji_last,)
|
|
self.dec = Generator(inter_channels, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gen_istft_n_fft, gin_channels=gin_channels)
|
|
self.enc_q = PosteriorEncoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels)
|
|
self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
|
|
self.aligner = modules.AlignmentEncoder(n_mel_channels=80, n_text_channels=hidden_channels, n_att_channels=80, temperature=0.0005)
|
|
self.satt_module = modules.SelfAttentionModule(n_text_channels=hidden_channels + self.moji_last, n_lm_tokens_channels=self.bert_size)
|
|
self.stft = TorchSTFT(self.gen_istft_n_fft, hop_length= self.gen_istft_hop_size,
|
|
win_length=self.gen_istft_n_fft)
|
|
|
|
self.symbol_emb = self.enc_p.emb
|
|
self.tm_enc = TMEncoder(moji_start_size,moji_enc_sizes)
|
|
self.satt_enc = nn.Sequential(
|
|
nn.Linear(hidden_channels + self.moji_last,self.bert_final),
|
|
nn.ReLU())
|
|
|
|
|
|
if use_sdp:
|
|
self.dp = StochasticDurationPredictor(hidden_channels + self.final_exp, 192, 3, 0.5, 4, gin_channels=gin_channels)
|
|
else:
|
|
self.dp = DurationPredictor(hidden_channels + self.final_exp, 256, 3, 0.5, gin_channels=gin_channels)
|
|
|
|
if n_speakers > 1:
|
|
self.emb_g = nn.Embedding(n_speakers, gin_channels)
|
|
|
|
|
|
|
|
|
|
@torch.jit.unused
|
|
def run_aligner(self, text, text_len, text_mask, spect, spect_len, attn_prior,cond):
|
|
text_emb = self.symbol_emb(text)
|
|
text_emb = text_emb.permute(0, 2, 1)
|
|
text_mask = text_mask.permute(0, 2, 1) # [b, 1, mxlen] => [b, mxlen, 1]
|
|
attn_soft, attn_logprob = self.aligner(
|
|
spect, text_emb, mask=text_mask == 0, attn_prior=attn_prior,conditioning=cond
|
|
)
|
|
attn_hard = modules.binarize_attention_parallel(attn_soft, text_len, spect_len)
|
|
attn_hard_dur = attn_hard.sum(2)
|
|
# assert torch.all(torch.eq(attn_hard_dur.sum(dim=1), spect_len))
|
|
# print(
|
|
return attn_soft, attn_logprob, attn_hard, attn_hard_dur
|
|
|
|
def forward(self, x, x_lengths, y, y_lengths, mel, tm_hidden, bert, bert_lens, sid=None):
|
|
x_orig = x
|
|
|
|
tm_encoded = self.tm_enc(tm_hidden) # [b, tm_last_size]
|
|
|
|
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, tm_encoded)
|
|
if self.n_speakers > 0:
|
|
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
|
else:
|
|
g = None
|
|
|
|
# bert = [b, tokens, channels], bert_lens = [b]
|
|
bert_mask = commons.sequence_mask(bert_lens, bert.size(1))
|
|
x_inp = x.permute(0, 2, 1) # [b, text_channels, t] -> [b, t, text_channels]
|
|
x_mask_inp = x_mask.bool().permute(0, 2, 1).squeeze() # [b, 1, t] (float) -> [b, t] (bool)
|
|
|
|
lm_features = self.satt_module(
|
|
x_inp, bert, bert, q_mask=x_mask_inp, kv_mask=bert_mask == 0
|
|
)
|
|
lm_features = torch.nan_to_num(lm_features) # prevent nan poisoning
|
|
lm_encoded = torch.nan_to_num(self.satt_enc(lm_features)) # [b, tokens, bert_final]
|
|
lm_encoded = lm_encoded.permute(0, 2, 1) # see x_inp but reverse / [b, text_channels, tokens]
|
|
x = torch.cat((x, lm_encoded), dim=1) # append encoded BERT as channels
|
|
|
|
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
|
|
z_p = self.flow(z, y_mask, g=g)
|
|
attn_soft, attn_logprob, attn, attn_hard_dur = self.run_aligner(x_orig, x_lengths, x_mask, mel, y_lengths,None,g)
|
|
|
|
w = attn_hard_dur
|
|
if self.use_sdp:
|
|
l_length = self.dp(x, x_mask, w, g=g)
|
|
l_length = l_length / torch.sum(x_mask)
|
|
else:
|
|
logw_ = torch.log(w + 1e-6) * x_mask
|
|
logw = self.dp(x, x_mask, g=g)
|
|
l_length = torch.sum((logw - logw_)**2, [1,2]) / torch.sum(x_mask) # for averaging
|
|
|
|
# expand prior
|
|
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2)
|
|
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2)
|
|
|
|
z_slice, ids_slice = commons.rand_slice_segments(z, y_lengths, self.segment_size)
|
|
|
|
spec, phase, x1 = self.dec(z_slice, g=g)
|
|
|
|
o = self.stft.inverse(spec, phase)
|
|
return o, x1, l_length, attn, attn_logprob, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
|
|
|
|
def infer(self, x, x_lengths, tm_hidden, bert, bert_lens, sid=None, noise_scale=1, length_scale=1, noise_scale_w=1., max_len=None):
|
|
tm_encoded = self.tm_enc(tm_hidden) # [b, tm_last_size]
|
|
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, tm_encoded)
|
|
if self.n_speakers > 0:
|
|
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
|
else:
|
|
g = None
|
|
|
|
# bert = [b, tokens, channels], bert_lens = [b]
|
|
bert_mask = commons.sequence_mask(bert_lens, bert.size(1))
|
|
|
|
x_inp = x.permute(0, 2, 1) # [b, text_channels, t] -> [b, t, text_channels]
|
|
x_mask_inp = x_mask.bool().permute(0, 2, 1).squeeze() # [b, 1, t] (float) -> [b, t] (bool)
|
|
if x_mask_inp.dim() < 2: # for single inference we might accidentally remove [1, t] batch dim
|
|
x_mask_inp = x_mask_inp.unsqueeze(0) # [t] -> [1, t]
|
|
|
|
lm_features = self.satt_module(
|
|
x_inp, bert, bert, q_mask=x_mask_inp, kv_mask=bert_mask == 0
|
|
)
|
|
lm_features = torch.nan_to_num(lm_features) # prevent nan poisoning
|
|
lm_encoded = torch.nan_to_num(self.satt_enc(lm_features)) # [b, tokens, bert_final]
|
|
lm_encoded = lm_encoded.permute(0, 2, 1) # see x_inp but reverse / [b, text_channels, tokens]
|
|
|
|
x = torch.cat((x, lm_encoded), dim=1) # append encoded BERT as channels
|
|
|
|
|
|
|
|
if self.use_sdp:
|
|
logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w)
|
|
else:
|
|
logw = self.dp(x, x_mask, g=g)
|
|
w = torch.exp(logw) * x_mask * length_scale
|
|
w_ceil = torch.ceil(w)
|
|
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
|
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, None), 1).to(x_mask.dtype)
|
|
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
|
attn = commons.generate_path(w_ceil, attn_mask)
|
|
|
|
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
|
|
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
|
z = self.flow(z_p, y_mask, g=g, reverse=True)
|
|
|
|
spec, phase, x1 = self.dec((z * y_mask)[:,:,:max_len], g=g)
|
|
o = self.stft.inverse(spec, phase)
|
|
|
|
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|
|
|
|
# HURR DURR COPYING FUNCTIONS LE BAD
|
|
# TorchScript tracing mysteries
|
|
def infer_ts(self, x, x_lengths, tm_hidden, bert, bert_lens, length_scale=1,sid=None,noise_scale=.667,noise_scale_w=0.8, max_len=None):
|
|
tm_encoded = self.tm_enc(tm_hidden) # [b, tm_last_size]
|
|
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, tm_encoded)
|
|
if self.n_speakers > 0:
|
|
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
|
else:
|
|
g = None
|
|
|
|
# bert = [b, tokens, channels], bert_lens = [b]
|
|
bert_mask = commons.sequence_mask(bert_lens, bert.size(1))
|
|
|
|
x_inp = x.permute(0, 2, 1) # [b, text_channels, t] -> [b, t, text_channels]
|
|
x_mask_inp = x_mask.bool().permute(0, 2, 1).squeeze() # [b, 1, t] (float) -> [b, t] (bool)
|
|
if x_mask_inp.dim() < 2: # for single inference we might accidentally remove [1, t] batch dim
|
|
x_mask_inp = x_mask_inp.unsqueeze(0) # [t] -> [1, t]
|
|
|
|
lm_features = self.satt_module(
|
|
x_inp, bert, bert, q_mask=x_mask_inp, kv_mask=bert_mask == 0
|
|
)
|
|
lm_features = torch.nan_to_num(lm_features) # prevent nan poisoning
|
|
lm_encoded = torch.nan_to_num(self.satt_enc(lm_features)) # [b, tokens, bert_final]
|
|
lm_encoded = lm_encoded.permute(0, 2, 1) # see x_inp but reverse / [b, text_channels, tokens]
|
|
|
|
x = torch.cat((x, lm_encoded), dim=1) # append encoded BERT as channels
|
|
|
|
|
|
|
|
if self.use_sdp:
|
|
logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w)
|
|
else:
|
|
logw = self.dp(x, x_mask, g=g)
|
|
w = torch.exp(logw) * x_mask * length_scale
|
|
w_ceil = torch.ceil(w)
|
|
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
|
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, None), 1).to(x_mask.dtype)
|
|
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
|
attn = commons.generate_path(w_ceil, attn_mask)
|
|
|
|
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
|
|
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
|
z = self.flow(z_p, y_mask, g=g, reverse=True)
|
|
|
|
spec, phase, x1 = self.dec((z * y_mask)[:,:,:max_len], g=g)
|
|
o = self.stft.inverse(spec, phase)
|
|
|
|
return o, attn
|
|
|
|
|
|
# HURR DURR COPYING FUNCTIONS LE BAD
|
|
# TorchScript tracing mysteries
|
|
def infer_ts_noistft(self, x, x_lengths, tm_hidden, bert, bert_lens, length_scale=1,sid=None,noise_scale=.667,noise_scale_w=0.8, max_len=None):
|
|
tm_encoded = self.tm_enc(tm_hidden) # [b, tm_last_size]
|
|
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, tm_encoded)
|
|
if self.n_speakers > 0:
|
|
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
|
else:
|
|
g = None
|
|
|
|
# bert = [b, tokens, channels], bert_lens = [b]
|
|
bert_mask = commons.sequence_mask(bert_lens, bert.size(1))
|
|
|
|
x_inp = x.permute(0, 2, 1) # [b, text_channels, t] -> [b, t, text_channels]
|
|
x_mask_inp = x_mask.bool().permute(0, 2, 1).squeeze() # [b, 1, t] (float) -> [b, t] (bool)
|
|
if x_mask_inp.dim() < 2: # for single inference we might accidentally remove [1, t] batch dim
|
|
x_mask_inp = x_mask_inp.unsqueeze(0) # [t] -> [1, t]
|
|
|
|
lm_features = self.satt_module(
|
|
x_inp, bert, bert, q_mask=x_mask_inp, kv_mask=bert_mask == 0
|
|
)
|
|
lm_features = torch.nan_to_num(lm_features) # prevent nan poisoning
|
|
lm_encoded = torch.nan_to_num(self.satt_enc(lm_features)) # [b, tokens, bert_final]
|
|
lm_encoded = lm_encoded.permute(0, 2, 1) # see x_inp but reverse / [b, text_channels, tokens]
|
|
|
|
x = torch.cat((x, lm_encoded), dim=1) # append encoded BERT as channels
|
|
|
|
if self.use_sdp:
|
|
logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w)
|
|
else:
|
|
logw = self.dp(x, x_mask, g=g)
|
|
w = torch.exp(logw) * x_mask * length_scale
|
|
w_ceil = torch.ceil(w)
|
|
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
|
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, None), 1).to(x_mask.dtype)
|
|
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
|
attn = commons.generate_path(w_ceil, attn_mask)
|
|
|
|
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
|
|
|
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
|
z = self.flow(z_p, y_mask, g=g, reverse=True)
|
|
|
|
spec, phase, x1 = self.dec((z * y_mask)[:,:,:max_len], g=g)
|
|
|
|
|
|
return spec, phase, x1, attn
|
|
|
|
def voice_conversion(self, y, y_lengths, sid_src, sid_tgt):
|
|
assert self.n_speakers > 0, "n_speakers have to be larger than 0."
|
|
g_src = self.emb_g(sid_src).unsqueeze(-1)
|
|
g_tgt = self.emb_g(sid_tgt).unsqueeze(-1)
|
|
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g_src)
|
|
z_p = self.flow(z, y_mask, g=g_src)
|
|
z_hat = self.flow(z_p, y_mask, g=g_tgt, reverse=True)
|
|
o_hat = self.dec(z_hat * y_mask, g=g_tgt)
|
|
return o_hat, y_mask, (z, z_p, z_hat)
|
|
|