-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_func.py
297 lines (244 loc) · 10.3 KB
/
common_func.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
#!/usr/bin/env python
# encoding: utf-8
"""
@Author: yangwenhao
@Contact: [email protected]
@Software: PyCharm
@File: common_func.py
@Time: 2021/10/15 09:45
@Overview:
"""
import os
import errno
import pdb
import sys
import kaldi_io
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.nn.parallel.distributed import DistributedDataParallel
from tqdm import tqdm
import gradients.constants as c
from loss.softmax import AdditiveMarginLinear
from models.ResNets import ResCNN
from models.TDNNs import TDNN
from evals.eval_metrics import evaluate_kaldi_eer, evaluate_kaldi_mindcf
import argparse
def create_optimizer(parameters, optimizer, **kwargs):
# setup optimizer
# parameters = filter(lambda p: p.requires_grad, parameters)
if optimizer == 'sgd':
opt = optim.SGD(parameters,
lr=kwargs['lr'],
momentum=kwargs['momentum'],
dampening=kwargs['dampening'],
weight_decay=kwargs['weight_decay'])
elif optimizer == 'adam':
opt = optim.Adam(parameters,
lr=kwargs['lr'],
weight_decay=kwargs['weight_decay'])
elif optimizer == 'adagrad':
opt = optim.Adagrad(parameters,
lr=kwargs['lr'],
lr_decay=kwargs['lr_decay'],
weight_decay=kwargs['weight_decay'])
elif optimizer == 'RMSprop':
opt = optim.RMSprop(parameters,
lr=kwargs['lr'],
momentum=kwargs['momentum'],
weight_decay=kwargs['weight_decay'])
return opt
__factory = {
'ResCNN': ResCNN,
'TDNN': TDNN,
}
def create_model(name, **kwargs):
if name not in __factory.keys():
raise KeyError("Unknown model: {}".format(name))
model = __factory[name](**kwargs)
if kwargs['loss_type'] in ['arcsoft']:
model.classifier = AdditiveMarginLinear(feat_dim=kwargs['embedding_size'],
num_classes=kwargs['num_classes'])
return model
def verification_extract(extract_loader, model, xvector_dir, epoch, test_input='fix', ark_num=50000, gpu=True,
verbose=False, xvector=False):
"""
:param extract_loader:
:param model:
:param xvector_dir:
:param epoch:
:param test_input:
:param ark_num:
:param gpu:
:param verbose:
:param xvector: extract xvectors in embedding-a layer
:return:
"""
model.eval()
if not os.path.exists(xvector_dir):
os.makedirs(xvector_dir)
# pbar =
pbar = tqdm(extract_loader, ncols=100) if verbose else extract_loader
uid2vectors = {}
with torch.no_grad():
if test_input == 'fix':
data = torch.tensor([])
num_seg_tensor = [0]
uid_lst = []
batch_size = 128 if torch.cuda.is_available() else 80
for batch_idx, (a_data, a_uid) in enumerate(pbar):
vec_shape = a_data.shape
if vec_shape[1] != 1:
a_data = a_data.reshape(vec_shape[0] * vec_shape[1], 1, vec_shape[2], vec_shape[3])
data = torch.cat((data, a_data), dim=0)
num_seg_tensor.append(num_seg_tensor[-1] + len(a_data))
uid_lst.append(a_uid[0])
if data.shape[0] >= batch_size or batch_idx + 1 == len(extract_loader):
if data.shape[0] > (3 * batch_size):
i = 0
out = []
while i < data.shape[0]:
data_part = data[i:(i + batch_size)]
data_part = data_part.cuda() if next(model.parameters()).is_cuda else data_part
model_out = model.xvector(data_part) if xvector else model(data_part)
try:
_, out_part, _, _ = model_out
except:
_, out_part = model_out
out.append(out_part)
i += batch_size
out = torch.cat(out, dim=0)
else:
data = data.cuda() if next(model.parameters()).is_cuda else data
model_out = model.xvector(data) if xvector else model(data)
try:
_, out, _, _ = model_out
except:
_, out = model_out
out = out.data.cpu().float().numpy()
# print(out.shape)
if len(out.shape) == 3:
out = out.squeeze(0)
for i, uid in enumerate(uid_lst):
uid2vectors[uid] = out[num_seg_tensor[i]:num_seg_tensor[i + 1]].mean(axis=0) # , uid[0])
data = torch.tensor([])
num_seg_tensor = [0]
uid_lst = []
elif test_input == 'var':
for batch_idx, (a_data, a_uid) in enumerate(pbar):
vec_shape = a_data.shape
if vec_shape[1] != 1:
a_data = a_data.reshape(vec_shape[0] * vec_shape[1], 1, vec_shape[2], vec_shape[3])
a_data = a_data.cuda() if next(model.parameters()).is_cuda else a_data
if vec_shape[2] > 10 * c.NUM_FRAMES_SPECT:
num_half = int(vec_shape[2] / 2)
half_a = a_data[:, :, :num_half, :]
half_b = a_data[:, :, -num_half:, :]
a_data = torch.cat((half_a, half_b), dim=0)
try:
if xvector:
model_out = model.module.xvector(a_data) if isinstance(model,
DistributedDataParallel) else model.xvector(
a_data)
else:
model_out = model(a_data)
except Exception as e:
pdb.set_trace()
print('\ninput shape is ', a_data.shape)
raise e
try:
_, out, _, _ = model_out
except:
_, out = model_out
if out.shape[0] != 1:
out = out.mean(dim=0, keepdim=True)
out = out.data.cpu().float().numpy()
# print(out.shape)
if len(out.shape) == 3:
out = out.squeeze(0)
uid2vectors[a_uid[0]] = out[0]
uids = list(uid2vectors.keys())
# print('There are %d vectors' % len(uids))
scp_file = xvector_dir + '/xvectors.scp'
scp = open(scp_file, 'w')
# write scp and ark file
# pdb.set_trace()
for set_id in range(int(np.ceil(len(uids) / ark_num))):
ark_file = xvector_dir + '/xvector.{}.ark'.format(set_id)
with open(ark_file, 'wb') as ark:
ranges = np.arange(len(uids))[int(set_id * ark_num):int((set_id + 1) * ark_num)]
for i in ranges:
key = uids[i]
vec = uid2vectors[key]
len_vec = len(vec.tobytes())
kaldi_io.write_vec_flt(ark, vec, key=key)
# print(ark.tell())
scp.write(str(uids[i]) + ' ' + str(ark_file) + ':' + str(ark.tell() - len_vec - 10) + '\n')
scp.close()
# print('Saving %d xvectors to %s' % (len(uids), xvector_dir))
torch.cuda.empty_cache()
def verification_test(test_loader, dist_type, log_interval, xvector_dir, epoch):
# switch to evaluate mode
labels, distances = [], []
dist_fn = nn.CosineSimilarity(dim=1).cuda() if dist_type == 'cos' else nn.PairwiseDistance(2)
# pbar = tqdm(enumerate(test_loader))
with torch.no_grad():
for batch_idx, (data_a, data_p, label) in enumerate(test_loader):
out_a = torch.tensor(data_a).cuda() # .view(-1, 4, embedding_size)
out_p = torch.tensor(data_p).cuda() # .view(-1, 4, embedding_size)
dists = dist_fn.forward(out_a, out_p).cpu().numpy()
distances.append(dists)
labels.append(label.numpy())
del out_a, out_p # , ae, pe
# if batch_idx % log_interval == 0:
# pbar.set_description('Verification Epoch {}: [{}/{} ({:.0f}%)]'.format(
# epoch, batch_idx * len(data_a), len(test_loader.dataset), 100. * batch_idx / len(test_loader)))
labels = np.array([sublabel for label in labels for sublabel in label])
distances = np.array([subdist for dist in distances for subdist in dist])
# this_xvector_dir = "%s/epoch_%s" % (xvector_dir, epoch)
with open('%s/scores' % xvector_dir, 'w') as f:
for d, l in zip(distances, labels):
f.write(str(d) + ' ' + str(l) + '\n')
eer, eer_threshold, accuracy = evaluate_kaldi_eer(distances, labels,
cos=True if dist_type == 'cos' else False,
re_thre=True)
mindcf_01, mindcf_001 = evaluate_kaldi_mindcf(distances, labels)
return eer, eer_threshold, mindcf_01, mindcf_001
def mkdir_if_missing(directory):
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
class NewLogger(object):
"""
Write console output to external text file.
Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py.
"""
def __init__(self, fpath=None, mode='a'):
self.console = sys.stdout
self.file = None
if fpath is not None:
mkdir_if_missing(os.path.dirname(fpath))
self.file = open(fpath, mode)
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()