-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathecapa-tdnn-xvector.py
372 lines (315 loc) · 15.1 KB
/
ecapa-tdnn-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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import sys
sys.path.insert(0, 'subtools/pytorch')
import libs.support.utils as utils
from libs.nnet import *
# refs:
# 1. ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in TDNN Based Speaker Verification
# https://arxiv.org/abs/2005.07143
# 2. Unofficial implementation of the ECAPA-TDNN model.
# https://github.com/lawlict/ECAPA-TDNN
''' Res2Conv1d + BatchNorm1d + ReLU
'''
class Res2Conv1dReluBn(nn.Module):
'''
inputs_dim == out_channels == channels
'''
def __init__(self, channels, kernel_size=1, stride=1, padding=0, dilation=1, bias=False, scale=4):
super().__init__()
assert channels % scale == 0, "{} % {} != 0".format(channels, scale)
self.scale = scale
self.width = channels // scale
self.nums = scale if scale == 1 else scale - 1
self.convs = []
self.bns = []
for i in range(self.nums):
self.convs.append(nn.Conv1d(self.width, self.width, kernel_size, stride, padding, dilation, bias=bias))
self.bns.append(nn.BatchNorm1d(self.width))
self.convs = nn.ModuleList(self.convs)
self.bns = nn.ModuleList(self.bns)
def forward(self, x):
out = []
spx = torch.split(x, self.width, 1)
for i in range(self.nums):
if i == 0:
sp = spx[i]
else:
sp = sp + spx[i]
# Order: conv -> relu -> bn
sp = self.convs[i](sp)
sp = self.bns[i](F.relu(sp))
out.append(sp)
if self.scale != 1:
out.append(spx[self.nums])
out = torch.cat(out, dim=1)
return out
''' Conv1d + BatchNorm1d + ReLU
'''
class Conv1dReluBn(nn.Module):
def __init__(self, inputs_dim, out_channels, kernel_size=1, stride=1, padding=0, dilation=1, bias=False):
super().__init__()
self.conv = nn.Conv1d(inputs_dim, out_channels, kernel_size, stride, padding, dilation, bias=bias)
self.bn = nn.BatchNorm1d(out_channels)
def forward(self, x):
return self.bn(F.relu(self.conv(x)))
''' The SE connection of 1D case.
'''
class SE_Connect(nn.Module):
def __init__(self, channels, s=4):
super().__init__()
assert channels % s == 0, "{} % {} != 0".format(channesl, s)
self.linear1 = nn.Linear(channels, channels // s)
self.linear2 = nn.Linear(channels // s, channels)
def forward(self, x):
out = x.mean(dim=2)
out = F.relu(self.linear1(out))
out = torch.sigmoid(self.linear2(out))
out = x * out.unsqueeze(2)
return out
#Another implementation of SE_Connect
# class SE_Connect(nn.Module):
# def __init__(self, channels, bottleneck=128):
# super(SE_Connect, self).__init__()
# self.se = nn.Sequential(
# nn.AdaptiveAvgPool1d(1),
# nn.Conv1d(channels, bottleneck, kernel_size=1, padding=0),
# nn.ReLU(),
# # nn.BatchNorm1d(bottleneck),
# nn.Conv1d(bottleneck, channels, kernel_size=1, padding=0),
# nn.Sigmoid(),
# )
# def forward(self, input):
# x = self.se(input)
# return input * x
''' SE-Res2Block.
Note: residual connection is implemented in the ECAPA_TDNN model, not here.
'''
def SE_Res2Block(channels, kernel_size, stride, padding, dilation, scale):
return nn.Sequential(
Conv1dReluBn(channels, channels, kernel_size=1, stride=1, padding=0),
Res2Conv1dReluBn(channels, kernel_size, stride, padding, dilation, scale=scale),
Conv1dReluBn(channels, channels, kernel_size=1, stride=1, padding=0),
SE_Connect(channels)
)
''' Attentive weighted mean and standard deviation pooling.
'''
class AttentiveStatsPool(nn.Module):
def __init__(self, in_dim, bottleneck_dim):
super().__init__()
# Use Conv1d with stride == 1 rather than Linear, then we don't need to transpose inputs.
self.linear1 = nn.Conv1d(in_dim, bottleneck_dim, kernel_size=1) # equals W and b in the paper
self.linear2 = nn.Conv1d(bottleneck_dim, in_dim, kernel_size=1) # equals V and k in the paper
def forward(self, x):
# DON'T use ReLU here! In experiments, I find ReLU hard to converge.
alpha = torch.tanh(self.linear1(x))
alpha = torch.softmax(self.linear2(alpha), dim=2)
mean = torch.sum(alpha * x, dim=2)
residuals = torch.sum(alpha * x ** 2, dim=2) - mean ** 2
std = torch.sqrt(residuals.clamp(min=1e-9))
return torch.cat([mean, std], dim=1)
''' Implementation of
"ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in TDNN Based Speaker Verification".
Note that we DON'T concatenate the last frame-wise layer with non-weighted mean and standard deviation,
because it brings little improvment but significantly increases model parameters.
As a result, this implementation basically equals the A.2 of Table 2 in the paper.
'''
class ECAPA_TDNN(TopVirtualNnet):
def init(self, inputs_dim, num_targets, channels=512, embd_dim=192,
aug_dropout=0., tail_dropout=0., training=True,
extracted_embedding="near", mixup=False, mixup_alpha=1.0,
pooling="ecpa-attentive", pooling_params={}, fc1=False, fc1_params={}, fc2_params={},
margin_loss= True, margin_loss_params={}, use_step=False, step_params={}, transfer_from="softmax_loss" ):
default_pooling_params = {
"num_head":1,
"hidden_size":64,
"share":True,
"affine_layers":1,
"context":[0],
"stddev":True,
"temperature":False,
"fixed":True
}
default_fc_params = {
"nonlinearity":'relu', "nonlinearity_params":{"inplace":True},
"bn-relu":False,
"bn":True,
"bn_params":{"momentum":0.5, "affine":True, "track_running_stats":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 = {
"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)
}
self.use_step = use_step
self.step_params = step_params
self.extracted_embedding = extracted_embedding
pooling_params = utils.assign_params_dict(default_pooling_params, pooling_params)
fc1_params = utils.assign_params_dict(default_fc_params, fc1_params)
fc2_params = utils.assign_params_dict(default_fc_params, fc2_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)
self.mixup = Mixup(alpha=mixup_alpha) if mixup else None
self.layer1 = Conv1dReluBn(inputs_dim, channels, kernel_size=5, padding=2)
self.layer2 = SE_Res2Block(channels, kernel_size=3, stride=1, padding=2, dilation=2, scale=8)
self.layer3 = SE_Res2Block(channels, kernel_size=3, stride=1, padding=3, dilation=3, scale=8)
self.layer4 = SE_Res2Block(channels, kernel_size=3, stride=1, padding=4, dilation=4, scale=8)
cat_channels = channels * 3
self.conv = nn.Conv1d(cat_channels, cat_channels, kernel_size=1)
self.bn_conv = nn.BatchNorm1d(cat_channels)
# Pooling
stddev = pooling_params.pop("stddev")
if pooling == "attentive":
self.stats = AttentiveStatisticsPooling(cat_channels, hidden_size=pooling_params["hidden_size"],context=pooling_params["context"], stddev=stddev)
self.bn_stats = nn.BatchNorm1d(cat_channels * 2)
self.fc1 = ReluBatchNormTdnnLayer(cat_channels * 2, embd_dim, **fc1_params) if fc1 else None
elif pooling == "ecpa-attentive":
self.stats = AttentiveStatsPool(cat_channels,128)
self.bn_stats = nn.BatchNorm1d(cat_channels * 2)
self.fc1 = ReluBatchNormTdnnLayer(cat_channels * 2, embd_dim, **fc1_params) if fc1 else None
elif pooling == "multi-head":
self.stats = MultiHeadAttentionPooling(cat_channels, stddev=stddev, **pooling_params)
self.bn_stats = nn.BatchNorm1d(cat_channels * 2)
self.fc1 = ReluBatchNormTdnnLayer(cat_channels * 2, embd_dim, **fc1_params) if fc1 else None
elif pooling == "global-multi":
self.stats = GlobalMultiHeadAttentionPooling(cat_channels,stddev=stddev, **pooling_params)
self.bn_stats = nn.BatchNorm1d(cat_channels * 2* pooling_params["num_head"])
self.fc1 = ReluBatchNormTdnnLayer(cat_channels * 2* pooling_params["num_head"], embd_dim, **fc1_params) if fc1 else None
elif pooling == "multi-resolution":
self.stats = MultiResolutionMultiHeadAttentionPooling(cat_channels, **pooling_params)
self.bn_stats = nn.BatchNorm1d(cat_channels * 2* pooling_params["num_head"])
self.fc1 = ReluBatchNormTdnnLayer(cat_channels * 2* pooling_params["num_head"], embd_dim, **fc1_params) if fc1 else None
else:
self.stats = StatisticsPooling(cat_channels, stddev=stddev)
self.bn_stats = nn.BatchNorm1d(cat_channels * 2)
self.fc1 = ReluBatchNormTdnnLayer(cat_channels * 2, embd_dim, **fc1_params) if fc1 else None
self.tail_dropout = torch.nn.Dropout2d(p=tail_dropout) if tail_dropout > 0 else None
if fc1:
fc2_in_dim = embd_dim
else:
fc2_in_dim = cat_channels * 2
self.fc2 = ReluBatchNormTdnnLayer(fc2_in_dim, embd_dim, **fc2_params)
self.tail_dropout = torch.nn.Dropout2d(p=tail_dropout) if tail_dropout > 0 else None
# Loss
# Do not need when extracting embedding.
if training :
if margin_loss:
self.loss = MarginSoftmaxLoss(embd_dim, num_targets, **margin_loss_params)
else:
self.loss = SoftmaxLoss(embd_dim, num_targets)
# self.loss = AngleLoss(embd_dim,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 = ["layer2","layer3","layer4","conv","stats","fc1","fc2"]
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, x):
out1 = self.layer1(x)
out2 = self.layer2(out1) + out1
out3 = self.layer3(out1 + out2) + out1 + out2
out4 = self.layer4(out1 + out2 + out3) + out1 + out2 + out3
out = torch.cat([out2, out3, out4], dim=1)
out = self.bn_conv(F.relu(self.conv(out)))
x = self.bn_stats(self.stats(out))
if len(x.shape) !=3:
x = x.unsqueeze(dim=2)
x = self.auto(self.mixup, x)
x = self.auto(self.fc1, x)
x = self.fc2(x)
x = self.auto(self.tail_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):
out1 = self.layer1(inputs)
out2 = self.layer2(out1) + out1
out3 = self.layer3(out1 + out2) + out1 + out2
out4 = self.layer4(out1 + out2 + out3) + out1 + out2 + out3
out = torch.cat([out2, out3, out4], dim=1)
out = self.bn_conv(F.relu(self.conv(out)))
x = self.bn_stats(self.stats(out))
if len(x.shape) !=3:
x = x.unsqueeze(dim=2)
if self.extracted_embedding == "far":
assert self.fc1 is not None
xvector = self.fc1.affine(x)
elif self.extracted_embedding == "near_affine":
x = self.auto(self.fc1, x)
xvector = self.fc2.affine(x)
elif self.extracted_embedding == "near":
x = self.auto(self.fc1, x)
xvector = self.fc2(x)
else:
raise TypeError("Expected far or near position, but got {}".format(self.extracted_embedding))
return xvector
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"]))
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 = 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]]
if __name__ == '__main__':
# Input size: batch_size * seq_len * feat_dim
x = torch.zeros(2, 26, 200)
model = ECAPA_TDNN(inputs_dim=26,num_targets=1211, channels=512, embd_dim=192)
out = model(x)
print(model)
print(out.shape) # should be [2, 192]