Spaces:
Sleeping
Sleeping
Commit
·
777eeb2
1
Parent(s):
184ae9b
Add Modules package (hifigan Decoder)
Browse files- Modules/__init__.py +0 -0
- Modules/hifigan.py +475 -0
- Modules/utils.py +14 -0
Modules/__init__.py
ADDED
|
File without changes
|
Modules/hifigan.py
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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+
import torch.nn as nn
|
| 4 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
| 5 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
| 6 |
+
from .utils import init_weights, get_padding
|
| 7 |
+
|
| 8 |
+
import math
|
| 9 |
+
import random
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
LRELU_SLOPE = 0.1
|
| 13 |
+
|
| 14 |
+
class AdaIN1d(nn.Module):
|
| 15 |
+
def __init__(self, style_dim, num_features):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.norm = nn.InstanceNorm1d(num_features, affine=False)
|
| 18 |
+
self.fc = nn.Linear(style_dim, num_features*2)
|
| 19 |
+
|
| 20 |
+
def forward(self, x, s):
|
| 21 |
+
h = self.fc(s)
|
| 22 |
+
h = h.view(h.size(0), h.size(1), 1)
|
| 23 |
+
gamma, beta = torch.chunk(h, chunks=2, dim=1)
|
| 24 |
+
return (1 + gamma) * self.norm(x) + beta
|
| 25 |
+
|
| 26 |
+
class AdaINResBlock1(torch.nn.Module):
|
| 27 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5), style_dim=64):
|
| 28 |
+
super(AdaINResBlock1, self).__init__()
|
| 29 |
+
self.convs1 = nn.ModuleList([
|
| 30 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
| 31 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
| 32 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
| 33 |
+
padding=get_padding(kernel_size, dilation[1]))),
|
| 34 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
|
| 35 |
+
padding=get_padding(kernel_size, dilation[2])))
|
| 36 |
+
])
|
| 37 |
+
self.convs1.apply(init_weights)
|
| 38 |
+
|
| 39 |
+
self.convs2 = nn.ModuleList([
|
| 40 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
| 41 |
+
padding=get_padding(kernel_size, 1))),
|
| 42 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
| 43 |
+
padding=get_padding(kernel_size, 1))),
|
| 44 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
| 45 |
+
padding=get_padding(kernel_size, 1)))
|
| 46 |
+
])
|
| 47 |
+
self.convs2.apply(init_weights)
|
| 48 |
+
|
| 49 |
+
self.adain1 = nn.ModuleList([
|
| 50 |
+
AdaIN1d(style_dim, channels),
|
| 51 |
+
AdaIN1d(style_dim, channels),
|
| 52 |
+
AdaIN1d(style_dim, channels),
|
| 53 |
+
])
|
| 54 |
+
|
| 55 |
+
self.adain2 = nn.ModuleList([
|
| 56 |
+
AdaIN1d(style_dim, channels),
|
| 57 |
+
AdaIN1d(style_dim, channels),
|
| 58 |
+
AdaIN1d(style_dim, channels),
|
| 59 |
+
])
|
| 60 |
+
|
| 61 |
+
self.alpha1 = nn.ParameterList([nn.Parameter(torch.ones(1, channels, 1)) for i in range(len(self.convs1))])
|
| 62 |
+
self.alpha2 = nn.ParameterList([nn.Parameter(torch.ones(1, channels, 1)) for i in range(len(self.convs2))])
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def forward(self, x, s):
|
| 66 |
+
for c1, c2, n1, n2, a1, a2 in zip(self.convs1, self.convs2, self.adain1, self.adain2, self.alpha1, self.alpha2):
|
| 67 |
+
xt = n1(x, s)
|
| 68 |
+
xt = xt + (1 / a1) * (torch.sin(a1 * xt) ** 2) # Snake1D
|
| 69 |
+
xt = c1(xt)
|
| 70 |
+
xt = n2(xt, s)
|
| 71 |
+
xt = xt + (1 / a2) * (torch.sin(a2 * xt) ** 2) # Snake1D
|
| 72 |
+
xt = c2(xt)
|
| 73 |
+
x = xt + x
|
| 74 |
+
return x
|
| 75 |
+
|
| 76 |
+
def remove_weight_norm(self):
|
| 77 |
+
for l in self.convs1:
|
| 78 |
+
remove_weight_norm(l)
|
| 79 |
+
for l in self.convs2:
|
| 80 |
+
remove_weight_norm(l)
|
| 81 |
+
|
| 82 |
+
class SineGen(torch.nn.Module):
|
| 83 |
+
""" Definition of sine generator
|
| 84 |
+
SineGen(samp_rate, harmonic_num = 0,
|
| 85 |
+
sine_amp = 0.1, noise_std = 0.003,
|
| 86 |
+
voiced_threshold = 0,
|
| 87 |
+
flag_for_pulse=False)
|
| 88 |
+
samp_rate: sampling rate in Hz
|
| 89 |
+
harmonic_num: number of harmonic overtones (default 0)
|
| 90 |
+
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
| 91 |
+
noise_std: std of Gaussian noise (default 0.003)
|
| 92 |
+
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
| 93 |
+
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
| 94 |
+
Note: when flag_for_pulse is True, the first time step of a voiced
|
| 95 |
+
segment is always sin(np.pi) or cos(0)
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
def __init__(self, samp_rate, upsample_scale, harmonic_num=0,
|
| 99 |
+
sine_amp=0.1, noise_std=0.003,
|
| 100 |
+
voiced_threshold=0,
|
| 101 |
+
flag_for_pulse=False):
|
| 102 |
+
super(SineGen, self).__init__()
|
| 103 |
+
self.sine_amp = sine_amp
|
| 104 |
+
self.noise_std = noise_std
|
| 105 |
+
self.harmonic_num = harmonic_num
|
| 106 |
+
self.dim = self.harmonic_num + 1
|
| 107 |
+
self.sampling_rate = samp_rate
|
| 108 |
+
self.voiced_threshold = voiced_threshold
|
| 109 |
+
self.flag_for_pulse = flag_for_pulse
|
| 110 |
+
self.upsample_scale = upsample_scale
|
| 111 |
+
|
| 112 |
+
def _f02uv(self, f0):
|
| 113 |
+
# generate uv signal
|
| 114 |
+
uv = (f0 > self.voiced_threshold).type(torch.float32)
|
| 115 |
+
return uv
|
| 116 |
+
|
| 117 |
+
def _f02sine(self, f0_values):
|
| 118 |
+
""" f0_values: (batchsize, length, dim)
|
| 119 |
+
where dim indicates fundamental tone and overtones
|
| 120 |
+
"""
|
| 121 |
+
# convert to F0 in rad. The interger part n can be ignored
|
| 122 |
+
# because 2 * np.pi * n doesn't affect phase
|
| 123 |
+
rad_values = (f0_values / self.sampling_rate) % 1
|
| 124 |
+
|
| 125 |
+
# initial phase noise (no noise for fundamental component)
|
| 126 |
+
rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
|
| 127 |
+
device=f0_values.device)
|
| 128 |
+
rand_ini[:, 0] = 0
|
| 129 |
+
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
| 130 |
+
|
| 131 |
+
# instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
|
| 132 |
+
if not self.flag_for_pulse:
|
| 133 |
+
# # for normal case
|
| 134 |
+
|
| 135 |
+
# # To prevent torch.cumsum numerical overflow,
|
| 136 |
+
# # it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
|
| 137 |
+
# # Buffer tmp_over_one_idx indicates the time step to add -1.
|
| 138 |
+
# # This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
|
| 139 |
+
# tmp_over_one = torch.cumsum(rad_values, 1) % 1
|
| 140 |
+
# tmp_over_one_idx = (padDiff(tmp_over_one)) < 0
|
| 141 |
+
# cumsum_shift = torch.zeros_like(rad_values)
|
| 142 |
+
# cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
| 143 |
+
|
| 144 |
+
# phase = torch.cumsum(rad_values, dim=1) * 2 * np.pi
|
| 145 |
+
rad_values = torch.nn.functional.interpolate(rad_values.transpose(1, 2),
|
| 146 |
+
scale_factor=1/self.upsample_scale,
|
| 147 |
+
mode="linear").transpose(1, 2)
|
| 148 |
+
|
| 149 |
+
# tmp_over_one = torch.cumsum(rad_values, 1) % 1
|
| 150 |
+
# tmp_over_one_idx = (padDiff(tmp_over_one)) < 0
|
| 151 |
+
# cumsum_shift = torch.zeros_like(rad_values)
|
| 152 |
+
# cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
| 153 |
+
|
| 154 |
+
phase = torch.cumsum(rad_values, dim=1) * 2 * np.pi
|
| 155 |
+
phase = torch.nn.functional.interpolate(phase.transpose(1, 2) * self.upsample_scale,
|
| 156 |
+
scale_factor=self.upsample_scale, mode="linear").transpose(1, 2)
|
| 157 |
+
sines = torch.sin(phase)
|
| 158 |
+
|
| 159 |
+
else:
|
| 160 |
+
# If necessary, make sure that the first time step of every
|
| 161 |
+
# voiced segments is sin(pi) or cos(0)
|
| 162 |
+
# This is used for pulse-train generation
|
| 163 |
+
|
| 164 |
+
# identify the last time step in unvoiced segments
|
| 165 |
+
uv = self._f02uv(f0_values)
|
| 166 |
+
uv_1 = torch.roll(uv, shifts=-1, dims=1)
|
| 167 |
+
uv_1[:, -1, :] = 1
|
| 168 |
+
u_loc = (uv < 1) * (uv_1 > 0)
|
| 169 |
+
|
| 170 |
+
# get the instantanouse phase
|
| 171 |
+
tmp_cumsum = torch.cumsum(rad_values, dim=1)
|
| 172 |
+
# different batch needs to be processed differently
|
| 173 |
+
for idx in range(f0_values.shape[0]):
|
| 174 |
+
temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
|
| 175 |
+
temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
|
| 176 |
+
# stores the accumulation of i.phase within
|
| 177 |
+
# each voiced segments
|
| 178 |
+
tmp_cumsum[idx, :, :] = 0
|
| 179 |
+
tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
|
| 180 |
+
|
| 181 |
+
# rad_values - tmp_cumsum: remove the accumulation of i.phase
|
| 182 |
+
# within the previous voiced segment.
|
| 183 |
+
i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
|
| 184 |
+
|
| 185 |
+
# get the sines
|
| 186 |
+
sines = torch.cos(i_phase * 2 * np.pi)
|
| 187 |
+
return sines
|
| 188 |
+
|
| 189 |
+
def forward(self, f0):
|
| 190 |
+
""" sine_tensor, uv = forward(f0)
|
| 191 |
+
input F0: tensor(batchsize=1, length, dim=1)
|
| 192 |
+
f0 for unvoiced steps should be 0
|
| 193 |
+
output sine_tensor: tensor(batchsize=1, length, dim)
|
| 194 |
+
output uv: tensor(batchsize=1, length, 1)
|
| 195 |
+
"""
|
| 196 |
+
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,
|
| 197 |
+
device=f0.device)
|
| 198 |
+
# fundamental component
|
| 199 |
+
fn = torch.multiply(f0, torch.FloatTensor([[range(1, self.harmonic_num + 2)]]).to(f0.device))
|
| 200 |
+
|
| 201 |
+
# generate sine waveforms
|
| 202 |
+
sine_waves = self._f02sine(fn) * self.sine_amp
|
| 203 |
+
|
| 204 |
+
# generate uv signal
|
| 205 |
+
# uv = torch.ones(f0.shape)
|
| 206 |
+
# uv = uv * (f0 > self.voiced_threshold)
|
| 207 |
+
uv = self._f02uv(f0)
|
| 208 |
+
|
| 209 |
+
# noise: for unvoiced should be similar to sine_amp
|
| 210 |
+
# std = self.sine_amp/3 -> max value ~ self.sine_amp
|
| 211 |
+
# . for voiced regions is self.noise_std
|
| 212 |
+
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
| 213 |
+
noise = noise_amp * torch.randn_like(sine_waves)
|
| 214 |
+
|
| 215 |
+
# first: set the unvoiced part to 0 by uv
|
| 216 |
+
# then: additive noise
|
| 217 |
+
sine_waves = sine_waves * uv + noise
|
| 218 |
+
return sine_waves, uv, noise
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
class SourceModuleHnNSF(torch.nn.Module):
|
| 222 |
+
""" SourceModule for hn-nsf
|
| 223 |
+
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
| 224 |
+
add_noise_std=0.003, voiced_threshod=0)
|
| 225 |
+
sampling_rate: sampling_rate in Hz
|
| 226 |
+
harmonic_num: number of harmonic above F0 (default: 0)
|
| 227 |
+
sine_amp: amplitude of sine source signal (default: 0.1)
|
| 228 |
+
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
| 229 |
+
note that amplitude of noise in unvoiced is decided
|
| 230 |
+
by sine_amp
|
| 231 |
+
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
| 232 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
| 233 |
+
F0_sampled (batchsize, length, 1)
|
| 234 |
+
Sine_source (batchsize, length, 1)
|
| 235 |
+
noise_source (batchsize, length 1)
|
| 236 |
+
uv (batchsize, length, 1)
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
def __init__(self, sampling_rate, upsample_scale, harmonic_num=0, sine_amp=0.1,
|
| 240 |
+
add_noise_std=0.003, voiced_threshod=0):
|
| 241 |
+
super(SourceModuleHnNSF, self).__init__()
|
| 242 |
+
|
| 243 |
+
self.sine_amp = sine_amp
|
| 244 |
+
self.noise_std = add_noise_std
|
| 245 |
+
|
| 246 |
+
# to produce sine waveforms
|
| 247 |
+
self.l_sin_gen = SineGen(sampling_rate, upsample_scale, harmonic_num,
|
| 248 |
+
sine_amp, add_noise_std, voiced_threshod)
|
| 249 |
+
|
| 250 |
+
# to merge source harmonics into a single excitation
|
| 251 |
+
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
| 252 |
+
self.l_tanh = torch.nn.Tanh()
|
| 253 |
+
|
| 254 |
+
def forward(self, x):
|
| 255 |
+
"""
|
| 256 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
| 257 |
+
F0_sampled (batchsize, length, 1)
|
| 258 |
+
Sine_source (batchsize, length, 1)
|
| 259 |
+
noise_source (batchsize, length 1)
|
| 260 |
+
"""
|
| 261 |
+
# source for harmonic branch
|
| 262 |
+
with torch.no_grad():
|
| 263 |
+
sine_wavs, uv, _ = self.l_sin_gen(x)
|
| 264 |
+
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
| 265 |
+
|
| 266 |
+
# source for noise branch, in the same shape as uv
|
| 267 |
+
noise = torch.randn_like(uv) * self.sine_amp / 3
|
| 268 |
+
return sine_merge, noise, uv
|
| 269 |
+
def padDiff(x):
|
| 270 |
+
return F.pad(F.pad(x, (0,0,-1,1), 'constant', 0) - x, (0,0,0,-1), 'constant', 0)
|
| 271 |
+
|
| 272 |
+
class Generator(torch.nn.Module):
|
| 273 |
+
def __init__(self, style_dim, resblock_kernel_sizes, upsample_rates, upsample_initial_channel, resblock_dilation_sizes, upsample_kernel_sizes):
|
| 274 |
+
super(Generator, self).__init__()
|
| 275 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
| 276 |
+
self.num_upsamples = len(upsample_rates)
|
| 277 |
+
resblock = AdaINResBlock1
|
| 278 |
+
|
| 279 |
+
self.m_source = SourceModuleHnNSF(
|
| 280 |
+
sampling_rate=24000,
|
| 281 |
+
upsample_scale=np.prod(upsample_rates),
|
| 282 |
+
harmonic_num=8, voiced_threshod=10)
|
| 283 |
+
|
| 284 |
+
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(upsample_rates))
|
| 285 |
+
self.noise_convs = nn.ModuleList()
|
| 286 |
+
self.ups = nn.ModuleList()
|
| 287 |
+
self.noise_res = nn.ModuleList()
|
| 288 |
+
|
| 289 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
| 290 |
+
c_cur = upsample_initial_channel // (2 ** (i + 1))
|
| 291 |
+
|
| 292 |
+
self.ups.append(weight_norm(ConvTranspose1d(upsample_initial_channel//(2**i),
|
| 293 |
+
upsample_initial_channel//(2**(i+1)),
|
| 294 |
+
k, u, padding=(u//2 + u%2), output_padding=u%2)))
|
| 295 |
+
|
| 296 |
+
if i + 1 < len(upsample_rates): #
|
| 297 |
+
stride_f0 = np.prod(upsample_rates[i + 1:])
|
| 298 |
+
self.noise_convs.append(Conv1d(
|
| 299 |
+
1, c_cur, kernel_size=stride_f0 * 2, stride=stride_f0, padding=(stride_f0+1) // 2))
|
| 300 |
+
self.noise_res.append(resblock(c_cur, 7, [1,3,5], style_dim))
|
| 301 |
+
else:
|
| 302 |
+
self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
|
| 303 |
+
self.noise_res.append(resblock(c_cur, 11, [1,3,5], style_dim))
|
| 304 |
+
|
| 305 |
+
self.resblocks = nn.ModuleList()
|
| 306 |
+
|
| 307 |
+
self.alphas = nn.ParameterList()
|
| 308 |
+
self.alphas.append(nn.Parameter(torch.ones(1, upsample_initial_channel, 1)))
|
| 309 |
+
|
| 310 |
+
for i in range(len(self.ups)):
|
| 311 |
+
ch = upsample_initial_channel//(2**(i+1))
|
| 312 |
+
self.alphas.append(nn.Parameter(torch.ones(1, ch, 1)))
|
| 313 |
+
|
| 314 |
+
for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)):
|
| 315 |
+
self.resblocks.append(resblock(ch, k, d, style_dim))
|
| 316 |
+
|
| 317 |
+
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
|
| 318 |
+
self.ups.apply(init_weights)
|
| 319 |
+
self.conv_post.apply(init_weights)
|
| 320 |
+
|
| 321 |
+
def forward(self, x, s, f0):
|
| 322 |
+
|
| 323 |
+
f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2) # bs,n,t
|
| 324 |
+
|
| 325 |
+
har_source, noi_source, uv = self.m_source(f0)
|
| 326 |
+
har_source = har_source.transpose(1, 2)
|
| 327 |
+
|
| 328 |
+
for i in range(self.num_upsamples):
|
| 329 |
+
x = x + (1 / self.alphas[i]) * (torch.sin(self.alphas[i] * x) ** 2)
|
| 330 |
+
x_source = self.noise_convs[i](har_source)
|
| 331 |
+
x_source = self.noise_res[i](x_source, s)
|
| 332 |
+
|
| 333 |
+
x = self.ups[i](x)
|
| 334 |
+
x = x + x_source
|
| 335 |
+
|
| 336 |
+
xs = None
|
| 337 |
+
for j in range(self.num_kernels):
|
| 338 |
+
if xs is None:
|
| 339 |
+
xs = self.resblocks[i*self.num_kernels+j](x, s)
|
| 340 |
+
else:
|
| 341 |
+
xs += self.resblocks[i*self.num_kernels+j](x, s)
|
| 342 |
+
x = xs / self.num_kernels
|
| 343 |
+
x = x + (1 / self.alphas[i+1]) * (torch.sin(self.alphas[i+1] * x) ** 2)
|
| 344 |
+
x = self.conv_post(x)
|
| 345 |
+
x = torch.tanh(x)
|
| 346 |
+
|
| 347 |
+
return x
|
| 348 |
+
|
| 349 |
+
def remove_weight_norm(self):
|
| 350 |
+
print('Removing weight norm...')
|
| 351 |
+
for l in self.ups:
|
| 352 |
+
remove_weight_norm(l)
|
| 353 |
+
for l in self.resblocks:
|
| 354 |
+
l.remove_weight_norm()
|
| 355 |
+
remove_weight_norm(self.conv_pre)
|
| 356 |
+
remove_weight_norm(self.conv_post)
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
class AdainResBlk1d(nn.Module):
|
| 360 |
+
def __init__(self, dim_in, dim_out, style_dim=64, actv=nn.LeakyReLU(0.2),
|
| 361 |
+
upsample='none', dropout_p=0.0):
|
| 362 |
+
super().__init__()
|
| 363 |
+
self.actv = actv
|
| 364 |
+
self.upsample_type = upsample
|
| 365 |
+
self.upsample = UpSample1d(upsample)
|
| 366 |
+
self.learned_sc = dim_in != dim_out
|
| 367 |
+
self._build_weights(dim_in, dim_out, style_dim)
|
| 368 |
+
self.dropout = nn.Dropout(dropout_p)
|
| 369 |
+
|
| 370 |
+
if upsample == 'none':
|
| 371 |
+
self.pool = nn.Identity()
|
| 372 |
+
else:
|
| 373 |
+
self.pool = weight_norm(nn.ConvTranspose1d(dim_in, dim_in, kernel_size=3, stride=2, groups=dim_in, padding=1, output_padding=1))
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
def _build_weights(self, dim_in, dim_out, style_dim):
|
| 377 |
+
self.conv1 = weight_norm(nn.Conv1d(dim_in, dim_out, 3, 1, 1))
|
| 378 |
+
self.conv2 = weight_norm(nn.Conv1d(dim_out, dim_out, 3, 1, 1))
|
| 379 |
+
self.norm1 = AdaIN1d(style_dim, dim_in)
|
| 380 |
+
self.norm2 = AdaIN1d(style_dim, dim_out)
|
| 381 |
+
if self.learned_sc:
|
| 382 |
+
self.conv1x1 = weight_norm(nn.Conv1d(dim_in, dim_out, 1, 1, 0, bias=False))
|
| 383 |
+
|
| 384 |
+
def _shortcut(self, x):
|
| 385 |
+
x = self.upsample(x)
|
| 386 |
+
if self.learned_sc:
|
| 387 |
+
x = self.conv1x1(x)
|
| 388 |
+
return x
|
| 389 |
+
|
| 390 |
+
def _residual(self, x, s):
|
| 391 |
+
x = self.norm1(x, s)
|
| 392 |
+
x = self.actv(x)
|
| 393 |
+
x = self.pool(x)
|
| 394 |
+
x = self.conv1(self.dropout(x))
|
| 395 |
+
x = self.norm2(x, s)
|
| 396 |
+
x = self.actv(x)
|
| 397 |
+
x = self.conv2(self.dropout(x))
|
| 398 |
+
return x
|
| 399 |
+
|
| 400 |
+
def forward(self, x, s):
|
| 401 |
+
out = self._residual(x, s)
|
| 402 |
+
out = (out + self._shortcut(x)) / math.sqrt(2)
|
| 403 |
+
return out
|
| 404 |
+
|
| 405 |
+
class UpSample1d(nn.Module):
|
| 406 |
+
def __init__(self, layer_type):
|
| 407 |
+
super().__init__()
|
| 408 |
+
self.layer_type = layer_type
|
| 409 |
+
|
| 410 |
+
def forward(self, x):
|
| 411 |
+
if self.layer_type == 'none':
|
| 412 |
+
return x
|
| 413 |
+
else:
|
| 414 |
+
return F.interpolate(x, scale_factor=2, mode='nearest')
|
| 415 |
+
|
| 416 |
+
class Decoder(nn.Module):
|
| 417 |
+
def __init__(self, dim_in=512, F0_channel=512, style_dim=64, dim_out=80,
|
| 418 |
+
resblock_kernel_sizes = [3,7,11],
|
| 419 |
+
upsample_rates = [10,5,3,2],
|
| 420 |
+
upsample_initial_channel=512,
|
| 421 |
+
resblock_dilation_sizes=[[1,3,5], [1,3,5], [1,3,5]],
|
| 422 |
+
upsample_kernel_sizes=[20,10,6,4]):
|
| 423 |
+
super().__init__()
|
| 424 |
+
|
| 425 |
+
self.decode = nn.ModuleList()
|
| 426 |
+
|
| 427 |
+
self.encode = AdainResBlk1d(dim_in + 2, 1024, style_dim)
|
| 428 |
+
|
| 429 |
+
self.decode.append(AdainResBlk1d(1024 + 2 + 64, 1024, style_dim))
|
| 430 |
+
self.decode.append(AdainResBlk1d(1024 + 2 + 64, 1024, style_dim))
|
| 431 |
+
self.decode.append(AdainResBlk1d(1024 + 2 + 64, 1024, style_dim))
|
| 432 |
+
self.decode.append(AdainResBlk1d(1024 + 2 + 64, 512, style_dim, upsample=True))
|
| 433 |
+
|
| 434 |
+
self.F0_conv = weight_norm(nn.Conv1d(1, 1, kernel_size=3, stride=2, groups=1, padding=1))
|
| 435 |
+
|
| 436 |
+
self.N_conv = weight_norm(nn.Conv1d(1, 1, kernel_size=3, stride=2, groups=1, padding=1))
|
| 437 |
+
|
| 438 |
+
self.asr_res = nn.Sequential(
|
| 439 |
+
weight_norm(nn.Conv1d(512, 64, kernel_size=1)),
|
| 440 |
+
)
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
self.generator = Generator(style_dim, resblock_kernel_sizes, upsample_rates, upsample_initial_channel, resblock_dilation_sizes, upsample_kernel_sizes)
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
def forward(self, asr, F0_curve, N, s):
|
| 447 |
+
if self.training:
|
| 448 |
+
downlist = [0, 3, 7]
|
| 449 |
+
F0_down = downlist[random.randint(0, 2)]
|
| 450 |
+
downlist = [0, 3, 7, 15]
|
| 451 |
+
N_down = downlist[random.randint(0, 3)]
|
| 452 |
+
if F0_down:
|
| 453 |
+
F0_curve = nn.functional.conv1d(F0_curve.unsqueeze(1), torch.ones(1, 1, F0_down).to(asr.device), padding=F0_down//2).squeeze(1) / F0_down
|
| 454 |
+
if N_down:
|
| 455 |
+
N = nn.functional.conv1d(N.unsqueeze(1), torch.ones(1, 1, N_down).to(asr.device), padding=N_down//2).squeeze(1) / N_down
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
F0 = self.F0_conv(F0_curve.unsqueeze(1))
|
| 459 |
+
N = self.N_conv(N.unsqueeze(1))
|
| 460 |
+
|
| 461 |
+
x = torch.cat([asr, F0, N], axis=1)
|
| 462 |
+
x = self.encode(x, s)
|
| 463 |
+
|
| 464 |
+
asr_res = self.asr_res(asr)
|
| 465 |
+
|
| 466 |
+
res = True
|
| 467 |
+
for block in self.decode:
|
| 468 |
+
if res:
|
| 469 |
+
x = torch.cat([x, asr_res, F0, N], axis=1)
|
| 470 |
+
x = block(x, s)
|
| 471 |
+
if block.upsample_type != "none":
|
| 472 |
+
res = False
|
| 473 |
+
|
| 474 |
+
x = self.generator(x, s, F0_curve)
|
| 475 |
+
return x
|
Modules/utils.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def init_weights(m, mean=0.0, std=0.01):
|
| 2 |
+
classname = m.__class__.__name__
|
| 3 |
+
if classname.find("Conv") != -1:
|
| 4 |
+
m.weight.data.normal_(mean, std)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def apply_weight_norm(m):
|
| 8 |
+
classname = m.__class__.__name__
|
| 9 |
+
if classname.find("Conv") != -1:
|
| 10 |
+
weight_norm(m)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_padding(kernel_size, dilation=1):
|
| 14 |
+
return int((kernel_size*dilation - dilation)/2)
|