-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaldi_nnet_tools.py
389 lines (346 loc) · 12.4 KB
/
kaldi_nnet_tools.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
import os
import ntpath
import subprocess
import re
import numpy as np
def set_trace():
from IPython.core.debugger import Pdb
import sys
Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)
def parse_kaldi_features(filepath, verbose=1):
feats = list(open(filepath, 'r'))
if len(feats) == 0:
return feats
feats[-1] = feats[-1][:-2]
data = np.array([np.array(feats[i].split(), dtype=float)
for i in xrange(1, len(feats))])
return data
def save_kaldi_features(filepath, verbose=True):
filename = os.path.splitext(ntpath.basename(filepath))[0]
folder_path = os.path.dirname(filepath)
data = parse_kaldi_features(filepath)
save_path = os.path.join(folder_path, filename+'.npy')
if len(data) == 0:
print "No data in {}".format(filepath)
else:
if verbose:
print "Saving {} to {}".format(filepath, save_path)
np.save(save_path, data)
def parseNNET(am_copy_path, am_info_path):
"""Converts a kaldi neural network into a python structure by using
non-binary files generated using kaldi's nnet-am-copy and nnet-am-info
PARAMS
------
am_copy_path : str
Path to text model file generated with nnet-am-copy
am_info_path
Path to text model file generated with nnet-am-info
RETURNS
-------
net : list
ANN model
"""
print 'Parsing nnet-am-copy'
with open(am_copy_path, 'r') as f:
ann = f.read().replace('\n', ' ')
regex = ur"</[a-zA-Z]+>"
components = ['<'+n[2:] for n in re.findall(regex, ann)]
ann = ann.split()
items = []
idx_node = -1
i = 0
last_tag = False
while i < len(ann):
if ann[i] == ('</Nnet>'): # is last tag?
last_tag = True
elif ann[i].startswith('</'): # is end of component
print 'Completed component {}'.format(ann[i])
elif ann[i].startswith('<'): # is component or attribute?
if ann[i] in components: # is component?
print '\nStarted component {}'.format(ann[i])
items.append({'<Name>': ann[i]})
idx_node += 1
else:
print 'Started attribute or feature {}'.format(ann[i])
cur_feat = ann[i]
items[idx_node][cur_feat] = []
elif ann[i] not in ('[', ']'):
items[idx_node][cur_feat].append(float(ann[i]))
elif ann[i] == '[' and last_tag:
print 'Started non-tagged attribute <Priors>'
items.append({'<Name>': '<Priors>'})
idx_node += 1
cur_feat = '<Values>'
items[idx_node][cur_feat] = []
i += 1
# convert lists with one item to single numbers and arrays to numpy arrays
for i in items:
for k in i.keys():
if k in('<Context>', '<Sizes>'):
i[k] = np.array(i[k], dtype=int)
elif not isinstance(i[k], str):
if len(i[k]) == 1:
if i[k][0].is_integer():
i[k] = int(i[k][0])
else:
i[k] = float(i[k][0])
else:
i[k] = np.array(i[k])
print '\nParsing nnet-am-info'
# assumes that the order is the same in nnet-am-copy and nnet-am-info
# and that the first index is the NNET and the second one instantiates
# the components tag
idx_component = 1
for line in tuple(open(am_info_path, 'r')):
line = line.replace(' = ', '=')
if line.startswith('component'):
print line
for i in line.split():
if 'Component' in i:
idx_component += 1
cur_feat = '<{}>'.format(i.replace(',', ''))
print 'Parsing Input and Output dimensions of {}'.format(
cur_feat)
elif i.startswith('input-dim'):
print 'Updating <InputDim> on {}'.format(cur_feat)
items[idx_component]['<InputDim>'] = int(
i.split('=')[1].replace(',', ''))
elif i.startswith('output-dim'):
print 'Updating <OutputDim> on {}'.format(cur_feat)
items[idx_component]['<OutputDim>'] = int(
i.split('=')[1].replace(',', ''))
print "\nSetting shape of Linear and Bias params"
for i in items:
if ('Component' in i['<Name>'] and '<InputDim>' in i and '<OutputDim>' in i
and i['<Name>'] not in (
'<NormalizeComponent>', '<PnormComponent>',
'<SpliceComponent>', '<SoftmaxComponent>',
'<SumGroupComponent>')):
print 'Param {}'.format(i['<Name>'])
i['<LinearParams>'] = i['<LinearParams>'].reshape(
i['<OutputDim>'], i['<InputDim>'])
i['<BiasParams>'] = i['<BiasParams>'].reshape(
1, i['<BiasParams>'].shape[0])
return items
def p_norm(data, size, p):
"""(\sum(\abs(Xi) ^ p) ^ 1/p)
PARAMS
------
size: int
The group size for dimensionality reduction
p: int
Norm
RETURNS
-------
P-Normed data
"""
# TODO add faster implementation
output = np.linalg.norm(
np.reshape(data, (size, data.shape[1]/size)),
p,
axis=1)
return output.reshape(1, output.shape[0])
def normalization_nonlinearity(data):
"""Fixed non-trainable non-linearity to renormalize the data to
have unit standard deviation. Used in kaldi's nnet2 model.
PARAMS
------
data : np.array
Data to apply normalization nonlinearity
RETURNS
-------
Normalized data
"""
sigma = np.sqrt(np.average(data ** 2))
if sigma > 1:
data = data * (1.0/sigma)
return data
def softmax(data):
"""Computes the softmax of input data
PARAMS
------
data : array
Data to apply normalization nonlinearity
RETURNS
-------
Softmax of input data
"""
return np.exp(data) / np.exp(data).sum()
def sum_group(data, groups):
"""Groups data according to groups. This can be used in the output layer of
neural networks to combine results. It is used in kaldi's nnet2.
PARAMS
------
data : array
Data to apply normalization nonlinearity
groups : list
Sequence of group sizes
RETURNS
-------
Grouped data
"""
idx_pairs = groups.cumsum().astype(int)
idx_pairs = np.dstack((idx_pairs[:-1], idx_pairs[1:])).flatten()
idx_pairs = np.insert(idx_pairs, 0, [0, idx_pairs[0]])
if groups[-1] == 1:
idx_pairs = idx_pairs[:-1]
return np.add.reduceat(data.T, idx_pairs)[::2].flatten()
def compute_linear(data, layer):
"""Computes the dot product of data and weights and sums bias given a layer
of a network created with parsetNNET.
PARAMS
------
data : array
Data to apply normalization nonlinearity
layer : dictionary
Layer created with parseNNET
RETURNS
-------
Linear combination
"""
return np.dot(data, layer['<LinearParams>'].T) + layer['<BiasParams>']
def apply_prior(data, layer):
"""Weights the data by the priors.
PARAMS
------
data : array
Data to apply normalization nonlinearity
layer : dictionary
Layer created with parseNNET
RETURNS
-------
Data weighted by the priors
"""
assert layer['<Name>'] == '<Priors>'
return data / layer['<Values>']
def forward(data, layers, per_layer=False, verbose=True):
"""Predicts the output of the network given data
PARAMS
------
data : array
Data to apply normalization nonlinearity
layer : list of dics
List of network layers created with parseNNET
verbose: boolean
Describe which computation is being performed
RETURNS
-------
prediction : list
Prediction for each frame in data
"""
output = data
outputs = []
for i in xrange(len(layers)):
if ('Component' in layers[i]['<Name>'] or
'Priors' in layers[i]['<Name>']):
if layers[i]['<Name>'] == '<PnormComponent>':
output = p_norm(
output, layers[i]['<OutputDim>'], layers[i]['<P>'])
elif layers[i]['<Name>'] == '<NormalizeComponent>':
output = normalization_nonlinearity(output)
elif layers[i]['<Name>'] == '<SoftmaxComponent>':
output = softmax(output)
elif layers[i]['<Name>'] == '<SumGroupComponent>':
output = sum_group(output, layers[i]['<Sizes>'])
elif layers[i]['<Name>'] == '<Priors>':
if i == len(layers) - 1:
output = apply_prior(output, layers[i])
else:
raise Exception('Should only apply priors to last layer!')
else:
output = compute_linear(output, layers[i])
if verbose:
print layers[i]['<Name>'], output.shape
if per_layer:
outputs.append(output)
if per_layer:
return outputs
return output
def extract_kaldi_features(wav_dump_features_path, options, spk2utt_rspecifier,
wav_rspecifier, feature_wspecifier):
"""Python wrapper for Kaldi's online online2-wav-dump-features
online2-wav-dump-features [options] <spk2utt-rspecifier> <wav-rspecifier>
<feature-wspecifier>
PARAMS
------
wav_dump_features_path : str
Check Kaldi's website for description
options : str
Check Kaldi's website for description
spk2utt_rspecifier : str
Check Kaldi's website for description
wav_rspecifier : str
Check Kaldi's website for description
feature_wspecifier : str
Check Kaldi's website for description
RETURNS
-------
data : np.array
Features saved as described in feature_wspecifier
"""
return None # not implemented yet
cmd = '{} {} "{}|" "{}|" "{}"'.format(
wav_dump_features_path, options, spk2utt_rspecifier, wav_rspecifier,
feature_wspecifier)
subprocess.Popen(cmd)
return read_kaldi_features(feature_wspecifier.split(":")[1])
def read_kaldi_features(filepath):
"""Read features saved using Kaldi's online online2-wav-dump-features
PARAMS
------
filepath : str
filepath of file to be loaded
RETURNS
-------
data : np.array
Features saved as described in feature_wspecifier
"""
feats = []
# slower but safer
for line in tuple(open(filepath, 'r')):
cur_frame = []
for i in ' '.join(line.split()).split():
try:
cur_frame.append(float(i))
except:
print "{} is not numeric".format(i)
if len(cur_frame) > 0:
feats.append(cur_frame)
else:
print "Ignoring {}".format(line)
return np.array(feats, ndmin=2)
def splice(data, left_context, right_context, const_component_dim):
prefix = np.tile(data[0], (left_context, 1))
suffix = np.tile(data[-1], (right_context, 1))
data = np.concatenate((prefix, data, suffix))
i = left_context
while i < len(data) - right_context:
feat = data[i-left_context:i+right_context+1]
feat = np.concatenate(
(feat[:, :data.shape[1]-const_component_dim].ravel(),
feat[0, -const_component_dim:]))
i += 1
yield feat
"""
# though good memory complexity, has very bad time complexity
# might need fixes
while i < len(data):
l_idx = i - left_context
r_idx = i + right_context + 1
feat = data[max(0, l_idx): min(len(data), r_idx)]
if l_idx < 0:
prefix = np.zeros((abs(l_idx), data.shape[1]))
feat = np.concatenate((prefix, feat))
if r_idx > len(data) - 1:
set_trace()
suffix = np.tile(data[-1], (r_idx - (len(data) - 1), 1))
feat = np.concatenate((feat, suffix))
feat = np.concatenate(
(feat[:, :data.shape[1]-const_component_dim].ravel(),
feat[:, -const_component_dim:].mean(axis=0)))
i+= 1
yield feat.reshape(1, len(feat))
"""
def save_kaldi_loglikelihoods(ll, filepath):
np.savetxt(filepath, ll, fmt="%10.12f", header='utterance-1d [', footer=']',
comments='')