-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutlis.py
394 lines (299 loc) · 13.5 KB
/
utlis.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
import numpy as np
import torch
import torch.nn as nn
import tensorflow
import tensorflow_hub as hub
from sklearn import preprocessing
import sys
import os
from pathlib import Path
import random
from src.zeroshot_networks.vae import EncoderTemplate, DecoderTemplate
from src.dataset_loaders._data_loader import DATA_LOADER as dataloader
# Data loader
def map_label(label, classes):
mapped_label = torch.LongTensor(label.size())
for i in range(classes.size(0)):
mapped_label[label==classes[i]] = i
return mapped_label
class DATA_LOADER(object):
def __init__(self, dataset, aux_datasource, device='cuda'):
print("The current working directory is")
print(os.getcwd())
folder = str(Path(os.getcwd()))
if folder[-5:] == 'model':
diploma_directory = Path(os.getcwd()).parent
else:
diploma_directory = folder
print('Diploma dir:')
print(diploma_directory)
data_path = str(diploma_directory) + '/data'
print('Data Path')
print(data_path)
sys.path.append(data_path)
self.data_path = data_path
self.device = device
self.dataset = dataset
self.auxiliary_data_source = aux_datasource
self.all_data_sources = ['libri_features'] + [self.auxiliary_data_source]
if self.dataset == 'google_speech_commands':
self.datadir = self.data_path + '/GSC/'
elif self.dataset == 'librispeech':
self.datadir = self.data_path + '/LIBRI/'
self.read_matdataset()
self.index_in_epoch = 0
self.epochs_completed = 0
def next_batch(self, batch_size):
idx = torch.randperm(self.ntrain)[0:batch_size]
batch_feature = self.data['train_seen']['jasper_features'][idx]
batch_label = self.data['train_seen']['labels'][idx]
batch_att = self.aux_data[batch_label]
return batch_label, [batch_feature, batch_att]
def gen_next_batch(self, batch_size, dset_part='train'):
if dset_part == 'train':
features = self.data['train_seen']['jasper_features']
labels = self.data['train_seen']['labels']
elif dset_part == 'test':
features = self.data['test_unseen']['jasper_features']
labels = self.data['test_unseen']['labels']
else:
raise ValueError('Your dataset do not supported')
iter_len = len(features) // batch_size + 1
for current_batch in range(iter_len):
current_idx = current_batch * batch_size
end_idx = current_idx + batch_size
batch_features = features[current_idx:end_idx]
batch_label = labels[current_idx:end_idx]
batch_attr = self.aux_data[batch_label]
yield batch_label, [batch_features, batch_attr]
scaler = preprocessing.MinMaxScaler()
train_feature = scaler.fit_transform(feature[trainval_loc])
test_seen_feature = scaler.fit_transform(feature[test_seen_loc])
test_unseen_feature = scaler.fit_transform(feature[test_unseen_loc])
train_feature = torch.from_numpy(train_feature).float().to(self.device)
test_seen_feature = torch.from_numpy(test_seen_feature).float().to(self.device)
test_unseen_feature = torch.from_numpy(test_unseen_feature).float().to(self.device)
train_label = torch.from_numpy(label[trainval_loc]).long().to(self.device)
test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long().to(self.device)
test_seen_label = torch.from_numpy(label[test_seen_loc]).long().to(self.device)
self.seenclasses = torch.from_numpy(np.unique(train_label.cpu().numpy())).to(self.device)
self.novelclasses = torch.from_numpy(np.unique(test_unseen_label.cpu().numpy())).to(self.device)
self.ntrain = train_feature.size()[0]
self.ntrain_class = self.seenclasses.size(0)
self.ntest_class = self.novelclasses.size(0)
self.train_class = self.seenclasses.clone()
self.allclasses = torch.arange(0, self.ntrain_class+self.ntest_class).long()
self.train_mapped_label = map_label(train_label, self.seenclasses)
self.data = {}
self.data['train_seen'] = {}
self.data['train_seen']['jasper_features'] = train_feature
self.data['train_seen']['labels'] = train_label
self.data['train_seen'][self.auxiliary_data_source] = self.aux_data[train_label]
self.data['train_unseen'] = {}
self.data['train_unseen']['jasper_features'] = None
self.data['train_unseen']['labels'] = None
self.data['test_seen'] = {}
self.data['test_seen']['jasper_features'] = test_seen_feature
self.data['test_seen']['labels'] = test_seen_label
self.data['test_unseen'] = {}
self.data['test_unseen']['jasper_features'] = test_unseen_feature
self.data['test_unseen'][self.auxiliary_data_source] = self.aux_data[test_unseen_label]
self.data['test_unseen']['labels'] = test_unseen_label
self.novelclass_aux_data = self.aux_data[self.novelclasses]
self.seenclass_aux_data = self.aux_data[self.seenclasses]
# Data loader
iter_len = len(features) // batch_size + 1
for current_batch in range(iter_len):
current_idx = current_batch * batch_size
end_idx = current_idx + batch_size
batch_features = features[current_idx:end_idx]
batch_label = labels[current_idx:end_idx]
batch_attr = self.aux_data[batch_label]
yield batch_label, [batch_features, batch_attr]
scaler = preprocessing.MinMaxScaler()
train_feature = scaler.fit_transform(feature[trainval_loc])
test_seen_feature = scaler.fit_transform(feature[test_seen_loc])
test_unseen_feature = scaler.fit_transform(feature[test_unseen_loc])
train_feature = torch.from_numpy(train_feature).float().to(self.device)
test_seen_feature = torch.from_numpy(test_seen_feature).float().to(self.device)
test_unseen_feature = torch.from_numpy(test_unseen_feature).float().to(self.device)
train_label = torch.from_numpy(label[trainval_loc]).long().to(self.device)
test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long().to(self.device)
test_seen_label = torch.from_numpy(label[test_seen_loc]).long().to(self.device)
self.seenclasses = torch.from_numpy(np.unique(train_label.cpu().numpy())).to(self.device)
self.novelclasses = torch.from_numpy(np.unique(test_unseen_label.cpu().numpy())).to(self.device)
self.ntrain = train_feature.size()[0]
self.ntrain_class = self.seenclasses.size(0)
self.ntest_class = self.novelclasses.size(0)
self.train_class = self.seenclasses.clone()
self.allclasses = torch.arange(0, self.ntrain_class+self.ntest_class).long()
self.train_mapped_label = map_label(train_label, self.seenclasses)
self.data = {}
self.data['train_seen'] = {}
self.data['train_seen']['jasper_features'] = train_feature
self.data['train_seen']['labels'] = train_label
self.data['train_seen'][self.auxiliary_data_source] = self.aux_data[train_label]
self.data['train_unseen'] = {}
self.data['train_unseen']['jasper_features'] = None
self.data['train_unseen']['labels'] = None
self.data['test_seen'] = {}
self.data['test_seen']['jasper_features'] = test_seen_feature
self.data['test_seen']['labels'] = test_seen_label
self.data['test_unseen'] = {}
self.data['test_unseen']['jasper_features'] = test_unseen_feature
self.data['test_unseen'][self.auxiliary_data_source] = self.aux_data[test_unseen_label]
self.data['test_unseen']['labels'] = test_unseen_label
self.novelclass_aux_data = self.aux_data[self.novelclasses]
self.seenclass_aux_data = self.aux_data[self.seenclasses]
# Feature extraction
class StatsPoolLayer(nn.Module):
def __init__(self, feat_in, pool_mode='xvector'):
super().__init__()
self.feat_in = 0
if pool_mode == 'gram':
gram = True
super_vector = False
elif pool_mode == 'superVector':
gram = True
super_vector = True
else:
gram = False
super_vector = False
if gram:
self.feat_in += feat_in ** 2
else:
self.feat_in += 2 * feat_in
if super_vector and gram:
self.feat_in += 2 * feat_in
self.gram = gram
self.super = super_vector
class MaskedConv1d(nn.Module):
__constants__ = ["use_conv_mask", "real_out_channels", "heads"]
def __init__(
self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
heads=-1,
bias=False,
use_mask=True,
quantize=False,
):
super(MaskedConv1d, self).__init__()
self.real_out_channels = out_channels
if heads != -1:
in_channels = heads
out_channels = heads
groups = heads
self._padding = padding
if type(padding) in (tuple, list):
self.pad_layer = nn.ConstantPad1d(padding, value=0.0)
else:
self.pad_layer = None
self.conv = nn.Conv1d(
in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups,
bias=bias,
)
self.use_mask = use_mask
self.heads = heads
self.same_padding = (self.conv.stride[0] == 1) and (
2 * self.conv.padding[0] == self.conv.dilation[0] * (self.conv.kernel_size[0] - 1)
)
if self.pad_layer is None:
self.same_padding_asymmetric = False
else:
self.same_padding_asymmetric = (self.conv.stride[0] == 1) and (
sum(self._padding) == self.conv.dilation[0] * (self.conv.kernel_size[0] - 1)
)
if self.use_mask:
self.max_len = 0
self.lens = None
def extract_features(self, x, pooling_mask=None):
if pooling_mask is not None:
x[pooling_mask] = 0
x_conv = self.pos_conv(x.transpose(1, 2))
x_conv = x_conv.transpose(1, 2)
x += x_conv
if not self.layer_norm_first:
x = self.layer_norm(x)
x = self.feature_dropout(x)
x = x.transpose(0, 1)
x = self.transformer_encoder(x, src_key_pooling_mask=pooling_mask)
x = x.transpose(0, 1)
return x
# USE
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
model = hub.load(module_url)
print ("module %s loaded" % module_url)
def embed(input):
return model(input)
logging.set_verbosity(logging.ERROR)
message_embeddings = embed(messages)
for i, message_embedding in enumerate(np.array(message_embeddings).tolist()):
message_embedding_snippet = ", ".join(
(str(x) for x in message_embedding[:3]))
# Word error rate
logging.root.setLevel(logging.INFO)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--hypo", help="hypo transcription", required=True)
parser.add_argument(
"-r", "--reference", help="reference transcription", required=True
)
return parser
def compute_wer(ref_uid_to_tra, hyp_uid_to_tra, g2p):
d_cnt = 0
w_cnt = 0
w_cnt_h = 0
for uid in hyp_uid_to_tra:
ref = ref_uid_to_tra[uid].split()
if g2p is not None:
hyp = g2p(hyp_uid_to_tra[uid])
hyp = [p for p in hyp if p != "'" and p != " "]
hyp = [p[:-1] if p[-1].isnumeric() else p for p in hyp]
else:
hyp = hyp_uid_to_tra[uid].split()
d_cnt += editdistance.eval(ref, hyp)
w_cnt += len(ref)
w_cnt_h += len(hyp)
wer = float(d_cnt) / w_cnt
logger.debug(
(
f"wer = {wer * 100:.2f}%; num. of ref words = {w_cnt}; "
f"num. of hyp words = {w_cnt_h}; num. of sentences = {len(ref_uid_to_tra)}"
)
)
return wer
def main():
args = get_parser().parse_args()
errs = 0
count = 0
with open(args.hypo, "r") as hf, open(args.reference, "r") as rf:
for h, r in zip(hf, rf):
h = h.rstrip().split()
r = r.rstrip().split()
errs += editdistance.eval(r, h)
count += len(r)
logger.info(f"UER: {errs / count * 100:.2f}%")
if __name__ == "__main__":
main()
def load_tra(tra_path):
with open(tra_path, "r") as f:
uid_to_tra = {}
for line in f:
uid, tra = line.split(None, 1)
uid_to_tra[uid] = tra
logger.debug(f"loaded {len(uid_to_tra)} utterances from {tra_path}")
return uid_to_tra