Kangarroar commited on
Commit
ab04d74
·
1 Parent(s): f944675

Delete modules

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. modules/commons/__pycache__/common_layers.cpython-38.pyc +0 -0
  2. modules/commons/__pycache__/espnet_positional_embedding.cpython-38.pyc +0 -0
  3. modules/commons/__pycache__/ssim.cpython-38.pyc +0 -0
  4. modules/commons/common_layers.py +0 -671
  5. modules/commons/espnet_positional_embedding.py +0 -113
  6. modules/commons/ssim.py +0 -391
  7. modules/fastspeech/__pycache__/fs2.cpython-38.pyc +0 -0
  8. modules/fastspeech/__pycache__/pe.cpython-38.pyc +0 -0
  9. modules/fastspeech/__pycache__/tts_modules.cpython-38.pyc +0 -0
  10. modules/fastspeech/fs2.py +0 -255
  11. modules/fastspeech/pe.py +0 -149
  12. modules/fastspeech/tts_modules.py +0 -364
  13. modules/hifigan/__pycache__/hifigan.cpython-38.pyc +0 -0
  14. modules/hifigan/hifigan.py +0 -365
  15. modules/hifigan/mel_utils.py +0 -80
  16. modules/nsf_hifigan/__pycache__/env.cpython-38.pyc +0 -0
  17. modules/nsf_hifigan/__pycache__/models.cpython-38.pyc +0 -0
  18. modules/nsf_hifigan/__pycache__/nvSTFT.cpython-38.pyc +0 -0
  19. modules/nsf_hifigan/__pycache__/utils.cpython-38.pyc +0 -0
  20. modules/nsf_hifigan/env.py +0 -15
  21. modules/nsf_hifigan/models.py +0 -549
  22. modules/nsf_hifigan/nvSTFT.py +0 -111
  23. modules/nsf_hifigan/utils.py +0 -67
  24. modules/parallel_wavegan/__init__.py +0 -0
  25. modules/parallel_wavegan/__pycache__/__init__.cpython-38.pyc +0 -0
  26. modules/parallel_wavegan/layers/__init__.py +0 -5
  27. modules/parallel_wavegan/layers/__pycache__/__init__.cpython-38.pyc +0 -0
  28. modules/parallel_wavegan/layers/__pycache__/causal_conv.cpython-38.pyc +0 -0
  29. modules/parallel_wavegan/layers/__pycache__/pqmf.cpython-38.pyc +0 -0
  30. modules/parallel_wavegan/layers/__pycache__/residual_block.cpython-38.pyc +0 -0
  31. modules/parallel_wavegan/layers/__pycache__/residual_stack.cpython-38.pyc +0 -0
  32. modules/parallel_wavegan/layers/__pycache__/upsample.cpython-38.pyc +0 -0
  33. modules/parallel_wavegan/layers/causal_conv.py +0 -56
  34. modules/parallel_wavegan/layers/pqmf.py +0 -129
  35. modules/parallel_wavegan/layers/residual_block.py +0 -129
  36. modules/parallel_wavegan/layers/residual_stack.py +0 -75
  37. modules/parallel_wavegan/layers/tf_layers.py +0 -129
  38. modules/parallel_wavegan/layers/upsample.py +0 -183
  39. modules/parallel_wavegan/losses/__init__.py +0 -1
  40. modules/parallel_wavegan/losses/stft_loss.py +0 -153
  41. modules/parallel_wavegan/models/__init__.py +0 -2
  42. modules/parallel_wavegan/models/__pycache__/__init__.cpython-38.pyc +0 -0
  43. modules/parallel_wavegan/models/__pycache__/melgan.cpython-38.pyc +0 -0
  44. modules/parallel_wavegan/models/__pycache__/parallel_wavegan.cpython-38.pyc +0 -0
  45. modules/parallel_wavegan/models/__pycache__/source.cpython-38.pyc +0 -0
  46. modules/parallel_wavegan/models/melgan.py +0 -427
  47. modules/parallel_wavegan/models/parallel_wavegan.py +0 -434
  48. modules/parallel_wavegan/models/source.py +0 -538
  49. modules/parallel_wavegan/optimizers/__init__.py +0 -2
  50. modules/parallel_wavegan/optimizers/radam.py +0 -91
modules/commons/__pycache__/common_layers.cpython-38.pyc DELETED
Binary file (18.8 kB)
 
modules/commons/__pycache__/espnet_positional_embedding.cpython-38.pyc DELETED
Binary file (4.44 kB)
 
modules/commons/__pycache__/ssim.cpython-38.pyc DELETED
Binary file (2.69 kB)
 
modules/commons/common_layers.py DELETED
@@ -1,671 +0,0 @@
1
- import math
2
- import torch
3
- from torch import nn
4
- from torch.nn import Parameter
5
- import torch.onnx.operators
6
- import torch.nn.functional as F
7
- import utils
8
-
9
-
10
- class Reshape(nn.Module):
11
- def __init__(self, *args):
12
- super(Reshape, self).__init__()
13
- self.shape = args
14
-
15
- def forward(self, x):
16
- return x.view(self.shape)
17
-
18
-
19
- class Permute(nn.Module):
20
- def __init__(self, *args):
21
- super(Permute, self).__init__()
22
- self.args = args
23
-
24
- def forward(self, x):
25
- return x.permute(self.args)
26
-
27
-
28
- class LinearNorm(torch.nn.Module):
29
- def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
30
- super(LinearNorm, self).__init__()
31
- self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias)
32
-
33
- torch.nn.init.xavier_uniform_(
34
- self.linear_layer.weight,
35
- gain=torch.nn.init.calculate_gain(w_init_gain))
36
-
37
- def forward(self, x):
38
- return self.linear_layer(x)
39
-
40
-
41
- class ConvNorm(torch.nn.Module):
42
- def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
43
- padding=None, dilation=1, bias=True, w_init_gain='linear'):
44
- super(ConvNorm, self).__init__()
45
- if padding is None:
46
- assert (kernel_size % 2 == 1)
47
- padding = int(dilation * (kernel_size - 1) / 2)
48
-
49
- self.conv = torch.nn.Conv1d(in_channels, out_channels,
50
- kernel_size=kernel_size, stride=stride,
51
- padding=padding, dilation=dilation,
52
- bias=bias)
53
-
54
- torch.nn.init.xavier_uniform_(
55
- self.conv.weight, gain=torch.nn.init.calculate_gain(w_init_gain))
56
-
57
- def forward(self, signal):
58
- conv_signal = self.conv(signal)
59
- return conv_signal
60
-
61
-
62
- def Embedding(num_embeddings, embedding_dim, padding_idx=None):
63
- m = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
64
- nn.init.normal_(m.weight, mean=0, std=embedding_dim ** -0.5)
65
- if padding_idx is not None:
66
- nn.init.constant_(m.weight[padding_idx], 0)
67
- return m
68
-
69
-
70
- def LayerNorm(normalized_shape, eps=1e-5, elementwise_affine=True, export=False):
71
- if not export and torch.cuda.is_available():
72
- try:
73
- from apex.normalization import FusedLayerNorm
74
- return FusedLayerNorm(normalized_shape, eps, elementwise_affine)
75
- except ImportError:
76
- pass
77
- return torch.nn.LayerNorm(normalized_shape, eps, elementwise_affine)
78
-
79
-
80
- def Linear(in_features, out_features, bias=True):
81
- m = nn.Linear(in_features, out_features, bias)
82
- nn.init.xavier_uniform_(m.weight)
83
- if bias:
84
- nn.init.constant_(m.bias, 0.)
85
- return m
86
-
87
-
88
- class SinusoidalPositionalEmbedding(nn.Module):
89
- """This module produces sinusoidal positional embeddings of any length.
90
-
91
- Padding symbols are ignored.
92
- """
93
-
94
- def __init__(self, embedding_dim, padding_idx, init_size=1024):
95
- super().__init__()
96
- self.embedding_dim = embedding_dim
97
- self.padding_idx = padding_idx
98
- self.weights = SinusoidalPositionalEmbedding.get_embedding(
99
- init_size,
100
- embedding_dim,
101
- padding_idx,
102
- )
103
- self.register_buffer('_float_tensor', torch.FloatTensor(1))
104
-
105
- @staticmethod
106
- def get_embedding(num_embeddings, embedding_dim, padding_idx=None):
107
- """Build sinusoidal embeddings.
108
-
109
- This matches the implementation in tensor2tensor, but differs slightly
110
- from the description in Section 3.5 of "Attention Is All You Need".
111
- """
112
- half_dim = embedding_dim // 2
113
- emb = math.log(10000) / (half_dim - 1)
114
- emb = torch.exp(torch.arange(half_dim, dtype=torch.float) * -emb)
115
- emb = torch.arange(num_embeddings, dtype=torch.float).unsqueeze(1) * emb.unsqueeze(0)
116
- emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1).view(num_embeddings, -1)
117
- if embedding_dim % 2 == 1:
118
- # zero pad
119
- emb = torch.cat([emb, torch.zeros(num_embeddings, 1)], dim=1)
120
- if padding_idx is not None:
121
- emb[padding_idx, :] = 0
122
- return emb
123
-
124
- def forward(self, input, incremental_state=None, timestep=None, positions=None, **kwargs):
125
- """Input is expected to be of size [bsz x seqlen]."""
126
- bsz, seq_len = input.shape[:2]
127
- max_pos = self.padding_idx + 1 + seq_len
128
- if self.weights is None or max_pos > self.weights.size(0):
129
- # recompute/expand embeddings if needed
130
- self.weights = SinusoidalPositionalEmbedding.get_embedding(
131
- max_pos,
132
- self.embedding_dim,
133
- self.padding_idx,
134
- )
135
- self.weights = self.weights.to(self._float_tensor)
136
-
137
- if incremental_state is not None:
138
- # positions is the same for every token when decoding a single step
139
- pos = timestep.view(-1)[0] + 1 if timestep is not None else seq_len
140
- return self.weights[self.padding_idx + pos, :].expand(bsz, 1, -1)
141
-
142
- positions = utils.make_positions(input, self.padding_idx) if positions is None else positions
143
- return self.weights.index_select(0, positions.view(-1)).view(bsz, seq_len, -1).detach()
144
-
145
- def max_positions(self):
146
- """Maximum number of supported positions."""
147
- return int(1e5) # an arbitrary large number
148
-
149
-
150
- class ConvTBC(nn.Module):
151
- def __init__(self, in_channels, out_channels, kernel_size, padding=0):
152
- super(ConvTBC, self).__init__()
153
- self.in_channels = in_channels
154
- self.out_channels = out_channels
155
- self.kernel_size = kernel_size
156
- self.padding = padding
157
-
158
- self.weight = torch.nn.Parameter(torch.Tensor(
159
- self.kernel_size, in_channels, out_channels))
160
- self.bias = torch.nn.Parameter(torch.Tensor(out_channels))
161
-
162
- def forward(self, input):
163
- return torch.conv_tbc(input.contiguous(), self.weight, self.bias, self.padding)
164
-
165
-
166
- class MultiheadAttention(nn.Module):
167
- def __init__(self, embed_dim, num_heads, kdim=None, vdim=None, dropout=0., bias=True,
168
- add_bias_kv=False, add_zero_attn=False, self_attention=False,
169
- encoder_decoder_attention=False):
170
- super().__init__()
171
- self.embed_dim = embed_dim
172
- self.kdim = kdim if kdim is not None else embed_dim
173
- self.vdim = vdim if vdim is not None else embed_dim
174
- self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim
175
-
176
- self.num_heads = num_heads
177
- self.dropout = dropout
178
- self.head_dim = embed_dim // num_heads
179
- assert self.head_dim * num_heads == self.embed_dim, "embed_dim must be divisible by num_heads"
180
- self.scaling = self.head_dim ** -0.5
181
-
182
- self.self_attention = self_attention
183
- self.encoder_decoder_attention = encoder_decoder_attention
184
-
185
- assert not self.self_attention or self.qkv_same_dim, 'Self-attention requires query, key and ' \
186
- 'value to be of the same size'
187
-
188
- if self.qkv_same_dim:
189
- self.in_proj_weight = Parameter(torch.Tensor(3 * embed_dim, embed_dim))
190
- else:
191
- self.k_proj_weight = Parameter(torch.Tensor(embed_dim, self.kdim))
192
- self.v_proj_weight = Parameter(torch.Tensor(embed_dim, self.vdim))
193
- self.q_proj_weight = Parameter(torch.Tensor(embed_dim, embed_dim))
194
-
195
- if bias:
196
- self.in_proj_bias = Parameter(torch.Tensor(3 * embed_dim))
197
- else:
198
- self.register_parameter('in_proj_bias', None)
199
-
200
- self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
201
-
202
- if add_bias_kv:
203
- self.bias_k = Parameter(torch.Tensor(1, 1, embed_dim))
204
- self.bias_v = Parameter(torch.Tensor(1, 1, embed_dim))
205
- else:
206
- self.bias_k = self.bias_v = None
207
-
208
- self.add_zero_attn = add_zero_attn
209
-
210
- self.reset_parameters()
211
-
212
- self.enable_torch_version = False
213
- if hasattr(F, "multi_head_attention_forward"):
214
- self.enable_torch_version = True
215
- else:
216
- self.enable_torch_version = False
217
- self.last_attn_probs = None
218
-
219
- def reset_parameters(self):
220
- if self.qkv_same_dim:
221
- nn.init.xavier_uniform_(self.in_proj_weight)
222
- else:
223
- nn.init.xavier_uniform_(self.k_proj_weight)
224
- nn.init.xavier_uniform_(self.v_proj_weight)
225
- nn.init.xavier_uniform_(self.q_proj_weight)
226
-
227
- nn.init.xavier_uniform_(self.out_proj.weight)
228
- if self.in_proj_bias is not None:
229
- nn.init.constant_(self.in_proj_bias, 0.)
230
- nn.init.constant_(self.out_proj.bias, 0.)
231
- if self.bias_k is not None:
232
- nn.init.xavier_normal_(self.bias_k)
233
- if self.bias_v is not None:
234
- nn.init.xavier_normal_(self.bias_v)
235
-
236
- def forward(
237
- self,
238
- query, key, value,
239
- key_padding_mask=None,
240
- incremental_state=None,
241
- need_weights=True,
242
- static_kv=False,
243
- attn_mask=None,
244
- before_softmax=False,
245
- need_head_weights=False,
246
- enc_dec_attn_constraint_mask=None,
247
- reset_attn_weight=None
248
- ):
249
- """Input shape: Time x Batch x Channel
250
-
251
- Args:
252
- key_padding_mask (ByteTensor, optional): mask to exclude
253
- keys that are pads, of shape `(batch, src_len)`, where
254
- padding elements are indicated by 1s.
255
- need_weights (bool, optional): return the attention weights,
256
- averaged over heads (default: False).
257
- attn_mask (ByteTensor, optional): typically used to
258
- implement causal attention, where the mask prevents the
259
- attention from looking forward in time (default: None).
260
- before_softmax (bool, optional): return the raw attention
261
- weights and values before the attention softmax.
262
- need_head_weights (bool, optional): return the attention
263
- weights for each head. Implies *need_weights*. Default:
264
- return the average attention weights over all heads.
265
- """
266
- if need_head_weights:
267
- need_weights = True
268
-
269
- tgt_len, bsz, embed_dim = query.size()
270
- assert embed_dim == self.embed_dim
271
- assert list(query.size()) == [tgt_len, bsz, embed_dim]
272
-
273
- if self.enable_torch_version and incremental_state is None and not static_kv and reset_attn_weight is None:
274
- if self.qkv_same_dim:
275
- return F.multi_head_attention_forward(query, key, value,
276
- self.embed_dim, self.num_heads,
277
- self.in_proj_weight,
278
- self.in_proj_bias, self.bias_k, self.bias_v,
279
- self.add_zero_attn, self.dropout,
280
- self.out_proj.weight, self.out_proj.bias,
281
- self.training, key_padding_mask, need_weights,
282
- attn_mask)
283
- else:
284
- return F.multi_head_attention_forward(query, key, value,
285
- self.embed_dim, self.num_heads,
286
- torch.empty([0]),
287
- self.in_proj_bias, self.bias_k, self.bias_v,
288
- self.add_zero_attn, self.dropout,
289
- self.out_proj.weight, self.out_proj.bias,
290
- self.training, key_padding_mask, need_weights,
291
- attn_mask, use_separate_proj_weight=True,
292
- q_proj_weight=self.q_proj_weight,
293
- k_proj_weight=self.k_proj_weight,
294
- v_proj_weight=self.v_proj_weight)
295
-
296
- if incremental_state is not None:
297
- print('Not implemented error.')
298
- exit()
299
- else:
300
- saved_state = None
301
-
302
- if self.self_attention:
303
- # self-attention
304
- q, k, v = self.in_proj_qkv(query)
305
- elif self.encoder_decoder_attention:
306
- # encoder-decoder attention
307
- q = self.in_proj_q(query)
308
- if key is None:
309
- assert value is None
310
- k = v = None
311
- else:
312
- k = self.in_proj_k(key)
313
- v = self.in_proj_v(key)
314
-
315
- else:
316
- q = self.in_proj_q(query)
317
- k = self.in_proj_k(key)
318
- v = self.in_proj_v(value)
319
- q *= self.scaling
320
-
321
- if self.bias_k is not None:
322
- assert self.bias_v is not None
323
- k = torch.cat([k, self.bias_k.repeat(1, bsz, 1)])
324
- v = torch.cat([v, self.bias_v.repeat(1, bsz, 1)])
325
- if attn_mask is not None:
326
- attn_mask = torch.cat([attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1)
327
- if key_padding_mask is not None:
328
- key_padding_mask = torch.cat(
329
- [key_padding_mask, key_padding_mask.new_zeros(key_padding_mask.size(0), 1)], dim=1)
330
-
331
- q = q.contiguous().view(tgt_len, bsz * self.num_heads, self.head_dim).transpose(0, 1)
332
- if k is not None:
333
- k = k.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1)
334
- if v is not None:
335
- v = v.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1)
336
-
337
- if saved_state is not None:
338
- print('Not implemented error.')
339
- exit()
340
-
341
- src_len = k.size(1)
342
-
343
- # This is part of a workaround to get around fork/join parallelism
344
- # not supporting Optional types.
345
- if key_padding_mask is not None and key_padding_mask.shape == torch.Size([]):
346
- key_padding_mask = None
347
-
348
- if key_padding_mask is not None:
349
- assert key_padding_mask.size(0) == bsz
350
- assert key_padding_mask.size(1) == src_len
351
-
352
- if self.add_zero_attn:
353
- src_len += 1
354
- k = torch.cat([k, k.new_zeros((k.size(0), 1) + k.size()[2:])], dim=1)
355
- v = torch.cat([v, v.new_zeros((v.size(0), 1) + v.size()[2:])], dim=1)
356
- if attn_mask is not None:
357
- attn_mask = torch.cat([attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1)
358
- if key_padding_mask is not None:
359
- key_padding_mask = torch.cat(
360
- [key_padding_mask, torch.zeros(key_padding_mask.size(0), 1).type_as(key_padding_mask)], dim=1)
361
-
362
- attn_weights = torch.bmm(q, k.transpose(1, 2))
363
- attn_weights = self.apply_sparse_mask(attn_weights, tgt_len, src_len, bsz)
364
-
365
- assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len]
366
-
367
- if attn_mask is not None:
368
- if len(attn_mask.shape) == 2:
369
- attn_mask = attn_mask.unsqueeze(0)
370
- elif len(attn_mask.shape) == 3:
371
- attn_mask = attn_mask[:, None].repeat([1, self.num_heads, 1, 1]).reshape(
372
- bsz * self.num_heads, tgt_len, src_len)
373
- attn_weights = attn_weights + attn_mask
374
-
375
- if enc_dec_attn_constraint_mask is not None: # bs x head x L_kv
376
- attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
377
- attn_weights = attn_weights.masked_fill(
378
- enc_dec_attn_constraint_mask.unsqueeze(2).bool(),
379
- -1e9,
380
- )
381
- attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
382
-
383
- if key_padding_mask is not None:
384
- # don't attend to padding symbols
385
- attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
386
- attn_weights = attn_weights.masked_fill(
387
- key_padding_mask.unsqueeze(1).unsqueeze(2),
388
- -1e9,
389
- )
390
- attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
391
-
392
- attn_logits = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
393
-
394
- if before_softmax:
395
- return attn_weights, v
396
-
397
- attn_weights_float = utils.softmax(attn_weights, dim=-1)
398
- attn_weights = attn_weights_float.type_as(attn_weights)
399
- attn_probs = F.dropout(attn_weights_float.type_as(attn_weights), p=self.dropout, training=self.training)
400
-
401
- if reset_attn_weight is not None:
402
- if reset_attn_weight:
403
- self.last_attn_probs = attn_probs.detach()
404
- else:
405
- assert self.last_attn_probs is not None
406
- attn_probs = self.last_attn_probs
407
- attn = torch.bmm(attn_probs, v)
408
- assert list(attn.size()) == [bsz * self.num_heads, tgt_len, self.head_dim]
409
- attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)
410
- attn = self.out_proj(attn)
411
-
412
- if need_weights:
413
- attn_weights = attn_weights_float.view(bsz, self.num_heads, tgt_len, src_len).transpose(1, 0)
414
- if not need_head_weights:
415
- # average attention weights over heads
416
- attn_weights = attn_weights.mean(dim=0)
417
- else:
418
- attn_weights = None
419
-
420
- return attn, (attn_weights, attn_logits)
421
-
422
- def in_proj_qkv(self, query):
423
- return self._in_proj(query).chunk(3, dim=-1)
424
-
425
- def in_proj_q(self, query):
426
- if self.qkv_same_dim:
427
- return self._in_proj(query, end=self.embed_dim)
428
- else:
429
- bias = self.in_proj_bias
430
- if bias is not None:
431
- bias = bias[:self.embed_dim]
432
- return F.linear(query, self.q_proj_weight, bias)
433
-
434
- def in_proj_k(self, key):
435
- if self.qkv_same_dim:
436
- return self._in_proj(key, start=self.embed_dim, end=2 * self.embed_dim)
437
- else:
438
- weight = self.k_proj_weight
439
- bias = self.in_proj_bias
440
- if bias is not None:
441
- bias = bias[self.embed_dim:2 * self.embed_dim]
442
- return F.linear(key, weight, bias)
443
-
444
- def in_proj_v(self, value):
445
- if self.qkv_same_dim:
446
- return self._in_proj(value, start=2 * self.embed_dim)
447
- else:
448
- weight = self.v_proj_weight
449
- bias = self.in_proj_bias
450
- if bias is not None:
451
- bias = bias[2 * self.embed_dim:]
452
- return F.linear(value, weight, bias)
453
-
454
- def _in_proj(self, input, start=0, end=None):
455
- weight = self.in_proj_weight
456
- bias = self.in_proj_bias
457
- weight = weight[start:end, :]
458
- if bias is not None:
459
- bias = bias[start:end]
460
- return F.linear(input, weight, bias)
461
-
462
-
463
- def apply_sparse_mask(self, attn_weights, tgt_len, src_len, bsz):
464
- return attn_weights
465
-
466
-
467
- class Swish(torch.autograd.Function):
468
- @staticmethod
469
- def forward(ctx, i):
470
- result = i * torch.sigmoid(i)
471
- ctx.save_for_backward(i)
472
- return result
473
-
474
- @staticmethod
475
- def backward(ctx, grad_output):
476
- i = ctx.saved_variables[0]
477
- sigmoid_i = torch.sigmoid(i)
478
- return grad_output * (sigmoid_i * (1 + i * (1 - sigmoid_i)))
479
-
480
-
481
- class CustomSwish(nn.Module):
482
- def forward(self, input_tensor):
483
- return Swish.apply(input_tensor)
484
-
485
- class Mish(nn.Module):
486
- def forward(self, x):
487
- return x * torch.tanh(F.softplus(x))
488
-
489
- class TransformerFFNLayer(nn.Module):
490
- def __init__(self, hidden_size, filter_size, padding="SAME", kernel_size=1, dropout=0., act='gelu'):
491
- super().__init__()
492
- self.kernel_size = kernel_size
493
- self.dropout = dropout
494
- self.act = act
495
- if padding == 'SAME':
496
- self.ffn_1 = nn.Conv1d(hidden_size, filter_size, kernel_size, padding=kernel_size // 2)
497
- elif padding == 'LEFT':
498
- self.ffn_1 = nn.Sequential(
499
- nn.ConstantPad1d((kernel_size - 1, 0), 0.0),
500
- nn.Conv1d(hidden_size, filter_size, kernel_size)
501
- )
502
- self.ffn_2 = Linear(filter_size, hidden_size)
503
- if self.act == 'swish':
504
- self.swish_fn = CustomSwish()
505
-
506
- def forward(self, x, incremental_state=None):
507
- # x: T x B x C
508
- if incremental_state is not None:
509
- assert incremental_state is None, 'Nar-generation does not allow this.'
510
- exit(1)
511
-
512
- x = self.ffn_1(x.permute(1, 2, 0)).permute(2, 0, 1)
513
- x = x * self.kernel_size ** -0.5
514
-
515
- if incremental_state is not None:
516
- x = x[-1:]
517
- if self.act == 'gelu':
518
- x = F.gelu(x)
519
- if self.act == 'relu':
520
- x = F.relu(x)
521
- if self.act == 'swish':
522
- x = self.swish_fn(x)
523
- x = F.dropout(x, self.dropout, training=self.training)
524
- x = self.ffn_2(x)
525
- return x
526
-
527
-
528
- class BatchNorm1dTBC(nn.Module):
529
- def __init__(self, c):
530
- super(BatchNorm1dTBC, self).__init__()
531
- self.bn = nn.BatchNorm1d(c)
532
-
533
- def forward(self, x):
534
- """
535
-
536
- :param x: [T, B, C]
537
- :return: [T, B, C]
538
- """
539
- x = x.permute(1, 2, 0) # [B, C, T]
540
- x = self.bn(x) # [B, C, T]
541
- x = x.permute(2, 0, 1) # [T, B, C]
542
- return x
543
-
544
-
545
- class EncSALayer(nn.Module):
546
- def __init__(self, c, num_heads, dropout, attention_dropout=0.1,
547
- relu_dropout=0.1, kernel_size=9, padding='SAME', norm='ln', act='gelu'):
548
- super().__init__()
549
- self.c = c
550
- self.dropout = dropout
551
- self.num_heads = num_heads
552
- if num_heads > 0:
553
- if norm == 'ln':
554
- self.layer_norm1 = LayerNorm(c)
555
- elif norm == 'bn':
556
- self.layer_norm1 = BatchNorm1dTBC(c)
557
- self.self_attn = MultiheadAttention(
558
- self.c, num_heads, self_attention=True, dropout=attention_dropout, bias=False,
559
- )
560
- if norm == 'ln':
561
- self.layer_norm2 = LayerNorm(c)
562
- elif norm == 'bn':
563
- self.layer_norm2 = BatchNorm1dTBC(c)
564
- self.ffn = TransformerFFNLayer(
565
- c, 4 * c, kernel_size=kernel_size, dropout=relu_dropout, padding=padding, act=act)
566
-
567
- def forward(self, x, encoder_padding_mask=None, **kwargs):
568
- layer_norm_training = kwargs.get('layer_norm_training', None)
569
- if layer_norm_training is not None:
570
- self.layer_norm1.training = layer_norm_training
571
- self.layer_norm2.training = layer_norm_training
572
- if self.num_heads > 0:
573
- residual = x
574
- x = self.layer_norm1(x)
575
- x, _, = self.self_attn(
576
- query=x,
577
- key=x,
578
- value=x,
579
- key_padding_mask=encoder_padding_mask
580
- )
581
- x = F.dropout(x, self.dropout, training=self.training)
582
- x = residual + x
583
- x = x * (1 - encoder_padding_mask.float()).transpose(0, 1)[..., None]
584
-
585
- residual = x
586
- x = self.layer_norm2(x)
587
- x = self.ffn(x)
588
- x = F.dropout(x, self.dropout, training=self.training)
589
- x = residual + x
590
- x = x * (1 - encoder_padding_mask.float()).transpose(0, 1)[..., None]
591
- return x
592
-
593
-
594
- class DecSALayer(nn.Module):
595
- def __init__(self, c, num_heads, dropout, attention_dropout=0.1, relu_dropout=0.1, kernel_size=9, act='gelu'):
596
- super().__init__()
597
- self.c = c
598
- self.dropout = dropout
599
- self.layer_norm1 = LayerNorm(c)
600
- self.self_attn = MultiheadAttention(
601
- c, num_heads, self_attention=True, dropout=attention_dropout, bias=False
602
- )
603
- self.layer_norm2 = LayerNorm(c)
604
- self.encoder_attn = MultiheadAttention(
605
- c, num_heads, encoder_decoder_attention=True, dropout=attention_dropout, bias=False,
606
- )
607
- self.layer_norm3 = LayerNorm(c)
608
- self.ffn = TransformerFFNLayer(
609
- c, 4 * c, padding='LEFT', kernel_size=kernel_size, dropout=relu_dropout, act=act)
610
-
611
- def forward(
612
- self,
613
- x,
614
- encoder_out=None,
615
- encoder_padding_mask=None,
616
- incremental_state=None,
617
- self_attn_mask=None,
618
- self_attn_padding_mask=None,
619
- attn_out=None,
620
- reset_attn_weight=None,
621
- **kwargs,
622
- ):
623
- layer_norm_training = kwargs.get('layer_norm_training', None)
624
- if layer_norm_training is not None:
625
- self.layer_norm1.training = layer_norm_training
626
- self.layer_norm2.training = layer_norm_training
627
- self.layer_norm3.training = layer_norm_training
628
- residual = x
629
- x = self.layer_norm1(x)
630
- x, _ = self.self_attn(
631
- query=x,
632
- key=x,
633
- value=x,
634
- key_padding_mask=self_attn_padding_mask,
635
- incremental_state=incremental_state,
636
- attn_mask=self_attn_mask
637
- )
638
- x = F.dropout(x, self.dropout, training=self.training)
639
- x = residual + x
640
-
641
- residual = x
642
- x = self.layer_norm2(x)
643
- if encoder_out is not None:
644
- x, attn = self.encoder_attn(
645
- query=x,
646
- key=encoder_out,
647
- value=encoder_out,
648
- key_padding_mask=encoder_padding_mask,
649
- incremental_state=incremental_state,
650
- static_kv=True,
651
- enc_dec_attn_constraint_mask=None, #utils.get_incremental_state(self, incremental_state, 'enc_dec_attn_constraint_mask'),
652
- reset_attn_weight=reset_attn_weight
653
- )
654
- attn_logits = attn[1]
655
- else:
656
- assert attn_out is not None
657
- x = self.encoder_attn.in_proj_v(attn_out.transpose(0, 1))
658
- attn_logits = None
659
- x = F.dropout(x, self.dropout, training=self.training)
660
- x = residual + x
661
-
662
- residual = x
663
- x = self.layer_norm3(x)
664
- x = self.ffn(x, incremental_state=incremental_state)
665
- x = F.dropout(x, self.dropout, training=self.training)
666
- x = residual + x
667
- # if len(attn_logits.size()) > 3:
668
- # indices = attn_logits.softmax(-1).max(-1).values.sum(-1).argmax(-1)
669
- # attn_logits = attn_logits.gather(1,
670
- # indices[:, None, None, None].repeat(1, 1, attn_logits.size(-2), attn_logits.size(-1))).squeeze(1)
671
- return x, attn_logits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/commons/espnet_positional_embedding.py DELETED
@@ -1,113 +0,0 @@
1
- import math
2
- import torch
3
-
4
-
5
- class PositionalEncoding(torch.nn.Module):
6
- """Positional encoding.
7
- Args:
8
- d_model (int): Embedding dimension.
9
- dropout_rate (float): Dropout rate.
10
- max_len (int): Maximum input length.
11
- reverse (bool): Whether to reverse the input position.
12
- """
13
-
14
- def __init__(self, d_model, dropout_rate, max_len=5000, reverse=False):
15
- """Construct an PositionalEncoding object."""
16
- super(PositionalEncoding, self).__init__()
17
- self.d_model = d_model
18
- self.reverse = reverse
19
- self.xscale = math.sqrt(self.d_model)
20
- self.dropout = torch.nn.Dropout(p=dropout_rate)
21
- self.pe = None
22
- self.extend_pe(torch.tensor(0.0).expand(1, max_len))
23
-
24
- def extend_pe(self, x):
25
- """Reset the positional encodings."""
26
- if self.pe is not None:
27
- if self.pe.size(1) >= x.size(1):
28
- if self.pe.dtype != x.dtype or self.pe.device != x.device:
29
- self.pe = self.pe.to(dtype=x.dtype, device=x.device)
30
- return
31
- pe = torch.zeros(x.size(1), self.d_model)
32
- if self.reverse:
33
- position = torch.arange(
34
- x.size(1) - 1, -1, -1.0, dtype=torch.float32
35
- ).unsqueeze(1)
36
- else:
37
- position = torch.arange(0, x.size(1), dtype=torch.float32).unsqueeze(1)
38
- div_term = torch.exp(
39
- torch.arange(0, self.d_model, 2, dtype=torch.float32)
40
- * -(math.log(10000.0) / self.d_model)
41
- )
42
- pe[:, 0::2] = torch.sin(position * div_term)
43
- pe[:, 1::2] = torch.cos(position * div_term)
44
- pe = pe.unsqueeze(0)
45
- self.pe = pe.to(device=x.device, dtype=x.dtype)
46
-
47
- def forward(self, x: torch.Tensor):
48
- """Add positional encoding.
49
- Args:
50
- x (torch.Tensor): Input tensor (batch, time, `*`).
51
- Returns:
52
- torch.Tensor: Encoded tensor (batch, time, `*`).
53
- """
54
- self.extend_pe(x)
55
- x = x * self.xscale + self.pe[:, : x.size(1)]
56
- return self.dropout(x)
57
-
58
-
59
- class ScaledPositionalEncoding(PositionalEncoding):
60
- """Scaled positional encoding module.
61
- See Sec. 3.2 https://arxiv.org/abs/1809.08895
62
- Args:
63
- d_model (int): Embedding dimension.
64
- dropout_rate (float): Dropout rate.
65
- max_len (int): Maximum input length.
66
- """
67
-
68
- def __init__(self, d_model, dropout_rate, max_len=5000):
69
- """Initialize class."""
70
- super().__init__(d_model=d_model, dropout_rate=dropout_rate, max_len=max_len)
71
- self.alpha = torch.nn.Parameter(torch.tensor(1.0))
72
-
73
- def reset_parameters(self):
74
- """Reset parameters."""
75
- self.alpha.data = torch.tensor(1.0)
76
-
77
- def forward(self, x):
78
- """Add positional encoding.
79
- Args:
80
- x (torch.Tensor): Input tensor (batch, time, `*`).
81
- Returns:
82
- torch.Tensor: Encoded tensor (batch, time, `*`).
83
- """
84
- self.extend_pe(x)
85
- x = x + self.alpha * self.pe[:, : x.size(1)]
86
- return self.dropout(x)
87
-
88
-
89
- class RelPositionalEncoding(PositionalEncoding):
90
- """Relative positional encoding module.
91
- See : Appendix B in https://arxiv.org/abs/1901.02860
92
- Args:
93
- d_model (int): Embedding dimension.
94
- dropout_rate (float): Dropout rate.
95
- max_len (int): Maximum input length.
96
- """
97
-
98
- def __init__(self, d_model, dropout_rate, max_len=5000):
99
- """Initialize class."""
100
- super().__init__(d_model, dropout_rate, max_len, reverse=True)
101
-
102
- def forward(self, x):
103
- """Compute positional encoding.
104
- Args:
105
- x (torch.Tensor): Input tensor (batch, time, `*`).
106
- Returns:
107
- torch.Tensor: Encoded tensor (batch, time, `*`).
108
- torch.Tensor: Positional embedding tensor (1, time, `*`).
109
- """
110
- self.extend_pe(x)
111
- x = x * self.xscale
112
- pos_emb = self.pe[:, : x.size(1)]
113
- return self.dropout(x) + self.dropout(pos_emb)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/commons/ssim.py DELETED
@@ -1,391 +0,0 @@
1
- # '''
2
- # https://github.com/One-sixth/ms_ssim_pytorch/blob/master/ssim.py
3
- # '''
4
- #
5
- # import torch
6
- # import torch.jit
7
- # import torch.nn.functional as F
8
- #
9
- #
10
- # @torch.jit.script
11
- # def create_window(window_size: int, sigma: float, channel: int):
12
- # '''
13
- # Create 1-D gauss kernel
14
- # :param window_size: the size of gauss kernel
15
- # :param sigma: sigma of normal distribution
16
- # :param channel: input channel
17
- # :return: 1D kernel
18
- # '''
19
- # coords = torch.arange(window_size, dtype=torch.float)
20
- # coords -= window_size // 2
21
- #
22
- # g = torch.exp(-(coords ** 2) / (2 * sigma ** 2))
23
- # g /= g.sum()
24
- #
25
- # g = g.reshape(1, 1, 1, -1).repeat(channel, 1, 1, 1)
26
- # return g
27
- #
28
- #
29
- # @torch.jit.script
30
- # def _gaussian_filter(x, window_1d, use_padding: bool):
31
- # '''
32
- # Blur input with 1-D kernel
33
- # :param x: batch of tensors to be blured
34
- # :param window_1d: 1-D gauss kernel
35
- # :param use_padding: padding image before conv
36
- # :return: blured tensors
37
- # '''
38
- # C = x.shape[1]
39
- # padding = 0
40
- # if use_padding:
41
- # window_size = window_1d.shape[3]
42
- # padding = window_size // 2
43
- # out = F.conv2d(x, window_1d, stride=1, padding=(0, padding), groups=C)
44
- # out = F.conv2d(out, window_1d.transpose(2, 3), stride=1, padding=(padding, 0), groups=C)
45
- # return out
46
- #
47
- #
48
- # @torch.jit.script
49
- # def ssim(X, Y, window, data_range: float, use_padding: bool = False):
50
- # '''
51
- # Calculate ssim index for X and Y
52
- # :param X: images [B, C, H, N_bins]
53
- # :param Y: images [B, C, H, N_bins]
54
- # :param window: 1-D gauss kernel
55
- # :param data_range: value range of input images. (usually 1.0 or 255)
56
- # :param use_padding: padding image before conv
57
- # :return:
58
- # '''
59
- #
60
- # K1 = 0.01
61
- # K2 = 0.03
62
- # compensation = 1.0
63
- #
64
- # C1 = (K1 * data_range) ** 2
65
- # C2 = (K2 * data_range) ** 2
66
- #
67
- # mu1 = _gaussian_filter(X, window, use_padding)
68
- # mu2 = _gaussian_filter(Y, window, use_padding)
69
- # sigma1_sq = _gaussian_filter(X * X, window, use_padding)
70
- # sigma2_sq = _gaussian_filter(Y * Y, window, use_padding)
71
- # sigma12 = _gaussian_filter(X * Y, window, use_padding)
72
- #
73
- # mu1_sq = mu1.pow(2)
74
- # mu2_sq = mu2.pow(2)
75
- # mu1_mu2 = mu1 * mu2
76
- #
77
- # sigma1_sq = compensation * (sigma1_sq - mu1_sq)
78
- # sigma2_sq = compensation * (sigma2_sq - mu2_sq)
79
- # sigma12 = compensation * (sigma12 - mu1_mu2)
80
- #
81
- # cs_map = (2 * sigma12 + C2) / (sigma1_sq + sigma2_sq + C2)
82
- # # Fixed the issue that the negative value of cs_map caused ms_ssim to output Nan.
83
- # cs_map = cs_map.clamp_min(0.)
84
- # ssim_map = ((2 * mu1_mu2 + C1) / (mu1_sq + mu2_sq + C1)) * cs_map
85
- #
86
- # ssim_val = ssim_map.mean(dim=(1, 2, 3)) # reduce along CHW
87
- # cs = cs_map.mean(dim=(1, 2, 3))
88
- #
89
- # return ssim_val, cs
90
- #
91
- #
92
- # @torch.jit.script
93
- # def ms_ssim(X, Y, window, data_range: float, weights, use_padding: bool = False, eps: float = 1e-8):
94
- # '''
95
- # interface of ms-ssim
96
- # :param X: a batch of images, (N,C,H,W)
97
- # :param Y: a batch of images, (N,C,H,W)
98
- # :param window: 1-D gauss kernel
99
- # :param data_range: value range of input images. (usually 1.0 or 255)
100
- # :param weights: weights for different levels
101
- # :param use_padding: padding image before conv
102
- # :param eps: use for avoid grad nan.
103
- # :return:
104
- # '''
105
- # levels = weights.shape[0]
106
- # cs_vals = []
107
- # ssim_vals = []
108
- # for _ in range(levels):
109
- # ssim_val, cs = ssim(X, Y, window=window, data_range=data_range, use_padding=use_padding)
110
- # # Use for fix a issue. When c = a ** b and a is 0, c.backward() will cause the a.grad become inf.
111
- # ssim_val = ssim_val.clamp_min(eps)
112
- # cs = cs.clamp_min(eps)
113
- # cs_vals.append(cs)
114
- #
115
- # ssim_vals.append(ssim_val)
116
- # padding = (X.shape[2] % 2, X.shape[3] % 2)
117
- # X = F.avg_pool2d(X, kernel_size=2, stride=2, padding=padding)
118
- # Y = F.avg_pool2d(Y, kernel_size=2, stride=2, padding=padding)
119
- #
120
- # cs_vals = torch.stack(cs_vals, dim=0)
121
- # ms_ssim_val = torch.prod((cs_vals[:-1] ** weights[:-1].unsqueeze(1)) * (ssim_vals[-1] ** weights[-1]), dim=0)
122
- # return ms_ssim_val
123
- #
124
- #
125
- # class SSIM(torch.jit.ScriptModule):
126
- # __constants__ = ['data_range', 'use_padding']
127
- #
128
- # def __init__(self, window_size=11, window_sigma=1.5, data_range=255., channel=3, use_padding=False):
129
- # '''
130
- # :param window_size: the size of gauss kernel
131
- # :param window_sigma: sigma of normal distribution
132
- # :param data_range: value range of input images. (usually 1.0 or 255)
133
- # :param channel: input channels (default: 3)
134
- # :param use_padding: padding image before conv
135
- # '''
136
- # super().__init__()
137
- # assert window_size % 2 == 1, 'Window size must be odd.'
138
- # window = create_window(window_size, window_sigma, channel)
139
- # self.register_buffer('window', window)
140
- # self.data_range = data_range
141
- # self.use_padding = use_padding
142
- #
143
- # @torch.jit.script_method
144
- # def forward(self, X, Y):
145
- # r = ssim(X, Y, window=self.window, data_range=self.data_range, use_padding=self.use_padding)
146
- # return r[0]
147
- #
148
- #
149
- # class MS_SSIM(torch.jit.ScriptModule):
150
- # __constants__ = ['data_range', 'use_padding', 'eps']
151
- #
152
- # def __init__(self, window_size=11, window_sigma=1.5, data_range=255., channel=3, use_padding=False, weights=None,
153
- # levels=None, eps=1e-8):
154
- # '''
155
- # class for ms-ssim
156
- # :param window_size: the size of gauss kernel
157
- # :param window_sigma: sigma of normal distribution
158
- # :param data_range: value range of input images. (usually 1.0 or 255)
159
- # :param channel: input channels
160
- # :param use_padding: padding image before conv
161
- # :param weights: weights for different levels. (default [0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
162
- # :param levels: number of downsampling
163
- # :param eps: Use for fix a issue. When c = a ** b and a is 0, c.backward() will cause the a.grad become inf.
164
- # '''
165
- # super().__init__()
166
- # assert window_size % 2 == 1, 'Window size must be odd.'
167
- # self.data_range = data_range
168
- # self.use_padding = use_padding
169
- # self.eps = eps
170
- #
171
- # window = create_window(window_size, window_sigma, channel)
172
- # self.register_buffer('window', window)
173
- #
174
- # if weights is None:
175
- # weights = [0.0448, 0.2856, 0.3001, 0.2363, 0.1333]
176
- # weights = torch.tensor(weights, dtype=torch.float)
177
- #
178
- # if levels is not None:
179
- # weights = weights[:levels]
180
- # weights = weights / weights.sum()
181
- #
182
- # self.register_buffer('weights', weights)
183
- #
184
- # @torch.jit.script_method
185
- # def forward(self, X, Y):
186
- # return ms_ssim(X, Y, window=self.window, data_range=self.data_range, weights=self.weights,
187
- # use_padding=self.use_padding, eps=self.eps)
188
- #
189
- #
190
- # if __name__ == '__main__':
191
- # print('Simple Test')
192
- # im = torch.randint(0, 255, (5, 3, 256, 256), dtype=torch.float, device='cuda')
193
- # img1 = im / 255
194
- # img2 = img1 * 0.5
195
- #
196
- # losser = SSIM(data_range=1.).cuda()
197
- # loss = losser(img1, img2).mean()
198
- #
199
- # losser2 = MS_SSIM(data_range=1.).cuda()
200
- # loss2 = losser2(img1, img2).mean()
201
- #
202
- # print(loss.item())
203
- # print(loss2.item())
204
- #
205
- # if __name__ == '__main__':
206
- # print('Training Test')
207
- # import cv2
208
- # import torch.optim
209
- # import numpy as np
210
- # import imageio
211
- # import time
212
- #
213
- # out_test_video = False
214
- # # 最好不要直接输出gif图,会非常大,最好先输出mkv文件后用ffmpeg转换到GIF
215
- # video_use_gif = False
216
- #
217
- # im = cv2.imread('test_img1.jpg', 1)
218
- # t_im = torch.from_numpy(im).cuda().permute(2, 0, 1).float()[None] / 255.
219
- #
220
- # if out_test_video:
221
- # if video_use_gif:
222
- # fps = 0.5
223
- # out_wh = (im.shape[1] // 2, im.shape[0] // 2)
224
- # suffix = '.gif'
225
- # else:
226
- # fps = 5
227
- # out_wh = (im.shape[1], im.shape[0])
228
- # suffix = '.mkv'
229
- # video_last_time = time.perf_counter()
230
- # video = imageio.get_writer('ssim_test' + suffix, fps=fps)
231
- #
232
- # # 测试ssim
233
- # print('Training SSIM')
234
- # rand_im = torch.randint_like(t_im, 0, 255, dtype=torch.float32) / 255.
235
- # rand_im.requires_grad = True
236
- # optim = torch.optim.Adam([rand_im], 0.003, eps=1e-8)
237
- # losser = SSIM(data_range=1., channel=t_im.shape[1]).cuda()
238
- # ssim_score = 0
239
- # while ssim_score < 0.999:
240
- # optim.zero_grad()
241
- # loss = losser(rand_im, t_im)
242
- # (-loss).sum().backward()
243
- # ssim_score = loss.item()
244
- # optim.step()
245
- # r_im = np.transpose(rand_im.detach().cpu().numpy().clip(0, 1) * 255, [0, 2, 3, 1]).astype(np.uint8)[0]
246
- # r_im = cv2.putText(r_im, 'ssim %f' % ssim_score, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
247
- #
248
- # if out_test_video:
249
- # if time.perf_counter() - video_last_time > 1. / fps:
250
- # video_last_time = time.perf_counter()
251
- # out_frame = cv2.cvtColor(r_im, cv2.COLOR_BGR2RGB)
252
- # out_frame = cv2.resize(out_frame, out_wh, interpolation=cv2.INTER_AREA)
253
- # if isinstance(out_frame, cv2.UMat):
254
- # out_frame = out_frame.get()
255
- # video.append_data(out_frame)
256
- #
257
- # cv2.imshow('ssim', r_im)
258
- # cv2.setWindowTitle('ssim', 'ssim %f' % ssim_score)
259
- # cv2.waitKey(1)
260
- #
261
- # if out_test_video:
262
- # video.close()
263
- #
264
- # # 测试ms_ssim
265
- # if out_test_video:
266
- # if video_use_gif:
267
- # fps = 0.5
268
- # out_wh = (im.shape[1] // 2, im.shape[0] // 2)
269
- # suffix = '.gif'
270
- # else:
271
- # fps = 5
272
- # out_wh = (im.shape[1], im.shape[0])
273
- # suffix = '.mkv'
274
- # video_last_time = time.perf_counter()
275
- # video = imageio.get_writer('ms_ssim_test' + suffix, fps=fps)
276
- #
277
- # print('Training MS_SSIM')
278
- # rand_im = torch.randint_like(t_im, 0, 255, dtype=torch.float32) / 255.
279
- # rand_im.requires_grad = True
280
- # optim = torch.optim.Adam([rand_im], 0.003, eps=1e-8)
281
- # losser = MS_SSIM(data_range=1., channel=t_im.shape[1]).cuda()
282
- # ssim_score = 0
283
- # while ssim_score < 0.999:
284
- # optim.zero_grad()
285
- # loss = losser(rand_im, t_im)
286
- # (-loss).sum().backward()
287
- # ssim_score = loss.item()
288
- # optim.step()
289
- # r_im = np.transpose(rand_im.detach().cpu().numpy().clip(0, 1) * 255, [0, 2, 3, 1]).astype(np.uint8)[0]
290
- # r_im = cv2.putText(r_im, 'ms_ssim %f' % ssim_score, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
291
- #
292
- # if out_test_video:
293
- # if time.perf_counter() - video_last_time > 1. / fps:
294
- # video_last_time = time.perf_counter()
295
- # out_frame = cv2.cvtColor(r_im, cv2.COLOR_BGR2RGB)
296
- # out_frame = cv2.resize(out_frame, out_wh, interpolation=cv2.INTER_AREA)
297
- # if isinstance(out_frame, cv2.UMat):
298
- # out_frame = out_frame.get()
299
- # video.append_data(out_frame)
300
- #
301
- # cv2.imshow('ms_ssim', r_im)
302
- # cv2.setWindowTitle('ms_ssim', 'ms_ssim %f' % ssim_score)
303
- # cv2.waitKey(1)
304
- #
305
- # if out_test_video:
306
- # video.close()
307
-
308
- """
309
- Adapted from https://github.com/Po-Hsun-Su/pytorch-ssim
310
- """
311
-
312
- import torch
313
- import torch.nn.functional as F
314
- from torch.autograd import Variable
315
- import numpy as np
316
- from math import exp
317
-
318
-
319
- def gaussian(window_size, sigma):
320
- gauss = torch.Tensor([exp(-(x - window_size // 2) ** 2 / float(2 * sigma ** 2)) for x in range(window_size)])
321
- return gauss / gauss.sum()
322
-
323
-
324
- def create_window(window_size, channel):
325
- _1D_window = gaussian(window_size, 1.5).unsqueeze(1)
326
- _2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
327
- window = Variable(_2D_window.expand(channel, 1, window_size, window_size).contiguous())
328
- return window
329
-
330
-
331
- def _ssim(img1, img2, window, window_size, channel, size_average=True):
332
- mu1 = F.conv2d(img1, window, padding=window_size // 2, groups=channel)
333
- mu2 = F.conv2d(img2, window, padding=window_size // 2, groups=channel)
334
-
335
- mu1_sq = mu1.pow(2)
336
- mu2_sq = mu2.pow(2)
337
- mu1_mu2 = mu1 * mu2
338
-
339
- sigma1_sq = F.conv2d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq
340
- sigma2_sq = F.conv2d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq
341
- sigma12 = F.conv2d(img1 * img2, window, padding=window_size // 2, groups=channel) - mu1_mu2
342
-
343
- C1 = 0.01 ** 2
344
- C2 = 0.03 ** 2
345
-
346
- ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
347
-
348
- if size_average:
349
- return ssim_map.mean()
350
- else:
351
- return ssim_map.mean(1)
352
-
353
-
354
- class SSIM(torch.nn.Module):
355
- def __init__(self, window_size=11, size_average=True):
356
- super(SSIM, self).__init__()
357
- self.window_size = window_size
358
- self.size_average = size_average
359
- self.channel = 1
360
- self.window = create_window(window_size, self.channel)
361
-
362
- def forward(self, img1, img2):
363
- (_, channel, _, _) = img1.size()
364
-
365
- if channel == self.channel and self.window.data.type() == img1.data.type():
366
- window = self.window
367
- else:
368
- window = create_window(self.window_size, channel)
369
-
370
- if img1.is_cuda:
371
- window = window.cuda(img1.get_device())
372
- window = window.type_as(img1)
373
-
374
- self.window = window
375
- self.channel = channel
376
-
377
- return _ssim(img1, img2, window, self.window_size, channel, self.size_average)
378
-
379
-
380
- window = None
381
-
382
-
383
- def ssim(img1, img2, window_size=11, size_average=True):
384
- (_, channel, _, _) = img1.size()
385
- global window
386
- if window is None:
387
- window = create_window(window_size, channel)
388
- if img1.is_cuda:
389
- window = window.cuda(img1.get_device())
390
- window = window.type_as(img1)
391
- return _ssim(img1, img2, window, window_size, channel, size_average)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/fastspeech/__pycache__/fs2.cpython-38.pyc DELETED
Binary file (5.87 kB)
 
modules/fastspeech/__pycache__/pe.cpython-38.pyc DELETED
Binary file (5.05 kB)
 
modules/fastspeech/__pycache__/tts_modules.cpython-38.pyc DELETED
Binary file (13.6 kB)
 
modules/fastspeech/fs2.py DELETED
@@ -1,255 +0,0 @@
1
- from modules.commons.common_layers import *
2
- from modules.commons.common_layers import Embedding
3
- from modules.fastspeech.tts_modules import FastspeechDecoder, DurationPredictor, LengthRegulator, PitchPredictor, \
4
- EnergyPredictor, FastspeechEncoder
5
- from utils.cwt import cwt2f0
6
- from utils.hparams import hparams
7
- from utils.pitch_utils import f0_to_coarse, denorm_f0, norm_f0
8
-
9
- FS_ENCODERS = {
10
- 'fft': lambda hp: FastspeechEncoder(
11
- hp['hidden_size'], hp['enc_layers'], hp['enc_ffn_kernel_size'],
12
- num_heads=hp['num_heads']),
13
- }
14
-
15
- FS_DECODERS = {
16
- 'fft': lambda hp: FastspeechDecoder(
17
- hp['hidden_size'], hp['dec_layers'], hp['dec_ffn_kernel_size'], hp['num_heads']),
18
- }
19
-
20
-
21
- class FastSpeech2(nn.Module):
22
- def __init__(self, dictionary, out_dims=None):
23
- super().__init__()
24
- # self.dictionary = dictionary
25
- self.padding_idx = 0
26
- if not hparams['no_fs2'] if 'no_fs2' in hparams.keys() else True:
27
- self.enc_layers = hparams['enc_layers']
28
- self.dec_layers = hparams['dec_layers']
29
- self.encoder = FS_ENCODERS[hparams['encoder_type']](hparams)
30
- self.decoder = FS_DECODERS[hparams['decoder_type']](hparams)
31
- self.hidden_size = hparams['hidden_size']
32
- # self.encoder_embed_tokens = self.build_embedding(self.dictionary, self.hidden_size)
33
- self.out_dims = out_dims
34
- if out_dims is None:
35
- self.out_dims = hparams['audio_num_mel_bins']
36
- self.mel_out = Linear(self.hidden_size, self.out_dims, bias=True)
37
- #=========not used===========
38
- # if hparams['use_spk_id']:
39
- # self.spk_embed_proj = Embedding(hparams['num_spk'] + 1, self.hidden_size)
40
- # if hparams['use_split_spk_id']:
41
- # self.spk_embed_f0 = Embedding(hparams['num_spk'] + 1, self.hidden_size)
42
- # self.spk_embed_dur = Embedding(hparams['num_spk'] + 1, self.hidden_size)
43
- # elif hparams['use_spk_embed']:
44
- # self.spk_embed_proj = Linear(256, self.hidden_size, bias=True)
45
- predictor_hidden = hparams['predictor_hidden'] if hparams['predictor_hidden'] > 0 else self.hidden_size
46
- # self.dur_predictor = DurationPredictor(
47
- # self.hidden_size,
48
- # n_chans=predictor_hidden,
49
- # n_layers=hparams['dur_predictor_layers'],
50
- # dropout_rate=hparams['predictor_dropout'], padding=hparams['ffn_padding'],
51
- # kernel_size=hparams['dur_predictor_kernel'])
52
- # self.length_regulator = LengthRegulator()
53
- if hparams['use_pitch_embed']:
54
- self.pitch_embed = Embedding(300, self.hidden_size, self.padding_idx)
55
- if hparams['pitch_type'] == 'cwt':
56
- h = hparams['cwt_hidden_size']
57
- cwt_out_dims = 10
58
- if hparams['use_uv']:
59
- cwt_out_dims = cwt_out_dims + 1
60
- self.cwt_predictor = nn.Sequential(
61
- nn.Linear(self.hidden_size, h),
62
- PitchPredictor(
63
- h,
64
- n_chans=predictor_hidden,
65
- n_layers=hparams['predictor_layers'],
66
- dropout_rate=hparams['predictor_dropout'], odim=cwt_out_dims,
67
- padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel']))
68
- self.cwt_stats_layers = nn.Sequential(
69
- nn.Linear(self.hidden_size, h), nn.ReLU(),
70
- nn.Linear(h, h), nn.ReLU(), nn.Linear(h, 2)
71
- )
72
- else:
73
- self.pitch_predictor = PitchPredictor(
74
- self.hidden_size,
75
- n_chans=predictor_hidden,
76
- n_layers=hparams['predictor_layers'],
77
- dropout_rate=hparams['predictor_dropout'],
78
- odim=2 if hparams['pitch_type'] == 'frame' else 1,
79
- padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel'])
80
- if hparams['use_energy_embed']:
81
- self.energy_embed = Embedding(256, self.hidden_size, self.padding_idx)
82
- # self.energy_predictor = EnergyPredictor(
83
- # self.hidden_size,
84
- # n_chans=predictor_hidden,
85
- # n_layers=hparams['predictor_layers'],
86
- # dropout_rate=hparams['predictor_dropout'], odim=1,
87
- # padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel'])
88
-
89
- # def build_embedding(self, dictionary, embed_dim):
90
- # num_embeddings = len(dictionary)
91
- # emb = Embedding(num_embeddings, embed_dim, self.padding_idx)
92
- # return emb
93
-
94
- def forward(self, hubert, mel2ph=None, spk_embed=None,
95
- ref_mels=None, f0=None, uv=None, energy=None, skip_decoder=True,
96
- spk_embed_dur_id=None, spk_embed_f0_id=None, infer=False, **kwargs):
97
- ret = {}
98
- if not hparams['no_fs2'] if 'no_fs2' in hparams.keys() else True:
99
- encoder_out =self.encoder(hubert) # [B, T, C]
100
- else:
101
- encoder_out =hubert
102
- src_nonpadding = (hubert!=0).any(-1)[:,:,None]
103
-
104
- # add ref style embed
105
- # Not implemented
106
- # variance encoder
107
- var_embed = 0
108
-
109
- # encoder_out_dur denotes encoder outputs for duration predictor
110
- # in speech adaptation, duration predictor use old speaker embedding
111
- if hparams['use_spk_embed']:
112
- spk_embed_dur = spk_embed_f0 = spk_embed = self.spk_embed_proj(spk_embed)[:, None, :]
113
- elif hparams['use_spk_id']:
114
- spk_embed_id = spk_embed
115
- if spk_embed_dur_id is None:
116
- spk_embed_dur_id = spk_embed_id
117
- if spk_embed_f0_id is None:
118
- spk_embed_f0_id = spk_embed_id
119
- spk_embed = self.spk_embed_proj(spk_embed_id)[:, None, :]
120
- spk_embed_dur = spk_embed_f0 = spk_embed
121
- if hparams['use_split_spk_id']:
122
- spk_embed_dur = self.spk_embed_dur(spk_embed_dur_id)[:, None, :]
123
- spk_embed_f0 = self.spk_embed_f0(spk_embed_f0_id)[:, None, :]
124
- else:
125
- spk_embed_dur = spk_embed_f0 = spk_embed = 0
126
-
127
- # add dur
128
- # dur_inp = (encoder_out + var_embed + spk_embed_dur) * src_nonpadding
129
-
130
- # mel2ph = self.add_dur(dur_inp, mel2ph, hubert, ret)
131
- ret['mel2ph'] = mel2ph
132
-
133
- decoder_inp = F.pad(encoder_out, [0, 0, 1, 0])
134
-
135
- mel2ph_ = mel2ph[..., None].repeat([1, 1, encoder_out.shape[-1]])
136
- decoder_inp_origin = decoder_inp = torch.gather(decoder_inp, 1, mel2ph_) # [B, T, H]
137
-
138
- tgt_nonpadding = (mel2ph > 0).float()[:, :, None]
139
-
140
- # add pitch and energy embed
141
- pitch_inp = (decoder_inp_origin + var_embed + spk_embed_f0) * tgt_nonpadding
142
- if hparams['use_pitch_embed']:
143
- pitch_inp_ph = (encoder_out + var_embed + spk_embed_f0) * src_nonpadding
144
- decoder_inp = decoder_inp + self.add_pitch(pitch_inp, f0, uv, mel2ph, ret, encoder_out=pitch_inp_ph)
145
- if hparams['use_energy_embed']:
146
- decoder_inp = decoder_inp + self.add_energy(pitch_inp, energy, ret)
147
-
148
- ret['decoder_inp'] = decoder_inp = (decoder_inp + spk_embed) * tgt_nonpadding
149
- if not hparams['no_fs2'] if 'no_fs2' in hparams.keys() else True:
150
- if skip_decoder:
151
- return ret
152
- ret['mel_out'] = self.run_decoder(decoder_inp, tgt_nonpadding, ret, infer=infer, **kwargs)
153
-
154
- return ret
155
-
156
- def add_dur(self, dur_input, mel2ph, hubert, ret):
157
- src_padding = (hubert==0).all(-1)
158
- dur_input = dur_input.detach() + hparams['predictor_grad'] * (dur_input - dur_input.detach())
159
- if mel2ph is None:
160
- dur, xs = self.dur_predictor.inference(dur_input, src_padding)
161
- ret['dur'] = xs
162
- ret['dur_choice'] = dur
163
- mel2ph = self.length_regulator(dur, src_padding).detach()
164
- else:
165
- ret['dur'] = self.dur_predictor(dur_input, src_padding)
166
- ret['mel2ph'] = mel2ph
167
- return mel2ph
168
-
169
- def run_decoder(self, decoder_inp, tgt_nonpadding, ret, infer, **kwargs):
170
- x = decoder_inp # [B, T, H]
171
- x = self.decoder(x)
172
- x = self.mel_out(x)
173
- return x * tgt_nonpadding
174
-
175
- def cwt2f0_norm(self, cwt_spec, mean, std, mel2ph):
176
- f0 = cwt2f0(cwt_spec, mean, std, hparams['cwt_scales'])
177
- f0 = torch.cat(
178
- [f0] + [f0[:, -1:]] * (mel2ph.shape[1] - f0.shape[1]), 1)
179
- f0_norm = norm_f0(f0, None, hparams)
180
- return f0_norm
181
-
182
- def out2mel(self, out):
183
- return out
184
-
185
- def add_pitch(self,decoder_inp, f0, uv, mel2ph, ret, encoder_out=None):
186
- # if hparams['pitch_type'] == 'ph':
187
- # pitch_pred_inp = encoder_out.detach() + hparams['predictor_grad'] * (encoder_out - encoder_out.detach())
188
- # pitch_padding = (encoder_out.sum().abs() == 0)
189
- # ret['pitch_pred'] = pitch_pred = self.pitch_predictor(pitch_pred_inp)
190
- # if f0 is None:
191
- # f0 = pitch_pred[:, :, 0]
192
- # ret['f0_denorm'] = f0_denorm = denorm_f0(f0, None, hparams, pitch_padding=pitch_padding)
193
- # pitch = f0_to_coarse(f0_denorm) # start from 0 [B, T_txt]
194
- # pitch = F.pad(pitch, [1, 0])
195
- # pitch = torch.gather(pitch, 1, mel2ph) # [B, T_mel]
196
- # pitch_embedding = pitch_embed(pitch)
197
- # return pitch_embedding
198
-
199
- decoder_inp = decoder_inp.detach() + hparams['predictor_grad'] * (decoder_inp - decoder_inp.detach())
200
-
201
- pitch_padding = (mel2ph == 0)
202
-
203
- # if hparams['pitch_type'] == 'cwt':
204
- # # NOTE: this part of script is *isolated* from other scripts, which means
205
- # # it may not be compatible with the current version.
206
- # pass
207
- # # pitch_padding = None
208
- # # ret['cwt'] = cwt_out = self.cwt_predictor(decoder_inp)
209
- # # stats_out = self.cwt_stats_layers(encoder_out[:, 0, :]) # [B, 2]
210
- # # mean = ret['f0_mean'] = stats_out[:, 0]
211
- # # std = ret['f0_std'] = stats_out[:, 1]
212
- # # cwt_spec = cwt_out[:, :, :10]
213
- # # if f0 is None:
214
- # # std = std * hparams['cwt_std_scale']
215
- # # f0 = self.cwt2f0_norm(cwt_spec, mean, std, mel2ph)
216
- # # if hparams['use_uv']:
217
- # # assert cwt_out.shape[-1] == 11
218
- # # uv = cwt_out[:, :, -1] > 0
219
- # elif hparams['pitch_ar']:
220
- # ret['pitch_pred'] = pitch_pred = self.pitch_predictor(decoder_inp, f0 if is_training else None)
221
- # if f0 is None:
222
- # f0 = pitch_pred[:, :, 0]
223
- # else:
224
- #ret['pitch_pred'] = pitch_pred = self.pitch_predictor(decoder_inp)
225
- # if f0 is None:
226
- # f0 = pitch_pred[:, :, 0]
227
- # if hparams['use_uv'] and uv is None:
228
- # uv = pitch_pred[:, :, 1] > 0
229
- ret['f0_denorm'] = f0_denorm = denorm_f0(f0, uv, hparams, pitch_padding=pitch_padding)
230
- if pitch_padding is not None:
231
- f0[pitch_padding] = 0
232
-
233
- pitch = f0_to_coarse(f0_denorm,hparams) # start from 0
234
- ret['pitch_pred']=pitch.unsqueeze(-1)
235
- # print(ret['pitch_pred'].shape)
236
- # print(pitch.shape)
237
- pitch_embedding = self.pitch_embed(pitch)
238
- return pitch_embedding
239
-
240
- def add_energy(self,decoder_inp, energy, ret):
241
- decoder_inp = decoder_inp.detach() + hparams['predictor_grad'] * (decoder_inp - decoder_inp.detach())
242
- ret['energy_pred'] = energy#energy_pred = self.energy_predictor(decoder_inp)[:, :, 0]
243
- # if energy is None:
244
- # energy = energy_pred
245
- energy = torch.clamp(energy * 256 // 4, max=255).long() # energy_to_coarse
246
- energy_embedding = self.energy_embed(energy)
247
- return energy_embedding
248
-
249
- @staticmethod
250
- def mel_norm(x):
251
- return (x + 5.5) / (6.3 / 2) - 1
252
-
253
- @staticmethod
254
- def mel_denorm(x):
255
- return (x + 1) * (6.3 / 2) - 5.5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/fastspeech/pe.py DELETED
@@ -1,149 +0,0 @@
1
- from modules.commons.common_layers import *
2
- from utils.hparams import hparams
3
- from modules.fastspeech.tts_modules import PitchPredictor
4
- from utils.pitch_utils import denorm_f0
5
-
6
-
7
- class Prenet(nn.Module):
8
- def __init__(self, in_dim=80, out_dim=256, kernel=5, n_layers=3, strides=None):
9
- super(Prenet, self).__init__()
10
- padding = kernel // 2
11
- self.layers = []
12
- self.strides = strides if strides is not None else [1] * n_layers
13
- for l in range(n_layers):
14
- self.layers.append(nn.Sequential(
15
- nn.Conv1d(in_dim, out_dim, kernel_size=kernel, padding=padding, stride=self.strides[l]),
16
- nn.ReLU(),
17
- nn.BatchNorm1d(out_dim)
18
- ))
19
- in_dim = out_dim
20
- self.layers = nn.ModuleList(self.layers)
21
- self.out_proj = nn.Linear(out_dim, out_dim)
22
-
23
- def forward(self, x):
24
- """
25
-
26
- :param x: [B, T, 80]
27
- :return: [L, B, T, H], [B, T, H]
28
- """
29
- # padding_mask = x.abs().sum(-1).eq(0).data # [B, T]
30
- padding_mask = x.abs().sum(-1).eq(0).detach()
31
- nonpadding_mask_TB = 1 - padding_mask.float()[:, None, :] # [B, 1, T]
32
- x = x.transpose(1, 2)
33
- hiddens = []
34
- for i, l in enumerate(self.layers):
35
- nonpadding_mask_TB = nonpadding_mask_TB[:, :, ::self.strides[i]]
36
- x = l(x) * nonpadding_mask_TB
37
- hiddens.append(x)
38
- hiddens = torch.stack(hiddens, 0) # [L, B, H, T]
39
- hiddens = hiddens.transpose(2, 3) # [L, B, T, H]
40
- x = self.out_proj(x.transpose(1, 2)) # [B, T, H]
41
- x = x * nonpadding_mask_TB.transpose(1, 2)
42
- return hiddens, x
43
-
44
-
45
- class ConvBlock(nn.Module):
46
- def __init__(self, idim=80, n_chans=256, kernel_size=3, stride=1, norm='gn', dropout=0):
47
- super().__init__()
48
- self.conv = ConvNorm(idim, n_chans, kernel_size, stride=stride)
49
- self.norm = norm
50
- if self.norm == 'bn':
51
- self.norm = nn.BatchNorm1d(n_chans)
52
- elif self.norm == 'in':
53
- self.norm = nn.InstanceNorm1d(n_chans, affine=True)
54
- elif self.norm == 'gn':
55
- self.norm = nn.GroupNorm(n_chans // 16, n_chans)
56
- elif self.norm == 'ln':
57
- self.norm = LayerNorm(n_chans // 16, n_chans)
58
- elif self.norm == 'wn':
59
- self.conv = torch.nn.utils.weight_norm(self.conv.conv)
60
- self.dropout = nn.Dropout(dropout)
61
- self.relu = nn.ReLU()
62
-
63
- def forward(self, x):
64
- """
65
-
66
- :param x: [B, C, T]
67
- :return: [B, C, T]
68
- """
69
- x = self.conv(x)
70
- if not isinstance(self.norm, str):
71
- if self.norm == 'none':
72
- pass
73
- elif self.norm == 'ln':
74
- x = self.norm(x.transpose(1, 2)).transpose(1, 2)
75
- else:
76
- x = self.norm(x)
77
- x = self.relu(x)
78
- x = self.dropout(x)
79
- return x
80
-
81
-
82
- class ConvStacks(nn.Module):
83
- def __init__(self, idim=80, n_layers=5, n_chans=256, odim=32, kernel_size=5, norm='gn',
84
- dropout=0, strides=None, res=True):
85
- super().__init__()
86
- self.conv = torch.nn.ModuleList()
87
- self.kernel_size = kernel_size
88
- self.res = res
89
- self.in_proj = Linear(idim, n_chans)
90
- if strides is None:
91
- strides = [1] * n_layers
92
- else:
93
- assert len(strides) == n_layers
94
- for idx in range(n_layers):
95
- self.conv.append(ConvBlock(
96
- n_chans, n_chans, kernel_size, stride=strides[idx], norm=norm, dropout=dropout))
97
- self.out_proj = Linear(n_chans, odim)
98
-
99
- def forward(self, x, return_hiddens=False):
100
- """
101
-
102
- :param x: [B, T, H]
103
- :return: [B, T, H]
104
- """
105
- x = self.in_proj(x)
106
- x = x.transpose(1, -1) # (B, idim, Tmax)
107
- hiddens = []
108
- for f in self.conv:
109
- x_ = f(x)
110
- x = x + x_ if self.res else x_ # (B, C, Tmax)
111
- hiddens.append(x)
112
- x = x.transpose(1, -1)
113
- x = self.out_proj(x) # (B, Tmax, H)
114
- if return_hiddens:
115
- hiddens = torch.stack(hiddens, 1) # [B, L, C, T]
116
- return x, hiddens
117
- return x
118
-
119
-
120
- class PitchExtractor(nn.Module):
121
- def __init__(self, n_mel_bins=80, conv_layers=2):
122
- super().__init__()
123
- self.hidden_size = hparams['hidden_size']
124
- self.predictor_hidden = hparams['predictor_hidden'] if hparams['predictor_hidden'] > 0 else self.hidden_size
125
- self.conv_layers = conv_layers
126
-
127
- self.mel_prenet = Prenet(n_mel_bins, self.hidden_size, strides=[1, 1, 1])
128
- if self.conv_layers > 0:
129
- self.mel_encoder = ConvStacks(
130
- idim=self.hidden_size, n_chans=self.hidden_size, odim=self.hidden_size, n_layers=self.conv_layers)
131
- self.pitch_predictor = PitchPredictor(
132
- self.hidden_size, n_chans=self.predictor_hidden,
133
- n_layers=5, dropout_rate=0.1, odim=2,
134
- padding=hparams['ffn_padding'], kernel_size=hparams['predictor_kernel'])
135
-
136
- def forward(self, mel_input=None):
137
- ret = {}
138
- mel_hidden = self.mel_prenet(mel_input)[1]
139
- if self.conv_layers > 0:
140
- mel_hidden = self.mel_encoder(mel_hidden)
141
-
142
- ret['pitch_pred'] = pitch_pred = self.pitch_predictor(mel_hidden)
143
-
144
- pitch_padding = mel_input.abs().sum(-1) == 0
145
- use_uv = hparams['pitch_type'] == 'frame' #and hparams['use_uv']
146
- ret['f0_denorm_pred'] = denorm_f0(
147
- pitch_pred[:, :, 0], (pitch_pred[:, :, 1] > 0) if use_uv else None,
148
- hparams, pitch_padding=pitch_padding)
149
- return ret
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/fastspeech/tts_modules.py DELETED
@@ -1,364 +0,0 @@
1
- import logging
2
- import math
3
-
4
- import torch
5
- import torch.nn as nn
6
- from torch.nn import functional as F
7
-
8
- from modules.commons.espnet_positional_embedding import RelPositionalEncoding
9
- from modules.commons.common_layers import SinusoidalPositionalEmbedding, Linear, EncSALayer, DecSALayer, BatchNorm1dTBC
10
- from utils.hparams import hparams
11
-
12
- DEFAULT_MAX_SOURCE_POSITIONS = 2000
13
- DEFAULT_MAX_TARGET_POSITIONS = 2000
14
-
15
-
16
- class TransformerEncoderLayer(nn.Module):
17
- def __init__(self, hidden_size, dropout, kernel_size=None, num_heads=2, norm='ln'):
18
- super().__init__()
19
- self.hidden_size = hidden_size
20
- self.dropout = dropout
21
- self.num_heads = num_heads
22
- self.op = EncSALayer(
23
- hidden_size, num_heads, dropout=dropout,
24
- attention_dropout=0.0, relu_dropout=dropout,
25
- kernel_size=kernel_size
26
- if kernel_size is not None else hparams['enc_ffn_kernel_size'],
27
- padding=hparams['ffn_padding'],
28
- norm=norm, act=hparams['ffn_act'])
29
-
30
- def forward(self, x, **kwargs):
31
- return self.op(x, **kwargs)
32
-
33
-
34
- ######################
35
- # fastspeech modules
36
- ######################
37
- class LayerNorm(torch.nn.LayerNorm):
38
- """Layer normalization module.
39
- :param int nout: output dim size
40
- :param int dim: dimension to be normalized
41
- """
42
-
43
- def __init__(self, nout, dim=-1):
44
- """Construct an LayerNorm object."""
45
- super(LayerNorm, self).__init__(nout, eps=1e-12)
46
- self.dim = dim
47
-
48
- def forward(self, x):
49
- """Apply layer normalization.
50
- :param torch.Tensor x: input tensor
51
- :return: layer normalized tensor
52
- :rtype torch.Tensor
53
- """
54
- if self.dim == -1:
55
- return super(LayerNorm, self).forward(x)
56
- return super(LayerNorm, self).forward(x.transpose(1, -1)).transpose(1, -1)
57
-
58
-
59
- class DurationPredictor(torch.nn.Module):
60
- """Duration predictor module.
61
- This is a module of duration predictor described in `FastSpeech: Fast, Robust and Controllable Text to Speech`_.
62
- The duration predictor predicts a duration of each frame in log domain from the hidden embeddings of encoder.
63
- .. _`FastSpeech: Fast, Robust and Controllable Text to Speech`:
64
- https://arxiv.org/pdf/1905.09263.pdf
65
- Note:
66
- The calculation domain of outputs is different between in `forward` and in `inference`. In `forward`,
67
- the outputs are calculated in log domain but in `inference`, those are calculated in linear domain.
68
- """
69
-
70
- def __init__(self, idim, n_layers=2, n_chans=384, kernel_size=3, dropout_rate=0.1, offset=1.0, padding='SAME'):
71
- """Initilize duration predictor module.
72
- Args:
73
- idim (int): Input dimension.
74
- n_layers (int, optional): Number of convolutional layers.
75
- n_chans (int, optional): Number of channels of convolutional layers.
76
- kernel_size (int, optional): Kernel size of convolutional layers.
77
- dropout_rate (float, optional): Dropout rate.
78
- offset (float, optional): Offset value to avoid nan in log domain.
79
- """
80
- super(DurationPredictor, self).__init__()
81
- self.offset = offset
82
- self.conv = torch.nn.ModuleList()
83
- self.kernel_size = kernel_size
84
- self.padding = padding
85
- for idx in range(n_layers):
86
- in_chans = idim if idx == 0 else n_chans
87
- self.conv += [torch.nn.Sequential(
88
- torch.nn.ConstantPad1d(((kernel_size - 1) // 2, (kernel_size - 1) // 2)
89
- if padding == 'SAME'
90
- else (kernel_size - 1, 0), 0),
91
- torch.nn.Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=0),
92
- torch.nn.ReLU(),
93
- LayerNorm(n_chans, dim=1),
94
- torch.nn.Dropout(dropout_rate)
95
- )]
96
- if hparams['dur_loss'] in ['mse', 'huber']:
97
- odims = 1
98
- elif hparams['dur_loss'] == 'mog':
99
- odims = 15
100
- elif hparams['dur_loss'] == 'crf':
101
- odims = 32
102
- from torchcrf import CRF
103
- self.crf = CRF(odims, batch_first=True)
104
- self.linear = torch.nn.Linear(n_chans, odims)
105
-
106
- def _forward(self, xs, x_masks=None, is_inference=False):
107
- xs = xs.transpose(1, -1) # (B, idim, Tmax)
108
- for f in self.conv:
109
- xs = f(xs) # (B, C, Tmax)
110
- if x_masks is not None:
111
- xs = xs * (1 - x_masks.float())[:, None, :]
112
-
113
- xs = self.linear(xs.transpose(1, -1)) # [B, T, C]
114
- xs = xs * (1 - x_masks.float())[:, :, None] # (B, T, C)
115
- if is_inference:
116
- return self.out2dur(xs), xs
117
- else:
118
- if hparams['dur_loss'] in ['mse']:
119
- xs = xs.squeeze(-1) # (B, Tmax)
120
- return xs
121
-
122
- def out2dur(self, xs):
123
- if hparams['dur_loss'] in ['mse']:
124
- # NOTE: calculate in log domain
125
- xs = xs.squeeze(-1) # (B, Tmax)
126
- dur = torch.clamp(torch.round(xs.exp() - self.offset), min=0).long() # avoid negative value
127
- elif hparams['dur_loss'] == 'mog':
128
- return NotImplementedError
129
- elif hparams['dur_loss'] == 'crf':
130
- dur = torch.LongTensor(self.crf.decode(xs)).cuda()
131
- return dur
132
-
133
- def forward(self, xs, x_masks=None):
134
- """Calculate forward propagation.
135
- Args:
136
- xs (Tensor): Batch of input sequences (B, Tmax, idim).
137
- x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
138
- Returns:
139
- Tensor: Batch of predicted durations in log domain (B, Tmax).
140
- """
141
- return self._forward(xs, x_masks, False)
142
-
143
- def inference(self, xs, x_masks=None):
144
- """Inference duration.
145
- Args:
146
- xs (Tensor): Batch of input sequences (B, Tmax, idim).
147
- x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
148
- Returns:
149
- LongTensor: Batch of predicted durations in linear domain (B, Tmax).
150
- """
151
- return self._forward(xs, x_masks, True)
152
-
153
-
154
- class LengthRegulator(torch.nn.Module):
155
- def __init__(self, pad_value=0.0):
156
- super(LengthRegulator, self).__init__()
157
- self.pad_value = pad_value
158
-
159
- def forward(self, dur, dur_padding=None, alpha=1.0):
160
- """
161
- Example (no batch dim version):
162
- 1. dur = [2,2,3]
163
- 2. token_idx = [[1],[2],[3]], dur_cumsum = [2,4,7], dur_cumsum_prev = [0,2,4]
164
- 3. token_mask = [[1,1,0,0,0,0,0],
165
- [0,0,1,1,0,0,0],
166
- [0,0,0,0,1,1,1]]
167
- 4. token_idx * token_mask = [[1,1,0,0,0,0,0],
168
- [0,0,2,2,0,0,0],
169
- [0,0,0,0,3,3,3]]
170
- 5. (token_idx * token_mask).sum(0) = [1,1,2,2,3,3,3]
171
-
172
- :param dur: Batch of durations of each frame (B, T_txt)
173
- :param dur_padding: Batch of padding of each frame (B, T_txt)
174
- :param alpha: duration rescale coefficient
175
- :return:
176
- mel2ph (B, T_speech)
177
- """
178
- assert alpha > 0
179
- dur = torch.round(dur.float() * alpha).long()
180
- if dur_padding is not None:
181
- dur = dur * (1 - dur_padding.long())
182
- token_idx = torch.arange(1, dur.shape[1] + 1)[None, :, None].to(dur.device)
183
- dur_cumsum = torch.cumsum(dur, 1)
184
- dur_cumsum_prev = F.pad(dur_cumsum, [1, -1], mode='constant', value=0)
185
-
186
- pos_idx = torch.arange(dur.sum(-1).max())[None, None].to(dur.device)
187
- token_mask = (pos_idx >= dur_cumsum_prev[:, :, None]) & (pos_idx < dur_cumsum[:, :, None])
188
- mel2ph = (token_idx * token_mask.long()).sum(1)
189
- return mel2ph
190
-
191
-
192
- class PitchPredictor(torch.nn.Module):
193
- def __init__(self, idim, n_layers=5, n_chans=384, odim=2, kernel_size=5,
194
- dropout_rate=0.1, padding='SAME'):
195
- """Initilize pitch predictor module.
196
- Args:
197
- idim (int): Input dimension.
198
- n_layers (int, optional): Number of convolutional layers.
199
- n_chans (int, optional): Number of channels of convolutional layers.
200
- kernel_size (int, optional): Kernel size of convolutional layers.
201
- dropout_rate (float, optional): Dropout rate.
202
- """
203
- super(PitchPredictor, self).__init__()
204
- self.conv = torch.nn.ModuleList()
205
- self.kernel_size = kernel_size
206
- self.padding = padding
207
- for idx in range(n_layers):
208
- in_chans = idim if idx == 0 else n_chans
209
- self.conv += [torch.nn.Sequential(
210
- torch.nn.ConstantPad1d(((kernel_size - 1) // 2, (kernel_size - 1) // 2)
211
- if padding == 'SAME'
212
- else (kernel_size - 1, 0), 0),
213
- torch.nn.Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=0),
214
- torch.nn.ReLU(),
215
- LayerNorm(n_chans, dim=1),
216
- torch.nn.Dropout(dropout_rate)
217
- )]
218
- self.linear = torch.nn.Linear(n_chans, odim)
219
- self.embed_positions = SinusoidalPositionalEmbedding(idim, 0, init_size=4096)
220
- self.pos_embed_alpha = nn.Parameter(torch.Tensor([1]))
221
-
222
- def forward(self, xs):
223
- """
224
-
225
- :param xs: [B, T, H]
226
- :return: [B, T, H]
227
- """
228
- positions = self.pos_embed_alpha * self.embed_positions(xs[..., 0])
229
- xs = xs + positions
230
- xs = xs.transpose(1, -1) # (B, idim, Tmax)
231
- for f in self.conv:
232
- xs = f(xs) # (B, C, Tmax)
233
- # NOTE: calculate in log domain
234
- xs = self.linear(xs.transpose(1, -1)) # (B, Tmax, H)
235
- return xs
236
-
237
-
238
- class EnergyPredictor(PitchPredictor):
239
- pass
240
-
241
-
242
- def mel2ph_to_dur(mel2ph, T_txt, max_dur=None):
243
- B, _ = mel2ph.shape
244
- dur = mel2ph.new_zeros(B, T_txt + 1).scatter_add(1, mel2ph, torch.ones_like(mel2ph))
245
- dur = dur[:, 1:]
246
- if max_dur is not None:
247
- dur = dur.clamp(max=max_dur)
248
- return dur
249
-
250
-
251
- class FFTBlocks(nn.Module):
252
- def __init__(self, hidden_size, num_layers, ffn_kernel_size=9, dropout=None, num_heads=2,
253
- use_pos_embed=True, use_last_norm=True, norm='ln', use_pos_embed_alpha=True):
254
- super().__init__()
255
- self.num_layers = num_layers
256
- embed_dim = self.hidden_size = hidden_size
257
- self.dropout = dropout if dropout is not None else hparams['dropout']
258
- self.use_pos_embed = use_pos_embed
259
- self.use_last_norm = use_last_norm
260
- if use_pos_embed:
261
- self.max_source_positions = DEFAULT_MAX_TARGET_POSITIONS
262
- self.padding_idx = 0
263
- self.pos_embed_alpha = nn.Parameter(torch.Tensor([1])) if use_pos_embed_alpha else 1
264
- self.embed_positions = SinusoidalPositionalEmbedding(
265
- embed_dim, self.padding_idx, init_size=DEFAULT_MAX_TARGET_POSITIONS,
266
- )
267
-
268
- self.layers = nn.ModuleList([])
269
- self.layers.extend([
270
- TransformerEncoderLayer(self.hidden_size, self.dropout,
271
- kernel_size=ffn_kernel_size, num_heads=num_heads)
272
- for _ in range(self.num_layers)
273
- ])
274
- if self.use_last_norm:
275
- if norm == 'ln':
276
- self.layer_norm = nn.LayerNorm(embed_dim)
277
- elif norm == 'bn':
278
- self.layer_norm = BatchNorm1dTBC(embed_dim)
279
- else:
280
- self.layer_norm = None
281
-
282
- def forward(self, x, padding_mask=None, attn_mask=None, return_hiddens=False):
283
- """
284
- :param x: [B, T, C]
285
- :param padding_mask: [B, T]
286
- :return: [B, T, C] or [L, B, T, C]
287
- """
288
- # padding_mask = x.abs().sum(-1).eq(0).data if padding_mask is None else padding_mask
289
- padding_mask = x.abs().sum(-1).eq(0).detach() if padding_mask is None else padding_mask
290
- nonpadding_mask_TB = 1 - padding_mask.transpose(0, 1).float()[:, :, None] # [T, B, 1]
291
- if self.use_pos_embed:
292
- positions = self.pos_embed_alpha * self.embed_positions(x[..., 0])
293
- x = x + positions
294
- x = F.dropout(x, p=self.dropout, training=self.training)
295
- # B x T x C -> T x B x C
296
- x = x.transpose(0, 1) * nonpadding_mask_TB
297
- hiddens = []
298
- for layer in self.layers:
299
- x = layer(x, encoder_padding_mask=padding_mask, attn_mask=attn_mask) * nonpadding_mask_TB
300
- hiddens.append(x)
301
- if self.use_last_norm:
302
- x = self.layer_norm(x) * nonpadding_mask_TB
303
- if return_hiddens:
304
- x = torch.stack(hiddens, 0) # [L, T, B, C]
305
- x = x.transpose(1, 2) # [L, B, T, C]
306
- else:
307
- x = x.transpose(0, 1) # [B, T, C]
308
- return x
309
-
310
-
311
- class FastspeechEncoder(FFTBlocks):
312
- '''
313
- compared to FFTBlocks:
314
- - input is [B, T, H], not [B, T, C]
315
- - supports "relative" positional encoding
316
- '''
317
- def __init__(self, hidden_size=None, num_layers=None, kernel_size=None, num_heads=2):
318
- hidden_size = hparams['hidden_size'] if hidden_size is None else hidden_size
319
- kernel_size = hparams['enc_ffn_kernel_size'] if kernel_size is None else kernel_size
320
- num_layers = hparams['dec_layers'] if num_layers is None else num_layers
321
- super().__init__(hidden_size, num_layers, kernel_size, num_heads=num_heads,
322
- use_pos_embed=False) # use_pos_embed_alpha for compatibility
323
- #self.embed_tokens = embed_tokens
324
- self.embed_scale = math.sqrt(hidden_size)
325
- self.padding_idx = 0
326
- if hparams.get('rel_pos') is not None and hparams['rel_pos']:
327
- self.embed_positions = RelPositionalEncoding(hidden_size, dropout_rate=0.0)
328
- else:
329
- self.embed_positions = SinusoidalPositionalEmbedding(
330
- hidden_size, self.padding_idx, init_size=DEFAULT_MAX_TARGET_POSITIONS,
331
- )
332
-
333
- def forward(self, hubert):
334
- """
335
-
336
- :param hubert: [B, T, H ]
337
- :return: {
338
- 'encoder_out': [T x B x C]
339
- }
340
- """
341
- # encoder_padding_mask = txt_tokens.eq(self.padding_idx).data
342
- encoder_padding_mask = (hubert==0).all(-1)
343
- x = self.forward_embedding(hubert) # [B, T, H]
344
- x = super(FastspeechEncoder, self).forward(x, encoder_padding_mask)
345
- return x
346
-
347
- def forward_embedding(self, hubert):
348
- # embed tokens and positions
349
- x = self.embed_scale * hubert
350
- if hparams['use_pos_embed']:
351
- positions = self.embed_positions(hubert)
352
- x = x + positions
353
- x = F.dropout(x, p=self.dropout, training=self.training)
354
- return x
355
-
356
-
357
- class FastspeechDecoder(FFTBlocks):
358
- def __init__(self, hidden_size=None, num_layers=None, kernel_size=None, num_heads=None):
359
- num_heads = hparams['num_heads'] if num_heads is None else num_heads
360
- hidden_size = hparams['hidden_size'] if hidden_size is None else hidden_size
361
- kernel_size = hparams['dec_ffn_kernel_size'] if kernel_size is None else kernel_size
362
- num_layers = hparams['dec_layers'] if num_layers is None else num_layers
363
- super().__init__(hidden_size, num_layers, kernel_size, num_heads=num_heads)
364
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/hifigan/__pycache__/hifigan.cpython-38.pyc DELETED
Binary file (11.5 kB)
 
modules/hifigan/hifigan.py DELETED
@@ -1,365 +0,0 @@
1
- import torch
2
- import torch.nn.functional as F
3
- 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
-
7
- from modules.parallel_wavegan.layers import UpsampleNetwork, ConvInUpsampleNetwork
8
- from modules.parallel_wavegan.models.source import SourceModuleHnNSF
9
- import numpy as np
10
-
11
- LRELU_SLOPE = 0.1
12
-
13
-
14
- def init_weights(m, mean=0.0, std=0.01):
15
- classname = m.__class__.__name__
16
- if classname.find("Conv") != -1:
17
- m.weight.data.normal_(mean, std)
18
-
19
-
20
- def apply_weight_norm(m):
21
- classname = m.__class__.__name__
22
- if classname.find("Conv") != -1:
23
- weight_norm(m)
24
-
25
-
26
- def get_padding(kernel_size, dilation=1):
27
- return int((kernel_size * dilation - dilation) / 2)
28
-
29
-
30
- class ResBlock1(torch.nn.Module):
31
- def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):
32
- super(ResBlock1, self).__init__()
33
- self.h = h
34
- self.convs1 = nn.ModuleList([
35
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
36
- padding=get_padding(kernel_size, dilation[0]))),
37
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
38
- padding=get_padding(kernel_size, dilation[1]))),
39
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
40
- padding=get_padding(kernel_size, dilation[2])))
41
- ])
42
- self.convs1.apply(init_weights)
43
-
44
- self.convs2 = nn.ModuleList([
45
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
46
- padding=get_padding(kernel_size, 1))),
47
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
48
- padding=get_padding(kernel_size, 1))),
49
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
50
- padding=get_padding(kernel_size, 1)))
51
- ])
52
- self.convs2.apply(init_weights)
53
-
54
- def forward(self, x):
55
- for c1, c2 in zip(self.convs1, self.convs2):
56
- xt = F.leaky_relu(x, LRELU_SLOPE)
57
- xt = c1(xt)
58
- xt = F.leaky_relu(xt, LRELU_SLOPE)
59
- xt = c2(xt)
60
- x = xt + x
61
- return x
62
-
63
- def remove_weight_norm(self):
64
- for l in self.convs1:
65
- remove_weight_norm(l)
66
- for l in self.convs2:
67
- remove_weight_norm(l)
68
-
69
-
70
- class ResBlock2(torch.nn.Module):
71
- def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):
72
- super(ResBlock2, self).__init__()
73
- self.h = h
74
- self.convs = nn.ModuleList([
75
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
76
- padding=get_padding(kernel_size, dilation[0]))),
77
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
78
- padding=get_padding(kernel_size, dilation[1])))
79
- ])
80
- self.convs.apply(init_weights)
81
-
82
- def forward(self, x):
83
- for c in self.convs:
84
- xt = F.leaky_relu(x, LRELU_SLOPE)
85
- xt = c(xt)
86
- x = xt + x
87
- return x
88
-
89
- def remove_weight_norm(self):
90
- for l in self.convs:
91
- remove_weight_norm(l)
92
-
93
-
94
- class Conv1d1x1(Conv1d):
95
- """1x1 Conv1d with customized initialization."""
96
-
97
- def __init__(self, in_channels, out_channels, bias):
98
- """Initialize 1x1 Conv1d module."""
99
- super(Conv1d1x1, self).__init__(in_channels, out_channels,
100
- kernel_size=1, padding=0,
101
- dilation=1, bias=bias)
102
-
103
-
104
- class HifiGanGenerator(torch.nn.Module):
105
- def __init__(self, h, c_out=1):
106
- super(HifiGanGenerator, self).__init__()
107
- self.h = h
108
- self.num_kernels = len(h['resblock_kernel_sizes'])
109
- self.num_upsamples = len(h['upsample_rates'])
110
-
111
- if h['use_pitch_embed']:
112
- self.harmonic_num = 8
113
- self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(h['upsample_rates']))
114
- self.m_source = SourceModuleHnNSF(
115
- sampling_rate=h['audio_sample_rate'],
116
- harmonic_num=self.harmonic_num)
117
- self.noise_convs = nn.ModuleList()
118
- self.conv_pre = weight_norm(Conv1d(80, h['upsample_initial_channel'], 7, 1, padding=3))
119
- resblock = ResBlock1 if h['resblock'] == '1' else ResBlock2
120
-
121
- self.ups = nn.ModuleList()
122
- for i, (u, k) in enumerate(zip(h['upsample_rates'], h['upsample_kernel_sizes'])):
123
- c_cur = h['upsample_initial_channel'] // (2 ** (i + 1))
124
- self.ups.append(weight_norm(
125
- ConvTranspose1d(c_cur * 2, c_cur, k, u, padding=(k - u) // 2)))
126
- if h['use_pitch_embed']:
127
- if i + 1 < len(h['upsample_rates']):
128
- stride_f0 = np.prod(h['upsample_rates'][i + 1:])
129
- self.noise_convs.append(Conv1d(
130
- 1, c_cur, kernel_size=stride_f0 * 2, stride=stride_f0, padding=stride_f0 // 2))
131
- else:
132
- self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
133
-
134
- self.resblocks = nn.ModuleList()
135
- for i in range(len(self.ups)):
136
- ch = h['upsample_initial_channel'] // (2 ** (i + 1))
137
- for j, (k, d) in enumerate(zip(h['resblock_kernel_sizes'], h['resblock_dilation_sizes'])):
138
- self.resblocks.append(resblock(h, ch, k, d))
139
-
140
- self.conv_post = weight_norm(Conv1d(ch, c_out, 7, 1, padding=3))
141
- self.ups.apply(init_weights)
142
- self.conv_post.apply(init_weights)
143
-
144
- def forward(self, x, f0=None):
145
- if f0 is not None:
146
- # harmonic-source signal, noise-source signal, uv flag
147
- f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2)
148
- har_source, noi_source, uv = self.m_source(f0)
149
- har_source = har_source.transpose(1, 2)
150
-
151
- x = self.conv_pre(x)
152
- for i in range(self.num_upsamples):
153
- x = F.leaky_relu(x, LRELU_SLOPE)
154
- x = self.ups[i](x)
155
- if f0 is not None:
156
- x_source = self.noise_convs[i](har_source)
157
- x = x + x_source
158
- xs = None
159
- for j in range(self.num_kernels):
160
- if xs is None:
161
- xs = self.resblocks[i * self.num_kernels + j](x)
162
- else:
163
- xs += self.resblocks[i * self.num_kernels + j](x)
164
- x = xs / self.num_kernels
165
- x = F.leaky_relu(x)
166
- x = self.conv_post(x)
167
- x = torch.tanh(x)
168
-
169
- return x
170
-
171
- def remove_weight_norm(self):
172
- print('Removing weight norm...')
173
- for l in self.ups:
174
- remove_weight_norm(l)
175
- for l in self.resblocks:
176
- l.remove_weight_norm()
177
- remove_weight_norm(self.conv_pre)
178
- remove_weight_norm(self.conv_post)
179
-
180
-
181
- class DiscriminatorP(torch.nn.Module):
182
- def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False, use_cond=False, c_in=1):
183
- super(DiscriminatorP, self).__init__()
184
- self.use_cond = use_cond
185
- if use_cond:
186
- from utils.hparams import hparams
187
- t = hparams['hop_size']
188
- self.cond_net = torch.nn.ConvTranspose1d(80, 1, t * 2, stride=t, padding=t // 2)
189
- c_in = 2
190
-
191
- self.period = period
192
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
193
- self.convs = nn.ModuleList([
194
- norm_f(Conv2d(c_in, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
195
- norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
196
- norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
197
- norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
198
- norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
199
- ])
200
- self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
201
-
202
- def forward(self, x, mel):
203
- fmap = []
204
- if self.use_cond:
205
- x_mel = self.cond_net(mel)
206
- x = torch.cat([x_mel, x], 1)
207
- # 1d to 2d
208
- b, c, t = x.shape
209
- if t % self.period != 0: # pad first
210
- n_pad = self.period - (t % self.period)
211
- x = F.pad(x, (0, n_pad), "reflect")
212
- t = t + n_pad
213
- x = x.view(b, c, t // self.period, self.period)
214
-
215
- for l in self.convs:
216
- x = l(x)
217
- x = F.leaky_relu(x, LRELU_SLOPE)
218
- fmap.append(x)
219
- x = self.conv_post(x)
220
- fmap.append(x)
221
- x = torch.flatten(x, 1, -1)
222
-
223
- return x, fmap
224
-
225
-
226
- class MultiPeriodDiscriminator(torch.nn.Module):
227
- def __init__(self, use_cond=False, c_in=1):
228
- super(MultiPeriodDiscriminator, self).__init__()
229
- self.discriminators = nn.ModuleList([
230
- DiscriminatorP(2, use_cond=use_cond, c_in=c_in),
231
- DiscriminatorP(3, use_cond=use_cond, c_in=c_in),
232
- DiscriminatorP(5, use_cond=use_cond, c_in=c_in),
233
- DiscriminatorP(7, use_cond=use_cond, c_in=c_in),
234
- DiscriminatorP(11, use_cond=use_cond, c_in=c_in),
235
- ])
236
-
237
- def forward(self, y, y_hat, mel=None):
238
- y_d_rs = []
239
- y_d_gs = []
240
- fmap_rs = []
241
- fmap_gs = []
242
- for i, d in enumerate(self.discriminators):
243
- y_d_r, fmap_r = d(y, mel)
244
- y_d_g, fmap_g = d(y_hat, mel)
245
- y_d_rs.append(y_d_r)
246
- fmap_rs.append(fmap_r)
247
- y_d_gs.append(y_d_g)
248
- fmap_gs.append(fmap_g)
249
-
250
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
251
-
252
-
253
- class DiscriminatorS(torch.nn.Module):
254
- def __init__(self, use_spectral_norm=False, use_cond=False, upsample_rates=None, c_in=1):
255
- super(DiscriminatorS, self).__init__()
256
- self.use_cond = use_cond
257
- if use_cond:
258
- t = np.prod(upsample_rates)
259
- self.cond_net = torch.nn.ConvTranspose1d(80, 1, t * 2, stride=t, padding=t // 2)
260
- c_in = 2
261
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
262
- self.convs = nn.ModuleList([
263
- norm_f(Conv1d(c_in, 128, 15, 1, padding=7)),
264
- norm_f(Conv1d(128, 128, 41, 2, groups=4, padding=20)),
265
- norm_f(Conv1d(128, 256, 41, 2, groups=16, padding=20)),
266
- norm_f(Conv1d(256, 512, 41, 4, groups=16, padding=20)),
267
- norm_f(Conv1d(512, 1024, 41, 4, groups=16, padding=20)),
268
- norm_f(Conv1d(1024, 1024, 41, 1, groups=16, padding=20)),
269
- norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
270
- ])
271
- self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
272
-
273
- def forward(self, x, mel):
274
- if self.use_cond:
275
- x_mel = self.cond_net(mel)
276
- x = torch.cat([x_mel, x], 1)
277
- fmap = []
278
- for l in self.convs:
279
- x = l(x)
280
- x = F.leaky_relu(x, LRELU_SLOPE)
281
- fmap.append(x)
282
- x = self.conv_post(x)
283
- fmap.append(x)
284
- x = torch.flatten(x, 1, -1)
285
-
286
- return x, fmap
287
-
288
-
289
- class MultiScaleDiscriminator(torch.nn.Module):
290
- def __init__(self, use_cond=False, c_in=1):
291
- super(MultiScaleDiscriminator, self).__init__()
292
- from utils.hparams import hparams
293
- self.discriminators = nn.ModuleList([
294
- DiscriminatorS(use_spectral_norm=True, use_cond=use_cond,
295
- upsample_rates=[4, 4, hparams['hop_size'] // 16],
296
- c_in=c_in),
297
- DiscriminatorS(use_cond=use_cond,
298
- upsample_rates=[4, 4, hparams['hop_size'] // 32],
299
- c_in=c_in),
300
- DiscriminatorS(use_cond=use_cond,
301
- upsample_rates=[4, 4, hparams['hop_size'] // 64],
302
- c_in=c_in),
303
- ])
304
- self.meanpools = nn.ModuleList([
305
- AvgPool1d(4, 2, padding=1),
306
- AvgPool1d(4, 2, padding=1)
307
- ])
308
-
309
- def forward(self, y, y_hat, mel=None):
310
- y_d_rs = []
311
- y_d_gs = []
312
- fmap_rs = []
313
- fmap_gs = []
314
- for i, d in enumerate(self.discriminators):
315
- if i != 0:
316
- y = self.meanpools[i - 1](y)
317
- y_hat = self.meanpools[i - 1](y_hat)
318
- y_d_r, fmap_r = d(y, mel)
319
- y_d_g, fmap_g = d(y_hat, mel)
320
- y_d_rs.append(y_d_r)
321
- fmap_rs.append(fmap_r)
322
- y_d_gs.append(y_d_g)
323
- fmap_gs.append(fmap_g)
324
-
325
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
326
-
327
-
328
- def feature_loss(fmap_r, fmap_g):
329
- loss = 0
330
- for dr, dg in zip(fmap_r, fmap_g):
331
- for rl, gl in zip(dr, dg):
332
- loss += torch.mean(torch.abs(rl - gl))
333
-
334
- return loss * 2
335
-
336
-
337
- def discriminator_loss(disc_real_outputs, disc_generated_outputs):
338
- r_losses = 0
339
- g_losses = 0
340
- for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
341
- r_loss = torch.mean((1 - dr) ** 2)
342
- g_loss = torch.mean(dg ** 2)
343
- r_losses += r_loss
344
- g_losses += g_loss
345
- r_losses = r_losses / len(disc_real_outputs)
346
- g_losses = g_losses / len(disc_real_outputs)
347
- return r_losses, g_losses
348
-
349
-
350
- def cond_discriminator_loss(outputs):
351
- loss = 0
352
- for dg in outputs:
353
- g_loss = torch.mean(dg ** 2)
354
- loss += g_loss
355
- loss = loss / len(outputs)
356
- return loss
357
-
358
-
359
- def generator_loss(disc_outputs):
360
- loss = 0
361
- for dg in disc_outputs:
362
- l = torch.mean((1 - dg) ** 2)
363
- loss += l
364
- loss = loss / len(disc_outputs)
365
- return loss
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/hifigan/mel_utils.py DELETED
@@ -1,80 +0,0 @@
1
- import numpy as np
2
- import torch
3
- import torch.utils.data
4
- from librosa.filters import mel as librosa_mel_fn
5
- from scipy.io.wavfile import read
6
-
7
- MAX_WAV_VALUE = 32768.0
8
-
9
-
10
- def load_wav(full_path):
11
- sampling_rate, data = read(full_path)
12
- return data, sampling_rate
13
-
14
-
15
- def dynamic_range_compression(x, C=1, clip_val=1e-5):
16
- return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
17
-
18
-
19
- def dynamic_range_decompression(x, C=1):
20
- return np.exp(x) / C
21
-
22
-
23
- def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
24
- return torch.log(torch.clamp(x, min=clip_val) * C)
25
-
26
-
27
- def dynamic_range_decompression_torch(x, C=1):
28
- return torch.exp(x) / C
29
-
30
-
31
- def spectral_normalize_torch(magnitudes):
32
- output = dynamic_range_compression_torch(magnitudes)
33
- return output
34
-
35
-
36
- def spectral_de_normalize_torch(magnitudes):
37
- output = dynamic_range_decompression_torch(magnitudes)
38
- return output
39
-
40
-
41
- mel_basis = {}
42
- hann_window = {}
43
-
44
-
45
- def mel_spectrogram(y, hparams, center=False, complex=False):
46
- # hop_size: 512 # For 22050Hz, 275 ~= 12.5 ms (0.0125 * sample_rate)
47
- # win_size: 2048 # For 22050Hz, 1100 ~= 50 ms (If None, win_size: fft_size) (0.05 * sample_rate)
48
- # fmin: 55 # Set this to 55 if your speaker is male! if female, 95 should help taking off noise. (To test depending on dataset. Pitch info: male~[65, 260], female~[100, 525])
49
- # fmax: 10000 # To be increased/reduced depending on data.
50
- # fft_size: 2048 # Extra window size is filled with 0 paddings to match this parameter
51
- # n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax,
52
- n_fft = hparams['fft_size']
53
- num_mels = hparams['audio_num_mel_bins']
54
- sampling_rate = hparams['audio_sample_rate']
55
- hop_size = hparams['hop_size']
56
- win_size = hparams['win_size']
57
- fmin = hparams['fmin']
58
- fmax = hparams['fmax']
59
- y = y.clamp(min=-1., max=1.)
60
- global mel_basis, hann_window
61
- if fmax not in mel_basis:
62
- mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)
63
- mel_basis[str(fmax) + '_' + str(y.device)] = torch.from_numpy(mel).float().to(y.device)
64
- hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device)
65
-
66
- y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
67
- mode='reflect')
68
- y = y.squeeze(1)
69
-
70
- spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[str(y.device)],
71
- center=center, pad_mode='reflect', normalized=False, onesided=True)
72
-
73
- if not complex:
74
- spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
75
- spec = torch.matmul(mel_basis[str(fmax) + '_' + str(y.device)], spec)
76
- spec = spectral_normalize_torch(spec)
77
- else:
78
- B, C, T, _ = spec.shape
79
- spec = spec.transpose(1, 2) # [B, T, n_fft, 2]
80
- return spec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/nsf_hifigan/__pycache__/env.cpython-38.pyc DELETED
Binary file (793 Bytes)
 
modules/nsf_hifigan/__pycache__/models.cpython-38.pyc DELETED
Binary file (16.3 kB)
 
modules/nsf_hifigan/__pycache__/nvSTFT.cpython-38.pyc DELETED
Binary file (3.93 kB)
 
modules/nsf_hifigan/__pycache__/utils.cpython-38.pyc DELETED
Binary file (2.34 kB)
 
modules/nsf_hifigan/env.py DELETED
@@ -1,15 +0,0 @@
1
- import os
2
- import shutil
3
-
4
-
5
- class AttrDict(dict):
6
- def __init__(self, *args, **kwargs):
7
- super(AttrDict, self).__init__(*args, **kwargs)
8
- self.__dict__ = self
9
-
10
-
11
- def build_env(config, config_name, path):
12
- t_path = os.path.join(path, config_name)
13
- if config != t_path:
14
- os.makedirs(path, exist_ok=True)
15
- shutil.copyfile(config, os.path.join(path, config_name))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/nsf_hifigan/models.py DELETED
@@ -1,549 +0,0 @@
1
- import os
2
- import json
3
- from .env import AttrDict
4
- import numpy as np
5
- import torch
6
- import torch.nn.functional as F
7
- import torch.nn as nn
8
- from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
9
- from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
10
- from .utils import init_weights, get_padding
11
-
12
- LRELU_SLOPE = 0.1
13
-
14
- def load_model(model_path, device='cuda'):
15
- config_file = os.path.join(os.path.split(model_path)[0], 'config.json')
16
- with open(config_file) as f:
17
- data = f.read()
18
-
19
- global h
20
- json_config = json.loads(data)
21
- h = AttrDict(json_config)
22
-
23
- generator = Generator(h).to(device)
24
-
25
- cp_dict = torch.load(model_path)
26
- generator.load_state_dict(cp_dict['generator'])
27
- generator.eval()
28
- generator.remove_weight_norm()
29
- del cp_dict
30
- return generator, h
31
-
32
-
33
- class ResBlock1(torch.nn.Module):
34
- def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):
35
- super(ResBlock1, self).__init__()
36
- self.h = h
37
- self.convs1 = nn.ModuleList([
38
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
39
- padding=get_padding(kernel_size, dilation[0]))),
40
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
41
- padding=get_padding(kernel_size, dilation[1]))),
42
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
43
- padding=get_padding(kernel_size, dilation[2])))
44
- ])
45
- self.convs1.apply(init_weights)
46
-
47
- self.convs2 = nn.ModuleList([
48
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
49
- padding=get_padding(kernel_size, 1))),
50
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
51
- padding=get_padding(kernel_size, 1))),
52
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
53
- padding=get_padding(kernel_size, 1)))
54
- ])
55
- self.convs2.apply(init_weights)
56
-
57
- def forward(self, x):
58
- for c1, c2 in zip(self.convs1, self.convs2):
59
- xt = F.leaky_relu(x, LRELU_SLOPE)
60
- xt = c1(xt)
61
- xt = F.leaky_relu(xt, LRELU_SLOPE)
62
- xt = c2(xt)
63
- x = xt + x
64
- return x
65
-
66
- def remove_weight_norm(self):
67
- for l in self.convs1:
68
- remove_weight_norm(l)
69
- for l in self.convs2:
70
- remove_weight_norm(l)
71
-
72
-
73
- class ResBlock2(torch.nn.Module):
74
- def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):
75
- super(ResBlock2, self).__init__()
76
- self.h = h
77
- self.convs = nn.ModuleList([
78
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
79
- padding=get_padding(kernel_size, dilation[0]))),
80
- weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
81
- padding=get_padding(kernel_size, dilation[1])))
82
- ])
83
- self.convs.apply(init_weights)
84
-
85
- def forward(self, x):
86
- for c in self.convs:
87
- xt = F.leaky_relu(x, LRELU_SLOPE)
88
- xt = c(xt)
89
- x = xt + x
90
- return x
91
-
92
- def remove_weight_norm(self):
93
- for l in self.convs:
94
- remove_weight_norm(l)
95
-
96
-
97
- class Generator(torch.nn.Module):
98
- def __init__(self, h):
99
- super(Generator, self).__init__()
100
- self.h = h
101
- self.num_kernels = len(h.resblock_kernel_sizes)
102
- self.num_upsamples = len(h.upsample_rates)
103
- self.conv_pre = weight_norm(Conv1d(h.num_mels, h.upsample_initial_channel, 7, 1, padding=3))
104
- resblock = ResBlock1 if h.resblock == '1' else ResBlock2
105
-
106
- self.ups = nn.ModuleList()
107
- for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):
108
- self.ups.append(weight_norm(
109
- ConvTranspose1d(h.upsample_initial_channel//(2**i), h.upsample_initial_channel//(2**(i+1)),
110
- k, u, padding=(k-u)//2)))
111
-
112
- self.resblocks = nn.ModuleList()
113
- for i in range(len(self.ups)):
114
- ch = h.upsample_initial_channel//(2**(i+1))
115
- for j, (k, d) in enumerate(zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)):
116
- self.resblocks.append(resblock(h, ch, k, d))
117
-
118
- self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
119
- self.ups.apply(init_weights)
120
- self.conv_post.apply(init_weights)
121
-
122
- def forward(self, x):
123
- x = self.conv_pre(x)
124
- for i in range(self.num_upsamples):
125
- x = F.leaky_relu(x, LRELU_SLOPE)
126
- x = self.ups[i](x)
127
- xs = None
128
- for j in range(self.num_kernels):
129
- if xs is None:
130
- xs = self.resblocks[i*self.num_kernels+j](x)
131
- else:
132
- xs += self.resblocks[i*self.num_kernels+j](x)
133
- x = xs / self.num_kernels
134
- x = F.leaky_relu(x)
135
- x = self.conv_post(x)
136
- x = torch.tanh(x)
137
-
138
- return x
139
-
140
- def remove_weight_norm(self):
141
- print('Removing weight norm...')
142
- for l in self.ups:
143
- remove_weight_norm(l)
144
- for l in self.resblocks:
145
- l.remove_weight_norm()
146
- remove_weight_norm(self.conv_pre)
147
- remove_weight_norm(self.conv_post)
148
- class SineGen(torch.nn.Module):
149
- """ Definition of sine generator
150
- SineGen(samp_rate, harmonic_num = 0,
151
- sine_amp = 0.1, noise_std = 0.003,
152
- voiced_threshold = 0,
153
- flag_for_pulse=False)
154
- samp_rate: sampling rate in Hz
155
- harmonic_num: number of harmonic overtones (default 0)
156
- sine_amp: amplitude of sine-wavefrom (default 0.1)
157
- noise_std: std of Gaussian noise (default 0.003)
158
- voiced_thoreshold: F0 threshold for U/V classification (default 0)
159
- flag_for_pulse: this SinGen is used inside PulseGen (default False)
160
- Note: when flag_for_pulse is True, the first time step of a voiced
161
- segment is always sin(np.pi) or cos(0)
162
- """
163
-
164
- def __init__(self, samp_rate, harmonic_num=0,
165
- sine_amp=0.1, noise_std=0.003,
166
- voiced_threshold=0,
167
- flag_for_pulse=False):
168
- super(SineGen, self).__init__()
169
- self.sine_amp = sine_amp
170
- self.noise_std = noise_std
171
- self.harmonic_num = harmonic_num
172
- self.dim = self.harmonic_num + 1
173
- self.sampling_rate = samp_rate
174
- self.voiced_threshold = voiced_threshold
175
- self.flag_for_pulse = flag_for_pulse
176
-
177
- def _f02uv(self, f0):
178
- # generate uv signal
179
- uv = torch.ones_like(f0)
180
- uv = uv * (f0 > self.voiced_threshold)
181
- return uv
182
-
183
- def _f02sine(self, f0_values):
184
- """ f0_values: (batchsize, length, dim)
185
- where dim indicates fundamental tone and overtones
186
- """
187
- # convert to F0 in rad. The interger part n can be ignored
188
- # because 2 * np.pi * n doesn't affect phase
189
- rad_values = (f0_values / self.sampling_rate) % 1
190
-
191
- # initial phase noise (no noise for fundamental component)
192
- rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
193
- device=f0_values.device)
194
- rand_ini[:, 0] = 0
195
- rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
196
-
197
- # instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
198
- if not self.flag_for_pulse:
199
- # for normal case
200
-
201
- # To prevent torch.cumsum numerical overflow,
202
- # it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
203
- # Buffer tmp_over_one_idx indicates the time step to add -1.
204
- # This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
205
- tmp_over_one = torch.cumsum(rad_values, 1) % 1
206
- tmp_over_one_idx = (tmp_over_one[:, 1:, :] -
207
- tmp_over_one[:, :-1, :]) < 0
208
- cumsum_shift = torch.zeros_like(rad_values)
209
- cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
210
-
211
- sines = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1)
212
- * 2 * np.pi)
213
- else:
214
- # If necessary, make sure that the first time step of every
215
- # voiced segments is sin(pi) or cos(0)
216
- # This is used for pulse-train generation
217
-
218
- # identify the last time step in unvoiced segments
219
- uv = self._f02uv(f0_values)
220
- uv_1 = torch.roll(uv, shifts=-1, dims=1)
221
- uv_1[:, -1, :] = 1
222
- u_loc = (uv < 1) * (uv_1 > 0)
223
-
224
- # get the instantanouse phase
225
- tmp_cumsum = torch.cumsum(rad_values, dim=1)
226
- # different batch needs to be processed differently
227
- for idx in range(f0_values.shape[0]):
228
- temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
229
- temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
230
- # stores the accumulation of i.phase within
231
- # each voiced segments
232
- tmp_cumsum[idx, :, :] = 0
233
- tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
234
-
235
- # rad_values - tmp_cumsum: remove the accumulation of i.phase
236
- # within the previous voiced segment.
237
- i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
238
-
239
- # get the sines
240
- sines = torch.cos(i_phase * 2 * np.pi)
241
- return sines
242
-
243
- def forward(self, f0):
244
- """ sine_tensor, uv = forward(f0)
245
- input F0: tensor(batchsize=1, length, dim=1)
246
- f0 for unvoiced steps should be 0
247
- output sine_tensor: tensor(batchsize=1, length, dim)
248
- output uv: tensor(batchsize=1, length, 1)
249
- """
250
- with torch.no_grad():
251
- f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,
252
- device=f0.device)
253
- # fundamental component
254
- f0_buf[:, :, 0] = f0[:, :, 0]
255
- for idx in np.arange(self.harmonic_num):
256
- # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
257
- f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (idx + 2)
258
-
259
- # generate sine waveforms
260
- sine_waves = self._f02sine(f0_buf) * self.sine_amp
261
-
262
- # generate uv signal
263
- # uv = torch.ones(f0.shape)
264
- # uv = uv * (f0 > self.voiced_threshold)
265
- uv = self._f02uv(f0)
266
-
267
- # noise: for unvoiced should be similar to sine_amp
268
- # std = self.sine_amp/3 -> max value ~ self.sine_amp
269
- # . for voiced regions is self.noise_std
270
- noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
271
- noise = noise_amp * torch.randn_like(sine_waves)
272
-
273
- # first: set the unvoiced part to 0 by uv
274
- # then: additive noise
275
- sine_waves = sine_waves * uv + noise
276
- return sine_waves, uv, noise
277
- class SourceModuleHnNSF(torch.nn.Module):
278
- """ SourceModule for hn-nsf
279
- SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
280
- add_noise_std=0.003, voiced_threshod=0)
281
- sampling_rate: sampling_rate in Hz
282
- harmonic_num: number of harmonic above F0 (default: 0)
283
- sine_amp: amplitude of sine source signal (default: 0.1)
284
- add_noise_std: std of additive Gaussian noise (default: 0.003)
285
- note that amplitude of noise in unvoiced is decided
286
- by sine_amp
287
- voiced_threshold: threhold to set U/V given F0 (default: 0)
288
- Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
289
- F0_sampled (batchsize, length, 1)
290
- Sine_source (batchsize, length, 1)
291
- noise_source (batchsize, length 1)
292
- uv (batchsize, length, 1)
293
- """
294
-
295
- def __init__(self, sampling_rate, harmonic_num=0, sine_amp=0.1,
296
- add_noise_std=0.003, voiced_threshod=0):
297
- super(SourceModuleHnNSF, self).__init__()
298
-
299
- self.sine_amp = sine_amp
300
- self.noise_std = add_noise_std
301
-
302
- # to produce sine waveforms
303
- self.l_sin_gen = SineGen(sampling_rate, harmonic_num,
304
- sine_amp, add_noise_std, voiced_threshod)
305
-
306
- # to merge source harmonics into a single excitation
307
- self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
308
- self.l_tanh = torch.nn.Tanh()
309
-
310
- def forward(self, x):
311
- """
312
- Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
313
- F0_sampled (batchsize, length, 1)
314
- Sine_source (batchsize, length, 1)
315
- noise_source (batchsize, length 1)
316
- """
317
- # source for harmonic branch
318
- sine_wavs, uv, _ = self.l_sin_gen(x)
319
- sine_merge = self.l_tanh(self.l_linear(sine_wavs))
320
-
321
- # source for noise branch, in the same shape as uv
322
- noise = torch.randn_like(uv) * self.sine_amp / 3
323
- return sine_merge, noise, uv
324
-
325
- class Generator(torch.nn.Module):
326
- def __init__(self, h):
327
- super(Generator, self).__init__()
328
- self.h = h
329
- self.num_kernels = len(h.resblock_kernel_sizes)
330
- self.num_upsamples = len(h.upsample_rates)
331
- self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(h.upsample_rates))
332
- self.m_source = SourceModuleHnNSF(
333
- sampling_rate=h.sampling_rate,
334
- harmonic_num=8)
335
- self.noise_convs = nn.ModuleList()
336
- self.conv_pre = weight_norm(Conv1d(h.num_mels, h.upsample_initial_channel, 7, 1, padding=3))
337
- resblock = ResBlock1 if h.resblock == '1' else ResBlock2
338
-
339
- self.ups = nn.ModuleList()
340
- for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):
341
- c_cur = h.upsample_initial_channel // (2 ** (i + 1))
342
- self.ups.append(weight_norm(
343
- ConvTranspose1d(h.upsample_initial_channel//(2**i), h.upsample_initial_channel//(2**(i+1)),
344
- k, u, padding=(k-u)//2)))
345
- if i + 1 < len(h.upsample_rates):#
346
- stride_f0 = np.prod(h.upsample_rates[i + 1:])
347
- self.noise_convs.append(Conv1d(
348
- 1, c_cur, kernel_size=stride_f0 * 2, stride=stride_f0, padding=stride_f0 // 2))
349
- else:
350
- self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
351
- self.resblocks = nn.ModuleList()
352
- for i in range(len(self.ups)):
353
- ch = h.upsample_initial_channel//(2**(i+1))
354
- for j, (k, d) in enumerate(zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)):
355
- self.resblocks.append(resblock(h, ch, k, d))
356
-
357
- self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
358
- self.ups.apply(init_weights)
359
- self.conv_post.apply(init_weights)
360
-
361
- def forward(self, x,f0):
362
- # print(1,x.shape,f0.shape,f0[:, None].shape)
363
- f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2)#bs,n,t
364
- # print(2,f0.shape)
365
- har_source, noi_source, uv = self.m_source(f0)
366
- har_source = har_source.transpose(1, 2)
367
- x = self.conv_pre(x)
368
- # print(124,x.shape,har_source.shape)
369
- for i in range(self.num_upsamples):
370
- x = F.leaky_relu(x, LRELU_SLOPE)
371
- # print(3,x.shape)
372
- x = self.ups[i](x)
373
- x_source = self.noise_convs[i](har_source)
374
- # print(4,x_source.shape,har_source.shape,x.shape)
375
- x = x + x_source
376
- xs = None
377
- for j in range(self.num_kernels):
378
- if xs is None:
379
- xs = self.resblocks[i*self.num_kernels+j](x)
380
- else:
381
- xs += self.resblocks[i*self.num_kernels+j](x)
382
- x = xs / self.num_kernels
383
- x = F.leaky_relu(x)
384
- x = self.conv_post(x)
385
- x = torch.tanh(x)
386
-
387
- return x
388
-
389
- def remove_weight_norm(self):
390
- print('Removing weight norm...')
391
- for l in self.ups:
392
- remove_weight_norm(l)
393
- for l in self.resblocks:
394
- l.remove_weight_norm()
395
- remove_weight_norm(self.conv_pre)
396
- remove_weight_norm(self.conv_post)
397
-
398
- class DiscriminatorP(torch.nn.Module):
399
- def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
400
- super(DiscriminatorP, self).__init__()
401
- self.period = period
402
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
403
- self.convs = nn.ModuleList([
404
- norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
405
- norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
406
- norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
407
- norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
408
- norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
409
- ])
410
- self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
411
-
412
- def forward(self, x):
413
- fmap = []
414
-
415
- # 1d to 2d
416
- b, c, t = x.shape
417
- if t % self.period != 0: # pad first
418
- n_pad = self.period - (t % self.period)
419
- x = F.pad(x, (0, n_pad), "reflect")
420
- t = t + n_pad
421
- x = x.view(b, c, t // self.period, self.period)
422
-
423
- for l in self.convs:
424
- x = l(x)
425
- x = F.leaky_relu(x, LRELU_SLOPE)
426
- fmap.append(x)
427
- x = self.conv_post(x)
428
- fmap.append(x)
429
- x = torch.flatten(x, 1, -1)
430
-
431
- return x, fmap
432
-
433
-
434
- class MultiPeriodDiscriminator(torch.nn.Module):
435
- def __init__(self, periods=None):
436
- super(MultiPeriodDiscriminator, self).__init__()
437
- self.periods = periods if periods is not None else [2, 3, 5, 7, 11]
438
- self.discriminators = nn.ModuleList()
439
- for period in self.periods:
440
- self.discriminators.append(DiscriminatorP(period))
441
-
442
- def forward(self, y, y_hat):
443
- y_d_rs = []
444
- y_d_gs = []
445
- fmap_rs = []
446
- fmap_gs = []
447
- for i, d in enumerate(self.discriminators):
448
- y_d_r, fmap_r = d(y)
449
- y_d_g, fmap_g = d(y_hat)
450
- y_d_rs.append(y_d_r)
451
- fmap_rs.append(fmap_r)
452
- y_d_gs.append(y_d_g)
453
- fmap_gs.append(fmap_g)
454
-
455
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
456
-
457
-
458
- class DiscriminatorS(torch.nn.Module):
459
- def __init__(self, use_spectral_norm=False):
460
- super(DiscriminatorS, self).__init__()
461
- norm_f = weight_norm if use_spectral_norm == False else spectral_norm
462
- self.convs = nn.ModuleList([
463
- norm_f(Conv1d(1, 128, 15, 1, padding=7)),
464
- norm_f(Conv1d(128, 128, 41, 2, groups=4, padding=20)),
465
- norm_f(Conv1d(128, 256, 41, 2, groups=16, padding=20)),
466
- norm_f(Conv1d(256, 512, 41, 4, groups=16, padding=20)),
467
- norm_f(Conv1d(512, 1024, 41, 4, groups=16, padding=20)),
468
- norm_f(Conv1d(1024, 1024, 41, 1, groups=16, padding=20)),
469
- norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
470
- ])
471
- self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
472
-
473
- def forward(self, x):
474
- fmap = []
475
- for l in self.convs:
476
- x = l(x)
477
- x = F.leaky_relu(x, LRELU_SLOPE)
478
- fmap.append(x)
479
- x = self.conv_post(x)
480
- fmap.append(x)
481
- x = torch.flatten(x, 1, -1)
482
-
483
- return x, fmap
484
-
485
-
486
- class MultiScaleDiscriminator(torch.nn.Module):
487
- def __init__(self):
488
- super(MultiScaleDiscriminator, self).__init__()
489
- self.discriminators = nn.ModuleList([
490
- DiscriminatorS(use_spectral_norm=True),
491
- DiscriminatorS(),
492
- DiscriminatorS(),
493
- ])
494
- self.meanpools = nn.ModuleList([
495
- AvgPool1d(4, 2, padding=2),
496
- AvgPool1d(4, 2, padding=2)
497
- ])
498
-
499
- def forward(self, y, y_hat):
500
- y_d_rs = []
501
- y_d_gs = []
502
- fmap_rs = []
503
- fmap_gs = []
504
- for i, d in enumerate(self.discriminators):
505
- if i != 0:
506
- y = self.meanpools[i-1](y)
507
- y_hat = self.meanpools[i-1](y_hat)
508
- y_d_r, fmap_r = d(y)
509
- y_d_g, fmap_g = d(y_hat)
510
- y_d_rs.append(y_d_r)
511
- fmap_rs.append(fmap_r)
512
- y_d_gs.append(y_d_g)
513
- fmap_gs.append(fmap_g)
514
-
515
- return y_d_rs, y_d_gs, fmap_rs, fmap_gs
516
-
517
-
518
- def feature_loss(fmap_r, fmap_g):
519
- loss = 0
520
- for dr, dg in zip(fmap_r, fmap_g):
521
- for rl, gl in zip(dr, dg):
522
- loss += torch.mean(torch.abs(rl - gl))
523
-
524
- return loss*2
525
-
526
-
527
- def discriminator_loss(disc_real_outputs, disc_generated_outputs):
528
- loss = 0
529
- r_losses = []
530
- g_losses = []
531
- for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
532
- r_loss = torch.mean((1-dr)**2)
533
- g_loss = torch.mean(dg**2)
534
- loss += (r_loss + g_loss)
535
- r_losses.append(r_loss.item())
536
- g_losses.append(g_loss.item())
537
-
538
- return loss, r_losses, g_losses
539
-
540
-
541
- def generator_loss(disc_outputs):
542
- loss = 0
543
- gen_losses = []
544
- for dg in disc_outputs:
545
- l = torch.mean((1-dg)**2)
546
- gen_losses.append(l)
547
- loss += l
548
-
549
- return loss, gen_losses
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/nsf_hifigan/nvSTFT.py DELETED
@@ -1,111 +0,0 @@
1
- import math
2
- import os
3
- os.environ["LRU_CACHE_CAPACITY"] = "3"
4
- import random
5
- import torch
6
- import torch.utils.data
7
- import numpy as np
8
- import librosa
9
- from librosa.util import normalize
10
- from librosa.filters import mel as librosa_mel_fn
11
- from scipy.io.wavfile import read
12
- import soundfile as sf
13
-
14
- def load_wav_to_torch(full_path, target_sr=None, return_empty_on_exception=False):
15
- sampling_rate = None
16
- try:
17
- data, sampling_rate = sf.read(full_path, always_2d=True)# than soundfile.
18
- except Exception as ex:
19
- print(f"'{full_path}' failed to load.\nException:")
20
- print(ex)
21
- if return_empty_on_exception:
22
- return [], sampling_rate or target_sr or 48000
23
- else:
24
- raise Exception(ex)
25
-
26
- if len(data.shape) > 1:
27
- data = data[:, 0]
28
- assert len(data) > 2# check duration of audio file is > 2 samples (because otherwise the slice operation was on the wrong dimension)
29
-
30
- if np.issubdtype(data.dtype, np.integer): # if audio data is type int
31
- max_mag = -np.iinfo(data.dtype).min # maximum magnitude = min possible value of intXX
32
- else: # if audio data is type fp32
33
- max_mag = max(np.amax(data), -np.amin(data))
34
- max_mag = (2**31)+1 if max_mag > (2**15) else ((2**15)+1 if max_mag > 1.01 else 1.0) # data should be either 16-bit INT, 32-bit INT or [-1 to 1] float32
35
-
36
- data = torch.FloatTensor(data.astype(np.float32))/max_mag
37
-
38
- if (torch.isinf(data) | torch.isnan(data)).any() and return_empty_on_exception:# resample will crash with inf/NaN inputs. return_empty_on_exception will return empty arr instead of except
39
- return [], sampling_rate or target_sr or 48000
40
- if target_sr is not None and sampling_rate != target_sr:
41
- data = torch.from_numpy(librosa.core.resample(data.numpy(), orig_sr=sampling_rate, target_sr=target_sr))
42
- sampling_rate = target_sr
43
-
44
- return data, sampling_rate
45
-
46
- def dynamic_range_compression(x, C=1, clip_val=1e-5):
47
- return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
48
-
49
- def dynamic_range_decompression(x, C=1):
50
- return np.exp(x) / C
51
-
52
- def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
53
- return torch.log(torch.clamp(x, min=clip_val) * C)
54
-
55
- def dynamic_range_decompression_torch(x, C=1):
56
- return torch.exp(x) / C
57
-
58
- class STFT():
59
- def __init__(self, sr=22050, n_mels=80, n_fft=1024, win_size=1024, hop_length=256, fmin=20, fmax=11025, clip_val=1e-5):
60
- self.target_sr = sr
61
-
62
- self.n_mels = n_mels
63
- self.n_fft = n_fft
64
- self.win_size = win_size
65
- self.hop_length = hop_length
66
- self.fmin = fmin
67
- self.fmax = fmax
68
- self.clip_val = clip_val
69
- self.mel_basis = {}
70
- self.hann_window = {}
71
-
72
- def get_mel(self, y, center=False):
73
- sampling_rate = self.target_sr
74
- n_mels = self.n_mels
75
- n_fft = self.n_fft
76
- win_size = self.win_size
77
- hop_length = self.hop_length
78
- fmin = self.fmin
79
- fmax = self.fmax
80
- clip_val = self.clip_val
81
-
82
- if torch.min(y) < -1.:
83
- print('min value is ', torch.min(y))
84
- if torch.max(y) > 1.:
85
- print('max value is ', torch.max(y))
86
-
87
- if fmax not in self.mel_basis:
88
- mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax)
89
- self.mel_basis[str(fmax)+'_'+str(y.device)] = torch.from_numpy(mel).float().to(y.device)
90
- self.hann_window[str(y.device)] = torch.hann_window(self.win_size).to(y.device)
91
-
92
- y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_length)/2), int((n_fft-hop_length)/2)), mode='reflect')
93
- y = y.squeeze(1)
94
-
95
- spec = torch.stft(y, n_fft, hop_length=hop_length, win_length=win_size, window=self.hann_window[str(y.device)],
96
- center=center, pad_mode='reflect', normalized=False, onesided=True)
97
- # print(111,spec)
98
- spec = torch.sqrt(spec.pow(2).sum(-1)+(1e-9))
99
- # print(222,spec)
100
- spec = torch.matmul(self.mel_basis[str(fmax)+'_'+str(y.device)], spec)
101
- # print(333,spec)
102
- spec = dynamic_range_compression_torch(spec, clip_val=clip_val)
103
- # print(444,spec)
104
- return spec
105
-
106
- def __call__(self, audiopath):
107
- audio, sr = load_wav_to_torch(audiopath, target_sr=self.target_sr)
108
- spect = self.get_mel(audio.unsqueeze(0)).squeeze(0)
109
- return spect
110
-
111
- stft = STFT()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/nsf_hifigan/utils.py DELETED
@@ -1,67 +0,0 @@
1
- import glob
2
- import os
3
- import matplotlib
4
- import torch
5
- from torch.nn.utils import weight_norm
6
- matplotlib.use("Agg")
7
- import matplotlib.pylab as plt
8
-
9
-
10
- def plot_spectrogram(spectrogram):
11
- fig, ax = plt.subplots(figsize=(10, 2))
12
- im = ax.imshow(spectrogram, aspect="auto", origin="lower",
13
- interpolation='none')
14
- plt.colorbar(im, ax=ax)
15
-
16
- fig.canvas.draw()
17
- plt.close()
18
-
19
- return fig
20
-
21
-
22
- def init_weights(m, mean=0.0, std=0.01):
23
- classname = m.__class__.__name__
24
- if classname.find("Conv") != -1:
25
- m.weight.data.normal_(mean, std)
26
-
27
-
28
- def apply_weight_norm(m):
29
- classname = m.__class__.__name__
30
- if classname.find("Conv") != -1:
31
- weight_norm(m)
32
-
33
-
34
- def get_padding(kernel_size, dilation=1):
35
- return int((kernel_size*dilation - dilation)/2)
36
-
37
-
38
- def load_checkpoint(filepath, device):
39
- assert os.path.isfile(filepath)
40
- print("Loading '{}'".format(filepath))
41
- checkpoint_dict = torch.load(filepath, map_location=device)
42
- print("Complete.")
43
- return checkpoint_dict
44
-
45
-
46
- def save_checkpoint(filepath, obj):
47
- print("Saving checkpoint to {}".format(filepath))
48
- torch.save(obj, filepath)
49
- print("Complete.")
50
-
51
-
52
- def del_old_checkpoints(cp_dir, prefix, n_models=2):
53
- pattern = os.path.join(cp_dir, prefix + '????????')
54
- cp_list = glob.glob(pattern) # get checkpoint paths
55
- cp_list = sorted(cp_list)# sort by iter
56
- if len(cp_list) > n_models: # if more than n_models models are found
57
- for cp in cp_list[:-n_models]:# delete the oldest models other than lastest n_models
58
- open(cp, 'w').close()# empty file contents
59
- os.unlink(cp)# delete file (move to trash when using Colab)
60
-
61
-
62
- def scan_checkpoint(cp_dir, prefix):
63
- pattern = os.path.join(cp_dir, prefix + '????????')
64
- cp_list = glob.glob(pattern)
65
- if len(cp_list) == 0:
66
- return None
67
- return sorted(cp_list)[-1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/__init__.py DELETED
File without changes
modules/parallel_wavegan/__pycache__/__init__.cpython-38.pyc DELETED
Binary file (145 Bytes)
 
modules/parallel_wavegan/layers/__init__.py DELETED
@@ -1,5 +0,0 @@
1
- from .causal_conv import * # NOQA
2
- from .pqmf import * # NOQA
3
- from .residual_block import * # NOQA
4
- from modules.parallel_wavegan.layers.residual_stack import * # NOQA
5
- from .upsample import * # NOQA
 
 
 
 
 
 
modules/parallel_wavegan/layers/__pycache__/__init__.cpython-38.pyc DELETED
Binary file (305 Bytes)
 
modules/parallel_wavegan/layers/__pycache__/causal_conv.cpython-38.pyc DELETED
Binary file (2.18 kB)
 
modules/parallel_wavegan/layers/__pycache__/pqmf.cpython-38.pyc DELETED
Binary file (3.89 kB)
 
modules/parallel_wavegan/layers/__pycache__/residual_block.cpython-38.pyc DELETED
Binary file (4.12 kB)
 
modules/parallel_wavegan/layers/__pycache__/residual_stack.cpython-38.pyc DELETED
Binary file (2.42 kB)
 
modules/parallel_wavegan/layers/__pycache__/upsample.cpython-38.pyc DELETED
Binary file (5.92 kB)
 
modules/parallel_wavegan/layers/causal_conv.py DELETED
@@ -1,56 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2020 Tomoki Hayashi
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """Causal convolusion layer modules."""
7
-
8
-
9
- import torch
10
-
11
-
12
- class CausalConv1d(torch.nn.Module):
13
- """CausalConv1d module with customized initialization."""
14
-
15
- def __init__(self, in_channels, out_channels, kernel_size,
16
- dilation=1, bias=True, pad="ConstantPad1d", pad_params={"value": 0.0}):
17
- """Initialize CausalConv1d module."""
18
- super(CausalConv1d, self).__init__()
19
- self.pad = getattr(torch.nn, pad)((kernel_size - 1) * dilation, **pad_params)
20
- self.conv = torch.nn.Conv1d(in_channels, out_channels, kernel_size,
21
- dilation=dilation, bias=bias)
22
-
23
- def forward(self, x):
24
- """Calculate forward propagation.
25
-
26
- Args:
27
- x (Tensor): Input tensor (B, in_channels, T).
28
-
29
- Returns:
30
- Tensor: Output tensor (B, out_channels, T).
31
-
32
- """
33
- return self.conv(self.pad(x))[:, :, :x.size(2)]
34
-
35
-
36
- class CausalConvTranspose1d(torch.nn.Module):
37
- """CausalConvTranspose1d module with customized initialization."""
38
-
39
- def __init__(self, in_channels, out_channels, kernel_size, stride, bias=True):
40
- """Initialize CausalConvTranspose1d module."""
41
- super(CausalConvTranspose1d, self).__init__()
42
- self.deconv = torch.nn.ConvTranspose1d(
43
- in_channels, out_channels, kernel_size, stride, bias=bias)
44
- self.stride = stride
45
-
46
- def forward(self, x):
47
- """Calculate forward propagation.
48
-
49
- Args:
50
- x (Tensor): Input tensor (B, in_channels, T_in).
51
-
52
- Returns:
53
- Tensor: Output tensor (B, out_channels, T_out).
54
-
55
- """
56
- return self.deconv(x)[:, :, :-self.stride]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/layers/pqmf.py DELETED
@@ -1,129 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2020 Tomoki Hayashi
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """Pseudo QMF modules."""
7
-
8
- import numpy as np
9
- import torch
10
- import torch.nn.functional as F
11
-
12
- from scipy.signal import kaiser
13
-
14
-
15
- def design_prototype_filter(taps=62, cutoff_ratio=0.15, beta=9.0):
16
- """Design prototype filter for PQMF.
17
-
18
- This method is based on `A Kaiser window approach for the design of prototype
19
- filters of cosine modulated filterbanks`_.
20
-
21
- Args:
22
- taps (int): The number of filter taps.
23
- cutoff_ratio (float): Cut-off frequency ratio.
24
- beta (float): Beta coefficient for kaiser window.
25
-
26
- Returns:
27
- ndarray: Impluse response of prototype filter (taps + 1,).
28
-
29
- .. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`:
30
- https://ieeexplore.ieee.org/abstract/document/681427
31
-
32
- """
33
- # check the arguments are valid
34
- assert taps % 2 == 0, "The number of taps mush be even number."
35
- assert 0.0 < cutoff_ratio < 1.0, "Cutoff ratio must be > 0.0 and < 1.0."
36
-
37
- # make initial filter
38
- omega_c = np.pi * cutoff_ratio
39
- with np.errstate(invalid='ignore'):
40
- h_i = np.sin(omega_c * (np.arange(taps + 1) - 0.5 * taps)) \
41
- / (np.pi * (np.arange(taps + 1) - 0.5 * taps))
42
- h_i[taps // 2] = np.cos(0) * cutoff_ratio # fix nan due to indeterminate form
43
-
44
- # apply kaiser window
45
- w = kaiser(taps + 1, beta)
46
- h = h_i * w
47
-
48
- return h
49
-
50
-
51
- class PQMF(torch.nn.Module):
52
- """PQMF module.
53
-
54
- This module is based on `Near-perfect-reconstruction pseudo-QMF banks`_.
55
-
56
- .. _`Near-perfect-reconstruction pseudo-QMF banks`:
57
- https://ieeexplore.ieee.org/document/258122
58
-
59
- """
60
-
61
- def __init__(self, subbands=4, taps=62, cutoff_ratio=0.15, beta=9.0):
62
- """Initilize PQMF module.
63
-
64
- Args:
65
- subbands (int): The number of subbands.
66
- taps (int): The number of filter taps.
67
- cutoff_ratio (float): Cut-off frequency ratio.
68
- beta (float): Beta coefficient for kaiser window.
69
-
70
- """
71
- super(PQMF, self).__init__()
72
-
73
- # define filter coefficient
74
- h_proto = design_prototype_filter(taps, cutoff_ratio, beta)
75
- h_analysis = np.zeros((subbands, len(h_proto)))
76
- h_synthesis = np.zeros((subbands, len(h_proto)))
77
- for k in range(subbands):
78
- h_analysis[k] = 2 * h_proto * np.cos(
79
- (2 * k + 1) * (np.pi / (2 * subbands)) *
80
- (np.arange(taps + 1) - ((taps - 1) / 2)) +
81
- (-1) ** k * np.pi / 4)
82
- h_synthesis[k] = 2 * h_proto * np.cos(
83
- (2 * k + 1) * (np.pi / (2 * subbands)) *
84
- (np.arange(taps + 1) - ((taps - 1) / 2)) -
85
- (-1) ** k * np.pi / 4)
86
-
87
- # convert to tensor
88
- analysis_filter = torch.from_numpy(h_analysis).float().unsqueeze(1)
89
- synthesis_filter = torch.from_numpy(h_synthesis).float().unsqueeze(0)
90
-
91
- # register coefficients as beffer
92
- self.register_buffer("analysis_filter", analysis_filter)
93
- self.register_buffer("synthesis_filter", synthesis_filter)
94
-
95
- # filter for downsampling & upsampling
96
- updown_filter = torch.zeros((subbands, subbands, subbands)).float()
97
- for k in range(subbands):
98
- updown_filter[k, k, 0] = 1.0
99
- self.register_buffer("updown_filter", updown_filter)
100
- self.subbands = subbands
101
-
102
- # keep padding info
103
- self.pad_fn = torch.nn.ConstantPad1d(taps // 2, 0.0)
104
-
105
- def analysis(self, x):
106
- """Analysis with PQMF.
107
-
108
- Args:
109
- x (Tensor): Input tensor (B, 1, T).
110
-
111
- Returns:
112
- Tensor: Output tensor (B, subbands, T // subbands).
113
-
114
- """
115
- x = F.conv1d(self.pad_fn(x), self.analysis_filter)
116
- return F.conv1d(x, self.updown_filter, stride=self.subbands)
117
-
118
- def synthesis(self, x):
119
- """Synthesis with PQMF.
120
-
121
- Args:
122
- x (Tensor): Input tensor (B, subbands, T // subbands).
123
-
124
- Returns:
125
- Tensor: Output tensor (B, 1, T).
126
-
127
- """
128
- x = F.conv_transpose1d(x, self.updown_filter * self.subbands, stride=self.subbands)
129
- return F.conv1d(self.pad_fn(x), self.synthesis_filter)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/layers/residual_block.py DELETED
@@ -1,129 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- """Residual block module in WaveNet.
4
-
5
- This code is modified from https://github.com/r9y9/wavenet_vocoder.
6
-
7
- """
8
-
9
- import math
10
-
11
- import torch
12
- import torch.nn.functional as F
13
-
14
-
15
- class Conv1d(torch.nn.Conv1d):
16
- """Conv1d module with customized initialization."""
17
-
18
- def __init__(self, *args, **kwargs):
19
- """Initialize Conv1d module."""
20
- super(Conv1d, self).__init__(*args, **kwargs)
21
-
22
- def reset_parameters(self):
23
- """Reset parameters."""
24
- torch.nn.init.kaiming_normal_(self.weight, nonlinearity="relu")
25
- if self.bias is not None:
26
- torch.nn.init.constant_(self.bias, 0.0)
27
-
28
-
29
- class Conv1d1x1(Conv1d):
30
- """1x1 Conv1d with customized initialization."""
31
-
32
- def __init__(self, in_channels, out_channels, bias):
33
- """Initialize 1x1 Conv1d module."""
34
- super(Conv1d1x1, self).__init__(in_channels, out_channels,
35
- kernel_size=1, padding=0,
36
- dilation=1, bias=bias)
37
-
38
-
39
- class ResidualBlock(torch.nn.Module):
40
- """Residual block module in WaveNet."""
41
-
42
- def __init__(self,
43
- kernel_size=3,
44
- residual_channels=64,
45
- gate_channels=128,
46
- skip_channels=64,
47
- aux_channels=80,
48
- dropout=0.0,
49
- dilation=1,
50
- bias=True,
51
- use_causal_conv=False
52
- ):
53
- """Initialize ResidualBlock module.
54
-
55
- Args:
56
- kernel_size (int): Kernel size of dilation convolution layer.
57
- residual_channels (int): Number of channels for residual connection.
58
- skip_channels (int): Number of channels for skip connection.
59
- aux_channels (int): Local conditioning channels i.e. auxiliary input dimension.
60
- dropout (float): Dropout probability.
61
- dilation (int): Dilation factor.
62
- bias (bool): Whether to add bias parameter in convolution layers.
63
- use_causal_conv (bool): Whether to use use_causal_conv or non-use_causal_conv convolution.
64
-
65
- """
66
- super(ResidualBlock, self).__init__()
67
- self.dropout = dropout
68
- # no future time stamps available
69
- if use_causal_conv:
70
- padding = (kernel_size - 1) * dilation
71
- else:
72
- assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
73
- padding = (kernel_size - 1) // 2 * dilation
74
- self.use_causal_conv = use_causal_conv
75
-
76
- # dilation conv
77
- self.conv = Conv1d(residual_channels, gate_channels, kernel_size,
78
- padding=padding, dilation=dilation, bias=bias)
79
-
80
- # local conditioning
81
- if aux_channels > 0:
82
- self.conv1x1_aux = Conv1d1x1(aux_channels, gate_channels, bias=False)
83
- else:
84
- self.conv1x1_aux = None
85
-
86
- # conv output is split into two groups
87
- gate_out_channels = gate_channels // 2
88
- self.conv1x1_out = Conv1d1x1(gate_out_channels, residual_channels, bias=bias)
89
- self.conv1x1_skip = Conv1d1x1(gate_out_channels, skip_channels, bias=bias)
90
-
91
- def forward(self, x, c):
92
- """Calculate forward propagation.
93
-
94
- Args:
95
- x (Tensor): Input tensor (B, residual_channels, T).
96
- c (Tensor): Local conditioning auxiliary tensor (B, aux_channels, T).
97
-
98
- Returns:
99
- Tensor: Output tensor for residual connection (B, residual_channels, T).
100
- Tensor: Output tensor for skip connection (B, skip_channels, T).
101
-
102
- """
103
- residual = x
104
- x = F.dropout(x, p=self.dropout, training=self.training)
105
- x = self.conv(x)
106
-
107
- # remove future time steps if use_causal_conv conv
108
- x = x[:, :, :residual.size(-1)] if self.use_causal_conv else x
109
-
110
- # split into two part for gated activation
111
- splitdim = 1
112
- xa, xb = x.split(x.size(splitdim) // 2, dim=splitdim)
113
-
114
- # local conditioning
115
- if c is not None:
116
- assert self.conv1x1_aux is not None
117
- c = self.conv1x1_aux(c)
118
- ca, cb = c.split(c.size(splitdim) // 2, dim=splitdim)
119
- xa, xb = xa + ca, xb + cb
120
-
121
- x = torch.tanh(xa) * torch.sigmoid(xb)
122
-
123
- # for skip connection
124
- s = self.conv1x1_skip(x)
125
-
126
- # for residual connection
127
- x = (self.conv1x1_out(x) + residual) * math.sqrt(0.5)
128
-
129
- return x, s
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/layers/residual_stack.py DELETED
@@ -1,75 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2020 Tomoki Hayashi
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """Residual stack module in MelGAN."""
7
-
8
- import torch
9
-
10
- from . import CausalConv1d
11
-
12
-
13
- class ResidualStack(torch.nn.Module):
14
- """Residual stack module introduced in MelGAN."""
15
-
16
- def __init__(self,
17
- kernel_size=3,
18
- channels=32,
19
- dilation=1,
20
- bias=True,
21
- nonlinear_activation="LeakyReLU",
22
- nonlinear_activation_params={"negative_slope": 0.2},
23
- pad="ReflectionPad1d",
24
- pad_params={},
25
- use_causal_conv=False,
26
- ):
27
- """Initialize ResidualStack module.
28
-
29
- Args:
30
- kernel_size (int): Kernel size of dilation convolution layer.
31
- channels (int): Number of channels of convolution layers.
32
- dilation (int): Dilation factor.
33
- bias (bool): Whether to add bias parameter in convolution layers.
34
- nonlinear_activation (str): Activation function module name.
35
- nonlinear_activation_params (dict): Hyperparameters for activation function.
36
- pad (str): Padding function module name before dilated convolution layer.
37
- pad_params (dict): Hyperparameters for padding function.
38
- use_causal_conv (bool): Whether to use causal convolution.
39
-
40
- """
41
- super(ResidualStack, self).__init__()
42
-
43
- # defile residual stack part
44
- if not use_causal_conv:
45
- assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
46
- self.stack = torch.nn.Sequential(
47
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
48
- getattr(torch.nn, pad)((kernel_size - 1) // 2 * dilation, **pad_params),
49
- torch.nn.Conv1d(channels, channels, kernel_size, dilation=dilation, bias=bias),
50
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
51
- torch.nn.Conv1d(channels, channels, 1, bias=bias),
52
- )
53
- else:
54
- self.stack = torch.nn.Sequential(
55
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
56
- CausalConv1d(channels, channels, kernel_size, dilation=dilation,
57
- bias=bias, pad=pad, pad_params=pad_params),
58
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
59
- torch.nn.Conv1d(channels, channels, 1, bias=bias),
60
- )
61
-
62
- # defile extra layer for skip connection
63
- self.skip_layer = torch.nn.Conv1d(channels, channels, 1, bias=bias)
64
-
65
- def forward(self, c):
66
- """Calculate forward propagation.
67
-
68
- Args:
69
- c (Tensor): Input tensor (B, channels, T).
70
-
71
- Returns:
72
- Tensor: Output tensor (B, chennels, T).
73
-
74
- """
75
- return self.stack(c) + self.skip_layer(c)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/layers/tf_layers.py DELETED
@@ -1,129 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2020 MINH ANH (@dathudeptrai)
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """Tensorflow Layer modules complatible with pytorch."""
7
-
8
- import tensorflow as tf
9
-
10
-
11
- class TFReflectionPad1d(tf.keras.layers.Layer):
12
- """Tensorflow ReflectionPad1d module."""
13
-
14
- def __init__(self, padding_size):
15
- """Initialize TFReflectionPad1d module.
16
-
17
- Args:
18
- padding_size (int): Padding size.
19
-
20
- """
21
- super(TFReflectionPad1d, self).__init__()
22
- self.padding_size = padding_size
23
-
24
- @tf.function
25
- def call(self, x):
26
- """Calculate forward propagation.
27
-
28
- Args:
29
- x (Tensor): Input tensor (B, T, 1, C).
30
-
31
- Returns:
32
- Tensor: Padded tensor (B, T + 2 * padding_size, 1, C).
33
-
34
- """
35
- return tf.pad(x, [[0, 0], [self.padding_size, self.padding_size], [0, 0], [0, 0]], "REFLECT")
36
-
37
-
38
- class TFConvTranspose1d(tf.keras.layers.Layer):
39
- """Tensorflow ConvTranspose1d module."""
40
-
41
- def __init__(self, channels, kernel_size, stride, padding):
42
- """Initialize TFConvTranspose1d( module.
43
-
44
- Args:
45
- channels (int): Number of channels.
46
- kernel_size (int): kernel size.
47
- strides (int): Stride width.
48
- padding (str): Padding type ("same" or "valid").
49
-
50
- """
51
- super(TFConvTranspose1d, self).__init__()
52
- self.conv1d_transpose = tf.keras.layers.Conv2DTranspose(
53
- filters=channels,
54
- kernel_size=(kernel_size, 1),
55
- strides=(stride, 1),
56
- padding=padding,
57
- )
58
-
59
- @tf.function
60
- def call(self, x):
61
- """Calculate forward propagation.
62
-
63
- Args:
64
- x (Tensor): Input tensor (B, T, 1, C).
65
-
66
- Returns:
67
- Tensors: Output tensor (B, T', 1, C').
68
-
69
- """
70
- x = self.conv1d_transpose(x)
71
- return x
72
-
73
-
74
- class TFResidualStack(tf.keras.layers.Layer):
75
- """Tensorflow ResidualStack module."""
76
-
77
- def __init__(self,
78
- kernel_size,
79
- channels,
80
- dilation,
81
- bias,
82
- nonlinear_activation,
83
- nonlinear_activation_params,
84
- padding,
85
- ):
86
- """Initialize TFResidualStack module.
87
-
88
- Args:
89
- kernel_size (int): Kernel size.
90
- channles (int): Number of channels.
91
- dilation (int): Dilation ine.
92
- bias (bool): Whether to add bias parameter in convolution layers.
93
- nonlinear_activation (str): Activation function module name.
94
- nonlinear_activation_params (dict): Hyperparameters for activation function.
95
- padding (str): Padding type ("same" or "valid").
96
-
97
- """
98
- super(TFResidualStack, self).__init__()
99
- self.block = [
100
- getattr(tf.keras.layers, nonlinear_activation)(**nonlinear_activation_params),
101
- TFReflectionPad1d(dilation),
102
- tf.keras.layers.Conv2D(
103
- filters=channels,
104
- kernel_size=(kernel_size, 1),
105
- dilation_rate=(dilation, 1),
106
- use_bias=bias,
107
- padding="valid",
108
- ),
109
- getattr(tf.keras.layers, nonlinear_activation)(**nonlinear_activation_params),
110
- tf.keras.layers.Conv2D(filters=channels, kernel_size=1, use_bias=bias)
111
- ]
112
- self.shortcut = tf.keras.layers.Conv2D(filters=channels, kernel_size=1, use_bias=bias)
113
-
114
- @tf.function
115
- def call(self, x):
116
- """Calculate forward propagation.
117
-
118
- Args:
119
- x (Tensor): Input tensor (B, T, 1, C).
120
-
121
- Returns:
122
- Tensor: Output tensor (B, T, 1, C).
123
-
124
- """
125
- _x = tf.identity(x)
126
- for i, layer in enumerate(self.block):
127
- _x = layer(_x)
128
- shortcut = self.shortcut(x)
129
- return shortcut + _x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/layers/upsample.py DELETED
@@ -1,183 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- """Upsampling module.
4
-
5
- This code is modified from https://github.com/r9y9/wavenet_vocoder.
6
-
7
- """
8
-
9
- import numpy as np
10
- import torch
11
- import torch.nn.functional as F
12
-
13
- from . import Conv1d
14
-
15
-
16
- class Stretch2d(torch.nn.Module):
17
- """Stretch2d module."""
18
-
19
- def __init__(self, x_scale, y_scale, mode="nearest"):
20
- """Initialize Stretch2d module.
21
-
22
- Args:
23
- x_scale (int): X scaling factor (Time axis in spectrogram).
24
- y_scale (int): Y scaling factor (Frequency axis in spectrogram).
25
- mode (str): Interpolation mode.
26
-
27
- """
28
- super(Stretch2d, self).__init__()
29
- self.x_scale = x_scale
30
- self.y_scale = y_scale
31
- self.mode = mode
32
-
33
- def forward(self, x):
34
- """Calculate forward propagation.
35
-
36
- Args:
37
- x (Tensor): Input tensor (B, C, F, T).
38
-
39
- Returns:
40
- Tensor: Interpolated tensor (B, C, F * y_scale, T * x_scale),
41
-
42
- """
43
- return F.interpolate(
44
- x, scale_factor=(self.y_scale, self.x_scale), mode=self.mode)
45
-
46
-
47
- class Conv2d(torch.nn.Conv2d):
48
- """Conv2d module with customized initialization."""
49
-
50
- def __init__(self, *args, **kwargs):
51
- """Initialize Conv2d module."""
52
- super(Conv2d, self).__init__(*args, **kwargs)
53
-
54
- def reset_parameters(self):
55
- """Reset parameters."""
56
- self.weight.data.fill_(1. / np.prod(self.kernel_size))
57
- if self.bias is not None:
58
- torch.nn.init.constant_(self.bias, 0.0)
59
-
60
-
61
- class UpsampleNetwork(torch.nn.Module):
62
- """Upsampling network module."""
63
-
64
- def __init__(self,
65
- upsample_scales,
66
- nonlinear_activation=None,
67
- nonlinear_activation_params={},
68
- interpolate_mode="nearest",
69
- freq_axis_kernel_size=1,
70
- use_causal_conv=False,
71
- ):
72
- """Initialize upsampling network module.
73
-
74
- Args:
75
- upsample_scales (list): List of upsampling scales.
76
- nonlinear_activation (str): Activation function name.
77
- nonlinear_activation_params (dict): Arguments for specified activation function.
78
- interpolate_mode (str): Interpolation mode.
79
- freq_axis_kernel_size (int): Kernel size in the direction of frequency axis.
80
-
81
- """
82
- super(UpsampleNetwork, self).__init__()
83
- self.use_causal_conv = use_causal_conv
84
- self.up_layers = torch.nn.ModuleList()
85
- for scale in upsample_scales:
86
- # interpolation layer
87
- stretch = Stretch2d(scale, 1, interpolate_mode)
88
- self.up_layers += [stretch]
89
-
90
- # conv layer
91
- assert (freq_axis_kernel_size - 1) % 2 == 0, "Not support even number freq axis kernel size."
92
- freq_axis_padding = (freq_axis_kernel_size - 1) // 2
93
- kernel_size = (freq_axis_kernel_size, scale * 2 + 1)
94
- if use_causal_conv:
95
- padding = (freq_axis_padding, scale * 2)
96
- else:
97
- padding = (freq_axis_padding, scale)
98
- conv = Conv2d(1, 1, kernel_size=kernel_size, padding=padding, bias=False)
99
- self.up_layers += [conv]
100
-
101
- # nonlinear
102
- if nonlinear_activation is not None:
103
- nonlinear = getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params)
104
- self.up_layers += [nonlinear]
105
-
106
- def forward(self, c):
107
- """Calculate forward propagation.
108
-
109
- Args:
110
- c : Input tensor (B, C, T).
111
-
112
- Returns:
113
- Tensor: Upsampled tensor (B, C, T'), where T' = T * prod(upsample_scales).
114
-
115
- """
116
- c = c.unsqueeze(1) # (B, 1, C, T)
117
- for f in self.up_layers:
118
- if self.use_causal_conv and isinstance(f, Conv2d):
119
- c = f(c)[..., :c.size(-1)]
120
- else:
121
- c = f(c)
122
- return c.squeeze(1) # (B, C, T')
123
-
124
-
125
- class ConvInUpsampleNetwork(torch.nn.Module):
126
- """Convolution + upsampling network module."""
127
-
128
- def __init__(self,
129
- upsample_scales,
130
- nonlinear_activation=None,
131
- nonlinear_activation_params={},
132
- interpolate_mode="nearest",
133
- freq_axis_kernel_size=1,
134
- aux_channels=80,
135
- aux_context_window=0,
136
- use_causal_conv=False
137
- ):
138
- """Initialize convolution + upsampling network module.
139
-
140
- Args:
141
- upsample_scales (list): List of upsampling scales.
142
- nonlinear_activation (str): Activation function name.
143
- nonlinear_activation_params (dict): Arguments for specified activation function.
144
- mode (str): Interpolation mode.
145
- freq_axis_kernel_size (int): Kernel size in the direction of frequency axis.
146
- aux_channels (int): Number of channels of pre-convolutional layer.
147
- aux_context_window (int): Context window size of the pre-convolutional layer.
148
- use_causal_conv (bool): Whether to use causal structure.
149
-
150
- """
151
- super(ConvInUpsampleNetwork, self).__init__()
152
- self.aux_context_window = aux_context_window
153
- self.use_causal_conv = use_causal_conv and aux_context_window > 0
154
- # To capture wide-context information in conditional features
155
- kernel_size = aux_context_window + 1 if use_causal_conv else 2 * aux_context_window + 1
156
- # NOTE(kan-bayashi): Here do not use padding because the input is already padded
157
- self.conv_in = Conv1d(aux_channels, aux_channels, kernel_size=kernel_size, bias=False)
158
- self.upsample = UpsampleNetwork(
159
- upsample_scales=upsample_scales,
160
- nonlinear_activation=nonlinear_activation,
161
- nonlinear_activation_params=nonlinear_activation_params,
162
- interpolate_mode=interpolate_mode,
163
- freq_axis_kernel_size=freq_axis_kernel_size,
164
- use_causal_conv=use_causal_conv,
165
- )
166
-
167
- def forward(self, c):
168
- """Calculate forward propagation.
169
-
170
- Args:
171
- c : Input tensor (B, C, T').
172
-
173
- Returns:
174
- Tensor: Upsampled tensor (B, C, T),
175
- where T = (T' - aux_context_window * 2) * prod(upsample_scales).
176
-
177
- Note:
178
- The length of inputs considers the context window size.
179
-
180
- """
181
- c_ = self.conv_in(c)
182
- c = c_[:, :, :-self.aux_context_window] if self.use_causal_conv else c_
183
- return self.upsample(c)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/losses/__init__.py DELETED
@@ -1 +0,0 @@
1
- from .stft_loss import * # NOQA
 
 
modules/parallel_wavegan/losses/stft_loss.py DELETED
@@ -1,153 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2019 Tomoki Hayashi
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """STFT-based Loss modules."""
7
-
8
- import torch
9
- import torch.nn.functional as F
10
-
11
-
12
- def stft(x, fft_size, hop_size, win_length, window):
13
- """Perform STFT and convert to magnitude spectrogram.
14
-
15
- Args:
16
- x (Tensor): Input signal tensor (B, T).
17
- fft_size (int): FFT size.
18
- hop_size (int): Hop size.
19
- win_length (int): Window length.
20
- window (str): Window function type.
21
-
22
- Returns:
23
- Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1).
24
-
25
- """
26
- x_stft = torch.stft(x, fft_size, hop_size, win_length, window)
27
- real = x_stft[..., 0]
28
- imag = x_stft[..., 1]
29
-
30
- # NOTE(kan-bayashi): clamp is needed to avoid nan or inf
31
- return torch.sqrt(torch.clamp(real ** 2 + imag ** 2, min=1e-7)).transpose(2, 1)
32
-
33
-
34
- class SpectralConvergengeLoss(torch.nn.Module):
35
- """Spectral convergence loss module."""
36
-
37
- def __init__(self):
38
- """Initilize spectral convergence loss module."""
39
- super(SpectralConvergengeLoss, self).__init__()
40
-
41
- def forward(self, x_mag, y_mag):
42
- """Calculate forward propagation.
43
-
44
- Args:
45
- x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins).
46
- y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins).
47
-
48
- Returns:
49
- Tensor: Spectral convergence loss value.
50
-
51
- """
52
- return torch.norm(y_mag - x_mag, p="fro") / torch.norm(y_mag, p="fro")
53
-
54
-
55
- class LogSTFTMagnitudeLoss(torch.nn.Module):
56
- """Log STFT magnitude loss module."""
57
-
58
- def __init__(self):
59
- """Initilize los STFT magnitude loss module."""
60
- super(LogSTFTMagnitudeLoss, self).__init__()
61
-
62
- def forward(self, x_mag, y_mag):
63
- """Calculate forward propagation.
64
-
65
- Args:
66
- x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins).
67
- y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins).
68
-
69
- Returns:
70
- Tensor: Log STFT magnitude loss value.
71
-
72
- """
73
- return F.l1_loss(torch.log(y_mag), torch.log(x_mag))
74
-
75
-
76
- class STFTLoss(torch.nn.Module):
77
- """STFT loss module."""
78
-
79
- def __init__(self, fft_size=1024, shift_size=120, win_length=600, window="hann_window"):
80
- """Initialize STFT loss module."""
81
- super(STFTLoss, self).__init__()
82
- self.fft_size = fft_size
83
- self.shift_size = shift_size
84
- self.win_length = win_length
85
- self.window = getattr(torch, window)(win_length)
86
- self.spectral_convergenge_loss = SpectralConvergengeLoss()
87
- self.log_stft_magnitude_loss = LogSTFTMagnitudeLoss()
88
-
89
- def forward(self, x, y):
90
- """Calculate forward propagation.
91
-
92
- Args:
93
- x (Tensor): Predicted signal (B, T).
94
- y (Tensor): Groundtruth signal (B, T).
95
-
96
- Returns:
97
- Tensor: Spectral convergence loss value.
98
- Tensor: Log STFT magnitude loss value.
99
-
100
- """
101
- x_mag = stft(x, self.fft_size, self.shift_size, self.win_length, self.window)
102
- y_mag = stft(y, self.fft_size, self.shift_size, self.win_length, self.window)
103
- sc_loss = self.spectral_convergenge_loss(x_mag, y_mag)
104
- mag_loss = self.log_stft_magnitude_loss(x_mag, y_mag)
105
-
106
- return sc_loss, mag_loss
107
-
108
-
109
- class MultiResolutionSTFTLoss(torch.nn.Module):
110
- """Multi resolution STFT loss module."""
111
-
112
- def __init__(self,
113
- fft_sizes=[1024, 2048, 512],
114
- hop_sizes=[120, 240, 50],
115
- win_lengths=[600, 1200, 240],
116
- window="hann_window"):
117
- """Initialize Multi resolution STFT loss module.
118
-
119
- Args:
120
- fft_sizes (list): List of FFT sizes.
121
- hop_sizes (list): List of hop sizes.
122
- win_lengths (list): List of window lengths.
123
- window (str): Window function type.
124
-
125
- """
126
- super(MultiResolutionSTFTLoss, self).__init__()
127
- assert len(fft_sizes) == len(hop_sizes) == len(win_lengths)
128
- self.stft_losses = torch.nn.ModuleList()
129
- for fs, ss, wl in zip(fft_sizes, hop_sizes, win_lengths):
130
- self.stft_losses += [STFTLoss(fs, ss, wl, window)]
131
-
132
- def forward(self, x, y):
133
- """Calculate forward propagation.
134
-
135
- Args:
136
- x (Tensor): Predicted signal (B, T).
137
- y (Tensor): Groundtruth signal (B, T).
138
-
139
- Returns:
140
- Tensor: Multi resolution spectral convergence loss value.
141
- Tensor: Multi resolution log STFT magnitude loss value.
142
-
143
- """
144
- sc_loss = 0.0
145
- mag_loss = 0.0
146
- for f in self.stft_losses:
147
- sc_l, mag_l = f(x, y)
148
- sc_loss += sc_l
149
- mag_loss += mag_l
150
- sc_loss /= len(self.stft_losses)
151
- mag_loss /= len(self.stft_losses)
152
-
153
- return sc_loss, mag_loss
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/models/__init__.py DELETED
@@ -1,2 +0,0 @@
1
- from .melgan import * # NOQA
2
- from .parallel_wavegan import * # NOQA
 
 
 
modules/parallel_wavegan/models/__pycache__/__init__.cpython-38.pyc DELETED
Binary file (203 Bytes)
 
modules/parallel_wavegan/models/__pycache__/melgan.cpython-38.pyc DELETED
Binary file (12.2 kB)
 
modules/parallel_wavegan/models/__pycache__/parallel_wavegan.cpython-38.pyc DELETED
Binary file (12.8 kB)
 
modules/parallel_wavegan/models/__pycache__/source.cpython-38.pyc DELETED
Binary file (13.2 kB)
 
modules/parallel_wavegan/models/melgan.py DELETED
@@ -1,427 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2020 Tomoki Hayashi
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """MelGAN Modules."""
7
-
8
- import logging
9
-
10
- import numpy as np
11
- import torch
12
-
13
- from modules.parallel_wavegan.layers import CausalConv1d
14
- from modules.parallel_wavegan.layers import CausalConvTranspose1d
15
- from modules.parallel_wavegan.layers import ResidualStack
16
-
17
-
18
- class MelGANGenerator(torch.nn.Module):
19
- """MelGAN generator module."""
20
-
21
- def __init__(self,
22
- in_channels=80,
23
- out_channels=1,
24
- kernel_size=7,
25
- channels=512,
26
- bias=True,
27
- upsample_scales=[8, 8, 2, 2],
28
- stack_kernel_size=3,
29
- stacks=3,
30
- nonlinear_activation="LeakyReLU",
31
- nonlinear_activation_params={"negative_slope": 0.2},
32
- pad="ReflectionPad1d",
33
- pad_params={},
34
- use_final_nonlinear_activation=True,
35
- use_weight_norm=True,
36
- use_causal_conv=False,
37
- ):
38
- """Initialize MelGANGenerator module.
39
-
40
- Args:
41
- in_channels (int): Number of input channels.
42
- out_channels (int): Number of output channels.
43
- kernel_size (int): Kernel size of initial and final conv layer.
44
- channels (int): Initial number of channels for conv layer.
45
- bias (bool): Whether to add bias parameter in convolution layers.
46
- upsample_scales (list): List of upsampling scales.
47
- stack_kernel_size (int): Kernel size of dilated conv layers in residual stack.
48
- stacks (int): Number of stacks in a single residual stack.
49
- nonlinear_activation (str): Activation function module name.
50
- nonlinear_activation_params (dict): Hyperparameters for activation function.
51
- pad (str): Padding function module name before dilated convolution layer.
52
- pad_params (dict): Hyperparameters for padding function.
53
- use_final_nonlinear_activation (torch.nn.Module): Activation function for the final layer.
54
- use_weight_norm (bool): Whether to use weight norm.
55
- If set to true, it will be applied to all of the conv layers.
56
- use_causal_conv (bool): Whether to use causal convolution.
57
-
58
- """
59
- super(MelGANGenerator, self).__init__()
60
-
61
- # check hyper parameters is valid
62
- assert channels >= np.prod(upsample_scales)
63
- assert channels % (2 ** len(upsample_scales)) == 0
64
- if not use_causal_conv:
65
- assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
66
-
67
- # add initial layer
68
- layers = []
69
- if not use_causal_conv:
70
- layers += [
71
- getattr(torch.nn, pad)((kernel_size - 1) // 2, **pad_params),
72
- torch.nn.Conv1d(in_channels, channels, kernel_size, bias=bias),
73
- ]
74
- else:
75
- layers += [
76
- CausalConv1d(in_channels, channels, kernel_size,
77
- bias=bias, pad=pad, pad_params=pad_params),
78
- ]
79
-
80
- for i, upsample_scale in enumerate(upsample_scales):
81
- # add upsampling layer
82
- layers += [getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params)]
83
- if not use_causal_conv:
84
- layers += [
85
- torch.nn.ConvTranspose1d(
86
- channels // (2 ** i),
87
- channels // (2 ** (i + 1)),
88
- upsample_scale * 2,
89
- stride=upsample_scale,
90
- padding=upsample_scale // 2 + upsample_scale % 2,
91
- output_padding=upsample_scale % 2,
92
- bias=bias,
93
- )
94
- ]
95
- else:
96
- layers += [
97
- CausalConvTranspose1d(
98
- channels // (2 ** i),
99
- channels // (2 ** (i + 1)),
100
- upsample_scale * 2,
101
- stride=upsample_scale,
102
- bias=bias,
103
- )
104
- ]
105
-
106
- # add residual stack
107
- for j in range(stacks):
108
- layers += [
109
- ResidualStack(
110
- kernel_size=stack_kernel_size,
111
- channels=channels // (2 ** (i + 1)),
112
- dilation=stack_kernel_size ** j,
113
- bias=bias,
114
- nonlinear_activation=nonlinear_activation,
115
- nonlinear_activation_params=nonlinear_activation_params,
116
- pad=pad,
117
- pad_params=pad_params,
118
- use_causal_conv=use_causal_conv,
119
- )
120
- ]
121
-
122
- # add final layer
123
- layers += [getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params)]
124
- if not use_causal_conv:
125
- layers += [
126
- getattr(torch.nn, pad)((kernel_size - 1) // 2, **pad_params),
127
- torch.nn.Conv1d(channels // (2 ** (i + 1)), out_channels, kernel_size, bias=bias),
128
- ]
129
- else:
130
- layers += [
131
- CausalConv1d(channels // (2 ** (i + 1)), out_channels, kernel_size,
132
- bias=bias, pad=pad, pad_params=pad_params),
133
- ]
134
- if use_final_nonlinear_activation:
135
- layers += [torch.nn.Tanh()]
136
-
137
- # define the model as a single function
138
- self.melgan = torch.nn.Sequential(*layers)
139
-
140
- # apply weight norm
141
- if use_weight_norm:
142
- self.apply_weight_norm()
143
-
144
- # reset parameters
145
- self.reset_parameters()
146
-
147
- def forward(self, c):
148
- """Calculate forward propagation.
149
-
150
- Args:
151
- c (Tensor): Input tensor (B, channels, T).
152
-
153
- Returns:
154
- Tensor: Output tensor (B, 1, T ** prod(upsample_scales)).
155
-
156
- """
157
- return self.melgan(c)
158
-
159
- def remove_weight_norm(self):
160
- """Remove weight normalization module from all of the layers."""
161
- def _remove_weight_norm(m):
162
- try:
163
- logging.debug(f"Weight norm is removed from {m}.")
164
- torch.nn.utils.remove_weight_norm(m)
165
- except ValueError: # this module didn't have weight norm
166
- return
167
-
168
- self.apply(_remove_weight_norm)
169
-
170
- def apply_weight_norm(self):
171
- """Apply weight normalization module from all of the layers."""
172
- def _apply_weight_norm(m):
173
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
174
- torch.nn.utils.weight_norm(m)
175
- logging.debug(f"Weight norm is applied to {m}.")
176
-
177
- self.apply(_apply_weight_norm)
178
-
179
- def reset_parameters(self):
180
- """Reset parameters.
181
-
182
- This initialization follows official implementation manner.
183
- https://github.com/descriptinc/melgan-neurips/blob/master/spec2wav/modules.py
184
-
185
- """
186
- def _reset_parameters(m):
187
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
188
- m.weight.data.normal_(0.0, 0.02)
189
- logging.debug(f"Reset parameters in {m}.")
190
-
191
- self.apply(_reset_parameters)
192
-
193
-
194
- class MelGANDiscriminator(torch.nn.Module):
195
- """MelGAN discriminator module."""
196
-
197
- def __init__(self,
198
- in_channels=1,
199
- out_channels=1,
200
- kernel_sizes=[5, 3],
201
- channels=16,
202
- max_downsample_channels=1024,
203
- bias=True,
204
- downsample_scales=[4, 4, 4, 4],
205
- nonlinear_activation="LeakyReLU",
206
- nonlinear_activation_params={"negative_slope": 0.2},
207
- pad="ReflectionPad1d",
208
- pad_params={},
209
- ):
210
- """Initilize MelGAN discriminator module.
211
-
212
- Args:
213
- in_channels (int): Number of input channels.
214
- out_channels (int): Number of output channels.
215
- kernel_sizes (list): List of two kernel sizes. The prod will be used for the first conv layer,
216
- and the first and the second kernel sizes will be used for the last two layers.
217
- For example if kernel_sizes = [5, 3], the first layer kernel size will be 5 * 3 = 15,
218
- the last two layers' kernel size will be 5 and 3, respectively.
219
- channels (int): Initial number of channels for conv layer.
220
- max_downsample_channels (int): Maximum number of channels for downsampling layers.
221
- bias (bool): Whether to add bias parameter in convolution layers.
222
- downsample_scales (list): List of downsampling scales.
223
- nonlinear_activation (str): Activation function module name.
224
- nonlinear_activation_params (dict): Hyperparameters for activation function.
225
- pad (str): Padding function module name before dilated convolution layer.
226
- pad_params (dict): Hyperparameters for padding function.
227
-
228
- """
229
- super(MelGANDiscriminator, self).__init__()
230
- self.layers = torch.nn.ModuleList()
231
-
232
- # check kernel size is valid
233
- assert len(kernel_sizes) == 2
234
- assert kernel_sizes[0] % 2 == 1
235
- assert kernel_sizes[1] % 2 == 1
236
-
237
- # add first layer
238
- self.layers += [
239
- torch.nn.Sequential(
240
- getattr(torch.nn, pad)((np.prod(kernel_sizes) - 1) // 2, **pad_params),
241
- torch.nn.Conv1d(in_channels, channels, np.prod(kernel_sizes), bias=bias),
242
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
243
- )
244
- ]
245
-
246
- # add downsample layers
247
- in_chs = channels
248
- for downsample_scale in downsample_scales:
249
- out_chs = min(in_chs * downsample_scale, max_downsample_channels)
250
- self.layers += [
251
- torch.nn.Sequential(
252
- torch.nn.Conv1d(
253
- in_chs, out_chs,
254
- kernel_size=downsample_scale * 10 + 1,
255
- stride=downsample_scale,
256
- padding=downsample_scale * 5,
257
- groups=in_chs // 4,
258
- bias=bias,
259
- ),
260
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
261
- )
262
- ]
263
- in_chs = out_chs
264
-
265
- # add final layers
266
- out_chs = min(in_chs * 2, max_downsample_channels)
267
- self.layers += [
268
- torch.nn.Sequential(
269
- torch.nn.Conv1d(
270
- in_chs, out_chs, kernel_sizes[0],
271
- padding=(kernel_sizes[0] - 1) // 2,
272
- bias=bias,
273
- ),
274
- getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
275
- )
276
- ]
277
- self.layers += [
278
- torch.nn.Conv1d(
279
- out_chs, out_channels, kernel_sizes[1],
280
- padding=(kernel_sizes[1] - 1) // 2,
281
- bias=bias,
282
- ),
283
- ]
284
-
285
- def forward(self, x):
286
- """Calculate forward propagation.
287
-
288
- Args:
289
- x (Tensor): Input noise signal (B, 1, T).
290
-
291
- Returns:
292
- List: List of output tensors of each layer.
293
-
294
- """
295
- outs = []
296
- for f in self.layers:
297
- x = f(x)
298
- outs += [x]
299
-
300
- return outs
301
-
302
-
303
- class MelGANMultiScaleDiscriminator(torch.nn.Module):
304
- """MelGAN multi-scale discriminator module."""
305
-
306
- def __init__(self,
307
- in_channels=1,
308
- out_channels=1,
309
- scales=3,
310
- downsample_pooling="AvgPool1d",
311
- # follow the official implementation setting
312
- downsample_pooling_params={
313
- "kernel_size": 4,
314
- "stride": 2,
315
- "padding": 1,
316
- "count_include_pad": False,
317
- },
318
- kernel_sizes=[5, 3],
319
- channels=16,
320
- max_downsample_channels=1024,
321
- bias=True,
322
- downsample_scales=[4, 4, 4, 4],
323
- nonlinear_activation="LeakyReLU",
324
- nonlinear_activation_params={"negative_slope": 0.2},
325
- pad="ReflectionPad1d",
326
- pad_params={},
327
- use_weight_norm=True,
328
- ):
329
- """Initilize MelGAN multi-scale discriminator module.
330
-
331
- Args:
332
- in_channels (int): Number of input channels.
333
- out_channels (int): Number of output channels.
334
- downsample_pooling (str): Pooling module name for downsampling of the inputs.
335
- downsample_pooling_params (dict): Parameters for the above pooling module.
336
- kernel_sizes (list): List of two kernel sizes. The sum will be used for the first conv layer,
337
- and the first and the second kernel sizes will be used for the last two layers.
338
- channels (int): Initial number of channels for conv layer.
339
- max_downsample_channels (int): Maximum number of channels for downsampling layers.
340
- bias (bool): Whether to add bias parameter in convolution layers.
341
- downsample_scales (list): List of downsampling scales.
342
- nonlinear_activation (str): Activation function module name.
343
- nonlinear_activation_params (dict): Hyperparameters for activation function.
344
- pad (str): Padding function module name before dilated convolution layer.
345
- pad_params (dict): Hyperparameters for padding function.
346
- use_causal_conv (bool): Whether to use causal convolution.
347
-
348
- """
349
- super(MelGANMultiScaleDiscriminator, self).__init__()
350
- self.discriminators = torch.nn.ModuleList()
351
-
352
- # add discriminators
353
- for _ in range(scales):
354
- self.discriminators += [
355
- MelGANDiscriminator(
356
- in_channels=in_channels,
357
- out_channels=out_channels,
358
- kernel_sizes=kernel_sizes,
359
- channels=channels,
360
- max_downsample_channels=max_downsample_channels,
361
- bias=bias,
362
- downsample_scales=downsample_scales,
363
- nonlinear_activation=nonlinear_activation,
364
- nonlinear_activation_params=nonlinear_activation_params,
365
- pad=pad,
366
- pad_params=pad_params,
367
- )
368
- ]
369
- self.pooling = getattr(torch.nn, downsample_pooling)(**downsample_pooling_params)
370
-
371
- # apply weight norm
372
- if use_weight_norm:
373
- self.apply_weight_norm()
374
-
375
- # reset parameters
376
- self.reset_parameters()
377
-
378
- def forward(self, x):
379
- """Calculate forward propagation.
380
-
381
- Args:
382
- x (Tensor): Input noise signal (B, 1, T).
383
-
384
- Returns:
385
- List: List of list of each discriminator outputs, which consists of each layer output tensors.
386
-
387
- """
388
- outs = []
389
- for f in self.discriminators:
390
- outs += [f(x)]
391
- x = self.pooling(x)
392
-
393
- return outs
394
-
395
- def remove_weight_norm(self):
396
- """Remove weight normalization module from all of the layers."""
397
- def _remove_weight_norm(m):
398
- try:
399
- logging.debug(f"Weight norm is removed from {m}.")
400
- torch.nn.utils.remove_weight_norm(m)
401
- except ValueError: # this module didn't have weight norm
402
- return
403
-
404
- self.apply(_remove_weight_norm)
405
-
406
- def apply_weight_norm(self):
407
- """Apply weight normalization module from all of the layers."""
408
- def _apply_weight_norm(m):
409
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
410
- torch.nn.utils.weight_norm(m)
411
- logging.debug(f"Weight norm is applied to {m}.")
412
-
413
- self.apply(_apply_weight_norm)
414
-
415
- def reset_parameters(self):
416
- """Reset parameters.
417
-
418
- This initialization follows official implementation manner.
419
- https://github.com/descriptinc/melgan-neurips/blob/master/spec2wav/modules.py
420
-
421
- """
422
- def _reset_parameters(m):
423
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
424
- m.weight.data.normal_(0.0, 0.02)
425
- logging.debug(f"Reset parameters in {m}.")
426
-
427
- self.apply(_reset_parameters)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/models/parallel_wavegan.py DELETED
@@ -1,434 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # Copyright 2019 Tomoki Hayashi
4
- # MIT License (https://opensource.org/licenses/MIT)
5
-
6
- """Parallel WaveGAN Modules."""
7
-
8
- import logging
9
- import math
10
-
11
- import torch
12
- from torch import nn
13
-
14
- from modules.parallel_wavegan.layers import Conv1d
15
- from modules.parallel_wavegan.layers import Conv1d1x1
16
- from modules.parallel_wavegan.layers import ResidualBlock
17
- from modules.parallel_wavegan.layers import upsample
18
- from modules.parallel_wavegan import models
19
-
20
-
21
- class ParallelWaveGANGenerator(torch.nn.Module):
22
- """Parallel WaveGAN Generator module."""
23
-
24
- def __init__(self,
25
- in_channels=1,
26
- out_channels=1,
27
- kernel_size=3,
28
- layers=30,
29
- stacks=3,
30
- residual_channels=64,
31
- gate_channels=128,
32
- skip_channels=64,
33
- aux_channels=80,
34
- aux_context_window=2,
35
- dropout=0.0,
36
- bias=True,
37
- use_weight_norm=True,
38
- use_causal_conv=False,
39
- upsample_conditional_features=True,
40
- upsample_net="ConvInUpsampleNetwork",
41
- upsample_params={"upsample_scales": [4, 4, 4, 4]},
42
- use_pitch_embed=False,
43
- ):
44
- """Initialize Parallel WaveGAN Generator module.
45
-
46
- Args:
47
- in_channels (int): Number of input channels.
48
- out_channels (int): Number of output channels.
49
- kernel_size (int): Kernel size of dilated convolution.
50
- layers (int): Number of residual block layers.
51
- stacks (int): Number of stacks i.e., dilation cycles.
52
- residual_channels (int): Number of channels in residual conv.
53
- gate_channels (int): Number of channels in gated conv.
54
- skip_channels (int): Number of channels in skip conv.
55
- aux_channels (int): Number of channels for auxiliary feature conv.
56
- aux_context_window (int): Context window size for auxiliary feature.
57
- dropout (float): Dropout rate. 0.0 means no dropout applied.
58
- bias (bool): Whether to use bias parameter in conv layer.
59
- use_weight_norm (bool): Whether to use weight norm.
60
- If set to true, it will be applied to all of the conv layers.
61
- use_causal_conv (bool): Whether to use causal structure.
62
- upsample_conditional_features (bool): Whether to use upsampling network.
63
- upsample_net (str): Upsampling network architecture.
64
- upsample_params (dict): Upsampling network parameters.
65
-
66
- """
67
- super(ParallelWaveGANGenerator, self).__init__()
68
- self.in_channels = in_channels
69
- self.out_channels = out_channels
70
- self.aux_channels = aux_channels
71
- self.layers = layers
72
- self.stacks = stacks
73
- self.kernel_size = kernel_size
74
-
75
- # check the number of layers and stacks
76
- assert layers % stacks == 0
77
- layers_per_stack = layers // stacks
78
-
79
- # define first convolution
80
- self.first_conv = Conv1d1x1(in_channels, residual_channels, bias=True)
81
-
82
- # define conv + upsampling network
83
- if upsample_conditional_features:
84
- upsample_params.update({
85
- "use_causal_conv": use_causal_conv,
86
- })
87
- if upsample_net == "MelGANGenerator":
88
- assert aux_context_window == 0
89
- upsample_params.update({
90
- "use_weight_norm": False, # not to apply twice
91
- "use_final_nonlinear_activation": False,
92
- })
93
- self.upsample_net = getattr(models, upsample_net)(**upsample_params)
94
- else:
95
- if upsample_net == "ConvInUpsampleNetwork":
96
- upsample_params.update({
97
- "aux_channels": aux_channels,
98
- "aux_context_window": aux_context_window,
99
- })
100
- self.upsample_net = getattr(upsample, upsample_net)(**upsample_params)
101
- else:
102
- self.upsample_net = None
103
-
104
- # define residual blocks
105
- self.conv_layers = torch.nn.ModuleList()
106
- for layer in range(layers):
107
- dilation = 2 ** (layer % layers_per_stack)
108
- conv = ResidualBlock(
109
- kernel_size=kernel_size,
110
- residual_channels=residual_channels,
111
- gate_channels=gate_channels,
112
- skip_channels=skip_channels,
113
- aux_channels=aux_channels,
114
- dilation=dilation,
115
- dropout=dropout,
116
- bias=bias,
117
- use_causal_conv=use_causal_conv,
118
- )
119
- self.conv_layers += [conv]
120
-
121
- # define output layers
122
- self.last_conv_layers = torch.nn.ModuleList([
123
- torch.nn.ReLU(inplace=True),
124
- Conv1d1x1(skip_channels, skip_channels, bias=True),
125
- torch.nn.ReLU(inplace=True),
126
- Conv1d1x1(skip_channels, out_channels, bias=True),
127
- ])
128
-
129
- self.use_pitch_embed = use_pitch_embed
130
- if use_pitch_embed:
131
- self.pitch_embed = nn.Embedding(300, aux_channels, 0)
132
- self.c_proj = nn.Linear(2 * aux_channels, aux_channels)
133
-
134
- # apply weight norm
135
- if use_weight_norm:
136
- self.apply_weight_norm()
137
-
138
- def forward(self, x, c=None, pitch=None, **kwargs):
139
- """Calculate forward propagation.
140
-
141
- Args:
142
- x (Tensor): Input noise signal (B, C_in, T).
143
- c (Tensor): Local conditioning auxiliary features (B, C ,T').
144
- pitch (Tensor): Local conditioning pitch (B, T').
145
-
146
- Returns:
147
- Tensor: Output tensor (B, C_out, T)
148
-
149
- """
150
- # perform upsampling
151
- if c is not None and self.upsample_net is not None:
152
- if self.use_pitch_embed:
153
- p = self.pitch_embed(pitch)
154
- c = self.c_proj(torch.cat([c.transpose(1, 2), p], -1)).transpose(1, 2)
155
- c = self.upsample_net(c)
156
- assert c.size(-1) == x.size(-1), (c.size(-1), x.size(-1))
157
-
158
- # encode to hidden representation
159
- x = self.first_conv(x)
160
- skips = 0
161
- for f in self.conv_layers:
162
- x, h = f(x, c)
163
- skips += h
164
- skips *= math.sqrt(1.0 / len(self.conv_layers))
165
-
166
- # apply final layers
167
- x = skips
168
- for f in self.last_conv_layers:
169
- x = f(x)
170
-
171
- return x
172
-
173
- def remove_weight_norm(self):
174
- """Remove weight normalization module from all of the layers."""
175
- def _remove_weight_norm(m):
176
- try:
177
- logging.debug(f"Weight norm is removed from {m}.")
178
- torch.nn.utils.remove_weight_norm(m)
179
- except ValueError: # this module didn't have weight norm
180
- return
181
-
182
- self.apply(_remove_weight_norm)
183
-
184
- def apply_weight_norm(self):
185
- """Apply weight normalization module from all of the layers."""
186
- def _apply_weight_norm(m):
187
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.Conv2d):
188
- torch.nn.utils.weight_norm(m)
189
- logging.debug(f"Weight norm is applied to {m}.")
190
-
191
- self.apply(_apply_weight_norm)
192
-
193
- @staticmethod
194
- def _get_receptive_field_size(layers, stacks, kernel_size,
195
- dilation=lambda x: 2 ** x):
196
- assert layers % stacks == 0
197
- layers_per_cycle = layers // stacks
198
- dilations = [dilation(i % layers_per_cycle) for i in range(layers)]
199
- return (kernel_size - 1) * sum(dilations) + 1
200
-
201
- @property
202
- def receptive_field_size(self):
203
- """Return receptive field size."""
204
- return self._get_receptive_field_size(self.layers, self.stacks, self.kernel_size)
205
-
206
-
207
- class ParallelWaveGANDiscriminator(torch.nn.Module):
208
- """Parallel WaveGAN Discriminator module."""
209
-
210
- def __init__(self,
211
- in_channels=1,
212
- out_channels=1,
213
- kernel_size=3,
214
- layers=10,
215
- conv_channels=64,
216
- dilation_factor=1,
217
- nonlinear_activation="LeakyReLU",
218
- nonlinear_activation_params={"negative_slope": 0.2},
219
- bias=True,
220
- use_weight_norm=True,
221
- ):
222
- """Initialize Parallel WaveGAN Discriminator module.
223
-
224
- Args:
225
- in_channels (int): Number of input channels.
226
- out_channels (int): Number of output channels.
227
- kernel_size (int): Number of output channels.
228
- layers (int): Number of conv layers.
229
- conv_channels (int): Number of chnn layers.
230
- dilation_factor (int): Dilation factor. For example, if dilation_factor = 2,
231
- the dilation will be 2, 4, 8, ..., and so on.
232
- nonlinear_activation (str): Nonlinear function after each conv.
233
- nonlinear_activation_params (dict): Nonlinear function parameters
234
- bias (bool): Whether to use bias parameter in conv.
235
- use_weight_norm (bool) Whether to use weight norm.
236
- If set to true, it will be applied to all of the conv layers.
237
-
238
- """
239
- super(ParallelWaveGANDiscriminator, self).__init__()
240
- assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
241
- assert dilation_factor > 0, "Dilation factor must be > 0."
242
- self.conv_layers = torch.nn.ModuleList()
243
- conv_in_channels = in_channels
244
- for i in range(layers - 1):
245
- if i == 0:
246
- dilation = 1
247
- else:
248
- dilation = i if dilation_factor == 1 else dilation_factor ** i
249
- conv_in_channels = conv_channels
250
- padding = (kernel_size - 1) // 2 * dilation
251
- conv_layer = [
252
- Conv1d(conv_in_channels, conv_channels,
253
- kernel_size=kernel_size, padding=padding,
254
- dilation=dilation, bias=bias),
255
- getattr(torch.nn, nonlinear_activation)(inplace=True, **nonlinear_activation_params)
256
- ]
257
- self.conv_layers += conv_layer
258
- padding = (kernel_size - 1) // 2
259
- last_conv_layer = Conv1d(
260
- conv_in_channels, out_channels,
261
- kernel_size=kernel_size, padding=padding, bias=bias)
262
- self.conv_layers += [last_conv_layer]
263
-
264
- # apply weight norm
265
- if use_weight_norm:
266
- self.apply_weight_norm()
267
-
268
- def forward(self, x):
269
- """Calculate forward propagation.
270
-
271
- Args:
272
- x (Tensor): Input noise signal (B, 1, T).
273
-
274
- Returns:
275
- Tensor: Output tensor (B, 1, T)
276
-
277
- """
278
- for f in self.conv_layers:
279
- x = f(x)
280
- return x
281
-
282
- def apply_weight_norm(self):
283
- """Apply weight normalization module from all of the layers."""
284
- def _apply_weight_norm(m):
285
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.Conv2d):
286
- torch.nn.utils.weight_norm(m)
287
- logging.debug(f"Weight norm is applied to {m}.")
288
-
289
- self.apply(_apply_weight_norm)
290
-
291
- def remove_weight_norm(self):
292
- """Remove weight normalization module from all of the layers."""
293
- def _remove_weight_norm(m):
294
- try:
295
- logging.debug(f"Weight norm is removed from {m}.")
296
- torch.nn.utils.remove_weight_norm(m)
297
- except ValueError: # this module didn't have weight norm
298
- return
299
-
300
- self.apply(_remove_weight_norm)
301
-
302
-
303
- class ResidualParallelWaveGANDiscriminator(torch.nn.Module):
304
- """Parallel WaveGAN Discriminator module."""
305
-
306
- def __init__(self,
307
- in_channels=1,
308
- out_channels=1,
309
- kernel_size=3,
310
- layers=30,
311
- stacks=3,
312
- residual_channels=64,
313
- gate_channels=128,
314
- skip_channels=64,
315
- dropout=0.0,
316
- bias=True,
317
- use_weight_norm=True,
318
- use_causal_conv=False,
319
- nonlinear_activation="LeakyReLU",
320
- nonlinear_activation_params={"negative_slope": 0.2},
321
- ):
322
- """Initialize Parallel WaveGAN Discriminator module.
323
-
324
- Args:
325
- in_channels (int): Number of input channels.
326
- out_channels (int): Number of output channels.
327
- kernel_size (int): Kernel size of dilated convolution.
328
- layers (int): Number of residual block layers.
329
- stacks (int): Number of stacks i.e., dilation cycles.
330
- residual_channels (int): Number of channels in residual conv.
331
- gate_channels (int): Number of channels in gated conv.
332
- skip_channels (int): Number of channels in skip conv.
333
- dropout (float): Dropout rate. 0.0 means no dropout applied.
334
- bias (bool): Whether to use bias parameter in conv.
335
- use_weight_norm (bool): Whether to use weight norm.
336
- If set to true, it will be applied to all of the conv layers.
337
- use_causal_conv (bool): Whether to use causal structure.
338
- nonlinear_activation_params (dict): Nonlinear function parameters
339
-
340
- """
341
- super(ResidualParallelWaveGANDiscriminator, self).__init__()
342
- assert (kernel_size - 1) % 2 == 0, "Not support even number kernel size."
343
-
344
- self.in_channels = in_channels
345
- self.out_channels = out_channels
346
- self.layers = layers
347
- self.stacks = stacks
348
- self.kernel_size = kernel_size
349
-
350
- # check the number of layers and stacks
351
- assert layers % stacks == 0
352
- layers_per_stack = layers // stacks
353
-
354
- # define first convolution
355
- self.first_conv = torch.nn.Sequential(
356
- Conv1d1x1(in_channels, residual_channels, bias=True),
357
- getattr(torch.nn, nonlinear_activation)(
358
- inplace=True, **nonlinear_activation_params),
359
- )
360
-
361
- # define residual blocks
362
- self.conv_layers = torch.nn.ModuleList()
363
- for layer in range(layers):
364
- dilation = 2 ** (layer % layers_per_stack)
365
- conv = ResidualBlock(
366
- kernel_size=kernel_size,
367
- residual_channels=residual_channels,
368
- gate_channels=gate_channels,
369
- skip_channels=skip_channels,
370
- aux_channels=-1,
371
- dilation=dilation,
372
- dropout=dropout,
373
- bias=bias,
374
- use_causal_conv=use_causal_conv,
375
- )
376
- self.conv_layers += [conv]
377
-
378
- # define output layers
379
- self.last_conv_layers = torch.nn.ModuleList([
380
- getattr(torch.nn, nonlinear_activation)(
381
- inplace=True, **nonlinear_activation_params),
382
- Conv1d1x1(skip_channels, skip_channels, bias=True),
383
- getattr(torch.nn, nonlinear_activation)(
384
- inplace=True, **nonlinear_activation_params),
385
- Conv1d1x1(skip_channels, out_channels, bias=True),
386
- ])
387
-
388
- # apply weight norm
389
- if use_weight_norm:
390
- self.apply_weight_norm()
391
-
392
- def forward(self, x):
393
- """Calculate forward propagation.
394
-
395
- Args:
396
- x (Tensor): Input noise signal (B, 1, T).
397
-
398
- Returns:
399
- Tensor: Output tensor (B, 1, T)
400
-
401
- """
402
- x = self.first_conv(x)
403
-
404
- skips = 0
405
- for f in self.conv_layers:
406
- x, h = f(x, None)
407
- skips += h
408
- skips *= math.sqrt(1.0 / len(self.conv_layers))
409
-
410
- # apply final layers
411
- x = skips
412
- for f in self.last_conv_layers:
413
- x = f(x)
414
- return x
415
-
416
- def apply_weight_norm(self):
417
- """Apply weight normalization module from all of the layers."""
418
- def _apply_weight_norm(m):
419
- if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.Conv2d):
420
- torch.nn.utils.weight_norm(m)
421
- logging.debug(f"Weight norm is applied to {m}.")
422
-
423
- self.apply(_apply_weight_norm)
424
-
425
- def remove_weight_norm(self):
426
- """Remove weight normalization module from all of the layers."""
427
- def _remove_weight_norm(m):
428
- try:
429
- logging.debug(f"Weight norm is removed from {m}.")
430
- torch.nn.utils.remove_weight_norm(m)
431
- except ValueError: # this module didn't have weight norm
432
- return
433
-
434
- self.apply(_remove_weight_norm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/models/source.py DELETED
@@ -1,538 +0,0 @@
1
- import torch
2
- import numpy as np
3
- import sys
4
- import torch.nn.functional as torch_nn_func
5
-
6
-
7
- class SineGen(torch.nn.Module):
8
- """ Definition of sine generator
9
- SineGen(samp_rate, harmonic_num = 0,
10
- sine_amp = 0.1, noise_std = 0.003,
11
- voiced_threshold = 0,
12
- flag_for_pulse=False)
13
-
14
- samp_rate: sampling rate in Hz
15
- harmonic_num: number of harmonic overtones (default 0)
16
- sine_amp: amplitude of sine-wavefrom (default 0.1)
17
- noise_std: std of Gaussian noise (default 0.003)
18
- voiced_thoreshold: F0 threshold for U/V classification (default 0)
19
- flag_for_pulse: this SinGen is used inside PulseGen (default False)
20
-
21
- Note: when flag_for_pulse is True, the first time step of a voiced
22
- segment is always sin(np.pi) or cos(0)
23
- """
24
-
25
- def __init__(self, samp_rate, harmonic_num=0,
26
- sine_amp=0.1, noise_std=0.003,
27
- voiced_threshold=0,
28
- flag_for_pulse=False):
29
- super(SineGen, self).__init__()
30
- self.sine_amp = sine_amp
31
- self.noise_std = noise_std
32
- self.harmonic_num = harmonic_num
33
- self.dim = self.harmonic_num + 1
34
- self.sampling_rate = samp_rate
35
- self.voiced_threshold = voiced_threshold
36
- self.flag_for_pulse = flag_for_pulse
37
-
38
- def _f02uv(self, f0):
39
- # generate uv signal
40
- uv = torch.ones_like(f0)
41
- uv = uv * (f0 > self.voiced_threshold)
42
- return uv
43
-
44
- def _f02sine(self, f0_values):
45
- """ f0_values: (batchsize, length, dim)
46
- where dim indicates fundamental tone and overtones
47
- """
48
- # convert to F0 in rad. The interger part n can be ignored
49
- # because 2 * np.pi * n doesn't affect phase
50
- rad_values = (f0_values / self.sampling_rate) % 1
51
-
52
- # initial phase noise (no noise for fundamental component)
53
- rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
54
- device=f0_values.device)
55
- rand_ini[:, 0] = 0
56
- rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
57
-
58
- # instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
59
- if not self.flag_for_pulse:
60
- # for normal case
61
-
62
- # To prevent torch.cumsum numerical overflow,
63
- # it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
64
- # Buffer tmp_over_one_idx indicates the time step to add -1.
65
- # This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
66
- tmp_over_one = torch.cumsum(rad_values, 1) % 1
67
- tmp_over_one_idx = (tmp_over_one[:, 1:, :] -
68
- tmp_over_one[:, :-1, :]) < 0
69
- cumsum_shift = torch.zeros_like(rad_values)
70
- cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
71
-
72
- sines = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1)
73
- * 2 * np.pi)
74
- else:
75
- # If necessary, make sure that the first time step of every
76
- # voiced segments is sin(pi) or cos(0)
77
- # This is used for pulse-train generation
78
-
79
- # identify the last time step in unvoiced segments
80
- uv = self._f02uv(f0_values)
81
- uv_1 = torch.roll(uv, shifts=-1, dims=1)
82
- uv_1[:, -1, :] = 1
83
- u_loc = (uv < 1) * (uv_1 > 0)
84
-
85
- # get the instantanouse phase
86
- tmp_cumsum = torch.cumsum(rad_values, dim=1)
87
- # different batch needs to be processed differently
88
- for idx in range(f0_values.shape[0]):
89
- temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
90
- temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
91
- # stores the accumulation of i.phase within
92
- # each voiced segments
93
- tmp_cumsum[idx, :, :] = 0
94
- tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
95
-
96
- # rad_values - tmp_cumsum: remove the accumulation of i.phase
97
- # within the previous voiced segment.
98
- i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
99
-
100
- # get the sines
101
- sines = torch.cos(i_phase * 2 * np.pi)
102
- return sines
103
-
104
- def forward(self, f0):
105
- """ sine_tensor, uv = forward(f0)
106
- input F0: tensor(batchsize=1, length, dim=1)
107
- f0 for unvoiced steps should be 0
108
- output sine_tensor: tensor(batchsize=1, length, dim)
109
- output uv: tensor(batchsize=1, length, 1)
110
- """
111
- with torch.no_grad():
112
- f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,
113
- device=f0.device)
114
- # fundamental component
115
- f0_buf[:, :, 0] = f0[:, :, 0]
116
- for idx in np.arange(self.harmonic_num):
117
- # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
118
- f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (idx + 2)
119
-
120
- # generate sine waveforms
121
- sine_waves = self._f02sine(f0_buf) * self.sine_amp
122
-
123
- # generate uv signal
124
- # uv = torch.ones(f0.shape)
125
- # uv = uv * (f0 > self.voiced_threshold)
126
- uv = self._f02uv(f0)
127
-
128
- # noise: for unvoiced should be similar to sine_amp
129
- # std = self.sine_amp/3 -> max value ~ self.sine_amp
130
- # . for voiced regions is self.noise_std
131
- noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
132
- noise = noise_amp * torch.randn_like(sine_waves)
133
-
134
- # first: set the unvoiced part to 0 by uv
135
- # then: additive noise
136
- sine_waves = sine_waves * uv + noise
137
- return sine_waves, uv, noise
138
-
139
-
140
- class PulseGen(torch.nn.Module):
141
- """ Definition of Pulse train generator
142
-
143
- There are many ways to implement pulse generator.
144
- Here, PulseGen is based on SinGen. For a perfect
145
- """
146
- def __init__(self, samp_rate, pulse_amp = 0.1,
147
- noise_std = 0.003, voiced_threshold = 0):
148
- super(PulseGen, self).__init__()
149
- self.pulse_amp = pulse_amp
150
- self.sampling_rate = samp_rate
151
- self.voiced_threshold = voiced_threshold
152
- self.noise_std = noise_std
153
- self.l_sinegen = SineGen(self.sampling_rate, harmonic_num=0, \
154
- sine_amp=self.pulse_amp, noise_std=0, \
155
- voiced_threshold=self.voiced_threshold, \
156
- flag_for_pulse=True)
157
-
158
- def forward(self, f0):
159
- """ Pulse train generator
160
- pulse_train, uv = forward(f0)
161
- input F0: tensor(batchsize=1, length, dim=1)
162
- f0 for unvoiced steps should be 0
163
- output pulse_train: tensor(batchsize=1, length, dim)
164
- output uv: tensor(batchsize=1, length, 1)
165
-
166
- Note: self.l_sine doesn't make sure that the initial phase of
167
- a voiced segment is np.pi, the first pulse in a voiced segment
168
- may not be at the first time step within a voiced segment
169
- """
170
- with torch.no_grad():
171
- sine_wav, uv, noise = self.l_sinegen(f0)
172
-
173
- # sine without additive noise
174
- pure_sine = sine_wav - noise
175
-
176
- # step t corresponds to a pulse if
177
- # sine[t] > sine[t+1] & sine[t] > sine[t-1]
178
- # & sine[t-1], sine[t+1], and sine[t] are voiced
179
- # or
180
- # sine[t] is voiced, sine[t-1] is unvoiced
181
- # we use torch.roll to simulate sine[t+1] and sine[t-1]
182
- sine_1 = torch.roll(pure_sine, shifts=1, dims=1)
183
- uv_1 = torch.roll(uv, shifts=1, dims=1)
184
- uv_1[:, 0, :] = 0
185
- sine_2 = torch.roll(pure_sine, shifts=-1, dims=1)
186
- uv_2 = torch.roll(uv, shifts=-1, dims=1)
187
- uv_2[:, -1, :] = 0
188
-
189
- loc = (pure_sine > sine_1) * (pure_sine > sine_2) \
190
- * (uv_1 > 0) * (uv_2 > 0) * (uv > 0) \
191
- + (uv_1 < 1) * (uv > 0)
192
-
193
- # pulse train without noise
194
- pulse_train = pure_sine * loc
195
-
196
- # additive noise to pulse train
197
- # note that noise from sinegen is zero in voiced regions
198
- pulse_noise = torch.randn_like(pure_sine) * self.noise_std
199
-
200
- # with additive noise on pulse, and unvoiced regions
201
- pulse_train += pulse_noise * loc + pulse_noise * (1 - uv)
202
- return pulse_train, sine_wav, uv, pulse_noise
203
-
204
-
205
- class SignalsConv1d(torch.nn.Module):
206
- """ Filtering input signal with time invariant filter
207
- Note: FIRFilter conducted filtering given fixed FIR weight
208
- SignalsConv1d convolves two signals
209
- Note: this is based on torch.nn.functional.conv1d
210
-
211
- """
212
-
213
- def __init__(self):
214
- super(SignalsConv1d, self).__init__()
215
-
216
- def forward(self, signal, system_ir):
217
- """ output = forward(signal, system_ir)
218
-
219
- signal: (batchsize, length1, dim)
220
- system_ir: (length2, dim)
221
-
222
- output: (batchsize, length1, dim)
223
- """
224
- if signal.shape[-1] != system_ir.shape[-1]:
225
- print("Error: SignalsConv1d expects shape:")
226
- print("signal (batchsize, length1, dim)")
227
- print("system_id (batchsize, length2, dim)")
228
- print("But received signal: {:s}".format(str(signal.shape)))
229
- print(" system_ir: {:s}".format(str(system_ir.shape)))
230
- sys.exit(1)
231
- padding_length = system_ir.shape[0] - 1
232
- groups = signal.shape[-1]
233
-
234
- # pad signal on the left
235
- signal_pad = torch_nn_func.pad(signal.permute(0, 2, 1), \
236
- (padding_length, 0))
237
- # prepare system impulse response as (dim, 1, length2)
238
- # also flip the impulse response
239
- ir = torch.flip(system_ir.unsqueeze(1).permute(2, 1, 0), \
240
- dims=[2])
241
- # convolute
242
- output = torch_nn_func.conv1d(signal_pad, ir, groups=groups)
243
- return output.permute(0, 2, 1)
244
-
245
-
246
- class CyclicNoiseGen_v1(torch.nn.Module):
247
- """ CyclicnoiseGen_v1
248
- Cyclic noise with a single parameter of beta.
249
- Pytorch v1 implementation assumes f_t is also fixed
250
- """
251
-
252
- def __init__(self, samp_rate,
253
- noise_std=0.003, voiced_threshold=0):
254
- super(CyclicNoiseGen_v1, self).__init__()
255
- self.samp_rate = samp_rate
256
- self.noise_std = noise_std
257
- self.voiced_threshold = voiced_threshold
258
-
259
- self.l_pulse = PulseGen(samp_rate, pulse_amp=1.0,
260
- noise_std=noise_std,
261
- voiced_threshold=voiced_threshold)
262
- self.l_conv = SignalsConv1d()
263
-
264
- def noise_decay(self, beta, f0mean):
265
- """ decayed_noise = noise_decay(beta, f0mean)
266
- decayed_noise = n[t]exp(-t * f_mean / beta / samp_rate)
267
-
268
- beta: (dim=1) or (batchsize=1, 1, dim=1)
269
- f0mean (batchsize=1, 1, dim=1)
270
-
271
- decayed_noise (batchsize=1, length, dim=1)
272
- """
273
- with torch.no_grad():
274
- # exp(-1.0 n / T) < 0.01 => n > -log(0.01)*T = 4.60*T
275
- # truncate the noise when decayed by -40 dB
276
- length = 4.6 * self.samp_rate / f0mean
277
- length = length.int()
278
- time_idx = torch.arange(0, length, device=beta.device)
279
- time_idx = time_idx.unsqueeze(0).unsqueeze(2)
280
- time_idx = time_idx.repeat(beta.shape[0], 1, beta.shape[2])
281
-
282
- noise = torch.randn(time_idx.shape, device=beta.device)
283
-
284
- # due to Pytorch implementation, use f0_mean as the f0 factor
285
- decay = torch.exp(-time_idx * f0mean / beta / self.samp_rate)
286
- return noise * self.noise_std * decay
287
-
288
- def forward(self, f0s, beta):
289
- """ Producde cyclic-noise
290
- """
291
- # pulse train
292
- pulse_train, sine_wav, uv, noise = self.l_pulse(f0s)
293
- pure_pulse = pulse_train - noise
294
-
295
- # decayed_noise (length, dim=1)
296
- if (uv < 1).all():
297
- # all unvoiced
298
- cyc_noise = torch.zeros_like(sine_wav)
299
- else:
300
- f0mean = f0s[uv > 0].mean()
301
-
302
- decayed_noise = self.noise_decay(beta, f0mean)[0, :, :]
303
- # convolute
304
- cyc_noise = self.l_conv(pure_pulse, decayed_noise)
305
-
306
- # add noise in invoiced segments
307
- cyc_noise = cyc_noise + noise * (1.0 - uv)
308
- return cyc_noise, pulse_train, sine_wav, uv, noise
309
-
310
-
311
- class SineGen(torch.nn.Module):
312
- """ Definition of sine generator
313
- SineGen(samp_rate, harmonic_num = 0,
314
- sine_amp = 0.1, noise_std = 0.003,
315
- voiced_threshold = 0,
316
- flag_for_pulse=False)
317
-
318
- samp_rate: sampling rate in Hz
319
- harmonic_num: number of harmonic overtones (default 0)
320
- sine_amp: amplitude of sine-wavefrom (default 0.1)
321
- noise_std: std of Gaussian noise (default 0.003)
322
- voiced_thoreshold: F0 threshold for U/V classification (default 0)
323
- flag_for_pulse: this SinGen is used inside PulseGen (default False)
324
-
325
- Note: when flag_for_pulse is True, the first time step of a voiced
326
- segment is always sin(np.pi) or cos(0)
327
- """
328
-
329
- def __init__(self, samp_rate, harmonic_num=0,
330
- sine_amp=0.1, noise_std=0.003,
331
- voiced_threshold=0,
332
- flag_for_pulse=False):
333
- super(SineGen, self).__init__()
334
- self.sine_amp = sine_amp
335
- self.noise_std = noise_std
336
- self.harmonic_num = harmonic_num
337
- self.dim = self.harmonic_num + 1
338
- self.sampling_rate = samp_rate
339
- self.voiced_threshold = voiced_threshold
340
- self.flag_for_pulse = flag_for_pulse
341
-
342
- def _f02uv(self, f0):
343
- # generate uv signal
344
- uv = torch.ones_like(f0)
345
- uv = uv * (f0 > self.voiced_threshold)
346
- return uv
347
-
348
- def _f02sine(self, f0_values):
349
- """ f0_values: (batchsize, length, dim)
350
- where dim indicates fundamental tone and overtones
351
- """
352
- # convert to F0 in rad. The interger part n can be ignored
353
- # because 2 * np.pi * n doesn't affect phase
354
- rad_values = (f0_values / self.sampling_rate) % 1
355
-
356
- # initial phase noise (no noise for fundamental component)
357
- rand_ini = torch.rand(f0_values.shape[0], f0_values.shape[2], \
358
- device=f0_values.device)
359
- rand_ini[:, 0] = 0
360
- rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
361
-
362
- # instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
363
- if not self.flag_for_pulse:
364
- # for normal case
365
-
366
- # To prevent torch.cumsum numerical overflow,
367
- # it is necessary to add -1 whenever \sum_k=1^n rad_value_k > 1.
368
- # Buffer tmp_over_one_idx indicates the time step to add -1.
369
- # This will not change F0 of sine because (x-1) * 2*pi = x * 2*pi
370
- tmp_over_one = torch.cumsum(rad_values, 1) % 1
371
- tmp_over_one_idx = (tmp_over_one[:, 1:, :] -
372
- tmp_over_one[:, :-1, :]) < 0
373
- cumsum_shift = torch.zeros_like(rad_values)
374
- cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
375
-
376
- sines = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1)
377
- * 2 * np.pi)
378
- else:
379
- # If necessary, make sure that the first time step of every
380
- # voiced segments is sin(pi) or cos(0)
381
- # This is used for pulse-train generation
382
-
383
- # identify the last time step in unvoiced segments
384
- uv = self._f02uv(f0_values)
385
- uv_1 = torch.roll(uv, shifts=-1, dims=1)
386
- uv_1[:, -1, :] = 1
387
- u_loc = (uv < 1) * (uv_1 > 0)
388
-
389
- # get the instantanouse phase
390
- tmp_cumsum = torch.cumsum(rad_values, dim=1)
391
- # different batch needs to be processed differently
392
- for idx in range(f0_values.shape[0]):
393
- temp_sum = tmp_cumsum[idx, u_loc[idx, :, 0], :]
394
- temp_sum[1:, :] = temp_sum[1:, :] - temp_sum[0:-1, :]
395
- # stores the accumulation of i.phase within
396
- # each voiced segments
397
- tmp_cumsum[idx, :, :] = 0
398
- tmp_cumsum[idx, u_loc[idx, :, 0], :] = temp_sum
399
-
400
- # rad_values - tmp_cumsum: remove the accumulation of i.phase
401
- # within the previous voiced segment.
402
- i_phase = torch.cumsum(rad_values - tmp_cumsum, dim=1)
403
-
404
- # get the sines
405
- sines = torch.cos(i_phase * 2 * np.pi)
406
- return sines
407
-
408
- def forward(self, f0):
409
- """ sine_tensor, uv = forward(f0)
410
- input F0: tensor(batchsize=1, length, dim=1)
411
- f0 for unvoiced steps should be 0
412
- output sine_tensor: tensor(batchsize=1, length, dim)
413
- output uv: tensor(batchsize=1, length, 1)
414
- """
415
- with torch.no_grad():
416
- f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, \
417
- device=f0.device)
418
- # fundamental component
419
- f0_buf[:, :, 0] = f0[:, :, 0]
420
- for idx in np.arange(self.harmonic_num):
421
- # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
422
- f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (idx + 2)
423
-
424
- # generate sine waveforms
425
- sine_waves = self._f02sine(f0_buf) * self.sine_amp
426
-
427
- # generate uv signal
428
- # uv = torch.ones(f0.shape)
429
- # uv = uv * (f0 > self.voiced_threshold)
430
- uv = self._f02uv(f0)
431
-
432
- # noise: for unvoiced should be similar to sine_amp
433
- # std = self.sine_amp/3 -> max value ~ self.sine_amp
434
- # . for voiced regions is self.noise_std
435
- noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
436
- noise = noise_amp * torch.randn_like(sine_waves)
437
-
438
- # first: set the unvoiced part to 0 by uv
439
- # then: additive noise
440
- sine_waves = sine_waves * uv + noise
441
- return sine_waves, uv, noise
442
-
443
-
444
- class SourceModuleCycNoise_v1(torch.nn.Module):
445
- """ SourceModuleCycNoise_v1
446
- SourceModule(sampling_rate, noise_std=0.003, voiced_threshod=0)
447
- sampling_rate: sampling_rate in Hz
448
-
449
- noise_std: std of Gaussian noise (default: 0.003)
450
- voiced_threshold: threshold to set U/V given F0 (default: 0)
451
-
452
- cyc, noise, uv = SourceModuleCycNoise_v1(F0_upsampled, beta)
453
- F0_upsampled (batchsize, length, 1)
454
- beta (1)
455
- cyc (batchsize, length, 1)
456
- noise (batchsize, length, 1)
457
- uv (batchsize, length, 1)
458
- """
459
-
460
- def __init__(self, sampling_rate, noise_std=0.003, voiced_threshod=0):
461
- super(SourceModuleCycNoise_v1, self).__init__()
462
- self.sampling_rate = sampling_rate
463
- self.noise_std = noise_std
464
- self.l_cyc_gen = CyclicNoiseGen_v1(sampling_rate, noise_std,
465
- voiced_threshod)
466
-
467
- def forward(self, f0_upsamped, beta):
468
- """
469
- cyc, noise, uv = SourceModuleCycNoise_v1(F0, beta)
470
- F0_upsampled (batchsize, length, 1)
471
- beta (1)
472
- cyc (batchsize, length, 1)
473
- noise (batchsize, length, 1)
474
- uv (batchsize, length, 1)
475
- """
476
- # source for harmonic branch
477
- cyc, pulse, sine, uv, add_noi = self.l_cyc_gen(f0_upsamped, beta)
478
-
479
- # source for noise branch, in the same shape as uv
480
- noise = torch.randn_like(uv) * self.noise_std / 3
481
- return cyc, noise, uv
482
-
483
-
484
- class SourceModuleHnNSF(torch.nn.Module):
485
- """ SourceModule for hn-nsf
486
- SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
487
- add_noise_std=0.003, voiced_threshod=0)
488
- sampling_rate: sampling_rate in Hz
489
- harmonic_num: number of harmonic above F0 (default: 0)
490
- sine_amp: amplitude of sine source signal (default: 0.1)
491
- add_noise_std: std of additive Gaussian noise (default: 0.003)
492
- note that amplitude of noise in unvoiced is decided
493
- by sine_amp
494
- voiced_threshold: threhold to set U/V given F0 (default: 0)
495
-
496
- Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
497
- F0_sampled (batchsize, length, 1)
498
- Sine_source (batchsize, length, 1)
499
- noise_source (batchsize, length 1)
500
- uv (batchsize, length, 1)
501
- """
502
-
503
- def __init__(self, sampling_rate, harmonic_num=0, sine_amp=0.1,
504
- add_noise_std=0.003, voiced_threshod=0):
505
- super(SourceModuleHnNSF, self).__init__()
506
-
507
- self.sine_amp = sine_amp
508
- self.noise_std = add_noise_std
509
-
510
- # to produce sine waveforms
511
- self.l_sin_gen = SineGen(sampling_rate, harmonic_num,
512
- sine_amp, add_noise_std, voiced_threshod)
513
-
514
- # to merge source harmonics into a single excitation
515
- self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
516
- self.l_tanh = torch.nn.Tanh()
517
-
518
- def forward(self, x):
519
- """
520
- Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
521
- F0_sampled (batchsize, length, 1)
522
- Sine_source (batchsize, length, 1)
523
- noise_source (batchsize, length 1)
524
- """
525
- # source for harmonic branch
526
- sine_wavs, uv, _ = self.l_sin_gen(x)
527
- sine_merge = self.l_tanh(self.l_linear(sine_wavs))
528
-
529
- # source for noise branch, in the same shape as uv
530
- noise = torch.randn_like(uv) * self.sine_amp / 3
531
- return sine_merge, noise, uv
532
-
533
-
534
- if __name__ == '__main__':
535
- source = SourceModuleCycNoise_v1(24000)
536
- x = torch.randn(16, 25600, 1)
537
-
538
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/parallel_wavegan/optimizers/__init__.py DELETED
@@ -1,2 +0,0 @@
1
- from torch.optim import * # NOQA
2
- from .radam import * # NOQA
 
 
 
modules/parallel_wavegan/optimizers/radam.py DELETED
@@ -1,91 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- """RAdam optimizer.
4
-
5
- This code is drived from https://github.com/LiyuanLucasLiu/RAdam.
6
- """
7
-
8
- import math
9
- import torch
10
-
11
- from torch.optim.optimizer import Optimizer
12
-
13
-
14
- class RAdam(Optimizer):
15
- """Rectified Adam optimizer."""
16
-
17
- def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0):
18
- """Initilize RAdam optimizer."""
19
- defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
20
- self.buffer = [[None, None, None] for ind in range(10)]
21
- super(RAdam, self).__init__(params, defaults)
22
-
23
- def __setstate__(self, state):
24
- """Set state."""
25
- super(RAdam, self).__setstate__(state)
26
-
27
- def step(self, closure=None):
28
- """Run one step."""
29
- loss = None
30
- if closure is not None:
31
- loss = closure()
32
-
33
- for group in self.param_groups:
34
-
35
- for p in group['params']:
36
- if p.grad is None:
37
- continue
38
- grad = p.grad.data.float()
39
- if grad.is_sparse:
40
- raise RuntimeError('RAdam does not support sparse gradients')
41
-
42
- p_data_fp32 = p.data.float()
43
-
44
- state = self.state[p]
45
-
46
- if len(state) == 0:
47
- state['step'] = 0
48
- state['exp_avg'] = torch.zeros_like(p_data_fp32)
49
- state['exp_avg_sq'] = torch.zeros_like(p_data_fp32)
50
- else:
51
- state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32)
52
- state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32)
53
-
54
- exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
55
- beta1, beta2 = group['betas']
56
-
57
- exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)
58
- exp_avg.mul_(beta1).add_(1 - beta1, grad)
59
-
60
- state['step'] += 1
61
- buffered = self.buffer[int(state['step'] % 10)]
62
- if state['step'] == buffered[0]:
63
- N_sma, step_size = buffered[1], buffered[2]
64
- else:
65
- buffered[0] = state['step']
66
- beta2_t = beta2 ** state['step']
67
- N_sma_max = 2 / (1 - beta2) - 1
68
- N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t)
69
- buffered[1] = N_sma
70
-
71
- # more conservative since it's an approximated value
72
- if N_sma >= 5:
73
- step_size = math.sqrt(
74
- (1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step']) # NOQA
75
- else:
76
- step_size = 1.0 / (1 - beta1 ** state['step'])
77
- buffered[2] = step_size
78
-
79
- if group['weight_decay'] != 0:
80
- p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)
81
-
82
- # more conservative since it's an approximated value
83
- if N_sma >= 5:
84
- denom = exp_avg_sq.sqrt().add_(group['eps'])
85
- p_data_fp32.addcdiv_(-step_size * group['lr'], exp_avg, denom)
86
- else:
87
- p_data_fp32.add_(-step_size * group['lr'], exp_avg)
88
-
89
- p.data.copy_(p_data_fp32)
90
-
91
- return loss