-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathsnowdar-xvector-FD-AL.py
396 lines (329 loc) · 15.2 KB
/
snowdar-xvector-FD-AL.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# -*- coding:utf-8 -*-
# Copyright xmuspeech.
# Reference paper: Deep Representation Decomposition for Rate-invariant Speaker Verification
# https://doi.org/10.48550/arXiv.2205.14294
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import libs.support.utils as utils
from libs.nnet import *
class SEBlock(torch.nn.Module):
def __init__(self, input_dim, ratio=8, inplace=True):
'''
@ratio: a reduction ratio which allows us to vary the capacity and computational cost of the SE blocks
in the network.
'''
super(SEBlock, self).__init__()
self.input_dim = input_dim
self.fc_1 = TdnnAffine(input_dim, input_dim//ratio)
self.relu = torch.nn.ReLU(inplace=inplace)
self.fc_2 = TdnnAffine(input_dim//ratio, input_dim)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
x = inputs.mean(dim=2, keepdim=True)
x = self.relu(self.fc_1(x))
scale = self.sigmoid(self.fc_2(x))
return scale
class RFM(TopVirtualNnet):
def init(self, input_dim, gradient_clipping_bounds=0.25, scale_factor=1.0):
tdnn_layer_params={"nonlinearity":'relu', "nonlinearity_params":{"inplace":True},
"bn-relu":False,
"bn":True,
"bn_params":{"momentum":0.5, "affine":False, "track_running_stats":True}}
tdnn_params={"nonlinearity":"", "bn":False}
self.layer1 = ReluBatchNormTdnnLayer(input_dim,512,**tdnn_layer_params)
self._lambda = scale_factor
self._clipping = gradient_clipping_bounds
@utils.for_device_free
def forward(self, inputs):
x = self.layer1(inputs)
return x
class DAL_regularizer(TopVirtualNnet):
def init(self, n_in):
self.w_id = TdnnAffine(n_in, n_in, bias=False)
self.w_noise = TdnnAffine(n_in, n_in, bias=False)
def forward(self, features_noise, features_id):
features_noise = self.w_noise(features_noise)
features_id = self.w_id(features_id)
normalized_id = F.normalize(features_id.squeeze(dim=2), dim=1)
normalized_noise = F.normalize(features_noise.squeeze(dim=2), dim=1)
cosine = torch.sum(normalized_id * normalized_noise, dim=1).mean()
return cosine.pow(2)
class Xvector(TopVirtualNnet):
""" A composite x-vector framework """
## Base parameters - components - loss - training strategy.
def init(self, inputs_dim, num_targets, extend=False, skip_connection=False,
mixup=False, mixup_pooling=False,mixup_alpha=1.0,
specaugment=False, specaugment_params={},
aug_dropout=0., context_dropout=0., hidden_dropout=0., dropout_params={},
SE=False, se_ratio=4,
tdnn_layer_params={},
tdnn6=True, tdnn7_params={},
pooling="statistics", pooling_params={},
margin_loss=False, margin_loss_params={},
use_step=False, step_params={},
transfer_from="softmax_loss",
training=True, extracted_embedding="far"):
## Params.
default_dropout_params = {
"type":"default", # default | random
"start_p":0.,
"dim":2,
"method":"uniform", # uniform | normals
"continuous":False,
"inplace":True
}
default_tdnn_layer_params = {
"nonlinearity":'relu', "nonlinearity_params":{"inplace":True},
"bn-relu":False, "bn":True, "bn_params":{"momentum":0.5, "affine":False, "track_running_stats":True}
}
default_pooling_params = {
"num_nodes":1500,
"num_head":1,
"share":True,
"affine_layers":1,
"hidden_size":64,
"context":[0],
"stddev":True,
"temperature":False,
"fixed":True,
"stddev":True
}
default_margin_loss_params = {
"method":"am", "m":0.2,
"feature_normalize":True, "s":30,
"double":False,
"mhe_loss":False, "mhe_w":0.01,
"inter_loss":0.,
"ring_loss":0.,
"curricular":False,
"noise":False,
"total_iter":1000000,
"double_target":False,
"reg_loss":False,
"sqrt":False,
"square":False,
"sub_group":False,
"only_sub":False
}
default_step_params = {
"T":None,
"m":False, "lambda_0":0, "lambda_b":1000, "alpha":5, "gamma":1e-4,
"s":False, "s_tuple":(30, 12), "s_list":None,
"t":False, "t_tuple":(0.5, 1.2),
"p":False, "p_tuple":(0.5, 0.1)
}
dropout_params = utils.assign_params_dict(default_dropout_params, dropout_params)
tdnn_layer_params = utils.assign_params_dict(default_tdnn_layer_params, tdnn_layer_params)
# If param is not be specified, default it w.r.t tdnn_layer_params.
tdnn7_params = utils.assign_params_dict(tdnn_layer_params, tdnn7_params)
pooling_params = utils.assign_params_dict(default_pooling_params, pooling_params)
margin_loss_params = utils.assign_params_dict(default_margin_loss_params, margin_loss_params)
step_params = utils.assign_params_dict(default_step_params, step_params)
## Var.
self.skip_connection = skip_connection
self.use_step = use_step
self.step_params = step_params
self.extracted_embedding = extracted_embedding # For extract.
## Nnet.
# Head
self.mixup = Mixup(alpha=mixup_alpha) if mixup or mixup_pooling else None
# self.mixup_pooling = Mixup(alpha=mixup_alpha) if mixup_pooling else None
self.specaugment = SpecAugment(**specaugment_params) if specaugment else None
self.aug_dropout = get_dropout_from_wrapper(aug_dropout, dropout_params)
self.context_dropout = ContextDropout(p=context_dropout) if context_dropout > 0 else None
self.hidden_dropout = get_dropout_from_wrapper(hidden_dropout, dropout_params)
# Frame level
self.tdnn1 = ReluBatchNormTdnnLayer(inputs_dim,512,[-2,-1,0,1,2], **tdnn_layer_params)
self.se1 = SEBlock(512, ratio=se_ratio) if SE else None
self.ex_tdnn1 = ReluBatchNormTdnnLayer(512,512, **tdnn_layer_params) if extend else None
self.tdnn2 = ReluBatchNormTdnnLayer(512,512,[-2,0,2], **tdnn_layer_params)
self.se2 = SEBlock(512, ratio=se_ratio) if SE else None
self.ex_tdnn2 = ReluBatchNormTdnnLayer(512,512, **tdnn_layer_params) if extend else None
self.tdnn3 = ReluBatchNormTdnnLayer(512,512,[-3,0,3], **tdnn_layer_params)
self.se3 = SEBlock(512, ratio=se_ratio) if SE else None
self.ex_tdnn3 = ReluBatchNormTdnnLayer(512,512, **tdnn_layer_params) if extend else None
self.ex_tdnn4 = ReluBatchNormTdnnLayer(512,512,[-4,0,4], **tdnn_layer_params) if extend else None
self.se4 = SEBlock(512, ratio=se_ratio) if SE and extend else None
self.ex_tdnn5 = ReluBatchNormTdnnLayer(512,512, **tdnn_layer_params) if extend else None
self.tdnn4 = ReluBatchNormTdnnLayer(512,512, **tdnn_layer_params)
num_nodes = pooling_params.pop("num_nodes")
self.tdnn5 = ReluBatchNormTdnnLayer(512, num_nodes, **tdnn_layer_params)
# Pooling
stddev = pooling_params.pop("stddev")
if pooling == "lde":
self.stats = LDEPooling(num_nodes, c_num=pooling_params["num_head"])
elif pooling == "attentive":
self.stats = AttentiveStatisticsPooling(num_nodes, affine_layers=pooling_params["affine_layers"],
hidden_size=pooling_params["hidden_size"],
context=pooling_params["context"], stddev=stddev)
elif pooling == "multi-head":
self.stats = MultiHeadAttentionPooling(num_nodes, stddev=stddev, **pooling_params)
elif pooling == "multi-resolution":
self.stats = MultiResolutionMultiHeadAttentionPooling(num_nodes, **pooling_params)
else:
self.stats = StatisticsPooling(num_nodes, stddev=stddev)
stats_dim = self.stats.get_output_dim()
# Segment level
if tdnn6:
self.tdnn6 = ReluBatchNormTdnnLayer(stats_dim, 512, **tdnn_layer_params)
tdnn7_dim = 512
else:
self.tdnn6 = None
tdnn7_dim = stats_dim
if tdnn7_params["nonlinearity"] == "default":
tdnn7_params["nonlinearity"] = tdnn_layer_params["nonlinearity"]
self.tdnn7 = ReluBatchNormTdnnLayer(tdnn7_dim,512, **tdnn7_params)
self.DAL = DAL_regularizer(512)
self.att = SEBlock(512)
# Loss
# Do not need when extracting embedding.
if training :
if margin_loss:
self.loss = MarginSoftmaxLoss(512, num_targets, **margin_loss_params)
self.loss2 = MarginSoftmaxLoss(512,9)
else:
self.loss = SoftmaxLoss(512, num_targets)
self.loss2 = SoftmaxLoss(512,9)
self.wrapper_loss = MixupLoss(self.loss, self.mixup) if mixup or mixup_pooling else None
# An example to using transform-learning without initializing loss.affine parameters
self.transform_keys = ["tdnn1","tdnn2","tdnn3","tdnn4","tdnn5","stats","tdnn6","tdnn7",
"ex_tdnn1","ex_tdnn2","ex_tdnn3","ex_tdnn4","ex_tdnn5",
"se1","se2","se3","se4","loss"]
if margin_loss and transfer_from == "softmax_loss":
# For softmax_loss to am_softmax_loss
self.rename_transform_keys = {"loss.affine.weight":"loss.weight"}
@utils.for_device_free
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
x = inputs
# x = self.auto(self.mixup, x) if mixup
x = self.auto(self.specaugment, x)
x = self.auto(self.aug_dropout, x)
x = self.auto(self.context_dropout, x)
x = self.tdnn1(x)
if self.skip_connection:
identity = x
x = self.auto(self.se1, x)
x = self.auto(self.ex_tdnn1, x)
x = self.tdnn2(x)
x = self.auto(self.se2, x)
x = self.auto(self.ex_tdnn2, x)
x = self.tdnn3(x)
x = self.auto(self.se3, x)
x = self.auto(self.ex_tdnn3, x)
x = self.auto(self.ex_tdnn4, x)
x = self.auto(self.se4, x)
x = self.auto(self.ex_tdnn5, x)
x = self.tdnn4(x)
if self.skip_connection:
x = x + identity
x = self.tdnn5(x)
x = self.stats(x)
# if mixup_pooling==True:
# x = self.auto(self.mixup, x)
x = self.auto(self.tdnn6, x)
x = self.tdnn7(x)
scale = self.att(x)
y = x*scale
x = x*(1-scale)
x = self.auto(self.hidden_dropout, x)
cc = self.DAL(y,x)
return x,y,cc
@utils.for_device_free
def get_loss(self, inputs, targets,inputs2,targets2):
"""Should call get_loss() after forward() with using Xvector model function.
e.g.:
m=Xvector(20,10)
loss=m.get_loss(m(inputs),targets)
model.get_loss [custom] -> loss.forward [custom]
|
v
model.get_accuracy [custom] -> loss.get_accuracy [custom] -> loss.compute_accuracy [static] -> loss.predict [static]
"""
if self.wrapper_loss is not None:
return self.wrapper_loss(inputs, targets)
else:
return self.loss(inputs, targets) + 0.1 * self.loss2(inputs2,targets2)
@utils.for_device_free
def get_accuracy(self, targets):
"""Should call get_accuracy() after get_loss().
@return: return accuracy
"""
if self.wrapper_loss is not None:
return self.wrapper_loss.get_accuracy(targets)
else:
return self.loss.get_accuracy(targets)
@for_extract_embedding(maxChunk=10000, isMatrix=True)
def extract_embedding(self, inputs):
"""
inputs: a 3-dimensional tensor with batch-dim = 1 or normal features matrix
return: an 1-dimensional vector after processed by decorator
"""
x = inputs
x = self.tdnn1(x)
if self.skip_connection:
identity = x
x = self.auto(self.se1, x)
x = self.auto(self.ex_tdnn1, x)
x = self.tdnn2(x)
x = self.auto(self.se2, x)
x = self.auto(self.ex_tdnn2, x)
x = self.tdnn3(x)
x = self.auto(self.se3, x)
x = self.auto(self.ex_tdnn3, x)
x = self.auto(self.ex_tdnn4, x)
x = self.auto(self.se4, x)
x = self.auto(self.ex_tdnn5, x)
x = self.tdnn4(x)
if self.skip_connection:
x = x + identity
x = self.tdnn5(x)
x = self.stats(x)
if self.extracted_embedding == "far" :
assert self.tdnn6 is not None
xvector = self.tdnn6.affine(x)
elif self.extracted_embedding == "near_affine":
x = self.auto(self.tdnn6, x)
xvector = self.tdnn7.affine(x)
elif self.extracted_embedding == "near":
x = self.auto(self.tdnn6, x)
xvector = self.tdnn7(x)
scale = self.att(xvector)
xvector = xvector*(1-scale)
return xvector
def get_warmR_T(T_0, T_mult, epoch):
n = int(math.log(max(0.05, (epoch / T_0 * (T_mult - 1) + 1)), T_mult))
T_cur = epoch - T_0 * (T_mult ** n - 1) / (T_mult - 1)
T_i = T_0 * T_mult ** (n)
return T_cur, T_i
def compute_decay_value(self, start, end, T_cur, T_i):
# Linear decay in every cycle time.
return start - (start - end)/(T_i-1) * (T_cur%T_i)
def step(self, epoch, this_iter, epoch_batchs):
# Heated up for t and s.
# Decay for margin and dropout p.
if self.use_step:
if self.step_params["m"]:
current_postion = epoch*epoch_batchs + this_iter
lambda_factor = max(self.step_params["lambda_0"],
self.step_params["lambda_b"]*(1+self.step_params["gamma"]*current_postion)**(-self.step_params["alpha"]))
self.loss.step(lambda_factor)
if self.step_params["T"] is not None and (self.step_params["t"] or self.step_params["p"]):
T_cur, T_i = get_warmR_T(*self.step_params["T"], epoch)
T_cur = T_cur*epoch_batchs + this_iter
T_i = T_i * epoch_batchs
if self.step_params["t"]:
self.loss.t = self.compute_decay_value(*self.step_params["t_tuple"], T_cur, T_i)
if self.step_params["p"]:
self.aug_dropout.p = self.compute_decay_value(*self.step_params["p_tuple"], T_cur, T_i)
if self.step_params["s"]:
self.loss.s = self.step_params["s_tuple"][self.step_params["s_list"][epoch]]