-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathsnowdar_xvector.py
executable file
·403 lines (342 loc) · 16.3 KB
/
snowdar_xvector.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
397
398
399
400
401
402
# -*- coding:utf-8 -*-
# Copyright xmuspeech (Author: Snowdar 2020-02-05)
import math
import torch
import torch.nn.functional as F
import libs.support.utils as utils
from libs.nnet import *
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_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
}
default_step_params = {
"margin_warm":False,
"margin_warm_conf":{"start_epoch":5.,"end_epoch":10.,"offset_margin":-0.2,"init_lambda":0.0},
"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 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)
elif pooling == "xi-postmean-softplus2":
self.stats = xivec_stdinit_softplus2_prec_pooling(num_nodes, hidden_size=pooling_params["hidden_size"], stddev=False)
elif pooling == "xi-postdist-softplus2":
self.stats = xivec_stdinit_softplus2_prec_pooling(num_nodes, hidden_size=pooling_params["hidden_size"], stddev=True)
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)
# Loss
# Do not need when extracting embedding.
if training :
if margin_loss:
self.loss = MarginSoftmaxLoss(512, num_targets, **margin_loss_params)
if self.use_step and self.step_params["margin_warm"]:
self.margin_warm = MarginWarm(**step_params["margin_warm_conf"])
else:
self.loss = SoftmaxLoss(512, num_targets)
self.wrapper_loss = MixupLoss(self.loss, self.mixup) if mixup 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"}
@torch.jit.unused
@utils.for_device_free
def forward(self, inputs, x_len: torch.Tensor=torch.empty(0)):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
x = inputs
x = self.auto(self.mixup, x)
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)
x = self.auto(self.tdnn6, x)
x = self.tdnn7(x)
x = self.auto(self.hidden_dropout, x)
return x
@utils.for_device_free
def get_loss(self, inputs, targets):
"""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)
@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)
return xvector
def extract_embedding_jit(self, x: torch.Tensor, position: str = 'near') -> torch.Tensor:
"""
inputs: a 3-dimensional tensor with batch-dim = 1 or normal features matrix
return: an 1-dimensional vector after processed by decorator
"""
# Tensor shape is not modified in libs.nnet.resnet.py for calling free, such as using this framework in cv.
x = x.unsqueeze(1)
x = self.repvgg(x)
x = x.reshape(x.shape[0], x.shape[1]*x.shape[2], x.shape[3])
x = self.stats(x)
if position == "far" and self.fc1 is not None:
xvector = self.fc1.affine(x)
elif position == "near_affine":
if self.fc1 is not None:
x=self.fc1(x)
xvector = self.fc2.affine(x)
elif position == "near":
if self.fc1 is not None:
x=self.fc1(x)
xvector = self.fc2(x)
# xvector = F.normalize(xvector)
else:
raise TypeError("Expected far or near position, but got {}".format(position))
return xvector
@torch.jit.export
def extract_embedding_whole(self, input: torch.Tensor, position: str = 'near', maxChunk: int = 4000, isMatrix: bool = True):
with torch.no_grad():
if isMatrix:
input = torch.unsqueeze(input, dim=0)
input = input.transpose(1, 2)
num_frames = input.shape[2]
num_split = (num_frames + maxChunk - 1) // maxChunk
split_size = num_frames // num_split
offset = 0
embedding_stats = torch.zeros(1, self.embd_dim, 1).to(input.device)
for _ in range(0, num_split-1):
this_embedding = self.extract_embedding_jit(
input[:, :, offset:offset+split_size], position)
offset += split_size
embedding_stats += split_size*this_embedding
last_embedding = self.extract_embedding_jit(
input[:, :, offset:], position)
embedding = (embedding_stats + (num_frames-offset)
* last_embedding) / num_frames
return torch.squeeze(embedding.transpose(1, 2)).cpu()
@torch.jit.export
def embedding_dim(self) -> int:
""" Export interface for c++ call, return embedding dim of the model
"""
return self.embd_dim
def get_warmR_T(self,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"]))
lambda_m = 1/(1 + lambda_factor)
self.loss.step(lambda_m)
if self.step_params["T"] is not None and (self.step_params["t"] or self.step_params["p"]):
T_cur, T_i = self.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]]
def step_iter(self, epoch, cur_step):
# For iterabledataset
if self.use_step:
if self.step_params["margin_warm"]:
offset_margin, lambda_m = self.margin_warm.step(cur_step)
lambda_m = max(1e-3,lambda_m)
self.loss.step(lambda_m,offset_margin)
if self.step_params["m"]:
lambda_factor = max(self.step_params["lambda_0"],
self.step_params["lambda_b"]*(1+self.step_params["gamma"]*cur_step)**(-self.step_params["alpha"]))
lambda_m = 1/(1 + lambda_factor)
self.loss.step(lambda_m)
if self.step_params["T"] is not None and (self.step_params["t"] or self.step_params["p"]):
T_cur, T_i = self.get_warmR_T(*self.step_params["T"], cur_step)
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]]