-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccStat.py
276 lines (177 loc) · 7.85 KB
/
accStat.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
from __future__ import division
import numpy as numpy
import sys
import math
import struct
import code
from multiprocessing import Process
from multiprocessing.queues import Queue
import pickle
import os, sys
import shutil
# tested on python 2.7
def Collect_Stats(data, m, v, w):
#estimate zero, first, second order statistics
try:
n_mixtures=numpy.size(w) #512x1
except:
n_mixtures=len(w)
try:
dim=numpy.size(m, axis=0) #57x512
except:
dim=len(m)
if dim != numpy.size(data, axis=0):
print "Feature dimension mismatch with model!!!"
exit()
# removing "nan or inf" frames from data
idx=numpy.argwhere(numpy.isnan(data))
data = numpy.delete(data, numpy.unique(idx[:,0]), axis=0) #axis=0 i.e. indicate rows, 1 - column
idx=numpy.argwhere(numpy.isinf(data)) #inf
data = numpy.delete(data, numpy.unique(idx[:,0]), axis=0)
n_frames=numpy.size(data, axis=1) # 57 X frames
if n_frames == 0:
print "avoiding empty file after remove Nan/Inf"
n, f, s, llk = [], [], [], []
else:
#compute the GMM posteriors for the given data
gammas, llk =GaussPosterior(data, m, v, w, n_mixtures, dim, n_frames)
#
n=sum(gammas.T) # over Mixtures, zero order stat
f=numpy.matrix(data) * numpy.matrix(gammas.T) # first order stat : [feat * GAMMAS']
s=numpy.matrix(numpy.multiply(data, data)) * numpy.matrix(gammas.T) #%2nd order stat (feat .* feat) * GAMMAS';
return n, f, s, llk, n_frames
def GaussPosterior(data, m, v, w, n_mixtures, dim, n_frames):
# Calculate posterior for a given data
# denominator part of gaussian pdf
B=(2*math.pi)
B=math.pow(B,dim/2)
A=B*(numpy.sqrt(numpy.prod(v.T, axis=1)))
a=w/numpy.matrix(A.T)
#---
B=(n_mixtures, n_frames)
gammas = numpy.zeros(B, dtype=numpy.float64)
for ii in range (0, n_mixtures):
gammas[ii,:] = GaussFun(data, a[:,ii], m[:,ii], v[:,ii], n_frames, dim, n_mixtures)
gamasum = sum(gammas)
llk=numpy.log(gamasum)
#normalize
gammas=gammas/gamasum
return gammas, llk
def GaussFun(data, aa, b, c, n_frames, dim, n_mixtures):
#fit data to pdf
auxC = -0.5/c
aux = numpy.subtract(data.T, b.T)
aux = numpy.power(aux, 2)
aux=aux.T
Y = numpy.matrix(auxC.T) * aux
Y = numpy.exp(Y) * numpy.asscalar(aa)
return Y
def MAPaDapt(ScpFileList, m, v, w, Tau, MapItr, parameter): # only mean adaptation implemented
#Douglas A. Reynolds, Thomas F. Quatieri, and Robert B. Dunn, "Speaker Verification Using Adapted Gaussian Mixture Models",Digital Signal Processing 10, 19-41 (2000)
for iter in range(0, MapItr):
#initialize
ixx=(numpy.size(w))
N_tot=numpy.zeros(ixx)
ixx=(numpy.size(m, axis=0), numpy.size(w))
F_tot, S_tot = numpy.zeros(ixx), numpy.zeros(ixx)
frm_tot, llk_tot = 0, 0.0 #for average llk
for file_ in ScpFileList: # number of files
data=htkread(file_)
data=data.T # dim x frames
N, F, S, llk, n_frames =Collect_Stats(data, m, v, w) # for statistics
print "MAP %d ...%s, frame %d llh %f" %(iter, file_, n_frames, sum(llk)/len(llk))
if (numpy.size(N, axis=0) == numpy.size(w) ) and (numpy.size(F, axis=0) == numpy.size(m, axis=0)): # avoid empty file
N_tot, F_tot, S_tot = N_tot + N, F_tot + F, S_tot + S
llk_tot, frm_tot = llk_tot + (sum(llk)/len(llk)), frm_tot + n_frames
else:
continue
#all stat estimated above
if parameter == 'Mean':
alpha, mean_ml = N_tot/(N_tot + Tau), F_tot/N_tot # ml estimate 'mean'
mu1, mu2=numpy.multiply(m, (1-alpha)), numpy.multiply(mean_ml, alpha)
m_new=mu1 + mu2
else:
print "code only support - mean adaptation!"
print "config parameter has to be 'm' -program terminated!"
exit()
# replace the old UBM parameters with re-estimated one
m=m_new
return m, v, w
def Loglikelihood(data, m, v, w):
N, F, S, llk, n_frames =Collect_Stats(data, m, v, w)
return llk
def htkread(Filename):
fid=open(Filename,'rb')
header = fid.read(12)
(htk_size, htk_period, vec_size, htk_kind) = struct.unpack('>iihh', header) #big endean data format
data = numpy.fromfile(fid, dtype='f')
param = data.reshape((htk_size, int(vec_size / 4))).byteswap()
return param
def multi_thread(CORES, Tstndx, Tardest, Scorefile, ubm_mu, ubm_cov, ubm_w):
#for scoring split the job among the different threads
if CORES <=0:
print "No of cores can't be <=0"
exit()
if Tstndx.size == 1: #for single trial only
y=(numpy.array_str(Tstndx)).split(',')
data=htkread(y[1])
with open( Tardest + '/' + y[0]) as f: #read target model
m_, v_, w_ = pickle.load(f)
llk_ubm, llk_tr=Loglikelihood(data.T, ubm_mu, ubm_cov, ubm_w), Loglikelihood(data.T, m_, v_, w_)
llr=(sum(llk_tr) - sum(llk_ubm))/numpy.size(llk_tr, axis=0)
txt= y[0] + ' ' + y[1] + ' ' + str(llr)
fp=open(Scorefile,'w')
fp.write(txt)
return
tmp='tmp'
if not os.path.exists(tmp):
os.makedirs(tmp)
queues = [Queue() for i in range(CORES)]
part=[]
if (Tstndx.size >= CORES) and (CORES > 1):
part=range(0,Tstndx.size, numpy.int(Tstndx.size/CORES))
part[0]=-1
if len(part) < (CORES+1):
part=numpy.append(part, Tstndx.size-1)
elif part[-1] != Tstndx.size-1:
part[-1]=Tstndx.size-1
args = [(part[i]+1,part[i+1], Tstndx, Tardest, i, tmp, ubm_mu, ubm_cov, ubm_w, True, queues[i]) for i in range(CORES)]
else:
args = [(0,Tstndx.size-1,Tstndx, Tardest, i, tmp, ubm_mu, ubm_cov, ubm_w, True, queues[i]) for i in range(CORES)]
jobs = [Process(target=Scoring, args=(a)) for a in args]
for j in jobs: j.start()
for j in jobs: j.join()
nt=0
with open(Scorefile, 'w') as outfile:
for j in range(len(jobs)):
tx=tmp+ '/part' + str(j) + '.score'
print tx
with open(tx) as infile:
x=infile.read()
outfile.write(x)
x=x.count("\n")
nt=nt+x
os.remove(tx)
outfile.close()
if os.path.isdir(tmp):
shutil.rmtree(tmp)
print("No. of trial %d --no. of score %d\n" %(Tstndx.size, nt))
def Scoring(iStart, iEnd, Tstndx, Tardest, j, tmp, ubm_mu, ubm_cov, ubm_w, multi=False, queue=0):
with open(tmp + '/part' + str(j) + '.score', 'w') as f2:
for i in range(iStart,iEnd+1):
x_=(Tstndx[i]).split(',')
with open( Tardest + '/' + x_[0]) as f: #read target model
m_, v_, w_ = pickle.load(f)
try:
data=htkread(x_[1])
llk_ubm, llk_tr=Loglikelihood(data.T, ubm_mu, ubm_cov, ubm_w), Loglikelihood(data.T, m_, v_, w_)
if numpy.size(llk_ubm, axis=0) != numpy.size(llk_tr, axis=0):
print "number of scores from target model differs from ubm!!!"
print x_[1]
exit()
llr=(sum(llk_tr) - sum(llk_ubm))/numpy.size(llk_tr, axis=0)
txt= x_[0] + ' ' + x_[1] + ' ' + str(llr)
f2.write("%s\n" %(txt))
except:
print("File avoided due to error ..%s\n" %(x_[1]))
f2.close()