File size: 2,764 Bytes
7e2301f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Codec GAN discriminators: multi-period (HiFi-GAN) + multi-resolution complex STFT."""

from __future__ import annotations

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.parametrizations import weight_norm


class PeriodDisc(nn.Module):
    def __init__(self, period: int, ch: int = 32):
        super().__init__()
        self.period = period
        chans = [1, ch, ch * 4, ch * 16, ch * 32]
        self.convs = nn.ModuleList(
            weight_norm(nn.Conv2d(chans[i], chans[i + 1], (5, 1), (3, 1), padding=(2, 0))) for i in range(4)
        )
        self.convs.append(weight_norm(nn.Conv2d(chans[-1], chans[-1], (5, 1), padding=(2, 0))))
        self.post = weight_norm(nn.Conv2d(chans[-1], 1, (3, 1), padding=(1, 0)))

    def forward(self, x):  # (B, 1, T)
        B, C, T = x.shape
        if T % self.period:
            x = F.pad(x, (0, self.period - T % self.period), mode="reflect")
        x = x.view(B, C, -1, self.period)
        fmaps = []
        for c in self.convs:
            x = F.leaky_relu(c(x), 0.1)
            fmaps.append(x)
        return self.post(x).flatten(1), fmaps


class STFTDisc(nn.Module):
    def __init__(self, n_fft: int, hop: int, ch: int = 32):
        super().__init__()
        self.n_fft, self.hop = n_fft, hop
        self.register_buffer("window", torch.hann_window(n_fft), persistent=False)
        self.convs = nn.ModuleList([
            weight_norm(nn.Conv2d(2, ch, (3, 9), padding=(1, 4))),
            weight_norm(nn.Conv2d(ch, ch, (3, 9), (1, 2), padding=(1, 4))),
            weight_norm(nn.Conv2d(ch, ch, (3, 9), (1, 2), padding=(1, 4))),
            weight_norm(nn.Conv2d(ch, ch, (3, 9), (1, 2), padding=(1, 4))),
            weight_norm(nn.Conv2d(ch, ch, (3, 3), padding=(1, 1))),
        ])
        self.post = weight_norm(nn.Conv2d(ch, 1, (3, 3), padding=(1, 1)))

    def forward(self, x):
        s = torch.stft(x[:, 0].float(), self.n_fft, self.hop, window=self.window, return_complex=True)
        x = torch.stack([s.real, s.imag], 1).transpose(2, 3)  # (B, 2, frames, bins)
        fmaps = []
        for c in self.convs:
            x = F.leaky_relu(c(x), 0.1)
            fmaps.append(x)
        return self.post(x).flatten(1), fmaps


class CodecDiscriminator(nn.Module):
    def __init__(self, periods=(2, 3, 5, 7, 11), stfts=((2048, 512), (1024, 256), (512, 128)), ch: int = 32):
        super().__init__()
        self.discs = nn.ModuleList([PeriodDisc(p, ch) for p in periods] + [STFTDisc(n, h, ch) for n, h in stfts])

    def forward(self, x):
        """Returns (list of logits, list of feature-map lists), one entry per sub-discriminator."""
        outs = [d(x) for d in self.discs]
        return [o[0] for o in outs], [o[1] for o in outs]