forked from swshon/multi-speakerID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mce2018_baseline_test.py
executable file
·258 lines (205 loc) · 9.95 KB
/
mce2018_baseline_test.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
import numpy as np
from sklearn.metrics import roc_curve
## making dictionary to find blacklist pair between train and test dataset
# bl_match = np.loadtxt('data/bl_matching_dev.csv',dtype='string')
bl_match = np.loadtxt('data/bl_matching.csv',dtype='string')
dev2train={}
dev2id={}
train2dev={}
train2id={}
test2train={}
train2test={}
for iter, line in enumerate(bl_match):
line_s = line.split(',')
dev2train[line_s[1].split('_')[-1]]= line_s[3].split('_')[-1]
dev2id[line_s[1].split('_')[-1]]= line_s[0].split('_')[-1]
train2dev[line_s[3].split('_')[-1]]= line_s[1].split('_')[-1]
train2id[line_s[3].split('_')[-1]]= line_s[0].split('_')[-1]
test2train[line_s[2].split('_')[-1]]= line_s[3].split('_')[-1]
train2test[line_s[3].split('_')[-1]]= line_s[2].split('_')[-1]
def load_ivector(filename):
utt = np.loadtxt(filename,dtype='string',delimiter=',',skiprows=1,usecols=[0])
ivector = np.loadtxt(filename,dtype='float32',delimiter=',',skiprows=1,usecols=range(1,601))
spk_id = []
for iter in range(len(utt)):
spk_id = np.append(spk_id,utt[iter].split('_')[0])
return spk_id, utt, ivector
def length_norm(mat):
# length normalization (l2 norm)
# input: mat = [utterances X vector dimension] ex) (float) 8631 X 600
norm_mat = []
for line in mat:
temp = line/np.math.sqrt(sum(np.power(line,2)))
norm_mat.append(temp)
norm_mat = np.array(norm_mat)
return norm_mat
def make_spkvec(mat, spk_label):
# calculating speaker mean vector
# input: mat = [utterances X vector dimension] ex) (float) 8631 X 600
# spk_label = string vector ex) ['abce','cdgd']
# for iter in range(len(spk_label)):
# spk_label[iter] = spk_label[iter].split('_')[0]
spk_label, spk_index = np.unique(spk_label,return_inverse=True)
spk_mean=[]
mat = np.array(mat)
# calculating speaker mean i-vector
for i, spk in enumerate(spk_label):
spk_mean.append(np.mean(mat[np.nonzero(spk_index==i)],axis=0))
spk_mean = length_norm(spk_mean)
return spk_mean, spk_label
def calculate_EER(trials, scores):
# calculating EER of Top-S detector
# input: trials = boolean(or int) vector, 1: postive(blacklist) 0: negative(background)
# scores = float vector
# Calculating EER
fpr,tpr,threshold = roc_curve(trials,scores,pos_label=1)
fnr = 1-tpr
EER_threshold = threshold[np.argmin(abs(fnr-fpr))]
# print EER_threshold
EER_fpr = fpr[np.argmin(np.absolute((fnr-fpr)))]
EER_fnr = fnr[np.argmin(np.absolute((fnr-fpr)))]
EER = 0.5 * (EER_fpr+EER_fnr)
print "Top S detector EER is %0.2f%%"% (EER*100)
return EER
def get_trials_label_with_confusion(identified_label, groundtruth_label,dict4spk,is_trial ):
# determine if the test utterance would make confusion error
# input: identified_label = string vector, identified result of test utterance among multi-target from the detection system
# groundtruth_label = string vector, ground truth speaker labels of test utterances
# dict4spk = dictionary, convert label to target set, ex) train2dev convert train id to dev id
trials = np.zeros(len(identified_label))
for iter in range(0,len(groundtruth_label)):
enroll = identified_label[iter].split('_')[0]
test = groundtruth_label[iter].split('_')[0]
if is_trial[iter]:
if enroll == dict4spk[test]:
trials[iter]=1 # for Target trial (blacklist speaker)
else:
trials[iter]=-1 # for Target trial (backlist speaker), but fail on blacklist classifier
else :
trials[iter]=0 # for non-target (non-blacklist speaker)
return trials
def calculate_EER_with_confusion(scores,trials):
# calculating EER of Top-1 detector
# input: trials = boolean(or int) vector, 1: postive(blacklist) 0: negative(background) -1: confusion(blacklist)
# scores = float vector
# exclude confusion error (trials==-1)
scores_wo_confusion = scores[np.nonzero(trials!=-1)[0]]
trials_wo_confusion = trials[np.nonzero(trials!=-1)[0]]
# dev_trials contain labels of target. (target=1, non-target=0)
fpr,tpr,threshold = roc_curve(trials_wo_confusion,scores_wo_confusion,pos_label=1, drop_intermediate=False)
fnr = 1-tpr
EER_threshold = threshold[np.argmin(abs(fnr-fpr))]
# EER withouth confusion error
EER = fpr[np.argmin(np.absolute((fnr-fpr)))]
# Add confusion error to false negative rate(Miss rate)
total_negative = len(np.nonzero(np.array(trials_wo_confusion)==0)[0])
total_positive = len(np.nonzero(np.array(trials_wo_confusion)==1)[0])
fp= fpr*np.float(total_negative)
fn= fnr*np.float(total_positive)
fn += len(np.nonzero(trials==-1)[0])
total_positive += len(np.nonzero(trials==-1)[0])
fpr= fp/total_negative
fnr= fn/total_positive
# EER with confusion Error
EER_threshold = threshold[np.argmin(abs(fnr-fpr))]
EER_fpr = fpr[np.argmin(np.absolute((fnr-fpr)))]
EER_fnr = fnr[np.argmin(np.absolute((fnr-fpr)))]
EER = 0.5 * (EER_fpr+EER_fnr)
print "Top 1 detector EER is %0.2f%% (Total confusion error is %d)"% ((EER*100), len(np.nonzero(trials==-1)[0]))
return EER
# Loading i-vector
trn_bl_id, trn_bl_utt, trn_bl_ivector = load_ivector('data/trn_blacklist.csv')
trn_bg_id, trn_bg_utt, trn_bg_ivector = load_ivector('data/trn_background.csv')
dev_bl_id, dev_bl_utt, dev_bl_ivector = load_ivector('data/dev_blacklist.csv')
dev_bg_id, dev_bg_utt, dev_bg_ivector = load_ivector('data/dev_background.csv')
tst_id, test_utt, tst_ivector = load_ivector('data/tst_evaluation.csv')
# Calculating speaker mean vector
spk_mean, spk_mean_label = make_spkvec(trn_bl_ivector,trn_bl_id)
#length normalization
trn_bl_ivector = length_norm(trn_bl_ivector)
trn_bg_ivector = length_norm(trn_bg_ivector)
dev_bl_ivector = length_norm(dev_bl_ivector)
dev_bg_ivector = length_norm(dev_bg_ivector)
tst_ivector = length_norm(tst_ivector)
# load test set information
filename = 'data/tst_evaluation_keys.csv'
tst_info = np.loadtxt(filename,dtype='string',delimiter=',',skiprows=1,usecols=range(0,3))
tst_trials = []
tst_trials_label = []
tst_ground_truth =[]
for iter in range(len(tst_info)):
tst_trials_label.extend([tst_info[iter,0]])
if tst_info[iter,1]=='background':
tst_trials = np.append(tst_trials,0)
else:
tst_trials = np.append(tst_trials,1)
print '\nDev set score using train set :'
# making trials of Dev set
dev_ivector = np.append(dev_bl_ivector, dev_bg_ivector,axis=0)
dev_trials = np.append( np.ones([len(dev_bl_id), 1]), np.zeros([len(dev_bg_id), 1]))
# Cosine distance scoring
scores = spk_mean.dot(dev_ivector.transpose())
# Multi-target normalization
blscores = spk_mean.dot(trn_bl_ivector.transpose())
mnorm_mu = np.mean(blscores,axis=1)
mnorm_std = np.std(blscores,axis=1)
for iter in range(np.shape(scores)[1]):
scores[:,iter]= (scores[:,iter] - mnorm_mu) / mnorm_std
dev_scores = np.max(scores,axis=0)
# Top-S detector EER
dev_EER = calculate_EER(dev_trials, dev_scores)
#divide trial label into target and non-target, plus confusion error(blacklist, fail at blacklist detector)
dev_identified_label = spk_mean_label[np.argmax(scores,axis=0)]
dev_trials_label = np.append( dev_bl_id,dev_bg_id)
# Top-1 detector EER
dev_trials_confusion = get_trials_label_with_confusion(dev_identified_label, dev_trials_label, dev2train, dev_trials )
dev_EER_confusion = calculate_EER_with_confusion(dev_scores,dev_trials_confusion)
print '\nTest set score using train set:'
#Cosine distance scoring on Test set
scores = spk_mean.dot(tst_ivector.transpose())
# Multi-target normalization
blscores = spk_mean.dot(trn_bl_ivector.transpose())
mnorm_mu = np.mean(blscores,axis=1)
mnorm_std = np.std(blscores,axis=1)
for iter in range(np.shape(scores)[1]):
scores[:,iter]= (scores[:,iter] - mnorm_mu) / mnorm_std
tst_scores = np.max(scores,axis=0)
# top-S detector EER
tst_EER = calculate_EER(tst_trials, tst_scores)
#divide trial label into target and non-target, plus confusion error(blacklist, fail at blacklist detector)
tst_identified_label = spk_mean_label[np.argmax(scores,axis=0)]
# Top-1 detector EER
tst_trials_confusion = get_trials_label_with_confusion(tst_identified_label, tst_trials_label, test2train, tst_trials )
tst_EER_confusion = calculate_EER_with_confusion(tst_scores,tst_trials_confusion)
print '\nTest set score using train + dev set:'
# get dev set id consistent with Train set
dev_bl_id_along_trnset = []
for iter in range(len(dev_bl_id)):
dev_bl_id_along_trnset.extend([dev2train[dev_bl_id[iter]]])
# Calculating speaker mean vector
spk_mean, spk_mean_label = make_spkvec(np.append(trn_bl_ivector,dev_bl_ivector,0),np.append(trn_bl_id,dev_bl_id_along_trnset))
#Cosine distance scoring on Test set
scores = spk_mean.dot(tst_ivector.transpose())
# tst_scores = np.max(scores,axis=0)
# Multi-target normalization
blscores = spk_mean.dot(np.append(trn_bl_ivector.transpose(),dev_bl_ivector.transpose(),axis=1))
mnorm_mu = np.mean(blscores,axis=1)
mnorm_std = np.std(blscores,axis=1)
for iter in range(np.shape(scores)[1]):
scores[:,iter]= (scores[:,iter] - mnorm_mu) / mnorm_std
tst_scores = np.max(scores,axis=0)
# top-S detector EER
tst_EER = calculate_EER(tst_trials, tst_scores)
#divide trial label into target and non-target, plus confusion error(blacklist, fail at blacklist detector)
tst_identified_label = spk_mean_label[np.argmax(scores,axis=0)]
# Top-1 detector EER
tst_trials_confusion = get_trials_label_with_confusion(tst_identified_label, tst_trials_label, test2train,tst_trials )
tst_EER_confusion = calculate_EER_with_confusion(tst_scores,tst_trials_confusion)
# Generating submission file on TST set for example
filename = 'teamname_fixed_primary_tst.csv'
# filename = 'teamname_fixed_contrastive1.csv'
with open(filename, "w") as text_file:
for iter,score in enumerate(tst_scores):
id_in_trainset = tst_identified_label[iter].split('_')[0]
input_file = tst_trials_label[iter]
text_file.write('%s,%s,%s\n' % (input_file,score,train2id[id_in_trainset]))