Delete src/miocodec/pupu/models/vocoders/gan/generator/pupuvocoder.py with huggingface_hub
Browse files
src/miocodec/pupu/models/vocoders/gan/generator/pupuvocoder.py
DELETED
|
@@ -1,228 +0,0 @@
|
|
| 1 |
-
import math
|
| 2 |
-
|
| 3 |
-
import julius
|
| 4 |
-
import torch
|
| 5 |
-
import torch.nn as nn
|
| 6 |
-
from torch.amp import autocast
|
| 7 |
-
from torch.nn import Conv1d
|
| 8 |
-
from torch.nn.utils import remove_weight_norm, weight_norm
|
| 9 |
-
|
| 10 |
-
from miocodec.pupu.modules.activation_functions import ADAASnakeBeta
|
| 11 |
-
from miocodec.pupu.modules.anti_aliasing import Activation1d
|
| 12 |
-
from miocodec.pupu.modules.vocoder_blocks import get_padding, init_weights
|
| 13 |
-
|
| 14 |
-
LRELU_SLOPE = 0.1
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
class ResBlock1(nn.Module):
|
| 18 |
-
def __init__(self, cfg, channels, kernel_size=3, dilation=(1, 3, 5)):
|
| 19 |
-
super(ResBlock1, self).__init__()
|
| 20 |
-
self.cfg = cfg
|
| 21 |
-
|
| 22 |
-
self.convs1 = nn.ModuleList(
|
| 23 |
-
[
|
| 24 |
-
weight_norm(
|
| 25 |
-
Conv1d(
|
| 26 |
-
channels,
|
| 27 |
-
channels,
|
| 28 |
-
kernel_size,
|
| 29 |
-
1,
|
| 30 |
-
dilation=dilation[0],
|
| 31 |
-
padding=get_padding(kernel_size, dilation[0]),
|
| 32 |
-
)
|
| 33 |
-
),
|
| 34 |
-
weight_norm(
|
| 35 |
-
Conv1d(
|
| 36 |
-
channels,
|
| 37 |
-
channels,
|
| 38 |
-
kernel_size,
|
| 39 |
-
1,
|
| 40 |
-
dilation=dilation[1],
|
| 41 |
-
padding=get_padding(kernel_size, dilation[1]),
|
| 42 |
-
)
|
| 43 |
-
),
|
| 44 |
-
weight_norm(
|
| 45 |
-
Conv1d(
|
| 46 |
-
channels,
|
| 47 |
-
channels,
|
| 48 |
-
kernel_size,
|
| 49 |
-
1,
|
| 50 |
-
dilation=dilation[2],
|
| 51 |
-
padding=get_padding(kernel_size, dilation[2]),
|
| 52 |
-
)
|
| 53 |
-
),
|
| 54 |
-
]
|
| 55 |
-
)
|
| 56 |
-
self.convs1.apply(init_weights)
|
| 57 |
-
|
| 58 |
-
self.convs2 = nn.ModuleList(
|
| 59 |
-
[
|
| 60 |
-
weight_norm(
|
| 61 |
-
Conv1d(
|
| 62 |
-
channels,
|
| 63 |
-
channels,
|
| 64 |
-
kernel_size,
|
| 65 |
-
1,
|
| 66 |
-
dilation=1,
|
| 67 |
-
padding=get_padding(kernel_size, 1),
|
| 68 |
-
)
|
| 69 |
-
),
|
| 70 |
-
weight_norm(
|
| 71 |
-
Conv1d(
|
| 72 |
-
channels,
|
| 73 |
-
channels,
|
| 74 |
-
kernel_size,
|
| 75 |
-
1,
|
| 76 |
-
dilation=1,
|
| 77 |
-
padding=get_padding(kernel_size, 1),
|
| 78 |
-
)
|
| 79 |
-
),
|
| 80 |
-
weight_norm(
|
| 81 |
-
Conv1d(
|
| 82 |
-
channels,
|
| 83 |
-
channels,
|
| 84 |
-
kernel_size,
|
| 85 |
-
1,
|
| 86 |
-
dilation=1,
|
| 87 |
-
padding=get_padding(kernel_size, 1),
|
| 88 |
-
)
|
| 89 |
-
),
|
| 90 |
-
]
|
| 91 |
-
)
|
| 92 |
-
self.convs2.apply(init_weights)
|
| 93 |
-
|
| 94 |
-
self.num_layers = len(self.convs1) + len(self.convs2)
|
| 95 |
-
self.activations = nn.ModuleList(
|
| 96 |
-
[
|
| 97 |
-
Activation1d(activation=ADAASnakeBeta(channels))
|
| 98 |
-
for _ in range(self.num_layers)
|
| 99 |
-
]
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
def forward(self, x):
|
| 103 |
-
acts1, acts2 = self.activations[::2], self.activations[1::2]
|
| 104 |
-
for c1, c2, a1, a2 in zip(self.convs1, self.convs2, acts1, acts2):
|
| 105 |
-
xt = a1(x)
|
| 106 |
-
xt = c1(xt)
|
| 107 |
-
xt = a2(xt)
|
| 108 |
-
xt = c2(xt)
|
| 109 |
-
x = xt + x
|
| 110 |
-
return x
|
| 111 |
-
|
| 112 |
-
def remove_weight_norm(self):
|
| 113 |
-
for l in self.convs1:
|
| 114 |
-
remove_weight_norm(l)
|
| 115 |
-
for l in self.convs2:
|
| 116 |
-
remove_weight_norm(l)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
class ResampleUpsampler(nn.Module):
|
| 120 |
-
def __init__(self, n_mel, upsample_rate, c_prev, c_cur):
|
| 121 |
-
super(ResampleUpsampler, self).__init__()
|
| 122 |
-
self.scale_factor = upsample_rate
|
| 123 |
-
self.convolution_after = weight_norm(Conv1d(c_prev, c_cur, 1, 1))
|
| 124 |
-
self.convolution_noise = weight_norm(Conv1d(n_mel, c_prev, 7, 1, padding=3))
|
| 125 |
-
|
| 126 |
-
def forward(self, x, x0, upps):
|
| 127 |
-
B, C, T = x0.shape
|
| 128 |
-
y0 = torch.zeros(B, C, T * upps, device=x0.device)
|
| 129 |
-
y0[:, :, ::upps] = x0
|
| 130 |
-
y0 = self.convolution_noise(y0)
|
| 131 |
-
with autocast(y0.device.type, enabled=False):
|
| 132 |
-
y0 = julius.highpass_filter(y0.float(), 0.5 / self.scale_factor)
|
| 133 |
-
|
| 134 |
-
B, C, T = x.shape
|
| 135 |
-
y = torch.zeros(B, C, T * self.scale_factor, device=x.device)
|
| 136 |
-
y[:, :, :: self.scale_factor] = x
|
| 137 |
-
with autocast(y.device.type, enabled=False):
|
| 138 |
-
y = julius.lowpass_filter(y.float(), 0.5 / self.scale_factor)
|
| 139 |
-
|
| 140 |
-
y0 = y0.to(x0.dtype)
|
| 141 |
-
y = y.to(x.dtype)
|
| 142 |
-
y = y + y0
|
| 143 |
-
y = self.convolution_after(y)
|
| 144 |
-
return y
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
class PupuVocoder(nn.Module):
|
| 148 |
-
def __init__(self, cfg):
|
| 149 |
-
super(PupuVocoder, self).__init__()
|
| 150 |
-
|
| 151 |
-
self.cfg = cfg
|
| 152 |
-
self.num_kernels = len(self.cfg.model.pupuvocoder.resblock_kernel_sizes)
|
| 153 |
-
self.num_upsamples = len(self.cfg.model.pupuvocoder.upsample_rates)
|
| 154 |
-
self.conv_pre = weight_norm(
|
| 155 |
-
Conv1d(
|
| 156 |
-
self.cfg.preprocess.n_mel,
|
| 157 |
-
self.cfg.model.pupuvocoder.upsample_initial_channel,
|
| 158 |
-
7,
|
| 159 |
-
1,
|
| 160 |
-
padding=3,
|
| 161 |
-
)
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
-
resblock = ResBlock1
|
| 165 |
-
|
| 166 |
-
self.ups = nn.ModuleList()
|
| 167 |
-
for i, (u, k) in enumerate(
|
| 168 |
-
zip(
|
| 169 |
-
self.cfg.model.pupuvocoder.upsample_rates,
|
| 170 |
-
self.cfg.model.pupuvocoder.upsample_kernel_sizes,
|
| 171 |
-
)
|
| 172 |
-
):
|
| 173 |
-
c_prev = self.cfg.model.pupuvocoder.upsample_initial_channel // (2**i)
|
| 174 |
-
c_cur = self.cfg.model.pupuvocoder.upsample_initial_channel // (
|
| 175 |
-
2 ** (i + 1)
|
| 176 |
-
)
|
| 177 |
-
self.ups.append(
|
| 178 |
-
ResampleUpsampler(
|
| 179 |
-
self.cfg.model.pupuvocoder.upsample_initial_channel,
|
| 180 |
-
u,
|
| 181 |
-
c_prev,
|
| 182 |
-
c_cur,
|
| 183 |
-
)
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
self.resblocks = nn.ModuleList()
|
| 187 |
-
ch = self.cfg.model.pupuvocoder.upsample_initial_channel
|
| 188 |
-
for i in range(len(self.ups)):
|
| 189 |
-
ch //= 2
|
| 190 |
-
for j, (k, d) in enumerate(
|
| 191 |
-
zip(
|
| 192 |
-
self.cfg.model.pupuvocoder.resblock_kernel_sizes,
|
| 193 |
-
self.cfg.model.pupuvocoder.resblock_dilation_sizes,
|
| 194 |
-
)
|
| 195 |
-
):
|
| 196 |
-
self.resblocks.append(resblock(cfg, ch, k, d))
|
| 197 |
-
|
| 198 |
-
self.activation_post = Activation1d(activation=ADAASnakeBeta(c_cur))
|
| 199 |
-
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3, bias=False))
|
| 200 |
-
|
| 201 |
-
self.ups.apply(init_weights)
|
| 202 |
-
self.conv_post.apply(init_weights)
|
| 203 |
-
self.upps = [
|
| 204 |
-
math.prod(self.cfg.model.pupuvocoder.upsample_rates[:-4]),
|
| 205 |
-
math.prod(self.cfg.model.pupuvocoder.upsample_rates[:-3]),
|
| 206 |
-
math.prod(self.cfg.model.pupuvocoder.upsample_rates[:-2]),
|
| 207 |
-
math.prod(self.cfg.model.pupuvocoder.upsample_rates[:-1]),
|
| 208 |
-
math.prod(self.cfg.model.pupuvocoder.upsample_rates),
|
| 209 |
-
]
|
| 210 |
-
|
| 211 |
-
def forward(self, x):
|
| 212 |
-
x = self.conv_pre(x)
|
| 213 |
-
x0 = x
|
| 214 |
-
for i in range(self.num_upsamples):
|
| 215 |
-
x = self.ups[i](x, x0, self.upps[i])
|
| 216 |
-
xs = None
|
| 217 |
-
for j in range(self.num_kernels):
|
| 218 |
-
if xs is None:
|
| 219 |
-
xs = self.resblocks[i * self.num_kernels + j](x)
|
| 220 |
-
else:
|
| 221 |
-
xs = xs + self.resblocks[i * self.num_kernels + j](x)
|
| 222 |
-
x = xs / self.num_kernels
|
| 223 |
-
|
| 224 |
-
x = self.activation_post(x)
|
| 225 |
-
x = self.conv_post(x)
|
| 226 |
-
x = torch.clamp(x, min=-1.0, max=1.0)
|
| 227 |
-
|
| 228 |
-
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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