-
Notifications
You must be signed in to change notification settings - Fork 7
/
data_pipeline.py
216 lines (180 loc) · 8 KB
/
data_pipeline.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
import re
import glob
import numpy as np
import pandas as pd
import tensorflow as tf
from random import shuffle
# for Word2Vec embeddings
import gensim
model = gensim.models.Word2Vec.load_word2vec_format(DATA_PATH+'GoogleNews-vectors-negative300.bin', binary=True)
DATA_PATH = '/home/karolina/Documents/GSOC/data/'
# file names are *.waw_st.npy
numpy_features = glob.glob(DATA_PATH+'IEMOCAP_full_release/Session*/sentences/wav/*/*st.npy')
# file names are *.wdseg
forced_alignments = glob.glob(DATA_PATH+'IEMOCAP_full_release/Session*/sentences/ForcedAlignment/Ses*/*.wdseg')
# *.txt files have assignments for the whole session not sentences
emo_evaluations = glob.glob(DATA_PATH+'IEMOCAP_full_release/Session*/dialog/EmoEvaluation/*.txt')
emotions_names = {'ang': np.int32(0), 'exc':np.int32(1), 'fru':np.int32(2),
'hap':np.int32(3), 'neu':np.int32(4), 'sad':np.int32(5)}
# audio features dict - assigning paths to file names *(without extension)
features_dict = {}
for p in numpy_features:
features_dict[p.split('/')[-1].split('.')[0]] = np.load(p)
# transcript files dict - assigning paths to file names *(without extension)
transcripts_paths_dict = {}
for p in forced_alignments:
transcripts_paths_dict[p.split('/')[-1].split('.')[0]] = p
###################### READ EMO FILES #####################
# messy evaluation dict - assigning evaluations (grand average) - from summary files to file names
file = emo_evaluations[1]
mean_emo_eval_dict = {}
emotional_eval_dict = {}
DATA_FLAG = False
values = [] # this is an empty value needed fot the loop
key = '' # this as well
for file in emo_evaluations:
with open(file) as f:
for line in f:
split = line.split('\t')
if len(split)>1:
split2 = split[1].split(';')
if len(split2)==1:
if len(values)!=0:
real_values = np.array([float(re.sub("[^0-9.]"," ", v)) for v in values.split(' ')
], dtype=np.float32)
mean_emo_eval_dict[key]=real_values
all_emotions.append(emo_tag)
if emo_tag in emotions_names:
emotional_eval_dict[key] = emotions_names[emo_tag]
key = split2[0]
emo_tag = split[2]
values = split[3]
real_values = np.array([float(re.sub("[^0-9.]"," ", v)) for v in values.split(' ')], dtype=np.float32)
mean_emo_eval_dict[key]=real_values #appending the last evaluation
if emo_tag in emotions_names:
emotional_eval_dict[key] = emotions_names[emo_tag]
###################### READ TRANSCRIPTS AND EMBEDDINGS #####################
LEN_WORD_LIM = 60
NUM_FEATURES = 34
LEN_SENTENCE = 25
def read_transcript(path):
"""
Reads and cleans the transcript from the forced-alignment files.
Cleans the 'non-words'
"""
df = pd.read_csv(path,delim_whitespace=True, usecols=['SFrm','EFrm','Word']).dropna()
df = df[~df.Word.str.contains('<')]
df = df[~df.Word.str.contains('"')]
df = df[~df.Word.str.contains('LAUGHTER')]
df = df[~df.Word.str.contains('BREATHING')]
df = df[~df.Word.str.contains('GARBAGE')]
n_rows = df.shape[0]
return df, n_rows
def clean_word(word):
word = re.sub("[^a-zA-Z]","", word).lower()
return word
def pad_feature(feature,audio_start,audio_end):
empty_feature = np.zeros((NUM_FEATURES,LEN_WORD_LIM), dtype=np.float32) #all features have this size
empty_feature[:,:audio_end-1-audio_start] = feature[:,audio_start:audio_end-1]
audio_feature = empty_feature[:,:,None]
return audio_feature
def pad_feature_sequence(feature_sequence):
sequence = np.zeros((NUM_FEATURES,LEN_WORD_LIM,LEN_SENTENCE),dtype=np.float32)
sequence[:,:,:feature_sequence.shape[2]] = feature_sequence
return sequence
def pad_word_sequence(word_sequence):
sequence = np.zeros((300,LEN_SENTENCE),dtype=np.float32)
sequence[:,:word_sequence.shape[1]] = word_sequence
return sequence
ready_audio_dict = {}
ready_word_embed_dict = {}
sentences_len_dict = {}
all_words = []
w2v_words = []
short_words = []
rejected_words = []
ALL_KEYS = emotional_eval_dict.keys() #transcripts_paths_dict.keys()
for key in list(ALL_KEYS):
features = features_dict[key]
if key in transcripts_paths_dict:
transcript, n_rows = read_transcript(transcripts_paths_dict[key])
OUTPUT_FLAG = False
for n in range(n_rows): #iteration over the whole sentence
w = transcript.Word.iloc[n]
lower = clean_word(w) #cleaning up the transcript
audio_start = int(transcript.SFrm.iloc[n])
audio_end = int(transcript.EFrm.iloc[n])
if audio_end-audio_start<LEN_WORD_LIM:
if lower in model.vocab:
word_embed = model[lower][:,None]
else:
word_embed = np.zeros((300,1))
feature = features_dict[key]
audio_feature = pad_feature(feature,audio_start,audio_end)
if OUTPUT_FLAG:
audio = np.concatenate((audio,audio_feature),axis=2)
word = np.concatenate((word,word_embed),axis=1)
else:
audio = audio_feature
word = word_embed
OUTPUT_FLAG = True
if word.shape[1] < LEN_SENTENCE:
ready_audio_dict[key] = pad_feature_sequence(audio)
ready_word_embed_dict[key] = pad_word_sequence(word)
sentences_len_dict[key] = np.int32(audio.shape[-1])
del model
GOOD_KEYS = ready_audio_dict.keys()
###################### DIVIDE DATA INTO SPLITS #####################
hash_dictionary = {}
for sess_name in list(GOOD_KEYS):
split = sess_name.split('_')
ses_num = split[0][3:5]
ses_g = split[0][-1]
scenario = re.sub("[^a-z]"," ", split[1]).strip(' \t\n\r')
scenario_num = re.sub("[^0-9]"," ", split[1]).strip(' \t\n\r')
if len(split)==4:
division_num = split[2]
g = split[3][0]
else:
division_num = '0'
g = split[2][0]
hash_key = ses_num+ses_g+scenario+scenario_num+division_num+g
if hash_key in hash_dictionary:
hash_dictionary[hash_key].append(sess_name)
else:
hash_dictionary[hash_key] = [sess_name]
n_splits = 8
splits = np.empty(n_splits,dtype=object)
EMPTY_FLAG = True
for k, v in hash_dictionary.items():
a = np.arange(len(v))
shuffle(v)
if EMPTY_FLAG:
for i in range(n_splits):
splits[i] = list(np.array(v)[a%n_splits==i])
EMPTY_FLAG = False
else:
for i in range(n_splits):
splits[i].extend(list(np.array(v)[a%n_splits==i]))
###################### WRITE TO BINARIES #####################
BINARIES_PATH = '/home/karolina/Documents/GSOC/multi-modal-emotion-prediction/data_IEMOCAP/'
for j,split in enumerate(list(splits)):
for i,key in enumerate(split):
# dividing into files
if i%100==0:
train_filename = BINARIES_PATH+'split_'+str(j)+'_'+str(i//100)+'.tfrecords'
writer = tf.python_io.TFRecordWriter(train_filename)
audio = ready_audio_dict[key].tobytes()
sentence_len = sentences_len_dict[key].tobytes()
word = ready_word_embed_dict[key].tobytes()
emo = mean_emo_eval_dict[key].tobytes()
label = emotional_eval_dict[key].tobytes()
example = tf.train.Example(features=tf.train.Features(feature={
'audio_features' : tf.train.Feature(bytes_list=tf.train.BytesList(value=[audio])),
'sentence_len' : tf.train.Feature(bytes_list=tf.train.BytesList(value=[sentence_len])),
'word_embeddings' : tf.train.Feature(bytes_list=tf.train.BytesList(value=[word])),
'y' : tf.train.Feature(bytes_list=tf.train.BytesList(value=[emo])),
'label' : tf.train.Feature(bytes_list=tf.train.BytesList(value=[label])),
}))
writer.write(example.SerializeToString())
writer.close()