forked from declare-lab/multimodal-deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
202 lines (161 loc) · 7.53 KB
/
utils.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
from torch.utils.data import Dataset
import sys
if sys.version_info.major == 2:
import cPickle as pickle
else:
import pickle
import pdb
AUDIO = b'covarep'
VISUAL = b'facet'
TEXT = b'glove'
LABEL = b'label'
TRAIN = b'train'
VALID = b'valid'
TEST = b'test'
def total(params):
'''
count the total number of hyperparameter settings
'''
settings = 1
for k, v in params.items():
settings *= len(v)
return settings
def load_pom(data_path):
# parse the input args
class POM(Dataset):
'''
PyTorch Dataset for POM, don't need to change this
'''
def __init__(self, audio, visual, text, labels):
self.audio = audio
self.visual = visual
self.text = text
self.labels = labels
def __getitem__(self, idx):
return [self.audio[idx, :], self.visual[idx, :], self.text[idx, :, :], self.labels[idx]]
def __len__(self):
return self.audio.shape[0]
if sys.version_info.major == 2:
pom_data = pickle.load(open(data_path + "pom.pkl", 'rb'))
else:
pom_data = pickle.load(open(data_path + "pom.pkl", 'rb'), encoding='bytes')
pom_train, pom_valid, pom_test = pom_data[TRAIN], pom_data[VALID], pom_data[TEST]
train_audio, train_visual, train_text, train_labels \
= pom_train[AUDIO], pom_train[VISUAL], pom_train[TEXT], pom_train[LABEL]
valid_audio, valid_visual, valid_text, valid_labels \
= pom_valid[AUDIO], pom_valid[VISUAL], pom_valid[TEXT], pom_valid[LABEL]
test_audio, test_visual, test_text, test_labels \
= pom_test[AUDIO], pom_test[VISUAL], pom_test[TEXT], pom_test[LABEL]
# code that instantiates the Dataset objects
train_set = POM(train_audio, train_visual, train_text, train_labels)
valid_set = POM(valid_audio, valid_visual, valid_text, valid_labels)
test_set = POM(test_audio, test_visual, test_text, test_labels)
audio_dim = train_set[0][0].shape[0]
print("Audio feature dimension is: {}".format(audio_dim))
visual_dim = train_set[0][1].shape[0]
print("Visual feature dimension is: {}".format(visual_dim))
text_dim = train_set[0][2].shape[1]
print("Text feature dimension is: {}".format(text_dim))
input_dims = (audio_dim, visual_dim, text_dim)
# remove possible NaN values
train_set.visual[train_set.visual != train_set.visual] = 0
valid_set.visual[valid_set.visual != valid_set.visual] = 0
test_set.visual[test_set.visual != test_set.visual] = 0
train_set.audio[train_set.audio != train_set.audio] = 0
valid_set.audio[valid_set.audio != valid_set.audio] = 0
test_set.audio[test_set.audio != test_set.audio] = 0
return train_set, valid_set, test_set, input_dims
def load_iemocap(data_path, emotion):
# parse the input args
class IEMOCAP(Dataset):
'''
PyTorch Dataset for IEMOCAP, don't need to change this
'''
def __init__(self, audio, visual, text, labels):
self.audio = audio
self.visual = visual
self.text = text
self.labels = labels
def __getitem__(self, idx):
return [self.audio[idx, :], self.visual[idx, :], self.text[idx, :, :], self.labels[idx]]
def __len__(self):
return self.audio.shape[0]
if sys.version_info.major == 2:
iemocap_data = pickle.load(open(data_path + "iemocap.pkl", 'rb'))
else:
iemocap_data = pickle.load(open(data_path + "iemocap.pkl", 'rb'), encoding='bytes')
iemocap_train, iemocap_valid, iemocap_test = iemocap_data[emotion][TRAIN], iemocap_data[emotion][VALID], iemocap_data[emotion][TEST]
train_audio, train_visual, train_text, train_labels \
= iemocap_train[AUDIO], iemocap_train[VISUAL], iemocap_train[TEXT], iemocap_train[LABEL]
valid_audio, valid_visual, valid_text, valid_labels \
= iemocap_valid[AUDIO], iemocap_valid[VISUAL], iemocap_valid[TEXT], iemocap_valid[LABEL]
test_audio, test_visual, test_text, test_labels \
= iemocap_test[AUDIO], iemocap_test[VISUAL], iemocap_test[TEXT], iemocap_test[LABEL]
# code that instantiates the Dataset objects
train_set = IEMOCAP(train_audio, train_visual, train_text, train_labels)
valid_set = IEMOCAP(valid_audio, valid_visual, valid_text, valid_labels)
test_set = IEMOCAP(test_audio, test_visual, test_text, test_labels)
audio_dim = train_set[0][0].shape[0]
print("Audio feature dimension is: {}".format(audio_dim))
visual_dim = train_set[0][1].shape[0]
print("Visual feature dimension is: {}".format(visual_dim))
text_dim = train_set[0][2].shape[1]
print("Text feature dimension is: {}".format(text_dim))
input_dims = (audio_dim, visual_dim, text_dim)
# remove possible NaN values
train_set.visual[train_set.visual != train_set.visual] = 0
valid_set.visual[valid_set.visual != valid_set.visual] = 0
test_set.visual[test_set.visual != test_set.visual] = 0
train_set.audio[train_set.audio != train_set.audio] = 0
valid_set.audio[valid_set.audio != valid_set.audio] = 0
test_set.audio[test_set.audio != test_set.audio] = 0
return train_set, valid_set, test_set, input_dims
def load_mosi(data_path):
# parse the input args
class MOSI(Dataset):
'''
PyTorch Dataset for MOSI, don't need to change this
'''
def __init__(self, audio, visual, text, labels):
self.audio = audio
self.visual = visual
self.text = text
self.labels = labels
def __getitem__(self, idx):
return [self.audio[idx, :], self.visual[idx, :], self.text[idx, :, :], self.labels[idx]]
def __len__(self):
return self.audio.shape[0]
if sys.version_info.major == 2:
mosi_data = pickle.load(open(data_path + "mosi.pkl", 'rb'))
else:
mosi_data = pickle.load(open(data_path + "mosi.pkl", 'rb'), encoding='bytes')
mosi_train, mosi_valid, mosi_test = mosi_data[TRAIN], mosi_data[VALID], mosi_data[TEST]
train_audio, train_visual, train_text, train_labels \
= mosi_train[AUDIO], mosi_train[VISUAL], mosi_train[TEXT], mosi_train[LABEL]
valid_audio, valid_visual, valid_text, valid_labels \
= mosi_valid[AUDIO], mosi_valid[VISUAL], mosi_valid[TEXT], mosi_valid[LABEL]
test_audio, test_visual, test_text, test_labels \
= mosi_test[AUDIO], mosi_test[VISUAL], mosi_test[TEXT], mosi_test[LABEL]
print(train_audio.shape)
print(train_visual.shape)
print(train_text.shape)
print(train_labels.shape)
# code that instantiates the Dataset objects
train_set = MOSI(train_audio, train_visual, train_text, train_labels)
valid_set = MOSI(valid_audio, valid_visual, valid_text, valid_labels)
test_set = MOSI(test_audio, test_visual, test_text, test_labels)
audio_dim = train_set[0][0].shape[0]
print("Audio feature dimension is: {}".format(audio_dim))
visual_dim = train_set[0][1].shape[0]
print("Visual feature dimension is: {}".format(visual_dim))
text_dim = train_set[0][2].shape[1]
print("Text feature dimension is: {}".format(text_dim))
input_dims = (audio_dim, visual_dim, text_dim)
# remove possible NaN values
train_set.visual[train_set.visual != train_set.visual] = 0
valid_set.visual[valid_set.visual != valid_set.visual] = 0
test_set.visual[test_set.visual != test_set.visual] = 0
train_set.audio[train_set.audio != train_set.audio] = 0
valid_set.audio[valid_set.audio != valid_set.audio] = 0
test_set.audio[test_set.audio != test_set.audio] = 0
return train_set, valid_set, test_set, input_dims