DEBUG interpolation of voice style
Browse files- Modules/hifigan.py +10 -9
- Modules/utils.py +0 -14
- models.py +30 -138
- msinference.py +31 -71
Modules/hifigan.py
CHANGED
|
@@ -3,11 +3,12 @@ import torch.nn.functional as F
|
|
| 3 |
import torch.nn as nn
|
| 4 |
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
| 5 |
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
| 6 |
-
from .utils import init_weights, get_padding
|
| 7 |
-
|
| 8 |
import math
|
| 9 |
import random
|
| 10 |
-
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
LRELU_SLOPE = 0.1
|
| 13 |
|
|
@@ -42,7 +43,7 @@ class AdaINResBlock1(torch.nn.Module):
|
|
| 42 |
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
|
| 43 |
padding=get_padding(kernel_size, dilation[2])))
|
| 44 |
])
|
| 45 |
-
self.convs1.apply(init_weights)
|
| 46 |
|
| 47 |
self.convs2 = nn.ModuleList([
|
| 48 |
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
|
@@ -52,7 +53,7 @@ class AdaINResBlock1(torch.nn.Module):
|
|
| 52 |
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
| 53 |
padding=get_padding(kernel_size, 1)))
|
| 54 |
])
|
| 55 |
-
self.convs2.apply(init_weights)
|
| 56 |
|
| 57 |
self.adain1 = nn.ModuleList([
|
| 58 |
AdaIN1d(style_dim, channels),
|
|
@@ -274,8 +275,6 @@ class SourceModuleHnNSF(torch.nn.Module):
|
|
| 274 |
# source for noise branch, in the same shape as uv
|
| 275 |
noise = torch.randn_like(uv) * self.sine_amp / 3
|
| 276 |
return sine_merge, noise, uv
|
| 277 |
-
def padDiff(x):
|
| 278 |
-
return F.pad(F.pad(x, (0,0,-1,1), 'constant', 0) - x, (0,0,0,-1), 'constant', 0)
|
| 279 |
|
| 280 |
class Generator(torch.nn.Module):
|
| 281 |
def __init__(self, style_dim, resblock_kernel_sizes, upsample_rates, upsample_initial_channel, resblock_dilation_sizes, upsample_kernel_sizes):
|
|
@@ -323,8 +322,7 @@ class Generator(torch.nn.Module):
|
|
| 323 |
self.resblocks.append(resblock(ch, k, d, style_dim))
|
| 324 |
|
| 325 |
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
|
| 326 |
-
|
| 327 |
-
self.conv_post.apply(init_weights)
|
| 328 |
|
| 329 |
def forward(self, x, s, f0):
|
| 330 |
|
|
@@ -365,6 +363,9 @@ class Generator(torch.nn.Module):
|
|
| 365 |
|
| 366 |
|
| 367 |
class AdainResBlk1d(nn.Module):
|
|
|
|
|
|
|
|
|
|
| 368 |
def __init__(self, dim_in, dim_out, style_dim=64, actv=nn.LeakyReLU(0.2),
|
| 369 |
upsample='none', dropout_p=0.0):
|
| 370 |
super().__init__()
|
|
|
|
| 3 |
import torch.nn as nn
|
| 4 |
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
| 5 |
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
|
|
|
|
|
|
| 6 |
import math
|
| 7 |
import random
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
def get_padding(kernel_size, dilation=1):
|
| 11 |
+
return int((kernel_size*dilation - dilation)/2)
|
| 12 |
|
| 13 |
LRELU_SLOPE = 0.1
|
| 14 |
|
|
|
|
| 43 |
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
|
| 44 |
padding=get_padding(kernel_size, dilation[2])))
|
| 45 |
])
|
| 46 |
+
# self.convs1.apply(init_weights)
|
| 47 |
|
| 48 |
self.convs2 = nn.ModuleList([
|
| 49 |
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
|
|
|
| 53 |
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
| 54 |
padding=get_padding(kernel_size, 1)))
|
| 55 |
])
|
| 56 |
+
# self.convs2.apply(init_weights)
|
| 57 |
|
| 58 |
self.adain1 = nn.ModuleList([
|
| 59 |
AdaIN1d(style_dim, channels),
|
|
|
|
| 275 |
# source for noise branch, in the same shape as uv
|
| 276 |
noise = torch.randn_like(uv) * self.sine_amp / 3
|
| 277 |
return sine_merge, noise, uv
|
|
|
|
|
|
|
| 278 |
|
| 279 |
class Generator(torch.nn.Module):
|
| 280 |
def __init__(self, style_dim, resblock_kernel_sizes, upsample_rates, upsample_initial_channel, resblock_dilation_sizes, upsample_kernel_sizes):
|
|
|
|
| 322 |
self.resblocks.append(resblock(ch, k, d, style_dim))
|
| 323 |
|
| 324 |
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
|
| 325 |
+
|
|
|
|
| 326 |
|
| 327 |
def forward(self, x, s, f0):
|
| 328 |
|
|
|
|
| 363 |
|
| 364 |
|
| 365 |
class AdainResBlk1d(nn.Module):
|
| 366 |
+
|
| 367 |
+
# also used in ProsodyPredictor()
|
| 368 |
+
|
| 369 |
def __init__(self, dim_in, dim_out, style_dim=64, actv=nn.LeakyReLU(0.2),
|
| 370 |
upsample='none', dropout_p=0.0):
|
| 371 |
super().__init__()
|
Modules/utils.py
DELETED
|
@@ -1,14 +0,0 @@
|
|
| 1 |
-
def init_weights(m, mean=0.0, std=0.01):
|
| 2 |
-
classname = m.__class__.__name__
|
| 3 |
-
if classname.find("Conv") != -1:
|
| 4 |
-
m.weight.data.normal_(mean, std)
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def apply_weight_norm(m):
|
| 8 |
-
classname = m.__class__.__name__
|
| 9 |
-
if classname.find("Conv") != -1:
|
| 10 |
-
weight_norm(m)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def get_padding(kernel_size, dilation=1):
|
| 14 |
-
return int((kernel_size*dilation - dilation)/2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.py
CHANGED
|
@@ -8,7 +8,7 @@ import torch.nn.functional as F
|
|
| 8 |
from torch.nn.utils import weight_norm, spectral_norm
|
| 9 |
from Utils.ASR.models import ASRCNN
|
| 10 |
from Utils.JDC.model import JDCNet
|
| 11 |
-
from Modules.hifigan import
|
| 12 |
import yaml
|
| 13 |
|
| 14 |
|
|
@@ -18,9 +18,11 @@ class LearnedDownSample(nn.Module):
|
|
| 18 |
self.layer_type = layer_type
|
| 19 |
|
| 20 |
if self.layer_type == 'none':
|
| 21 |
-
|
|
|
|
| 22 |
elif self.layer_type == 'timepreserve':
|
| 23 |
-
|
|
|
|
| 24 |
elif self.layer_type == 'half':
|
| 25 |
self.conv = spectral_norm(nn.Conv2d(dim_in, dim_in, kernel_size=(3, 3), stride=(2, 2), groups=dim_in, padding=1))
|
| 26 |
else:
|
|
@@ -48,20 +50,7 @@ class DownSample(nn.Module):
|
|
| 48 |
raise RuntimeError('Got unexpected donwsampletype %s, expected is [none, timepreserve, half]' % self.layer_type)
|
| 49 |
|
| 50 |
|
| 51 |
-
class UpSample(nn.Module):
|
| 52 |
-
def __init__(self, layer_type):
|
| 53 |
-
super().__init__()
|
| 54 |
-
self.layer_type = layer_type
|
| 55 |
|
| 56 |
-
def forward(self, x):
|
| 57 |
-
if self.layer_type == 'none':
|
| 58 |
-
return x
|
| 59 |
-
elif self.layer_type == 'timepreserve':
|
| 60 |
-
return F.interpolate(x, scale_factor=(2, 1), mode='nearest')
|
| 61 |
-
elif self.layer_type == 'half':
|
| 62 |
-
return F.interpolate(x, scale_factor=2, mode='nearest')
|
| 63 |
-
else:
|
| 64 |
-
raise RuntimeError('Got unexpected upsampletype %s, expected is [none, timepreserve, half]' % self.layer_type)
|
| 65 |
|
| 66 |
|
| 67 |
class ResBlk(nn.Module):
|
|
@@ -137,9 +126,11 @@ class StyleEncoder(nn.Module):
|
|
| 137 |
h = self.shared(x) # [bs, 512, 1, 11]
|
| 138 |
|
| 139 |
h = h.mean(3, keepdims=True) # UN COMMENT FOR TIME INVARIANT GLOBAL SPEAKER STYLE
|
| 140 |
-
|
| 141 |
h = h.transpose(1, 3)
|
| 142 |
s = self.unshared(h)
|
|
|
|
|
|
|
| 143 |
return s
|
| 144 |
|
| 145 |
|
|
@@ -249,114 +240,37 @@ class TextEncoder(nn.Module):
|
|
| 249 |
|
| 250 |
self.lstm = nn.LSTM(channels, channels//2, 1, batch_first=True, bidirectional=True)
|
| 251 |
|
| 252 |
-
def forward(self, x, input_lengths
|
| 253 |
x = self.embedding(x) # [B, T, emb]
|
| 254 |
x = x.transpose(1, 2) # [B, emb, T]
|
| 255 |
-
m = m.to(input_lengths.device).unsqueeze(1)
|
| 256 |
-
x.masked_fill_(m, 0.0)
|
| 257 |
-
|
| 258 |
for c in self.cnn:
|
| 259 |
-
x = c(x)
|
| 260 |
-
x.masked_fill_(m, 0.0)
|
| 261 |
-
|
| 262 |
x = x.transpose(1, 2) # [B, T, chn]
|
| 263 |
-
|
| 264 |
input_lengths = input_lengths.cpu().numpy()
|
| 265 |
x = nn.utils.rnn.pack_padded_sequence(
|
| 266 |
-
x, input_lengths,
|
| 267 |
-
|
|
|
|
| 268 |
self.lstm.flatten_parameters()
|
| 269 |
x, _ = self.lstm(x)
|
| 270 |
x, _ = nn.utils.rnn.pad_packed_sequence(
|
| 271 |
x, batch_first=True)
|
| 272 |
-
|
| 273 |
x = x.transpose(-1, -2)
|
| 274 |
-
x_pad = torch.zeros([x.shape[0], x.shape[1], m.shape[-1]])
|
| 275 |
-
|
| 276 |
-
x_pad[:, :, :x.shape[-1]] = x
|
| 277 |
-
x = x_pad.to(x.device)
|
| 278 |
-
|
| 279 |
-
x.masked_fill_(m, 0.0)
|
| 280 |
-
|
| 281 |
return x
|
| 282 |
|
| 283 |
-
def inference(self, x):
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
|
| 292 |
-
def length_to_mask(self, lengths):
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
class UpSample1d(nn.Module):
|
| 298 |
-
def __init__(self, layer_type):
|
| 299 |
-
super().__init__()
|
| 300 |
-
self.layer_type = layer_type
|
| 301 |
-
|
| 302 |
-
def forward(self, x):
|
| 303 |
-
if self.layer_type == 'none':
|
| 304 |
-
return x
|
| 305 |
-
else:
|
| 306 |
-
return F.interpolate(x, scale_factor=2, mode='nearest')
|
| 307 |
-
|
| 308 |
-
class AdainResBlk1d(nn.Module):
|
| 309 |
-
|
| 310 |
-
# only instantiated in ProsodyPredictor
|
| 311 |
-
|
| 312 |
-
def __init__(self, dim_in,
|
| 313 |
-
dim_out,
|
| 314 |
-
style_dim=64,
|
| 315 |
-
actv=nn.LeakyReLU(0.2),
|
| 316 |
-
upsample='none',
|
| 317 |
-
dropout_p=0.0):
|
| 318 |
-
super().__init__()
|
| 319 |
-
self.actv = actv
|
| 320 |
-
self.upsample_type = upsample
|
| 321 |
-
self.upsample = UpSample1d(upsample)
|
| 322 |
-
self.learned_sc = dim_in != dim_out
|
| 323 |
-
self._build_weights(dim_in, dim_out, style_dim)
|
| 324 |
-
self.dropout = nn.Dropout(dropout_p)
|
| 325 |
-
|
| 326 |
-
if upsample == 'none':
|
| 327 |
-
self.pool = nn.Identity()
|
| 328 |
-
else:
|
| 329 |
-
self.pool = weight_norm(nn.ConvTranspose1d(dim_in, dim_in, kernel_size=3, stride=2, groups=dim_in, padding=1, output_padding=1))
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
def _build_weights(self, dim_in, dim_out, style_dim):
|
| 333 |
-
self.conv1 = weight_norm(nn.Conv1d(dim_in, dim_out, 3, 1, 1))
|
| 334 |
-
self.conv2 = weight_norm(nn.Conv1d(dim_out, dim_out, 3, 1, 1))
|
| 335 |
-
self.norm1 = AdaIN1d(style_dim, dim_in)
|
| 336 |
-
self.norm2 = AdaIN1d(style_dim, dim_out)
|
| 337 |
-
if self.learned_sc:
|
| 338 |
-
self.conv1x1 = weight_norm(nn.Conv1d(dim_in, dim_out, 1, 1, 0, bias=False))
|
| 339 |
-
|
| 340 |
-
def _shortcut(self, x):
|
| 341 |
-
x = self.upsample(x)
|
| 342 |
-
if self.learned_sc:
|
| 343 |
-
x = self.conv1x1(x)
|
| 344 |
-
return x
|
| 345 |
-
|
| 346 |
-
def _residual(self, x, s):
|
| 347 |
-
x = self.norm1(x, s)
|
| 348 |
-
x = self.actv(x)
|
| 349 |
-
x = self.pool(x)
|
| 350 |
-
x = self.conv1(self.dropout(x))
|
| 351 |
-
x = self.norm2(x, s)
|
| 352 |
-
x = self.actv(x)
|
| 353 |
-
x = self.conv2(self.dropout(x))
|
| 354 |
-
return x
|
| 355 |
-
|
| 356 |
-
def forward(self, x, s):
|
| 357 |
-
out = self._residual(x, s)
|
| 358 |
-
out = (out + self._shortcut(x)) / math.sqrt(2)
|
| 359 |
-
return out
|
| 360 |
|
| 361 |
class AdaLayerNorm(nn.Module):
|
| 362 |
|
|
@@ -423,11 +337,6 @@ class ProsodyPredictor(nn.Module):
|
|
| 423 |
|
| 424 |
return F0.squeeze(1), N.squeeze(1)
|
| 425 |
|
| 426 |
-
def length_to_mask(self, lengths):
|
| 427 |
-
mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)
|
| 428 |
-
mask = torch.gt(mask+1, lengths.unsqueeze(1))
|
| 429 |
-
return mask
|
| 430 |
-
|
| 431 |
class DurationEncoder(nn.Module):
|
| 432 |
|
| 433 |
def __init__(self, sty_dim, d_model, nlayers, dropout=0.1):
|
|
@@ -447,21 +356,13 @@ class DurationEncoder(nn.Module):
|
|
| 447 |
self.d_model = d_model
|
| 448 |
self.sty_dim = sty_dim
|
| 449 |
|
| 450 |
-
def forward(self, x, style, text_lengths
|
| 451 |
-
masks = m.to(text_lengths.device)
|
| 452 |
-
|
| 453 |
|
| 454 |
-
|
| 455 |
-
# x : [bs, 512, 987]
|
| 456 |
-
# print('DURATION ENCODER', x.shape, style.shape, masks.shape)
|
| 457 |
-
# s = style.expand(x.shape[0], x.shape[1], -1)
|
| 458 |
style = style[:, :, 0, :].transpose(2, 1) # [bs, 128, 11]
|
| 459 |
-
|
| 460 |
-
style = F.interpolate(style, x.shape[2])
|
| 461 |
-
|
| 462 |
x = torch.cat([x, style], axis=1) # [bs, 640, 75]
|
| 463 |
-
x.masked_fill_(masks[:, None, :], 0.0)
|
| 464 |
-
|
| 465 |
|
| 466 |
input_lengths = text_lengths.cpu().numpy()
|
| 467 |
|
|
@@ -471,7 +372,7 @@ class DurationEncoder(nn.Module):
|
|
| 471 |
print(f'\n=========ENTER ADALAYNORM L479 models.py {x.shape=}, {style.shape=}')
|
| 472 |
x = block(x, style) # [bs, 75, 512]
|
| 473 |
x = torch.cat([x.transpose(1, 2), style], axis=1) # [bs, 512, 75]
|
| 474 |
-
|
| 475 |
else:
|
| 476 |
# print(f'{x.shape=} ENTER LSTM') # [bs, 640, 75] LSTM reduce ch 640 -> 512
|
| 477 |
x = x.transpose(-1, -2)
|
|
@@ -483,15 +384,6 @@ class DurationEncoder(nn.Module):
|
|
| 483 |
x, batch_first=True)
|
| 484 |
x = F.dropout(x, p=self.dropout, training=self.training)
|
| 485 |
x = x.transpose(-1, -2)
|
| 486 |
-
|
| 487 |
-
x_pad = torch.zeros([x.shape[0], x.shape[1], m.shape[-1]])
|
| 488 |
-
|
| 489 |
-
x_pad[:, :, :x.shape[-1]] = x
|
| 490 |
-
x = x_pad.to(x.device)
|
| 491 |
-
# print(f'{x.shape=} EXIR LSTM') # [bs, 512, 75]
|
| 492 |
-
# print('Calling Duration Encoder\n\n\n\n',x.shape, x.min(), x.max())
|
| 493 |
-
# Calling Duration Encoder
|
| 494 |
-
# torch.Size([1, 640, 107]) tensor(-3.0903, device='cuda:0') tensor(2.3089, device='cuda:0')
|
| 495 |
return x.transpose(-1, -2)
|
| 496 |
|
| 497 |
|
|
|
|
| 8 |
from torch.nn.utils import weight_norm, spectral_norm
|
| 9 |
from Utils.ASR.models import ASRCNN
|
| 10 |
from Utils.JDC.model import JDCNet
|
| 11 |
+
from Modules.hifigan import AdainResBlk1d
|
| 12 |
import yaml
|
| 13 |
|
| 14 |
|
|
|
|
| 18 |
self.layer_type = layer_type
|
| 19 |
|
| 20 |
if self.layer_type == 'none':
|
| 21 |
+
raise ValueError
|
| 22 |
+
# self.conv = nn.Identity()
|
| 23 |
elif self.layer_type == 'timepreserve':
|
| 24 |
+
raise ValueError
|
| 25 |
+
# self.conv = spectral_norm(nn.Conv2d(dim_in, dim_in, kernel_size=(3, 1), stride=(2, 1), groups=dim_in, padding=(1, 0)))
|
| 26 |
elif self.layer_type == 'half':
|
| 27 |
self.conv = spectral_norm(nn.Conv2d(dim_in, dim_in, kernel_size=(3, 3), stride=(2, 2), groups=dim_in, padding=1))
|
| 28 |
else:
|
|
|
|
| 50 |
raise RuntimeError('Got unexpected donwsampletype %s, expected is [none, timepreserve, half]' % self.layer_type)
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
class ResBlk(nn.Module):
|
|
|
|
| 126 |
h = self.shared(x) # [bs, 512, 1, 11]
|
| 127 |
|
| 128 |
h = h.mean(3, keepdims=True) # UN COMMENT FOR TIME INVARIANT GLOBAL SPEAKER STYLE
|
| 129 |
+
# h = .7 * h + .25 * h.mean(3, keepdims=True)
|
| 130 |
h = h.transpose(1, 3)
|
| 131 |
s = self.unshared(h)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
return s
|
| 135 |
|
| 136 |
|
|
|
|
| 240 |
|
| 241 |
self.lstm = nn.LSTM(channels, channels//2, 1, batch_first=True, bidirectional=True)
|
| 242 |
|
| 243 |
+
def forward(self, x, input_lengths):
|
| 244 |
x = self.embedding(x) # [B, T, emb]
|
| 245 |
x = x.transpose(1, 2) # [B, emb, T]
|
|
|
|
|
|
|
|
|
|
| 246 |
for c in self.cnn:
|
| 247 |
+
x = c(x)
|
|
|
|
|
|
|
| 248 |
x = x.transpose(1, 2) # [B, T, chn]
|
|
|
|
| 249 |
input_lengths = input_lengths.cpu().numpy()
|
| 250 |
x = nn.utils.rnn.pack_padded_sequence(
|
| 251 |
+
x, input_lengths,
|
| 252 |
+
batch_first=True,
|
| 253 |
+
enforce_sorted=False)
|
| 254 |
self.lstm.flatten_parameters()
|
| 255 |
x, _ = self.lstm(x)
|
| 256 |
x, _ = nn.utils.rnn.pad_packed_sequence(
|
| 257 |
x, batch_first=True)
|
|
|
|
| 258 |
x = x.transpose(-1, -2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
return x
|
| 260 |
|
| 261 |
+
# def inference(self, x):
|
| 262 |
+
# x = self.embedding(x)
|
| 263 |
+
# x = x.transpose(1, 2)
|
| 264 |
+
# x = self.cnn(x)
|
| 265 |
+
# x = x.transpose(1, 2)
|
| 266 |
+
# self.lstm.flatten_parameters()
|
| 267 |
+
# x, _ = self.lstm(x)
|
| 268 |
+
# return x
|
| 269 |
|
| 270 |
+
# def length_to_mask(self, lengths):
|
| 271 |
+
# mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)
|
| 272 |
+
# mask = torch.gt(mask+1, lengths.unsqueeze(1))
|
| 273 |
+
# return mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
class AdaLayerNorm(nn.Module):
|
| 276 |
|
|
|
|
| 337 |
|
| 338 |
return F0.squeeze(1), N.squeeze(1)
|
| 339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
class DurationEncoder(nn.Module):
|
| 341 |
|
| 342 |
def __init__(self, sty_dim, d_model, nlayers, dropout=0.1):
|
|
|
|
| 356 |
self.d_model = d_model
|
| 357 |
self.sty_dim = sty_dim
|
| 358 |
|
| 359 |
+
def forward(self, x, style, text_lengths):
|
|
|
|
|
|
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
style = style[:, :, 0, :].transpose(2, 1) # [bs, 128, 11]
|
| 362 |
+
|
| 363 |
+
style = F.interpolate(style, x.shape[2], mode='nearest')
|
| 364 |
+
|
| 365 |
x = torch.cat([x, style], axis=1) # [bs, 640, 75]
|
|
|
|
|
|
|
| 366 |
|
| 367 |
input_lengths = text_lengths.cpu().numpy()
|
| 368 |
|
|
|
|
| 372 |
print(f'\n=========ENTER ADALAYNORM L479 models.py {x.shape=}, {style.shape=}')
|
| 373 |
x = block(x, style) # [bs, 75, 512]
|
| 374 |
x = torch.cat([x.transpose(1, 2), style], axis=1) # [bs, 512, 75]
|
| 375 |
+
|
| 376 |
else:
|
| 377 |
# print(f'{x.shape=} ENTER LSTM') # [bs, 640, 75] LSTM reduce ch 640 -> 512
|
| 378 |
x = x.transpose(-1, -2)
|
|
|
|
| 384 |
x, batch_first=True)
|
| 385 |
x = F.dropout(x, p=self.dropout, training=self.training)
|
| 386 |
x = x.transpose(-1, -2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
return x.transpose(-1, -2)
|
| 388 |
|
| 389 |
|
msinference.py
CHANGED
|
@@ -51,23 +51,11 @@ to_mel = torchaudio.transforms.MelSpectrogram(
|
|
| 51 |
n_mels=80, n_fft=2048, win_length=1200, hop_length=300)
|
| 52 |
mean, std = -4, 4
|
| 53 |
|
| 54 |
-
# START UTIL
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
def alpha_num(f):
|
| 59 |
f = re.sub(' +', ' ', f) # delete spaces
|
| 60 |
f = re.sub(r'[^A-Z a-z0-9 ]+', '', f) # del non alpha num
|
| 61 |
return f
|
| 62 |
|
| 63 |
-
|
| 64 |
-
# ======== UTILS ABOVE
|
| 65 |
-
|
| 66 |
-
def length_to_mask(lengths):
|
| 67 |
-
mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)
|
| 68 |
-
mask = torch.gt(mask+1, lengths.unsqueeze(1))
|
| 69 |
-
return mask
|
| 70 |
-
|
| 71 |
def preprocess(wave):
|
| 72 |
wave_tensor = torch.from_numpy(wave).float()
|
| 73 |
mel_tensor = to_mel(wave_tensor)
|
|
@@ -201,51 +189,31 @@ params = params_whole['net']
|
|
| 201 |
# --
|
| 202 |
from collections import OrderedDict
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
new_state_dict[k[7:]] = v # del 'module.'
|
| 212 |
-
bert_encoder.load_state_dict(new_state_dict, strict=True)
|
| 213 |
-
# --
|
| 214 |
-
new_state_dict = OrderedDict()
|
| 215 |
-
for k, v in params['predictor'].items():
|
| 216 |
-
new_state_dict[k[7:]] = v # del 'module.'
|
| 217 |
-
predictor.load_state_dict(new_state_dict, strict=True) # XTRA non-ckpt LSTMs nlayers add slowiness to voice
|
| 218 |
-
# --
|
| 219 |
-
new_state_dict = OrderedDict()
|
| 220 |
-
for k, v in params['decoder'].items():
|
| 221 |
-
new_state_dict[k[7:]] = v
|
| 222 |
-
decoder.load_state_dict(new_state_dict, strict=True)
|
| 223 |
-
# --
|
| 224 |
-
new_state_dict = OrderedDict()
|
| 225 |
-
for k, v in params['text_encoder'].items():
|
| 226 |
-
new_state_dict[k[7:]] = v
|
| 227 |
-
text_encoder.load_state_dict(new_state_dict, strict=True)
|
| 228 |
-
# --
|
| 229 |
-
new_state_dict = OrderedDict()
|
| 230 |
-
for k, v in params['predictor_encoder'].items():
|
| 231 |
-
new_state_dict[k[7:]] = v
|
| 232 |
-
predictor_encoder.load_state_dict(new_state_dict, strict=True)
|
| 233 |
-
# --
|
| 234 |
-
new_state_dict = OrderedDict()
|
| 235 |
-
for k, v in params['style_encoder'].items():
|
| 236 |
-
new_state_dict[k[7:]] = v
|
| 237 |
-
style_encoder.load_state_dict(new_state_dict, strict=True)
|
| 238 |
-
# --
|
| 239 |
-
new_state_dict = OrderedDict()
|
| 240 |
-
for k, v in params['text_aligner'].items():
|
| 241 |
-
new_state_dict[k[7:]] = v # del 'module.'
|
| 242 |
-
text_aligner.load_state_dict(new_state_dict, strict=True)
|
| 243 |
-
# --
|
| 244 |
-
new_state_dict = OrderedDict()
|
| 245 |
-
for k, v in params['pitch_extractor'].items():
|
| 246 |
-
new_state_dict[k[7:]] = v
|
| 247 |
-
pitch_extractor.load_state_dict(new_state_dict, strict=True)
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
def inference(text,
|
| 251 |
ref_s,
|
|
@@ -267,7 +235,7 @@ def inference(text,
|
|
| 267 |
|
| 268 |
with torch.no_grad():
|
| 269 |
input_lengths = torch.LongTensor([tokens.shape[-1]]).to(device)
|
| 270 |
-
|
| 271 |
# -----------------------
|
| 272 |
# WHO TRANSLATES these tokens to sylla
|
| 273 |
# print(text_mask.shape, '\n__\n', tokens, '\n__\n', text_mask.min(), text_mask.max())
|
|
@@ -282,13 +250,9 @@ def inference(text,
|
|
| 282 |
# 54, 156, 63, 158, 147, 83, 56, 16, 4]], device='cuda:0')
|
| 283 |
|
| 284 |
|
| 285 |
-
t_en = text_encoder(tokens, input_lengths
|
| 286 |
-
bert_dur = bert(tokens, attention_mask=
|
| 287 |
d_en = bert_encoder(bert_dur).transpose(-1, -2)
|
| 288 |
-
# print('BERTdu', bert_dur.shape, tokens.shape, '\n') # bert what is the 768 per token -> IS USED in sampler
|
| 289 |
-
# BERTdu torch.Size([1, 11, 768]) torch.Size([1, 11])
|
| 290 |
-
|
| 291 |
-
|
| 292 |
|
| 293 |
ref = ref_s[:, :, :, :128] # [bs, 11, 1, 128]
|
| 294 |
s = ref_s[:, :, :, 128:] # have channels as last dim so it can go through nn.Linear layers
|
|
@@ -299,13 +263,13 @@ def inference(text,
|
|
| 299 |
# s = .74 * s # prosody / arousal & fading unvoiced syllabes [x0.7 - x1.2]
|
| 300 |
|
| 301 |
|
| 302 |
-
print(f'{d_en.shape=} {s.shape=} {input_lengths.shape=}
|
| 303 |
d = predictor.text_encoder(d_en,
|
| 304 |
s,
|
| 305 |
-
input_lengths
|
| 306 |
-
text_mask)
|
| 307 |
|
| 308 |
x, _ = predictor.lstm(d)
|
|
|
|
| 309 |
duration = predictor.duration_proj(x)
|
| 310 |
|
| 311 |
duration = torch.sigmoid(duration).sum(axis=-1)
|
|
@@ -364,14 +328,12 @@ def inference(text,
|
|
| 364 |
#
|
| 365 |
# This source code is licensed under the MIT license found in the
|
| 366 |
# LICENSE file in the root directory of this source tree.
|
| 367 |
-
|
| 368 |
import os
|
| 369 |
import re
|
| 370 |
import tempfile
|
| 371 |
import torch
|
| 372 |
import sys
|
| 373 |
-
import numpy as np
|
| 374 |
-
import audiofile
|
| 375 |
from huggingface_hub import hf_hub_download
|
| 376 |
|
| 377 |
# Setup TTS env
|
|
@@ -393,8 +355,6 @@ with open(f"Utils/all_langs.csv") as f:
|
|
| 393 |
|
| 394 |
# LOAD hun / ron / serbian - rmc-script_latin / cyrillic-Carpathian (not Vlax)
|
| 395 |
# ==============================================================================================
|
| 396 |
-
import re
|
| 397 |
-
from num2words import num2words
|
| 398 |
|
| 399 |
PHONEME_MAP = {
|
| 400 |
'služ' : 'sloooozz', # 'službeno'
|
|
|
|
| 51 |
n_mels=80, n_fft=2048, win_length=1200, hop_length=300)
|
| 52 |
mean, std = -4, 4
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def alpha_num(f):
|
| 55 |
f = re.sub(' +', ' ', f) # delete spaces
|
| 56 |
f = re.sub(r'[^A-Z a-z0-9 ]+', '', f) # del non alpha num
|
| 57 |
return f
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def preprocess(wave):
|
| 60 |
wave_tensor = torch.from_numpy(wave).float()
|
| 61 |
mel_tensor = to_mel(wave_tensor)
|
|
|
|
| 189 |
# --
|
| 190 |
from collections import OrderedDict
|
| 191 |
|
| 192 |
+
def _del_prefix(d):
|
| 193 |
+
# del ".module"
|
| 194 |
+
out = OrderedDict()
|
| 195 |
+
for k, v in d.items():
|
| 196 |
+
out[k[7:]] = v
|
| 197 |
+
return out
|
| 198 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
bert.load_state_dict( _del_prefix(params['bert']), strict=True)
|
| 201 |
+
bert_encoder.load_state_dict(_del_prefix(params['bert_encoder']), strict=True)
|
| 202 |
+
predictor.load_state_dict( _del_prefix(params['predictor']), strict=True) # XTRA non-ckpt LSTMs nlayers add slowiness to voice
|
| 203 |
+
decoder.load_state_dict( _del_prefix(params['decoder']), strict=True)
|
| 204 |
+
text_encoder.load_state_dict(_del_prefix(params['text_encoder']), strict=True)
|
| 205 |
+
predictor_encoder.load_state_dict(_del_prefix(params['predictor_encoder']), strict=True)
|
| 206 |
+
style_encoder.load_state_dict(_del_prefix(params['style_encoder']), strict=True)
|
| 207 |
+
text_aligner.load_state_dict( _del_prefix(params['text_aligner']), strict=True)
|
| 208 |
+
pitch_extractor.load_state_dict(_del_prefix(params['pitch_extractor']), strict=True)
|
| 209 |
+
|
| 210 |
+
# def _shift(x):
|
| 211 |
+
# # [bs, samples] shift circular each batch elem of sound
|
| 212 |
+
# n = x.shape[1]
|
| 213 |
+
# for i, batch_elem in enumerate(x):
|
| 214 |
+
# offset = np.random.randint(.24 * n, max(1, .74 * n)) # high should be above >= 0 TBD
|
| 215 |
+
# x[i, ...] = torch.roll(batch_elem, offset, dims=1) # batch_elem = [400000, ]
|
| 216 |
+
# return x
|
| 217 |
|
| 218 |
def inference(text,
|
| 219 |
ref_s,
|
|
|
|
| 235 |
|
| 236 |
with torch.no_grad():
|
| 237 |
input_lengths = torch.LongTensor([tokens.shape[-1]]).to(device)
|
| 238 |
+
|
| 239 |
# -----------------------
|
| 240 |
# WHO TRANSLATES these tokens to sylla
|
| 241 |
# print(text_mask.shape, '\n__\n', tokens, '\n__\n', text_mask.min(), text_mask.max())
|
|
|
|
| 250 |
# 54, 156, 63, 158, 147, 83, 56, 16, 4]], device='cuda:0')
|
| 251 |
|
| 252 |
|
| 253 |
+
t_en = text_encoder(tokens, input_lengths)
|
| 254 |
+
bert_dur = bert(tokens, attention_mask=None)
|
| 255 |
d_en = bert_encoder(bert_dur).transpose(-1, -2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
ref = ref_s[:, :, :, :128] # [bs, 11, 1, 128]
|
| 258 |
s = ref_s[:, :, :, 128:] # have channels as last dim so it can go through nn.Linear layers
|
|
|
|
| 263 |
# s = .74 * s # prosody / arousal & fading unvoiced syllabes [x0.7 - x1.2]
|
| 264 |
|
| 265 |
|
| 266 |
+
print(f'{d_en.shape=} {s.shape=} {input_lengths.shape=}')
|
| 267 |
d = predictor.text_encoder(d_en,
|
| 268 |
s,
|
| 269 |
+
input_lengths)
|
|
|
|
| 270 |
|
| 271 |
x, _ = predictor.lstm(d)
|
| 272 |
+
print(d.shape, x.shape, 'Lstm')
|
| 273 |
duration = predictor.duration_proj(x)
|
| 274 |
|
| 275 |
duration = torch.sigmoid(duration).sum(axis=-1)
|
|
|
|
| 328 |
#
|
| 329 |
# This source code is licensed under the MIT license found in the
|
| 330 |
# LICENSE file in the root directory of this source tree.
|
| 331 |
+
from num2words import num2words
|
| 332 |
import os
|
| 333 |
import re
|
| 334 |
import tempfile
|
| 335 |
import torch
|
| 336 |
import sys
|
|
|
|
|
|
|
| 337 |
from huggingface_hub import hf_hub_download
|
| 338 |
|
| 339 |
# Setup TTS env
|
|
|
|
| 355 |
|
| 356 |
# LOAD hun / ron / serbian - rmc-script_latin / cyrillic-Carpathian (not Vlax)
|
| 357 |
# ==============================================================================================
|
|
|
|
|
|
|
| 358 |
|
| 359 |
PHONEME_MAP = {
|
| 360 |
'služ' : 'sloooozz', # 'službeno'
|