forked from rishikksh20/AdaSpeech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variance_predictor.py
314 lines (247 loc) · 9.78 KB
/
variance_predictor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import torch
import torch.nn.functional as F
from typing import Optional
from core.modules import LayerNorm
class VariancePredictor(torch.nn.Module):
def __init__(
self,
idim: int,
n_layers: int = 2,
n_chans: int = 256,
out: int = 1,
kernel_size: int = 3,
dropout_rate: float = 0.5,
offset: float = 1.0,
):
super(VariancePredictor, self).__init__()
self.offset = offset
self.conv = torch.nn.ModuleList()
for idx in range(n_layers):
in_chans = idim if idx == 0 else n_chans
self.conv += [
torch.nn.Sequential(
torch.nn.Conv1d(
in_chans,
n_chans,
kernel_size,
stride=1,
padding=(kernel_size - 1) // 2,
),
torch.nn.ReLU(),
LayerNorm(n_chans),
torch.nn.Dropout(dropout_rate),
)
]
self.linear = torch.nn.Linear(n_chans, out)
def _forward(
self,
xs: torch.Tensor,
is_inference: bool = False,
is_log_output: bool = False,
alpha: float = 1.0,
) -> torch.Tensor:
xs = xs.transpose(1, -1) # (B, idim, Tmax)
for f in self.conv:
xs = f(xs) # (B, C, Tmax)
# NOTE: calculate in log domain
xs = self.linear(xs.transpose(1, -1)).squeeze(-1) # (B, Tmax)
if is_inference and is_log_output:
# # NOTE: calculate in linear domain
xs = torch.clamp(
torch.round(xs.exp() - self.offset), min=0
).long() # avoid negative value
xs = xs * alpha
return xs
def forward(
self, xs: torch.Tensor, x_masks: Optional[torch.Tensor] = None
) -> torch.Tensor:
"""Calculate forward propagation.
Args:
xs (Tensor): Batch of input sequences (B, Tmax, idim).
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
Returns:
Tensor: Batch of predicted durations in log domain (B, Tmax).
"""
xs = self._forward(xs)
if x_masks is not None:
xs = xs.masked_fill(x_masks, 0.0)
return xs
def inference(
self, xs: torch.Tensor, is_log_output: bool = False, alpha: float = 1.0
) -> torch.Tensor:
"""Inference duration.
Args:
xs (Tensor): Batch of input sequences (B, Tmax, idim).
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
Returns:
LongTensor: Batch of predicted durations in linear domain (B, Tmax).
"""
return self._forward(
xs, is_inference=True, is_log_output=is_log_output, alpha=alpha
)
class EnergyPredictor(torch.nn.Module):
def __init__(
self,
idim,
n_layers=2,
n_chans=256,
kernel_size=3,
dropout_rate=0.1,
offset=1.0,
min=0,
max=0,
n_bins=256,
):
"""Initilize Energy predictor module.
Args:
idim (int): Input dimension.
n_layers (int, optional): Number of convolutional layers.
n_chans (int, optional): Number of channels of convolutional layers.
kernel_size (int, optional): Kernel size of convolutional layers.
dropout_rate (float, optional): Dropout rate.
offset (float, optional): Offset value to avoid nan in log domain.
"""
super(EnergyPredictor, self).__init__()
# self.bins = torch.linspace(min, max, n_bins - 1).cuda()
self.register_buffer("energy_bins", torch.linspace(min, max, n_bins - 1))
self.predictor = VariancePredictor(idim)
def forward(self, xs: torch.Tensor, x_masks: torch.Tensor):
"""Calculate forward propagation.
Args:
xs (Tensor): Batch of input sequences (B, Tmax, idim).
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
Returns:
Tensor: Batch of predicted durations in log domain (B, Tmax).
"""
return self.predictor(xs, x_masks)
def inference(self, xs: torch.Tensor, alpha: float = 1.0):
"""Inference duration.
Args:
xs (Tensor): Batch of input sequences (B, Tmax, idim).
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
Returns:
LongTensor: Batch of predicted durations in linear domain (B, Tmax).
"""
out = self.predictor.inference(xs, False, alpha=alpha)
return self.to_one_hot(out) # Need to do One hot code
def to_one_hot(self, x):
# e = de_norm_mean_std(e, hp.e_mean, hp.e_std)
# For pytorch > = 1.6.0
quantize = torch.bucketize(x, self.energy_bins).to(device=x.device) # .cuda()
return F.one_hot(quantize.long(), 256).float()
class PitchPredictor(torch.nn.Module):
def __init__(
self,
idim,
n_layers=2,
n_chans=384,
kernel_size=3,
dropout_rate=0.1,
offset=1.0,
min=0,
max=0,
n_bins=256,
):
"""Initilize pitch predictor module.
Args:
idim (int): Input dimension.
n_layers (int, optional): Number of convolutional layers.
n_chans (int, optional): Number of channels of convolutional layers.
kernel_size (int, optional): Kernel size of convolutional layers.
dropout_rate (float, optional): Dropout rate.
offset (float, optional): Offset value to avoid nan in log domain.
"""
super(PitchPredictor, self).__init__()
# self.bins = torch.exp(torch.linspace(torch.log(torch.tensor(min)), torch.log(torch.tensor(max)), n_bins - 1)).cuda()
self.register_buffer(
"pitch_bins",
torch.exp(
torch.linspace(
torch.log(torch.tensor(min)),
torch.log(torch.tensor(max)),
n_bins - 1,
)
),
)
self.predictor = VariancePredictor(idim)
def forward(self, xs: torch.Tensor, x_masks: torch.Tensor):
"""Calculate forward propagation.
Args:
xs (Tensor): Batch of input sequences (B, Tmax, idim).
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
Returns:
Tensor: Batch of predicted durations in log domain (B, Tmax).
"""
return self.predictor(xs, x_masks)
def inference(self, xs: torch.Tensor, alpha: float = 1.0):
"""Inference duration.
Args:
xs (Tensor): Batch of input sequences (B, Tmax, idim).
x_masks (ByteTensor, optional): Batch of masks indicating padded part (B, Tmax).
Returns:
LongTensor: Batch of predicted durations in linear domain (B, Tmax).
"""
out = self.predictor.inference(xs, False, alpha=alpha)
return self.to_one_hot(out)
def to_one_hot(self, x: torch.Tensor):
# e = de_norm_mean_std(e, hp.e_mean, hp.e_std)
# For pytorch > = 1.6.0
quantize = torch.bucketize(x, self.pitch_bins).to(device=x.device) # .cuda()
return F.one_hot(quantize.long(), 256).float()
class PitchPredictorLoss(torch.nn.Module):
"""Loss function module for duration predictor.
The loss value is Calculated in log domain to make it Gaussian.
"""
def __init__(self, offset=1.0):
"""Initilize duration predictor loss module.
Args:
offset (float, optional): Offset value to avoid nan in log domain.
"""
super(PitchPredictorLoss, self).__init__()
self.criterion = torch.nn.MSELoss()
self.offset = offset
def forward(self, outputs, targets):
"""Calculate forward propagation.
Args:
outputs (Tensor): Batch of prediction durations in log domain (B, T)
targets (LongTensor): Batch of groundtruth durations in linear domain (B, T)
Returns:
Tensor: Mean squared error loss value.
Note:
`outputs` is in log domain but `targets` is in linear domain.
"""
# NOTE: We convert the output in log domain low error value
# print("Output :", outputs[0])
# print("Before Output :", targets[0])
# targets = torch.log(targets.float() + self.offset)
# print("Before Output :", targets[0])
# outputs = torch.log(outputs.float() + self.offset)
loss = self.criterion(outputs, targets)
# print(loss)
return loss
class EnergyPredictorLoss(torch.nn.Module):
"""Loss function module for duration predictor.
The loss value is Calculated in log domain to make it Gaussian.
"""
def __init__(self, offset=1.0):
"""Initilize duration predictor loss module.
Args:
offset (float, optional): Offset value to avoid nan in log domain.
"""
super(EnergyPredictorLoss, self).__init__()
self.criterion = torch.nn.MSELoss()
self.offset = offset
def forward(self, outputs, targets):
"""Calculate forward propagation.
Args:
outputs (Tensor): Batch of prediction durations in log domain (B, T)
targets (LongTensor): Batch of groundtruth durations in linear domain (B, T)
Returns:
Tensor: Mean squared error loss value.
Note:
`outputs` is in log domain but `targets` is in linear domain.
"""
# NOTE: outputs is in log domain while targets in linear
# targets = torch.log(targets.float() + self.offset)
loss = self.criterion(outputs, targets)
return loss