Spaces:
Runtime error
Runtime error
File size: 13,374 Bytes
c968fc3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# Copyright (c) 2023 Amphion.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
from torch.nn.utils import weight_norm, spectral_norm
from modules.vocoder_blocks import *
LRELU_SLOPE = 0.1
class ISTFT(nn.Module):
"""
Custom implementation of ISTFT since torch.istft doesn't allow custom padding (other than `center=True`) with
windowing. This is because the NOLA (Nonzero Overlap Add) check fails at the edges.
See issue: https://github.com/pytorch/pytorch/issues/62323
Specifically, in the context of neural vocoding we are interested in "same" padding analogous to CNNs.
The NOLA constraint is met as we trim padded samples anyway.
Args:
n_fft (int): Size of Fourier transform.
hop_length (int): The distance between neighboring sliding window frames.
win_length (int): The size of window frame and STFT filter.
padding (str, optional): Type of padding. Options are "center" or "same". Defaults to "same".
"""
def __init__(
self,
n_fft: int,
hop_length: int,
win_length: int,
padding: str = "same",
):
super().__init__()
if padding not in ["center", "same"]:
raise ValueError("Padding must be 'center' or 'same'.")
self.padding = padding
self.n_fft = n_fft
self.hop_length = hop_length
self.win_length = win_length
def forward(self, spec: torch.Tensor, window) -> torch.Tensor:
"""
Compute the Inverse Short Time Fourier Transform (ISTFT) of a complex spectrogram.
Args:
spec (Tensor): Input complex spectrogram of shape (B, N, T), where B is the batch size,
N is the number of frequency bins, and T is the number of time frames.
Returns:
Tensor: Reconstructed time-domain signal of shape (B, L), where L is the length of the output signal.
"""
if self.padding == "center":
# Fallback to pytorch native implementation
return torch.istft(
spec,
self.n_fft,
self.hop_length,
self.win_length,
window,
center=True,
)
elif self.padding == "same":
pad = (self.win_length - self.hop_length) // 2
else:
raise ValueError("Padding must be 'center' or 'same'.")
assert spec.dim() == 3, "Expected a 3D tensor as input"
B, N, T = spec.shape
# Inverse FFT
ifft = torch.fft.irfft(spec, self.n_fft, dim=1, norm="backward")
ifft = ifft * window[None, :, None]
# Overlap and Add
output_size = (T - 1) * self.hop_length + self.win_length
y = torch.nn.functional.fold(
ifft,
output_size=(1, output_size),
kernel_size=(1, self.win_length),
stride=(1, self.hop_length),
)[:, 0, 0, pad:-pad]
# Window envelope
window_sq = window.square().expand(1, T, -1).transpose(1, 2)
window_envelope = torch.nn.functional.fold(
window_sq,
output_size=(1, output_size),
kernel_size=(1, self.win_length),
stride=(1, self.hop_length),
).squeeze()[pad:-pad]
# Normalize
assert (window_envelope > 1e-11).all()
y = y / window_envelope
return y
# The ASP and PSP Module are adopted from APNet under the MIT License
# https://github.com/YangAi520/APNet/blob/main/models.py
class ASPResBlock(torch.nn.Module):
def __init__(self, cfg, channels, kernel_size=3, dilation=(1, 3, 5)):
super(ASPResBlock, self).__init__()
self.cfg = cfg
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)
def forward(self, x):
for c1, c2 in zip(self.convs1, self.convs2):
xt = F.leaky_relu(x, LRELU_SLOPE)
xt = c1(xt)
xt = F.leaky_relu(xt, LRELU_SLOPE)
xt = c2(xt)
x = xt + x
return x
class PSPResBlock(torch.nn.Module):
def __init__(self, cfg, channels, kernel_size=3, dilation=(1, 3, 5)):
super(PSPResBlock, self).__init__()
self.cfg = cfg
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)
def forward(self, x):
for c1, c2 in zip(self.convs1, self.convs2):
xt = F.leaky_relu(x, LRELU_SLOPE)
xt = c1(xt)
xt = F.leaky_relu(xt, LRELU_SLOPE)
xt = c2(xt)
x = xt + x
return x
class APNet(torch.nn.Module):
def __init__(self, cfg):
super(APNet, self).__init__()
self.cfg = cfg
self.ASP_num_kernels = len(cfg.model.apnet.ASP_resblock_kernel_sizes)
self.PSP_num_kernels = len(cfg.model.apnet.PSP_resblock_kernel_sizes)
self.ASP_input_conv = weight_norm(
Conv1d(
cfg.preprocess.n_mel,
cfg.model.apnet.ASP_channel,
cfg.model.apnet.ASP_input_conv_kernel_size,
1,
padding=get_padding(cfg.model.apnet.ASP_input_conv_kernel_size, 1),
)
)
self.PSP_input_conv = weight_norm(
Conv1d(
cfg.preprocess.n_mel,
cfg.model.apnet.PSP_channel,
cfg.model.apnet.PSP_input_conv_kernel_size,
1,
padding=get_padding(cfg.model.apnet.PSP_input_conv_kernel_size, 1),
)
)
self.ASP_ResNet = nn.ModuleList()
for j, (k, d) in enumerate(
zip(
cfg.model.apnet.ASP_resblock_kernel_sizes,
cfg.model.apnet.ASP_resblock_dilation_sizes,
)
):
self.ASP_ResNet.append(ASPResBlock(cfg, cfg.model.apnet.ASP_channel, k, d))
self.PSP_ResNet = nn.ModuleList()
for j, (k, d) in enumerate(
zip(
cfg.model.apnet.PSP_resblock_kernel_sizes,
cfg.model.apnet.PSP_resblock_dilation_sizes,
)
):
self.PSP_ResNet.append(PSPResBlock(cfg, cfg.model.apnet.PSP_channel, k, d))
self.ASP_output_conv = weight_norm(
Conv1d(
cfg.model.apnet.ASP_channel,
cfg.preprocess.n_fft // 2 + 1,
cfg.model.apnet.ASP_output_conv_kernel_size,
1,
padding=get_padding(cfg.model.apnet.ASP_output_conv_kernel_size, 1),
)
)
self.PSP_output_R_conv = weight_norm(
Conv1d(
cfg.model.apnet.PSP_channel,
cfg.preprocess.n_fft // 2 + 1,
cfg.model.apnet.PSP_output_R_conv_kernel_size,
1,
padding=get_padding(cfg.model.apnet.PSP_output_R_conv_kernel_size, 1),
)
)
self.PSP_output_I_conv = weight_norm(
Conv1d(
cfg.model.apnet.PSP_channel,
cfg.preprocess.n_fft // 2 + 1,
cfg.model.apnet.PSP_output_I_conv_kernel_size,
1,
padding=get_padding(cfg.model.apnet.PSP_output_I_conv_kernel_size, 1),
)
)
self.iSTFT = ISTFT(
self.cfg.preprocess.n_fft,
hop_length=self.cfg.preprocess.hop_size,
win_length=self.cfg.preprocess.win_size,
)
self.ASP_output_conv.apply(init_weights)
self.PSP_output_R_conv.apply(init_weights)
self.PSP_output_I_conv.apply(init_weights)
def forward(self, mel):
logamp = self.ASP_input_conv(mel)
logamps = None
for j in range(self.ASP_num_kernels):
if logamps is None:
logamps = self.ASP_ResNet[j](logamp)
else:
logamps += self.ASP_ResNet[j](logamp)
logamp = logamps / self.ASP_num_kernels
logamp = F.leaky_relu(logamp)
logamp = self.ASP_output_conv(logamp)
pha = self.PSP_input_conv(mel)
phas = None
for j in range(self.PSP_num_kernels):
if phas is None:
phas = self.PSP_ResNet[j](pha)
else:
phas += self.PSP_ResNet[j](pha)
pha = phas / self.PSP_num_kernels
pha = F.leaky_relu(pha)
R = self.PSP_output_R_conv(pha)
I = self.PSP_output_I_conv(pha)
pha = torch.atan2(I, R)
rea = torch.exp(logamp) * torch.cos(pha)
imag = torch.exp(logamp) * torch.sin(pha)
spec = torch.cat((rea.unsqueeze(-1), imag.unsqueeze(-1)), -1)
spec = torch.view_as_complex(spec)
audio = self.iSTFT.forward(
spec, torch.hann_window(self.cfg.preprocess.win_size).to(mel.device)
)
return logamp, pha, rea, imag, audio.unsqueeze(1)
|