forked from littleZY/SML_EW_EmotiW2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
300 lines (253 loc) · 11.3 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
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
import numpy as np
import pandas as pd
import os
class EngagementDataset():
def __init__(self, openface_features, vggface2_features, au_features, engagement_labels, n_segments=15, alpha=0.5,
augment=0, shuffle=False):
"""
Initialization
:param openface_features:
:param engagement_labels:
:param n_segments:
:param alpha:
"""
self.n_segments = n_segments
self.alpha = alpha
self.random_state = 97
labels_df = pd.read_csv(engagement_labels, header=None).values
self.vd_names = labels_df[:, 0]
self.vd_lbls = labels_df[:, 1].astype(float)
self.lbl_dict = {}
for idx, name in enumerate(self.vd_names):
self.lbl_dict[name] = self.vd_lbls[idx]
ld_with_ext = os.listdir(openface_features)
ld_no_ext = [x[:-4] for x in ld_with_ext if x.endswith('.csv')]
self.ld_no_ext = []
self.ld_csv = []
self.ld_vggface2 = []
self.ld_au = []
self.ld_dfscore = []
for x in ld_no_ext:
if x in self.vd_names:
self.ld_csv.append(os.path.join(openface_features, x + '.csv'))
self.ld_vggface2.append(os.path.join(vggface2_features, x + '.npz'))
self.ld_au.append(os.path.join(au_features, x + '.npz'))
self.ld_no_ext.append(x)
self.ld_dfscore.append(self.lbl_dict[x])
self.ld_csv = np.array(self.ld_csv)
self.ld_vggface2 = np.array(self.ld_vggface2)
self.ld_au = np.array(self.ld_au)
self.ld_no_ext = np.array(self.ld_no_ext)
self.ld_dfscore = np.array(self.ld_dfscore)
if augment:
minor_idx = np.where(self.ld_dfscore == 0.0)[0]
augment_idx = skutils.resample(minor_idx, random_state=self.random_state, replace=True,
n_samples=augment - len(minor_idx))
new_idx = np.hstack([np.arange(self.ld_dfscore.shape[0]), augment_idx])
else:
new_idx = np.arange(self.ld_dfscore.shape[0])
# new_idx = new_idx[:5]
if shuffle:
new_idx = skutils.shuffle(new_idx, random_state=self.random_state + 2)
self.ld_csv = self.ld_csv[new_idx]
self.ld_vggface2 = self.ld_vggface2[new_idx]
self.ld_au = self.ld_au[new_idx]
self.ld_no_ext = self.ld_no_ext[new_idx]
self.ld_dfscore = self.ld_dfscore[new_idx]
def get_gaze_features(self, raw_input):
"""
Get gaze features from raw input
:param raw_input:
:return:
"""
# Get statiscal feature from raw input
gaze_direction = raw_input[:, 5:11]
gaze_angle = raw_input[:, 11: 13]
eye_landmark2D = raw_input[:, 13: 125]
eye_landmark3D = raw_input[:, 125: 293]
pose_direction = raw_input[:, 293: 299]
face_landmark2D = raw_input[:, 299: 435]
face_landmark3D = raw_input[:, 435: 679]
au_reg = raw_input[:, 679: 695]
au_cls = raw_input[:, 695: 713]
gaze_direction_std = np.std(gaze_direction, axis=0)
gaze_direction_mean = np.mean(gaze_direction, axis=0)
gaze_angle_std = np.std(gaze_angle, axis=0)
gaze_angle_mean = np.mean(gaze_angle, axis=0)
eye_landmark2D_shape_0 = np.abs(eye_landmark2D[:, 56 + 9: 56 + 14] - eye_landmark2D[:, 56 + 19: 56 + 14: -1])
eye_landmark2D_shape_1 = np.abs(eye_landmark2D[:, 56 + 37: 56 + 42] - eye_landmark2D[:, 56 + 47: 56 + 42: -1])
eye_landmark2D_shape = np.hstack((eye_landmark2D_shape_0, eye_landmark2D_shape_1))
eye_landmark2D_shape_cov = np.divide(np.std(eye_landmark2D_shape, axis=0),
np.mean(eye_landmark2D_shape, axis=0))
eye_distance = 0.5 * (eye_landmark3D[:, 56 * 2 + 8] + eye_landmark3D[:, 56 * 2 + 42])
eye_distance_cov = np.std(eye_distance) / np.mean(eye_distance)
eye_distance_ratio = np.min(eye_distance) / np.max(eye_distance)
eye_distance_fea = np.array([eye_distance_cov, eye_distance_ratio])
eye_location2D = []
for idx in range(4):
cur_mean = np.mean(eye_landmark2D[:, 28 * idx: 28 * (idx + 1)], axis=1)
eye_location2D.append(cur_mean)
eye_location2D = np.vstack(eye_location2D).T
eye_location2D_mean = np.mean(eye_location2D, axis=0)
eye_location2D_std = np.std(eye_location2D, axis=0)
eye_location3D = []
for idx in range(6):
cur_mean = np.mean(eye_landmark3D[:, 28 * idx: 28 * (idx + 1)], axis=1)
eye_location3D.append(cur_mean)
eye_location3D = np.vstack(eye_location3D).T
eye_location3D_mean = np.mean(eye_location3D, axis=0)
eye_location3D_std = np.std(eye_location3D, axis=0)
pose_direction_mean = np.mean(pose_direction, axis=0)
pose_direction_std = np.std(pose_direction, axis=0)
ret_features = np.hstack((gaze_direction_std, gaze_direction_mean, gaze_angle_mean, gaze_angle_std,
eye_landmark2D_shape_cov, eye_location2D_mean, eye_location2D_std,
eye_location3D_mean,
eye_location3D_std, eye_distance_fea, pose_direction_mean, pose_direction_std))
return ret_features
def parse_gaze_features(self, txt_path):
"""
Divide txt to n_segments with same size
the end of the last segment: k + (n_segments-1)*(1-alpha)*k
try to choose alpha and n_segments in order to (n_segments-1)*(1-alpha) is integer
:param txt_path:
:param n_segments:
:param alpha: overlap percent
:return:
"""
df = pd.read_csv(txt_path, header=0, sep=',').values
face_id = df[:, 1]
seq_length = df.shape[0]
indexing = int((self.n_segments - 1) * (1 - self.alpha))
k_value = seq_length // (1 + indexing) # In some case, we will ignore some last frames
ret = []
index_st = 0
for idx in range(self.n_segments):
index_ed = k_value + int(k_value * (1 - self.alpha) * idx)
index_features = self.get_gaze_features(df[index_st: index_ed, :])
ret.append(index_features)
index_st = index_ed - int((1 - self.alpha) * k_value)
ret = np.vstack(ret)
return ret
def get_au_features(self, raw_input, ft_type):
"""
:param raw_input:
:param ft_type: 0 - mean, 1 - std, 2 - max, -1 - all
:return:
"""
if ft_type == 0:
ret = np.mean(raw_input, axis=0)
ret = np.sum(ret, axis=0)
elif ft_type == 1:
ret = np.std(raw_input, axis=0)
ret = np.sum(ret, axis=0)
elif ft_type == 2:
ret = np.max(raw_input, axis=0)
ret = np.sum(ret, axis=0)
else:
ret0 = np.mean(raw_input, axis=0)
ret1 = np.std(raw_input, axis=0)
ret2 = np.max(raw_input, axis=0)
ret = np.vstack([ret0, ret1, ret2])
return ret.reshape(1, -1)
def parse_au_features(self, au_path, sft=2):
dauz = np.load(os.path.join(au_path), allow_pickle=True)['values']
seq_length = dauz.shape[0]
indexing = int((self.n_segments - 1) * (1 - self.alpha))
k_value = seq_length // (1 + indexing) # In some case, we will ignore some last frames
ret = []
index_st = 0
for idx in range(self.n_segments):
index_ed = k_value + int(k_value * (1 - self.alpha) * idx)
# ft_type = [0, 1, 2, -1] - [mean, std, max, all]
index_features = self.get_au_features(dauz[index_st: index_ed, :], ft_type=sft)
ret.append(index_features)
index_st = index_ed - int((1 - self.alpha) * k_value)
ret = np.vstack(ret)
return ret
def get_vgg2_features(self, raw_input, ft_type):
"""
:param raw_input:
:param ft_type: 0 - mean, 1 - std, 2 - max, -1 - all
:return:
"""
if ft_type == 0:
ret = np.mean(raw_input, axis=0).reshape(1, -1)
elif ft_type == 1:
ret = np.std(raw_input, axis=0).reshape(1, -1)
elif ft_type == 2:
ret = np.max(raw_input, axis=0).reshape(1, -1)
else:
ret0 = np.mean(raw_input, axis=0).reshape(1, -1)
ret1 = np.std(raw_input, axis=0).reshape(1, -1)
ret2 = np.max(raw_input, axis=0).reshape(1, -1)
ret = np.vstack([ret0, ret1, ret2])
return ret
def parse_vgg2_features(self, vgg_path, sft=2):
dvggz = np.load(os.path.join(vgg_path), allow_pickle=True)['values']
seq_length = dvggz.shape[0]
indexing = int((self.n_segments - 1) * (1 - self.alpha))
k_value = seq_length // (1 + indexing) # In some case, we will ignore some last frames
ret = []
index_st = 0
for idx in range(self.n_segments):
index_ed = k_value + int(k_value * (1 - self.alpha) * idx)
# ft_type = [0, 1, 2, -1] - [mean, std, max, all]
index_features = self.get_vgg2_features(dvggz[index_st: index_ed, :], ft_type=sft)
ret.append(index_features)
index_st = index_ed - int((1 - self.alpha) * k_value)
ret = np.vstack(ret)
return ret
def __len__(self):
"Denotes the total number of samples"
return len(self.ld_csv)
def get_item(self, idx, ft_type, sft=2):
"""
Generate one sample of data
:param idx:
:param ft_type: [0, 1, 2] - [gaze, au, vgg]
:param sft: 0, 1, 2, -1, feature type (mean, std, max, all) for gaze, au, vgg respectively
:return:
"""
txt_score = self.lbl_dict[self.ld_no_ext[idx]]
if ft_type == 0:
txt_name = self.ld_csv[idx]
X = self.parse_gaze_features(txt_name)
elif ft_type == 1:
txt_name = self.ld_au[idx]
X = self.parse_au_features(txt_name, sft)
elif ft_type == 2:
txt_name = self.ld_vggface2[idx]
X = self.parse_vgg2_features(txt_name, sft)
else:
raise "Do not support ft_type = {}".format(ft_type)
return X, txt_score
def get_all_gaze_features(self):
pass
def get_all_au_features(self):
pass
def get_all_face_features(self):
pass
def get_all_data(self, ft_list, sft, num_jobs=4, shuffle=False, augment=False):
"""
Get all data
:param ft_list: list, [0, 1, 2] - [gaze, au, vgg]
:param sft: list, [-1, 1, 1], feature type (mean, std, max, all) for gaze, au, vgg respectively
:param num_jobs: num_job parallel
:param shuffle:
:param augment: augment minority class (0) by upsampling or not
:return:
"""
ret = []
for ft in ft_list:
print(ft)
current_parallel = Parallel(n_jobs=num_jobs)(
delayed(self.get_item)(ix, ft, sft[ft]) for ix in range(len(self.ld_csv)))
ld_features = []
ld_scores = []
for tup in current_parallel:
ld_features.append(tup[0])
ld_scores.append(tup[1])
ld_features = np.stack(ld_features)
ld_scores = np.array(ld_scores)
ret.append((ld_features, ld_scores))
return ret