-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_data_pro.py
441 lines (394 loc) · 14.7 KB
/
meta_data_pro.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import os
import codecs
import numpy as np
import random
import math
import pickle
import argparse
from collections import OrderedDict
from utils import MyThread, segment_file, get_spectrogram_data, get_librosa_feature, vad_project_fun, update_dict
from read_conf import read_spectrogram_conf
parser = argparse.ArgumentParser()
parser.add_argument('--exppath', type=str, help='indicate the path to svae the segment the audio',
default='/home/gkb/asr_model/IEMOCAP/segment_files2/')
parser.add_argument('--savdir', type=str, help='indicate the file name to save the lab_mat',
default='/home/gkb/asr_model/IEMOCAP/meta_multi_task_db2/')
parser.add_argument('--conf', type=str, help='indicate the feature conf to use',
default='/home/gkb/asr_model/IEMOCAP//config/spectrogram.conf')
parser.add_argument('--spect', type=str, default='2', help='1: DFT of 1600/10 hz resolution; '
'2: DFT of 800/20 hz resolution ')
parser.add_argument('--vad', type=str, default='T', help='if True, valence, arouse and domain are used for the model')
paras = parser.parse_args()
exp_path = paras.exppath
sav_dir = paras.savdir
conf = paras.conf
spect_type = 'spectrogram' + paras.spect
vad = paras.vad
if vad == 'T':
vad = True
elif vad == 'F':
vad = False
else:
print('vad should be T or F')
raise ValueError
print(spect_type)
random.seed(1234)
data_path_prefix = '/home/gkb/asr_model/IEMOCAP/'
lab_path_suffix = '/dialog/EmoEvaluation/'
wav_path_suffix = '/sentences/wav/'
lab_m_list = ['neu', 'ang', 'hap', 'sad']
lab_2_index = {lab_m_list[i]: i for i in range(len(lab_m_list[:4]))}
def txt_filter(file, other_rule=['script']):
'''
define the rule of extracting the needed audio
:param file:
:param other_rule:
:return:
'''
if '.txt' in file:
for rule in other_rule:
if rule in file:
return None
return file
else:
return None
def get_vad(vad_string, proj_range=[(1.0, 2.0), (2.0, 3.0), (3.0, 4.0), (4.0, 6.0)]):
'''
project the valence, arousal, dominance value to discrete value
:param vad_string:
:param proj_range:
:return:
'''
valence = (vad_string[0]).replace('[', ''); valence = valence.replace(',', ''); valence = float(valence)
arouse = (vad_string[1]).replace(',', ''); arouse = float(arouse)
domain = (vad_string[2]).replace(']', ''); domain = domain.replace(',', ''); domain = float(domain)
valence = vad_project_fun(valence, proj_range, 3)
arouse = vad_project_fun(arouse, proj_range, 3)
domain = vad_project_fun(domain, proj_range, 3)
return [valence, arouse, domain]
def get_emo_data(session_name, bool_seg=False, bool_vad=False, feature_extra='spectrogram', seg_dur=0, exp_path='',
conf=None, cfg_section=None):
'''
:param session_name:
:param bool_seg: bool, if segment the audio file
:param feature_extra: str, the type of feature, the value of which should be 'spectrogram' or 'mel_filter'
:param seg_dur: int, represents the length of each segment in second, and it works only when the bool_seg=True
:param exp_path: str, the path to save the segment of the audio, and it works only when the bool_seg=True
:return: data_dict: dict, the key is session_name + '_' + gender, the value is [feature mat and emotion kind]
'''
window_shift, windows_size, dft_length, freq_min, freq_max, hz_resultion = read_spectrogram_conf(conf, cfg_section)
dir_path = data_path_prefix + session_name + lab_path_suffix
file_list = os.listdir(dir_path)
file_list = list(filter(txt_filter, file_list))
data_dict = dict()
for file in file_list:
mask = False
with codecs.open(dir_path + file, 'r') as f:
for line in f.readlines():
data = line.strip()
if data == '':
mask = True
else:
if mask:
info_data = data.split()
# e.g info_data = [5.2300 - 8.7100] Ses02M_script01_1_F000 xxx [3.0000, 3.0000, 3.0000]
if bool_vad:
vad = info_data[5:]
vad = get_vad(vad)
emotion = info_data[4]
if emotion in lab_m_list:
file_name = info_data[3]
# e.g file_name = Ses02M_script01_1_F000
dir_name, speaker = file_name.rsplit('_', maxsplit=1)
speaker = speaker[0]
session_speaker = session_name + '_' + speaker
if data_dict.get(session_speaker) is None:
data_dict[session_speaker] = dict()
# if emotion == 'exc':
# emotion = 'hap'
wav_path = data_path_prefix + session_name + wav_path_suffix + dir_name + '/' + file_name\
+ '.wav'
if bool_seg:
new_wav_list = segment_file(wav_path, seg_dur, exp_path, 'wav')
else:
new_wav_list = []
new_wav_list.append(wav_path)
# wav_mat = get_librosa_feature(wav_path)
wav_info = []
for wav in new_wav_list:
if feature_extra == 'spectrogram':
wav_mat = get_spectrogram_data(input_file=wav, window_shift=window_shift,
winows_size=windows_size, dft_length=dft_length,
freq_min=freq_min, freq_max=freq_max,
hz_resultion=hz_resultion)
elif feature_extra == 'mel_filter':
wav_mat = get_librosa_feature(wav)
else:
print('The value of feature_extra should be %s or %s' % ('spectrogram',
'mel_filter')
)
raise ValueError
wav_info.append([wav_mat, emotion])
if bool_vad:
wav_info[-1].extend(vad)
data_dict[session_speaker][file_name] = wav_info
mask = False
else:
continue
return data_dict
def stort_mat_lab(X, Y, info_idx, block_num, avg_amount, total_amount, file_path=None, info=None, bool_vad=False):
# used for store training data
for i in range(block_num):
skip = i * avg_amount
skep = min((i + 1) * avg_amount, total_amount)
idx_list = info_idx[skip:skep]
data_mat = [ X[idx] for idx in idx_list ]
if bool_vad:
data_emo = []
data_valence = []
data_arouse = []
data_domain = []
for idx in idx_list:
emo, valence, arouse, domain = Y[idx]
data_emo.append(lab_2_index.get(emo))
data_valence.append(valence)
data_arouse.append(arouse)
data_domain.append(domain)
with open(file_path + '/' + info + '_' + str(i + 1) + '_emo.pkl', 'wb') as f:
pickle.dump(data_emo, f)
with open(file_path + '/' + info + '_' + str(i + 1) + '_v.pkl', 'wb') as f:
pickle.dump(data_valence, f)
with open(file_path + '/' + info + '_' + str(i + 1) + '_a.pkl', 'wb') as f:
pickle.dump(data_arouse, f)
with open(file_path + '/' + info + '_' + str(i + 1) + '_d.pkl', 'wb') as f:
pickle.dump(data_domain, f)
else:
data_lab = [ lab_2_index.get(Y[idx]) for idx in idx_list ]
# data = [ [ X[idx], Y[idx] ] for idx in idx_list ]
with open(file_path + '/' + info + '_' + str(i + 1) + '_lab.pkl', 'wb') as f:
pickle.dump(data_lab, f)
with open(file_path + '/' + info + '_' + str(i + 1) + '_mat.pkl', 'wb') as f:
pickle.dump(data_mat, f)
def store(file_path, train_key, test_key, data_dict, bias=0, bool_vad=False):
total_E_set = {i: 0 for i in range(4)}
total_V_set = {i: 0 for i in range(4)}
total_A_set = {i: 0 for i in range(4)}
total_D_set = {i: 0 for i in range(4)}
train_amount = 0
for session_speaker in train_key:
file_mat_lab_dict = data_dict.get(session_speaker)
for wav_file in file_mat_lab_dict:
mat_lab_list = file_mat_lab_dict.get(wav_file)
if bool_vad:
for _, label, valence, arouse, domain in mat_lab_list:
emo = lab_2_index.get(label)
total_E_set[emo] += 1
total_V_set[valence] += 1
total_A_set[arouse] += 1
total_D_set[domain] += 1
train_amount += 1
else:
print("Nothing")
total_E_set = {i: train_amount / total_E_set[i] for i in range(4)}
total_V_set = {i: train_amount / total_V_set[i] for i in range(4)}
total_A_set = {i: train_amount / total_A_set[i] for i in range(4)}
total_D_set = {i: train_amount / total_D_set[i] for i in range(4)}
# =====================================Construct meta-train dataset=====================================
k = 1
for session_speaker in train_key:
train_mat = []
train_Y = []
file_mat_lab_dict = data_dict.get(session_speaker)
for wav_file in file_mat_lab_dict:
mat_lab_list = file_mat_lab_dict.get(wav_file)
if bool_vad:
for mat, label, valence, arouse, domain in mat_lab_list:
mat = mat - bias
mat = (mat - mat.mean())/mat.std()
train_mat.append(mat)
emo = lab_2_index.get(label)
train_Y.append([emo, valence, arouse, domain])
else:
for mat, label in mat_lab_list:
mat = mat - bias
mat = (mat - mat.mean()) / mat.std()
train_mat.append(mat)
train_Y.append(label)
train_amount = len(train_Y)
train_idx = list(range(train_amount))
random.shuffle(train_idx)
features = []
labels = []
for idx in train_idx:
features.append(train_mat[idx])
labels.append(train_Y[idx])
file_name = file_path + '/' + 'train_task' + str(k) + '_mat.pkl'
with open(file_name, 'wb') as f:
pickle.dump(features, f)
file_name = file_path + '/' + 'train_task' + str(k) + '_lab.pkl'
with open(file_name, 'wb') as f:
pickle.dump(labels, f)
file_name = file_path + '/' + 'train_task' + str(k) + '_Vport.pkl'
with open(file_name, 'wb') as f:
pickle.dump(total_V_set, f)
file_name = file_path + '/' + 'train_task' + str(k) + '_Aport.pkl'
with open(file_name, 'wb') as f:
pickle.dump(total_A_set, f)
file_name = file_path + '/' + 'train_task' + str(k) + '_Dport.pkl'
with open(file_name, 'wb') as f:
pickle.dump(total_D_set, f)
file_name = file_path + '/' + 'train_task' + str(k) + '_Eport.pkl'
with open(file_name, 'wb') as f:
pickle.dump(total_E_set, f)
k += 1
print("After processing the trainset, k=%d" % k)
# =====================================Construct meta-test dataset=====================================
k = 1
test_dict = dict()
for session_speaker in test_key:
test_mat = []
test_Y = []
E_set = {i: 0 for i in range(4)}
V_set = {i: 0 for i in range(4)}
A_set = {i: 0 for i in range(4)}
D_set = {i: 0 for i in range(4)}
file_mat_lab_dict = data_dict.get(session_speaker)
for wav_file in file_mat_lab_dict:
test_dict[wav_file] = []
segment_mat = []
mat_lab_list = file_mat_lab_dict.get(wav_file)
if bool_vad:
for mat, label, valence, arouse, domain in mat_lab_list:
mat = mat - bias
mat = (mat - mat.mean())/mat.std()
test_mat.append(mat)
emo = lab_2_index.get(label)
test_Y.append([emo, valence, arouse, domain])
E_set[emo] += 1
V_set[valence] += 1
A_set[arouse] += 1
D_set[domain] += 1
segment_mat.append(mat)
else:
for mat, label in mat_lab_list:
mat = mat - bias
mat = (mat - mat.mean()) / mat.std()
test_mat.append(mat)
test_Y.append(label)
segment_mat.append(mat)
test_dict[wav_file] = [segment_mat, emo]
test_amount = len(test_Y)
test_idx = list(range(test_amount))
random.shuffle(test_idx)
print(session_speaker)
print("Emotion:", E_set)
print("Valence:", V_set)
print("Arousal:", A_set)
print("Dominance:", D_set)
features = []
labels = []
for idx in test_idx:
features.append(test_mat[idx])
labels.append(test_Y[idx])
file_name = file_path + '/' + 'test_set.pkl'
with open(file_name, 'wb') as f:
pickle.dump(test_dict, f)
file_name = file_path + '/' + 'test_task' + str(k) + '_mat.pkl'
with open(file_name, 'wb') as f:
pickle.dump(features, f)
file_name = file_path + '/' + 'test_task' + str(k) + '_lab.pkl'
with open(file_name, 'wb') as f:
pickle.dump(labels, f)
amount = test_amount
file_name = file_path + '/' + 'test_task' + str(k) + '_Vport.pkl'
portion_dict = {i: amount / V_set.get(i) for i in range(4)}
with open(file_name, 'wb') as f:
pickle.dump(portion_dict, f)
file_name = file_path + '/' + 'test_task' + str(k) + '_Aport.pkl'
portion_dict = {i: amount / A_set.get(i) for i in range(4)}
with open(file_name, 'wb') as f:
pickle.dump(portion_dict, f)
file_name = file_path + '/' + 'test_task' + str(k) + '_Dport.pkl'
portion_dict = {i: amount / D_set.get(i) for i in range(4)}
with open(file_name, 'wb') as f:
pickle.dump(portion_dict, f)
file_name = file_path + '/' + 'test_task' + str(k) + '_Eport.pkl'
portion_dict = {i: amount / E_set.get(i) for i in range(4)}
with open(file_name, 'wb') as f:
pickle.dump(portion_dict, f)
k += 1
print("After processing the testset, k=%d" % k)
def main(cross_num=5, exp_path='', sav_dir='', conf='', cfg_sec='', bool_vad=False):
if not os.path.exists(exp_path):
os.mkdir(exp_path)
for i in range(1, 6):
session_name = 'Session' + str(i)
session_path = exp_path + '/' + session_name
if not os.path.exists(session_path):
os.mkdir(session_path)
if not os.path.exists(sav_dir):
os.mkdir(sav_dir)
thread_list = []
lab_list = []
for i in range(5):
t = MyThread(get_emo_data, args=('Session' + str(i + 1), True, bool_vad, 'spectrogram', 3,
exp_path + 'Session' + str(i+1) + '/', conf, cfg_sec))
thread_list.append(t)
for t in thread_list:
t.start()
for t in thread_list:
t.join()
lab_list.append(t.get_result())
lab_dict = dict()
for item in lab_list:
lab_dict.update(item)
min_val = np.Inf
freq_bag = set()
time_bag = set()
for sess_spk in lab_dict.keys():
# sess_spk aims to specific session of F/M,
# wav_file_dict: {wav_file_name: [[mat1,lab1], [mat2, lab2] ...], wav_file_name: [[mat1,lab1], [mat2,
# lab2] ...] , ..}
wav_file_info_dict = lab_dict.get(sess_spk)
print(sess_spk, len(list(wav_file_info_dict.keys())))
for wav_file in wav_file_info_dict:
info_list = wav_file_info_dict.get(wav_file)
for mat, label, valence, arouse, domain in info_list:
min_val = min(min_val, mat.min())
# max_length = max(max_length, mat.shape[1])
# min_length = min(min_length, mat.shape[1])
time_bag.add(mat.shape[1])
freq_bag.add(mat.shape[0])
#
print('Time bag:\n\t', time_bag)
print('Freq bag:\n\t', freq_bag)
print(max(time_bag), min(time_bag))
bias = min_val-1
print(bias)
sess_spk = set(lab_dict.keys())
if cross_num == 5:
for i in range(1, 6):
# construct the dataset for five-fold cross validation
cross_val = 'leave_' + str(i)
file_path = sav_dir + '/' + cross_val
dev_key = {'Session' + str(i) + '_F'}
test_key = {'Session' + str(i) + '_M'}
train_key = sess_spk - test_key - dev_key
if not os.path.exists(file_path):
os.mkdir(file_path)
print(test_key)
store(file_path=file_path, train_key=train_key, test_key=test_key, data_dict=lab_dict, bias=bias,
bool_vad=bool_vad)
print()
else:
sess_spk_list = list(sess_spk)
# construct the dataset for ten-fold cross validation
for i in range(1, 11):
idx = sess_spk_list[i-1]
cross_val = 'leave_' + str(i)
file_path = data_path_prefix + '10cross_set/' + cross_val
test_key = {idx}
train_key = sess_spk - test_key
if not os.path.exists(file_path):
os.mkdir(file_path)
store(file_path=file_path, train_key=train_key, test_key=test_key, data_dict=lab_dict)