-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmusdk.py
483 lines (372 loc) · 12.6 KB
/
cmusdk.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import os
import re
import mmsdk
import numpy as np
from loguru import logger
from mmsdk import mmdatasdk as md
from slp.config.nlp import SPECIAL_TOKENS
from slp.util.system import pickle_dump, pickle_load, safe_mkdirs
from tqdm import tqdm
MOSEI_COVAREP_FACET_GLOVE = {
"audio": "CMU_MOSEI_COVAREP",
"text": "CMU_MOSEI_TimestampedWordVectors",
"raw": "CMU_MOSEI_TimestampedWords",
"visual": "CMU_MOSEI_Visual_Facet_42",
"labels": "CMU_MOSEI_Opinion_Labels",
}
def download_mmdata(base_path, dataset):
safe_mkdirs(base_path)
try:
md.mmdataset(dataset.highlevel, base_path)
except RuntimeError:
logger.info("High-level features have been downloaded previously.")
try:
md.mmdataset(dataset.raw, base_path)
except RuntimeError:
logger.info("Raw data have been downloaded previously.")
try:
md.mmdataset(dataset.labels, base_path)
except RuntimeError:
logger.info("Labels have been downloaded previously.")
def avg_collapse(intervals, features):
try:
return np.average(features, axis=0)
except Exception as e:
del e
return features
def deploy(in_dataset, destination):
deploy_files = {x: x for x in in_dataset.keys()}
in_dataset.deploy(destination, deploy_files)
def load_modality(base_path, feature_cfg, modality):
mfile = feature_cfg[modality]
path = os.path.join(base_path, "{}.csd".format(mfile))
logger.info("Using {} for {} modality".format(path, modality))
data = md.mmdataset(path)
return data
def get_vocabulary(text_dataset):
all_words = []
for seg in text_dataset.keys():
words = text_dataset[seg]["features"][0]
for w in words:
wi = w.decode("utf-8")
all_words.append(wi)
all_words = list(set(all_words))
return all_words
def create_word2idx(all_words):
word2idx, idx = {}, 0
for w in sorted(all_words):
if w not in word2idx:
word2idx[w] = idx
idx += 1
for t in SPECIAL_TOKENS:
word2idx[t.value] = idx
idx += 1
return word2idx
def select_dataset(dataset_name):
if dataset_name == "mosi":
dataset = md.cmu_mosi
elif dataset_name == "mosei":
dataset = md.cmu_mosei
elif dataset_name == "pom":
dataset = md.pom
else:
raise ValueError("Unsupported dataset. Use [mosei|mosi|pom]")
return dataset
def patch_missing_metadata(data):
# Remove need for annoying input that stops execution
for k in data.computational_sequences.keys():
data.computational_sequences[k].metadata[
"dimension names"
] = data.computational_sequences[k].metadata.get("dimension names", None)
data.computational_sequences[k].metadata[
"computational sequence version"
] = data.computational_sequences[k].metadata.get(
"computational sequence version", None
)
data.computational_sequences[k].metadata[
"dimension namescomputational sequence version"
] = None
def load_and_align(
base_path,
dataset="mosei",
feature_cfg=MOSEI_COVAREP_FACET_GLOVE,
modalities={"audio", "visual", "text"},
collapse=None,
):
dataset = select_dataset(dataset)
download_mmdata(base_path, dataset)
recipe = {
f: os.path.join(base_path, "{}.csd".format(f))
for k, f in feature_cfg.items()
if k in list(modalities) + ["raw"]
}
data = md.mmdataset(recipe)
patch_missing_metadata(data)
if collapse is None:
collapse = [avg_collapse]
# first we align to words with averaging
# collapse_function receives a list of functions
word_align_path = base_path + "_word_aligned"
safe_mkdirs(word_align_path)
data.align(feature_cfg["raw"], collapse_functions=collapse)
data.impute(feature_cfg["raw"])
deploy(data, word_align_path)
all_words = get_vocabulary(data[feature_cfg["raw"]])
word2idx = create_word2idx(all_words)
label_recipe = {
feature_cfg["labels"]: os.path.join(
base_path, "{}.csd".format(feature_cfg["labels"])
)
}
data.add_computational_sequences(label_recipe, destination=None)
patch_missing_metadata(data)
data.align(feature_cfg["labels"])
data.hard_unify()
align_path = base_path + "_final_aligned"
safe_mkdirs(align_path)
deploy(data, align_path)
return data, word2idx
def load_dataset(
base_path,
dataset="mosei",
feature_cfg=MOSEI_COVAREP_FACET_GLOVE,
modalities={"audio", "text", "visual"},
already_segmented=False,
):
dataset = select_dataset(dataset)
download_mmdata(base_path, dataset)
recipe = {
f: os.path.join(base_path, "{}.csd".format(f))
for k, f in feature_cfg.items()
if k in list(modalities) + ["raw"]
}
data = md.mmdataset(recipe)
patch_missing_metadata(data)
all_words = get_vocabulary(data[feature_cfg["raw"]])
word2idx = create_word2idx(all_words)
label_recipe = {
feature_cfg["labels"]: os.path.join(
base_path, "{}.csd".format(feature_cfg["labels"])
)
}
data.add_computational_sequences(label_recipe, destination=None)
patch_missing_metadata(data)
if not already_segmented:
data.align(feature_cfg["labels"])
data.hard_unify()
return data, word2idx
def remove_pause_tokens(mods, modalities, is_raw_text_feature):
# Handle speech pause
mods_nosp = {k: [] for k in modalities}
for m in modalities:
for i in range(len(mods[m])):
if mods["raw"][i] != "sp":
mods_nosp[m].append(mods[m][i])
return mods_nosp
def replace_sp_token(mods, is_raw_text_feature):
mods_nosp = mods
if is_raw_text_feature:
for i in range(len(mods["text"])):
if mods["text"][i] == "sp":
mods_nosp["text"][i] = SPECIAL_TOKENS.PAUSE.value
return mods_nosp
def pad_modality_features(
mods, modalities, max_length, pad_front, pad_back, is_raw_text_feature
):
if pad_front and pad_back:
raise ValueError("Only one of pad_front and pad_back should be true.")
def compute_padding(m, seglen):
t = []
for i in range(max_length[m] - seglen):
if is_raw_text_feature and m == "text":
t.append(SPECIAL_TOKENS.PAD.value)
else:
vshape = mods[m][0].shape
pad = np.zeros(vshape)
t.append(pad)
return t
for m in modalities:
if isinstance(mods[m], np.ndarray):
mods[m] = [x for x in mods[m]]
seglen = len(mods[m])
if seglen >= max_length[m]:
t = []
for i in range(max_length[m]):
t.append(mods[m][i])
mods[m] = t
else:
if pad_front:
padding = compute_padding(m, seglen)
mods[m] = padding + mods[m]
if pad_back:
padding = compute_padding(m, seglen)
mods[m] = mods[m] + padding
return mods
def clean_split_dataset(
data,
dataset="mosei",
feature_cfg=MOSEI_COVAREP_FACET_GLOVE,
modalities={"audio", "text", "visual"},
remove_pauses=False,
max_length=-1,
pad_front=False,
pad_back=False,
aligned=True,
):
dataset = select_dataset(dataset)
train_split = dataset.standard_folds.standard_train_fold
dev_split = dataset.standard_folds.standard_valid_fold
test_split = dataset.standard_folds.standard_test_fold
train, dev, test = [], [], []
num_drop = 0
segments = data[feature_cfg["labels"]].keys()
if max_length < 0:
max_length = {
m: max([len(data[feature_cfg[m]][s]["features"]) for s in segments])
for m in list(modalities) + ["raw"]
}
else:
max_length = {m: max_length for m in list(modalities) + ["raw"]}
for segment in tqdm(segments):
# get the video ID and the features out of the aligned dataset
sidx = segment.find("[")
if sidx > 0:
vid = segment[:sidx]
else:
vid = segment
# if segment == 'c5xsKMxpXnc':
mods = {
k: data[feature_cfg[k]][segment]["features"]
for k in list(modalities) + ["raw"]
}
is_raw_text_feature = isinstance(mods["text"][0][0], bytes)
raw_text_mods = ["raw", "text"] if is_raw_text_feature else ["raw"]
mods_with_possible_nan = list(set(modalities) - set(raw_text_mods))
mods_without_raw_text = list(set(modalities) - set(raw_text_mods))
for m in mods_with_possible_nan:
mods[m] = np.nan_to_num(mods[m])
for m in raw_text_mods:
words = []
for i in range(len(mods[m])):
words.append(mods[m][i][0].decode("utf-8"))
mods[m] = words
if aligned:
num_drop = 0
# if the sequences are not same length after alignment,
# there must be some problem with some modalities
# we should drop it or inspect the data again
mod_shapes = {k: len(v) for k, v in mods.items()}
if not len(set(list(mod_shapes.values()))) <= 1:
logger.warning("Datapoint {} shape mismatch {}".format(vid, mod_shapes))
num_drop += 1
continue
if remove_pauses:
mods = remove_pause_tokens(mods, modalities, is_raw_text_feature)
mods = pad_modality_features(
mods, modalities, max_length, pad_front, pad_back, is_raw_text_feature
)
mods = replace_sp_token(mods, is_raw_text_feature)
for m in mods_without_raw_text:
mods[m] = np.asarray(mods[m])
mods["video_id"] = vid
mods["segment_id"] = segment
mods["label"] = np.nan_to_num(data[feature_cfg["labels"]][segment]["features"])
if vid in train_split:
train.append(mods)
elif vid in dev_split:
dev.append(mods)
elif vid in test_split:
test.append(mods)
else:
logger.warning("{} does not belong to any of the splits".format(vid))
logger.warning("Dropped {} data points".format(num_drop))
return train, dev, test
def load_splits(
base_path,
dataset="mosei",
feature_cfg=MOSEI_COVAREP_FACET_GLOVE,
modalities={"audio", "text", "visual"},
remove_pauses=False,
max_length=-1,
pad_front=False,
pad_back=False,
already_aligned=False,
align_features=True,
cache=None,
):
if cache is not None:
try:
return pickle_load(cache)
except FileNotFoundError:
pass
if not already_aligned and align_features:
data, word2idx = load_and_align(
base_path,
dataset=dataset,
feature_cfg=feature_cfg,
modalities=modalities,
collapse=[avg_collapse],
)
else:
data, word2idx = load_dataset(
base_path,
dataset=dataset,
feature_cfg=feature_cfg,
modalities=modalities,
already_segmented=already_aligned or align_features,
)
train, dev, test = clean_split_dataset(
data,
dataset=dataset,
feature_cfg=feature_cfg,
modalities=modalities,
remove_pauses=remove_pauses,
max_length=max_length,
pad_front=pad_front,
pad_back=pad_back,
aligned=already_aligned or align_features,
)
if cache is not None:
pickle_dump((train, dev, test, word2idx), cache)
return train, dev, test, word2idx
def mosei(
base_path,
feature_cfg=MOSEI_COVAREP_FACET_GLOVE,
modalities={"audio", "text", "visual"},
remove_pauses=False,
max_length=-1,
pad_front=False,
pad_back=False,
cache=None,
already_aligned=False,
align_features=True,
):
return load_splits(
base_path,
dataset="mosei",
feature_cfg=feature_cfg,
modalities=modalities,
remove_pauses=remove_pauses,
max_length=max_length,
pad_front=pad_front,
pad_back=pad_back,
cache=cache,
already_aligned=already_aligned,
align_features=align_features,
)
def data_pickle(fname):
data = pickle_load(fname)
return data["train"], data["valid"], data["test"], None
if __name__ == "__main__":
import sys
base_path = sys.argv[1]
train, dev, test, w2i = mosei(
base_path,
feature_cfg=MOSEI_COVAREP_FACET_GLOVE,
modalities=["audio", "text", "visual"],
remove_pauses=True,
pad_front=False,
pad_back=False,
already_aligned=False,
align_features=True,
)