Spaces:
Runtime error
Runtime error
File size: 4,993 Bytes
12bfd03 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from academicodec.modules import NormConv1d
from academicodec.modules import NormConv2d
from academicodec.utils import get_padding
from torch.nn import AvgPool1d
from torch.nn.utils import spectral_norm
from torch.nn.utils import weight_norm
LRELU_SLOPE = 0.1
class DiscriminatorP(torch.nn.Module):
def __init__(self,
period,
kernel_size=5,
stride=3,
use_spectral_norm=False,
activation: str='LeakyReLU',
activation_params: dict={'negative_slope': 0.2}):
super(DiscriminatorP, self).__init__()
self.period = period
norm_f = weight_norm if use_spectral_norm is False else spectral_norm
self.activation = getattr(torch.nn, activation)(**activation_params)
self.convs = nn.ModuleList([
NormConv2d(
1,
32, (kernel_size, 1), (stride, 1),
padding=(get_padding(5, 1), 0)),
NormConv2d(
32,
32, (kernel_size, 1), (stride, 1),
padding=(get_padding(5, 1), 0)),
NormConv2d(
32,
32, (kernel_size, 1), (stride, 1),
padding=(get_padding(5, 1), 0)),
NormConv2d(
32,
32, (kernel_size, 1), (stride, 1),
padding=(get_padding(5, 1), 0)),
NormConv2d(32, 32, (kernel_size, 1), 1, padding=(2, 0)),
])
self.conv_post = NormConv2d(32, 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 = self.activation(x)
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):
super(MultiPeriodDiscriminator, self).__init__()
self.discriminators = nn.ModuleList([
DiscriminatorP(2),
DiscriminatorP(3),
DiscriminatorP(5),
DiscriminatorP(7),
DiscriminatorP(11),
])
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)
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 DiscriminatorS(torch.nn.Module):
def __init__(self,
use_spectral_norm=False,
activation: str='LeakyReLU',
activation_params: dict={'negative_slope': 0.2}):
super(DiscriminatorS, self).__init__()
self.activation = getattr(torch.nn, activation)(**activation_params)
self.convs = nn.ModuleList([
NormConv1d(1, 32, 15, 1, padding=7),
NormConv1d(32, 32, 41, 2, groups=4, padding=20),
NormConv1d(32, 32, 41, 2, groups=16, padding=20),
NormConv1d(32, 32, 41, 4, groups=16, padding=20),
NormConv1d(32, 32, 41, 4, groups=16, padding=20),
NormConv1d(32, 32, 41, 1, groups=16, padding=20),
NormConv1d(32, 32, 5, 1, padding=2),
])
self.conv_post = NormConv1d(32, 1, 3, 1, padding=1)
def forward(self, x):
fmap = []
for l in self.convs:
x = l(x)
x = self.activation(x)
fmap.append(x)
x = self.conv_post(x)
fmap.append(x)
x = torch.flatten(x, 1, -1)
return x, fmap
class MultiScaleDiscriminator(torch.nn.Module):
def __init__(self):
super(MultiScaleDiscriminator, self).__init__()
self.discriminators = nn.ModuleList([
DiscriminatorS(),
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
|