-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
229 lines (189 loc) · 7.04 KB
/
utils.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
import numpy as np
import scipy.io.wavfile as wav
from copy import deepcopy
from python_speech_features import mfcc, logfbank
import os
import json
# Ignore DS_Store files found on Mac
def listdir(pth):
return [x for x in os.listdir(pth) if x != '.DS_Store']
def load_phone_mapping(config):
try:
file_name = config['dir']['dataset'] + 'phone_mapping.json'
with open(file_name, 'r') as f:
return json.load(f)
except:
print("Can't find phone mapping")
exit(0)
def make_folder_if_dne(path):
if not os.path.exists(path):
os.mkdir(path)
def softmax(x):
"""
Computes softmax for output of model
:param x: x has shape (batch x time x number of classes)
:return: softmax(x)
"""
return np.exp(x) / np.expand_dims(np.sum(np.exp(x), axis=-1), axis=-1)
def replacement_dict():
return {'aa': ['ao'], 'ah': ['ax', 'ax-h'], 'er': ['axr'], 'hh': ['hv'], 'ih': ['ix'],
'l': ['el'], 'm': ['em'], 'n': ['en', 'nx'], 'ng': ['eng'], 'sh': ['zh'],
'pau': ['pcl', 'tcl', 'kcl', 'bcl', 'dcl', 'gcl', 'h#', 'epi', 'q'],
'uw': ['ux']}
def collapse_phones(seq):
replacement = replacement_dict()
final = []
for phone in seq:
for father, sons in replacement.items():
if phone in sons:
phone = father
break
final.append(phone)
return final
def read_wav(file_path, winlen, winstep, fbank_filt, mfcc_filt):
(rate, sig) = wav.read(file_path)
assert rate == 16000
# sig ranges from -32768 to +32768 AND NOT -1 to +1
features = None
if fbank_filt:
# log filterbank features
logfbank_feat = logfbank(sig, samplerate=rate, winlen=winlen, winstep=winstep, nfilt=fbank_filt)
if features is None:
features = logfbank_feat
else:
features = np.concatenate((features, logfbank_feat), axis=1)
if mfcc_filt:
# mfcc features
mfcc_feat = mfcc(sig, samplerate=rate, winlen=winlen, winstep=winstep, numcep=mfcc_filt // 3,
winfunc=np.hamming)
mfcc_delta = np.concatenate((mfcc_feat[0:1, :], mfcc_feat[1:, :] - mfcc_feat[:-1, :]))
mfcc_delta_delta = np.concatenate((mfcc_delta[0:1, :], mfcc_delta[1:, :] - mfcc_delta[:-1, :]))
if features is None:
features = np.concatenate((mfcc_feat, mfcc_delta, mfcc_delta_delta), axis=1)
else:
features = np.concatenate((features, mfcc_feat, mfcc_delta, mfcc_delta_delta), axis=1)
return features
def edit_distance(s1, s2):
"""
Score for converting s1 into s2. Both s1 and s2 is a vector of phone IDs and not phones
:param s1: string 1
:param s2: string 2
:return: edit distance and insert, delete and substitution probabilities
"""
m, n = len(s1), len(s2)
dp = np.zeros((m + 1, n + 1))
op_dict = {}
for i in range(m + 1):
op_dict[i] = {}
for j in range(n + 1):
op_dict[i][j] = {'matches': [], 'insertions': [], 'deletions': [], 'substitutions': []}
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
dp[i][j] = j
op_dict[i][j]['insertions'] = s2[:j]
elif j == 0:
dp[i][j] = i
op_dict[i][j]['deletions'] = s1[:i]
elif s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
op_dict[i][j] = deepcopy(op_dict[i - 1][j - 1])
op_dict[i][j]['matches'].append(s1[i - 1])
else:
best = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1)
dp[i][j] = best
if best == dp[i - 1][j] + 1:
op_dict[i][j] = deepcopy(op_dict[i - 1][j])
op_dict[i][j]['deletions'].append(s1[i - 1])
elif best == dp[i][j - 1] + 1:
op_dict[i][j] = deepcopy(op_dict[i][j - 1])
op_dict[i][j]['insertions'].append(s2[j - 1])
else:
op_dict[i][j] = deepcopy(op_dict[i - 1][j - 1])
op_dict[i][j]['substitutions'].append((s1[i - 1], s2[j - 1]))
return dp[m][n], op_dict[m][n]
def read_PHN_file(phone_file_path):
"""
Read .PHN file and return a compressed sequence of phones
:param phone_file_path: path of .PHN file
:param replacement: phones which are to be collapsed
:return: a list of phones
"""
labels = []
with open(phone_file_path, 'r') as f:
a = f.readlines()
for phone in a:
s_e_i = phone[:-1].split(' ') # start, end, phone e.g. 0 5432 'aa'
_, _, ph = int(s_e_i[0]), int(s_e_i[1]), s_e_i[2]
# Collapse
for father, son in utils.replacement_dict().items():
if ph in son:
ph = father
break
# Append to list
labels.append(ph)
return labels
def compress_seq(data):
"""
Compresses a sequence (a,a,b,b,b,b,c,d,d....) into [(a,0,1),(b,2,5),...] i.e. [(phone, start_id, end_index]
:param data: list of elements
:return: data in the above format
"""
final = []
current_ph, current_start_idx = data[0], 0
for i in range(1, len(data)):
now_ph = data[i]
if now_ph == current_ph:
# same so continue
continue
else:
# different element so append current and move on to the next
final.append((current_ph, current_start_idx, i - 1))
current_start_idx = i
current_ph = now_ph
# final element yet to be appended
final.append((current_ph, current_start_idx, len(data) - 1))
return final
def collapse_frames(data, blank_id):
"""
Collapse consecutive frames and then remove blank tokens
:param data: list of elements e.g. [1,1,2,3,2,0,2,0]
:param blank_id: blank token id
:return: [1,2,3,2,2] in the above case
"""
final = []
current_ph = data[0]
for i in range(1, len(data)):
now_ph = data[i]
if now_ph == current_ph:
continue
else:
final.append(current_ph)
current_ph = now_ph
# Append final element
final.append(current_ph)
# weed out the blank tokens
final = [x for x in final if x != blank_id]
return final
def frame_to_sample(frame_no, rate=16000, hop=10, window=25):
if frame_no == 0:
return 0
multiplier = rate // 1000
return multiplier * window + (frame_no - 1) * hop * multiplier
def sample_to_frame(num, is_start, rate=16000, window=25, hop=10):
multi = rate // 1000
if num < window * multi:
return 0
else:
base_frame = (num - multi * window) // (multi * hop) + 1
base_sample = frame_to_sample(base_frame)
if is_start:
if num - base_sample <= multi * hop // 2:
return base_frame
else:
return base_frame + 1
else:
if num - base_sample <= multi * hop // 2:
return base_frame - 1
else:
return base_frame