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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Optional, Tuple
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from einops import rearrange
|
| 5 |
+
from torch import nn
|
| 6 |
+
from torch.nn import Conv2d
|
| 7 |
+
from torch.nn.utils import weight_norm
|
| 8 |
+
from torchaudio.transforms import Spectrogram
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class MultiPeriodDiscriminator(nn.Module):
|
| 12 |
+
"""
|
| 13 |
+
Multi-Period Discriminator module adapted from https://github.com/jik876/hifi-gan.
|
| 14 |
+
Additionally, it allows incorporating conditional information with a learned embeddings table.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
periods (tuple[int]): Tuple of periods for each discriminator.
|
| 18 |
+
num_embeddings (int, optional): Number of embeddings. None means non-conditional discriminator.
|
| 19 |
+
Defaults to None.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
def __init__(self, periods: Tuple[int, ...] = (2, 3, 5, 7, 11), num_embeddings: Optional[int] = None):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.discriminators = nn.ModuleList([DiscriminatorP(period=p, num_embeddings=num_embeddings) for p in periods])
|
| 25 |
+
|
| 26 |
+
def forward(
|
| 27 |
+
self, y: torch.Tensor, y_hat: torch.Tensor, bandwidth_id: Optional[torch.Tensor] = None
|
| 28 |
+
) -> Tuple[List[torch.Tensor], List[torch.Tensor], List[List[torch.Tensor]], List[List[torch.Tensor]]]:
|
| 29 |
+
y_d_rs = []
|
| 30 |
+
y_d_gs = []
|
| 31 |
+
fmap_rs = []
|
| 32 |
+
fmap_gs = []
|
| 33 |
+
for d in self.discriminators:
|
| 34 |
+
y_d_r, fmap_r = d(x=y, cond_embedding_id=bandwidth_id)
|
| 35 |
+
y_d_g, fmap_g = d(x=y_hat, cond_embedding_id=bandwidth_id)
|
| 36 |
+
y_d_rs.append(y_d_r)
|
| 37 |
+
fmap_rs.append(fmap_r)
|
| 38 |
+
y_d_gs.append(y_d_g)
|
| 39 |
+
fmap_gs.append(fmap_g)
|
| 40 |
+
|
| 41 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class DiscriminatorP(nn.Module):
|
| 45 |
+
def __init__(
|
| 46 |
+
self,
|
| 47 |
+
period: int,
|
| 48 |
+
in_channels: int = 1,
|
| 49 |
+
kernel_size: int = 5,
|
| 50 |
+
stride: int = 3,
|
| 51 |
+
lrelu_slope: float = 0.1,
|
| 52 |
+
num_embeddings: Optional[int] = None,
|
| 53 |
+
):
|
| 54 |
+
super().__init__()
|
| 55 |
+
self.period = period
|
| 56 |
+
self.convs = nn.ModuleList(
|
| 57 |
+
[
|
| 58 |
+
weight_norm(Conv2d(in_channels, 32, (kernel_size, 1), (stride, 1), padding=(kernel_size // 2, 0))),
|
| 59 |
+
weight_norm(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(kernel_size // 2, 0))),
|
| 60 |
+
weight_norm(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(kernel_size // 2, 0))),
|
| 61 |
+
weight_norm(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(kernel_size // 2, 0))),
|
| 62 |
+
weight_norm(Conv2d(1024, 1024, (kernel_size, 1), (1, 1), padding=(kernel_size // 2, 0))),
|
| 63 |
+
]
|
| 64 |
+
)
|
| 65 |
+
if num_embeddings is not None:
|
| 66 |
+
self.emb = torch.nn.Embedding(num_embeddings=num_embeddings, embedding_dim=1024)
|
| 67 |
+
torch.nn.init.zeros_(self.emb.weight)
|
| 68 |
+
|
| 69 |
+
self.conv_post = weight_norm(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
| 70 |
+
self.lrelu_slope = lrelu_slope
|
| 71 |
+
|
| 72 |
+
def forward(
|
| 73 |
+
self, x: torch.Tensor, cond_embedding_id: Optional[torch.Tensor] = None
|
| 74 |
+
) -> Tuple[torch.Tensor, List[torch.Tensor]]:
|
| 75 |
+
x = x.unsqueeze(1)
|
| 76 |
+
fmap = []
|
| 77 |
+
# 1d to 2d
|
| 78 |
+
b, c, t = x.shape
|
| 79 |
+
if t % self.period != 0: # pad first
|
| 80 |
+
n_pad = self.period - (t % self.period)
|
| 81 |
+
x = torch.nn.functional.pad(x, (0, n_pad), "reflect")
|
| 82 |
+
t = t + n_pad
|
| 83 |
+
x = x.view(b, c, t // self.period, self.period)
|
| 84 |
+
|
| 85 |
+
for i, l in enumerate(self.convs):
|
| 86 |
+
x = l(x)
|
| 87 |
+
x = torch.nn.functional.leaky_relu(x, self.lrelu_slope)
|
| 88 |
+
if i > 0:
|
| 89 |
+
fmap.append(x)
|
| 90 |
+
if cond_embedding_id is not None:
|
| 91 |
+
emb = self.emb(cond_embedding_id)
|
| 92 |
+
h = (emb.view(1, -1, 1, 1) * x).sum(dim=1, keepdims=True)
|
| 93 |
+
else:
|
| 94 |
+
h = 0
|
| 95 |
+
x = self.conv_post(x)
|
| 96 |
+
fmap.append(x)
|
| 97 |
+
x += h
|
| 98 |
+
x = torch.flatten(x, 1, -1)
|
| 99 |
+
|
| 100 |
+
return x, fmap
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
class MultiResolutionDiscriminator(nn.Module):
|
| 104 |
+
def __init__(
|
| 105 |
+
self,
|
| 106 |
+
fft_sizes: Tuple[int, ...] = (2048, 1024, 512),
|
| 107 |
+
num_embeddings: Optional[int] = None,
|
| 108 |
+
):
|
| 109 |
+
"""
|
| 110 |
+
Multi-Resolution Discriminator module adapted from https://github.com/descriptinc/descript-audio-codec.
|
| 111 |
+
Additionally, it allows incorporating conditional information with a learned embeddings table.
|
| 112 |
+
|
| 113 |
+
Args:
|
| 114 |
+
fft_sizes (tuple[int]): Tuple of window lengths for FFT. Defaults to (2048, 1024, 512).
|
| 115 |
+
num_embeddings (int, optional): Number of embeddings. None means non-conditional discriminator.
|
| 116 |
+
Defaults to None.
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
super().__init__()
|
| 120 |
+
self.discriminators = nn.ModuleList(
|
| 121 |
+
[DiscriminatorR(window_length=w, num_embeddings=num_embeddings) for w in fft_sizes]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
def forward(
|
| 125 |
+
self, y: torch.Tensor, y_hat: torch.Tensor, bandwidth_id: torch.Tensor = None
|
| 126 |
+
) -> Tuple[List[torch.Tensor], List[torch.Tensor], List[List[torch.Tensor]], List[List[torch.Tensor]]]:
|
| 127 |
+
y_d_rs = []
|
| 128 |
+
y_d_gs = []
|
| 129 |
+
fmap_rs = []
|
| 130 |
+
fmap_gs = []
|
| 131 |
+
|
| 132 |
+
for d in self.discriminators:
|
| 133 |
+
y_d_r, fmap_r = d(x=y, cond_embedding_id=bandwidth_id)
|
| 134 |
+
y_d_g, fmap_g = d(x=y_hat, cond_embedding_id=bandwidth_id)
|
| 135 |
+
y_d_rs.append(y_d_r)
|
| 136 |
+
fmap_rs.append(fmap_r)
|
| 137 |
+
y_d_gs.append(y_d_g)
|
| 138 |
+
fmap_gs.append(fmap_g)
|
| 139 |
+
|
| 140 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class DiscriminatorR(nn.Module):
|
| 144 |
+
def __init__(
|
| 145 |
+
self,
|
| 146 |
+
window_length: int,
|
| 147 |
+
num_embeddings: Optional[int] = None,
|
| 148 |
+
channels: int = 32,
|
| 149 |
+
hop_factor: float = 0.25,
|
| 150 |
+
bands: Tuple[Tuple[float, float], ...] = ((0.0, 0.1), (0.1, 0.25), (0.25, 0.5), (0.5, 0.75), (0.75, 1.0)),
|
| 151 |
+
):
|
| 152 |
+
super().__init__()
|
| 153 |
+
self.window_length = window_length
|
| 154 |
+
self.hop_factor = hop_factor
|
| 155 |
+
self.spec_fn = Spectrogram(
|
| 156 |
+
n_fft=window_length, hop_length=int(window_length * hop_factor), win_length=window_length, power=None
|
| 157 |
+
)
|
| 158 |
+
n_fft = window_length // 2 + 1
|
| 159 |
+
bands = [(int(b[0] * n_fft), int(b[1] * n_fft)) for b in bands]
|
| 160 |
+
self.bands = bands
|
| 161 |
+
convs = lambda: nn.ModuleList(
|
| 162 |
+
[
|
| 163 |
+
weight_norm(nn.Conv2d(2, channels, (3, 9), (1, 1), padding=(1, 4))),
|
| 164 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 9), (1, 2), padding=(1, 4))),
|
| 165 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 9), (1, 2), padding=(1, 4))),
|
| 166 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 9), (1, 2), padding=(1, 4))),
|
| 167 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 3), (1, 1), padding=(1, 1))),
|
| 168 |
+
]
|
| 169 |
+
)
|
| 170 |
+
self.band_convs = nn.ModuleList([convs() for _ in range(len(self.bands))])
|
| 171 |
+
|
| 172 |
+
if num_embeddings is not None:
|
| 173 |
+
self.emb = torch.nn.Embedding(num_embeddings=num_embeddings, embedding_dim=channels)
|
| 174 |
+
torch.nn.init.zeros_(self.emb.weight)
|
| 175 |
+
|
| 176 |
+
self.conv_post = weight_norm(nn.Conv2d(channels, 1, (3, 3), (1, 1), padding=(1, 1)))
|
| 177 |
+
|
| 178 |
+
def spectrogram(self, x):
|
| 179 |
+
# Remove DC offset
|
| 180 |
+
x = x - x.mean(dim=-1, keepdims=True)
|
| 181 |
+
# Peak normalize the volume of input audio
|
| 182 |
+
x = 0.8 * x / (x.abs().max(dim=-1, keepdim=True)[0] + 1e-9)
|
| 183 |
+
x = self.spec_fn(x)
|
| 184 |
+
x = torch.view_as_real(x)
|
| 185 |
+
x = rearrange(x, "b f t c -> b c t f")
|
| 186 |
+
# Split into bands
|
| 187 |
+
x_bands = [x[..., b[0] : b[1]] for b in self.bands]
|
| 188 |
+
return x_bands
|
| 189 |
+
|
| 190 |
+
def forward(self, x: torch.Tensor, cond_embedding_id: torch.Tensor = None):
|
| 191 |
+
x_bands = self.spectrogram(x)
|
| 192 |
+
fmap = []
|
| 193 |
+
x = []
|
| 194 |
+
for band, stack in zip(x_bands, self.band_convs):
|
| 195 |
+
for i, layer in enumerate(stack):
|
| 196 |
+
band = layer(band)
|
| 197 |
+
band = torch.nn.functional.leaky_relu(band, 0.1)
|
| 198 |
+
if i > 0:
|
| 199 |
+
fmap.append(band)
|
| 200 |
+
x.append(band)
|
| 201 |
+
x = torch.cat(x, dim=-1)
|
| 202 |
+
if cond_embedding_id is not None:
|
| 203 |
+
emb = self.emb(cond_embedding_id)
|
| 204 |
+
h = (emb.view(1, -1, 1, 1) * x).sum(dim=1, keepdims=True)
|
| 205 |
+
else:
|
| 206 |
+
h = 0
|
| 207 |
+
x = self.conv_post(x)
|
| 208 |
+
fmap.append(x)
|
| 209 |
+
x += h
|
| 210 |
+
|
| 211 |
+
return x, fmap
|