-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathviterbiDecodingPhonemeSeg.pyx
317 lines (260 loc) · 10.2 KB
/
viterbiDecodingPhonemeSeg.pyx
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
import numpy as np
from scipy.stats import norm
from general.parameters import hopsize_t
cimport cython
# from numpy.math cimport INFINITY
value_eps = np.finfo(float).eps
def FdurationProba2( syllable_duration, param_s ):
M1 = syllable_duration/hopsize_t
# % delta
if param_s['delta_mode'] == 'constant':
delta = param_s['delta']
elif param_s['delta_mode'] == 'proportion':
delta = syllable_duration * param_s['delta']
else:
raise('Error: delta_default should be either constant or proportion.')
S1 = delta/hopsize_t
# % duration max is three times of standard deviation
duration_max = syllable_duration + 3.0*delta
tmin = 0
tmax = int(duration_max/hopsize_t)
# Ps = pdf('Normal',(tmin : tmax), M1, S1)
x = range(tmin, tmax)
Ps = norm.pdf(x, M1, S1)
return Ps, tmin, tmax
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
def viterbiSegmental2(P, sd, param_s):
"""
:param P: NxT emission probability state sequence (P(j,t) = emission probability of symbol j at time t)
:param sd: 1xT score duration array
:param param_s:
:return:
"""
# preventsingularities
P[P == 0] = value_eps
i_bound = np.where(P > value_eps)[0]
N = len(i_bound)
T = len(sd)
# log - likelihood
delta = np.zeros((N, T), dtype=np.double)
psi = np.zeros((N, T), dtype=np.double)
logP = np.log(P, dtype=np.double)
# duration probability
Ps, _, _ = FdurationProba2(sd[0], param_s)
Ps[Ps == 0] = value_eps
C = len(Ps)
logPs = np.log(Ps, dtype=np.double)
cdef double [:, ::1] cdelta = delta
cdef double [:, ::1] cpsi = psi
cdef double [::1] clogP = logP
cdef double [::1] clogPs = logPs
cdef int [::1] ci_bound = np.array(i_bound, dtype=np.intc)
# % % % % % % % % % % % % % % % % % %
# % Initialisation %
# % % % % % % % % % % % % % % % % % %
# % not a possible transition from > 0 time to 1
cdelta[0, 0] = -np.inf
cpsi[:,0] = 0
for jj in range(1,N):
d = ci_bound[jj] - ci_bound[0]
# print(jj, i_bound[jj], d, C)
if d >= C:
cdelta[jj, 0] = -np.inf
else:
cdelta[jj, 0] = clogPs[d] + clogP[i_bound[jj]]
clogPs = None
# % % % % % % % % % % % % % % % % % %
# % Recursion %
# % % % % % % % % % % % % % % % % % %
delta_current = np.zeros((N,), dtype=np.double)
cdef double [::1] cdelta_current = delta_current
for t in range(1,T - 1):
# print(t)
# % duration probability
Ps, _, _ = FdurationProba2(sd[t], param_s)
Ps[Ps == 0] = value_eps
C = len(Ps)
logPs = np.log(Ps, dtype=np.double)
for jj in range(N):
for ii in range(N):
# print(i_bound, jj, ii)
d = ci_bound[jj] - ci_bound[ii]
# print(d, C)
if d >= C or d <= 0:
cdelta_current[ii] = -np.inf
else:
cdelta_current[ii] = cdelta[ii, t - 1] + logPs[d]
I_delta = np.argmax(cdelta_current)
M_delta = cdelta_current[I_delta]
cdelta[jj, t] = M_delta + clogP[i_bound[jj]] # add emission because it's a constance
cpsi[jj, t] = I_delta
# % duration probability
Ps, tmin, tmax = FdurationProba2(sd[T-1], param_s)
Ps[Ps == 0] = value_eps
C = len(Ps)
logPs = np.log(Ps, dtype=np.double)
clogPs = logPs
# delta_current = np.zeros((N,))
for ii in range(N):
d = ci_bound[N-1] - ci_bound[ii]
if d >= C or d <= 0:
cdelta_current[ii] = -np.inf
else:
cdelta_current[ii] = cdelta[ii, T-2] + clogPs[d]
I_delta = np.argmax(cdelta_current)
M_delta = cdelta_current[I_delta] # the posterior proba
cdelta[N-1, T-1] = M_delta + clogP[i_bound[N-1]]
cpsi[N-1, T-1] = I_delta
# % % % % % % % % % % % % % % % % % %
# % Backtrack %
# % % % % % % % % % % % % % % % % % %
i_best_sequence = np.zeros((T+1,),dtype=int)
# print(i_best_sequence)
i_best_sequence[T] = N-1
for t in range(T)[::-1]:
# print(t+1, i_best_sequence[t+1])
i_best_sequence[t] = int(cpsi[int(i_best_sequence[t + 1]), t])
# print(i_best_sequence)
i_boundary = [i_bound[ii] for ii in i_best_sequence]
cdelta = None
cdelta_current = None
clogPs = None
clogP = None
cpsi = None
ci_bound = None
return i_boundary
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
def viterbiSegmentalPenalized(P, sd, param_s):
"""
:param P: NxT emission probability state sequence (P(j,t) = emission probability of symbol j at time t)
:param sd: 1xT score duration array
:param param_s:
:return:
"""
# preventsingularities
P[P == 0] = value_eps
i_bound = np.where(P > value_eps)[0]
N = len(i_bound)
T = len(sd)
# log - likelihood
delta = np.zeros((N, T), dtype=np.double)
psi = np.zeros((N, T), dtype=np.double)
logP = np.log(P, dtype=np.double)
# duration probability
Ps, _, _ = FdurationProba2(sd[0], param_s)
Ps[Ps == 0] = value_eps
C = len(Ps)
logPs = np.log(Ps, dtype=np.double)
cdef double [:, ::1] cdelta = delta
cdef double [:, ::1] cpsi = psi
cdef double [::1] clogP = logP
cdef double [::1] clogPs = logPs
cdef int [::1] ci_bound = np.array(i_bound, dtype=np.intc)
# # penalizing variables
# cdef double alpha = param_s['penalized_alpha']
# cdef double beta = param_s['penalized_beta']
#
# delta_penalized = np.zeros((N, T), dtype=np.double)
# logP_inverse = np.log(1.0 - P, dtype=np.double)*alpha # emission
# logPs_inverse = np.log(1.0 - Ps, dtype=np.double)*beta # transition
# cdef double [:, ::1] cdelta_penalized = delta_penalized
# cdef double [::1] clogPs_inverse = logPs_inverse
# cdef double [::1] clogP_inverse = logP_inverse
# % % % % % % % % % % % % % % % % % %
# % Initialisation %
# % % % % % % % % % % % % % % % % % %
# % not a possible transition from > 0 time to 1
cdelta[0, 0] = -np.inf
cpsi[:,0] = 0
for jj in range(1,N):
d = ci_bound[jj] - ci_bound[0]
# print(jj, i_bound[jj], d, C)
if d >= C:
cdelta[jj, 0] = -np.inf
# cdelta_penalized[jj, 0] = -np.inf
else:
cdelta[jj, 0] = clogPs[d] + clogP[i_bound[jj]]
# cdelta_penalized[jj, 0] = cdelta[jj, 0] - (clogPs_inverse[d] + clogP_inverse[i_bound[jj]])
clogPs = None
clogPs_inverse = None
# % % % % % % % % % % % % % % % % % %
# % Recursion %
# % % % % % % % % % % % % % % % % % %
delta_current = np.zeros((N,), dtype=np.double)
cdef double [::1] cdelta_current = delta_current
# delta_current_penalized = np.zeros((N,), dtype=np.double)
# cdef double [::1] cdelta_current_penalized = delta_current_penalized
for t in range(1,T - 1):
# print(t)
# % duration probability
Ps, _, _ = FdurationProba2(sd[t], param_s)
Ps[Ps == 0] = value_eps
C = len(Ps)
logPs = np.log(Ps, dtype=np.double)
# logPs_inverse = np.log(1.0 - Ps, dtype=np.double)*beta
for jj in range(N):
for ii in range(N):
# print(i_bound, jj, ii)
d = ci_bound[jj] - ci_bound[ii]
# print(d, C)
if d >= C or d <= 0:
cdelta_current[ii] = -np.inf
# cdelta_current_penalized[ii] = -np.inf
else:
cdelta_current[ii] = cdelta[ii, t - 1] + logPs[d]
# cdelta_current_penalized[ii] = cdelta_penalized[ii, t - 1] + logPs[d] - logPs_inverse[d]
I_delta = np.argmax(cdelta_current)
M_delta = cdelta_current[I_delta]
cdelta[jj, t] = M_delta + clogP[i_bound[jj]]
cpsi[jj, t] = I_delta
# cdelta_penalized[jj, t] = cdelta_current_penalized[I_delta] + clogP[i_bound[jj]] - clogP_inverse[i_bound[jj]]
# % duration probability
Ps, tmin, tmax = FdurationProba2(sd[T-1], param_s)
Ps[Ps == 0] = value_eps
C = len(Ps)
logPs = np.log(Ps, dtype=np.double)
clogPs = logPs
# logPs_inverse = np.log(1.0 - Ps, dtype=np.double)*beta
# clogPs_inverse = logPs_inverse
for ii in range(N):
d = ci_bound[N-1] - ci_bound[ii]
if d >= C or d <= 0:
cdelta_current[ii] = -np.inf
# cdelta_current_penalized[ii] = -np.inf
else:
cdelta_current[ii] = cdelta[ii, T-2] + clogPs[d]
# cdelta_current_penalized[ii] = cdelta_penalized[ii, T-2] + clogPs[d] - clogPs_inverse[d]
I_delta = np.argmax(cdelta_current)
M_delta = cdelta_current[I_delta]
cdelta[N-1, T-1] = M_delta + clogP[i_bound[N-1]]
cpsi[N-1, T-1] = I_delta
# cdelta_penalized[N-1, T-1] = cdelta_current_penalized[I_delta]
# print(np.asarray(cdelta))
# print(np.asarray(cdelta_penalized))
# % % % % % % % % % % % % % % % % % %
# % Backtrack %
# % % % % % % % % % % % % % % % % % %
i_best_sequence = np.zeros((T+1,),dtype=int)
# print(i_best_sequence)
i_best_sequence[T] = N-1
for t in range(T)[::-1]:
# print(t+1, i_best_sequence[t+1])
i_best_sequence[t] = int(cpsi[int(i_best_sequence[t + 1]), t])
# print(i_best_sequence)
i_boundary = [i_bound[ii] for ii in i_best_sequence]
posterior = M_delta
# penalized posterior
# posterior = cdelta_penalized[N-1, T-1]
cdelta = None
cdelta_current = None
clogPs = None
clogP = None
cpsi = None
ci_bound = None
# cdelta_penalized = None
# cdelta_current_penalized = None
return i_boundary, posterior