-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
289 lines (211 loc) · 10.5 KB
/
dataset.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
import torch.utils.data as data
import cv2
from PIL import Image
import os
import os.path
import numpy as np
from numpy.random import randint
import pandas as pd
import torch
import torchvision.transforms.functional as tF
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
class VideoRecord(object):
def __init__(self, row):
self._data = row
@property
def path(self):
return self._data[0]
@property
def num_frames(self):
return int(self._data[1])
@property
def min_frame(self):
return int(self._data[2])
@property
def max_frame(self):
return int(self._data[3])
class TSNDataSet(data.Dataset):
def __init__(self, mode,
num_segments=3, new_length=1, modality='RGB',
image_tmpl='img_{:05d}.jpg', transform=None,
force_grayscale=False, random_shift=True, test_mode=False, context=False):
self.num_segments = num_segments
self.new_length = new_length
self.modality = modality
self.image_tmpl = image_tmpl
self.transform = transform
self.random_shift = random_shift
self.test_mode = test_mode
self.bold_path = "/gpu-data/filby/BoLD/BOLD_public"
self.context = context
self.categorical_emotions = ["Peace", "Affection", "Esteem", "Anticipation", "Engagement", "Confidence", "Happiness",
"Pleasure", "Excitement", "Surprise", "Sympathy", "Doubt/Confusion", "Disconnect",
"Fatigue", "Embarrassment", "Yearning", "Disapproval", "Aversion", "Annoyance", "Anger",
"Sensitivity", "Sadness", "Disquietment", "Fear", "Pain", "Suffering"]
self.continuous_emotions = ["Valence", "Arousal", "Dominance"]
self.attributes = ["Gender", "Age", "Ethnicity"]
header = ["video", "person_id", "min_frame", "max_frame"] + self.categorical_emotions + self.continuous_emotions + self.attributes + ["annotation_confidence"]
# self.df = pd.read_csv(os.path.join(self.bold_path, "annotations/{}_extra.csv".format(mode)))
self.df = pd.read_csv(os.path.join(self.bold_path, "annotations/{}.csv".format(mode)), names=header)
self.df["joints_path"] = self.df["video"].apply(rreplace,args=[".mp4",".npy",1])
self.video_list = self.df["video"]
self.mode = mode
self.embeddings = np.load("glove_840B_embeddings.npy")
def get_context(self, image, joints, format="cv2"):
joints = joints.reshape((18,3))
joints[joints[:,2]<0.1] = np.nan
joints[np.isnan(joints[:,2])] = np.nan
joint_min_x = int(round(np.nanmin(joints[:,0])))
joint_min_y = int(round(np.nanmin(joints[:,1])))
joint_max_x = int(round(np.nanmax(joints[:,0])))
joint_max_y = int(round(np.nanmax(joints[:,1])))
expand_x = int(round(10/100 * (joint_max_x-joint_min_x)))
expand_y = int(round(10/100 * (joint_max_y-joint_min_y)))
if format == "cv2":
image[max(0, joint_min_x - expand_x):min(joint_max_x + expand_x, image.shape[1])] = [0,0,0]
elif format == "PIL":
bottom = min(joint_max_y+expand_y, image.height)
right = min(joint_max_x+expand_x,image.width)
top = max(0,joint_min_y-expand_y)
left = max(0,joint_min_x-expand_x)
image = np.array(image)
if len(image.shape) == 3:
image[top:bottom,left:right] = [0,0,0]
else:
image[top:bottom,left:right] = np.min(image)
return Image.fromarray(image)
def get_bounding_box(self, image, joints, format="cv2"):
joints = joints.reshape((18,3))
joints[joints[:,2]<0.1] = np.nan
joints[np.isnan(joints[:,2])] = np.nan
joint_min_x = int(round(np.nanmin(joints[:,0])))
joint_min_y = int(round(np.nanmin(joints[:,1])))
joint_max_x = int(round(np.nanmax(joints[:,0])))
joint_max_y = int(round(np.nanmax(joints[:,1])))
expand_x = int(round(100/100 * (joint_max_x-joint_min_x)))
expand_y = int(round(100/100 * (joint_max_y-joint_min_y)))
if format == "cv2":
return image[max(0,joint_min_y-expand_y):min(joint_max_y+expand_y, image.shape[0]), max(0,joint_min_x-expand_x):min(joint_max_x+expand_x,image.shape[1])]
elif format == "PIL":
bottom = min(joint_max_y+expand_y, image.height)
right = min(joint_max_x+expand_x,image.width)
top = max(0,joint_min_y-expand_y)
left = max(0,joint_min_x-expand_x)
return tF.crop(image, top, left, bottom-top ,right-left)
def joints(self, index):
sample = self.df.iloc[index]
joints_path = os.path.join(self.bold_path, "joints", sample["joints_path"])
joints18 = np.load(joints_path)
joints18[:,0] -= joints18[0,0]
return joints18
def _load_image(self, directory, idx, index, mode="body"):
joints = self.joints(index)
poi_joints = joints[joints[:, 0] + 1 == idx]
sample = self.df.iloc[index]
poi_joints = poi_joints[(poi_joints[:, 1] == sample["person_id"]), 2:]
if self.modality == 'RGB' or self.modality == 'RGBDiff':
frame = Image.open(os.path.join(directory, self.image_tmpl.format(idx))).convert("RGB")
if mode == "context":
if poi_joints.size == 0:
return [frame]
context = self.get_context(frame, poi_joints, format="PIL")
return [context]
if poi_joints.size == 0:
body = frame
pass #just do the whole frame
else:
body = self.get_bounding_box(frame, poi_joints, format="PIL")
if body.size == 0:
print(poi_joints)
body = frame
return [body]
# return [Image.open(os.path.join(directory, self.image_tmpl.format(idx))).convert('RGB')]
elif self.modality == 'Flow':
frame_x = Image.open(os.path.join(directory, self.image_tmpl.format('flow_x', idx))).convert('L')
frame_y = Image.open(os.path.join(directory, self.image_tmpl.format('flow_y', idx))).convert('L')
# frame = cv2.imread(os.path.join(directory, 'img_{:05d}.jpg'.format(idx)))
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if mode == "context":
if poi_joints.size == 0:
return [frame_x, frame_y]
context_x = self.get_context(frame_x, poi_joints, format="PIL")
context_y = self.get_context(frame_y, poi_joints, format="PIL")
return [context_x, context_y]
if poi_joints.size == 0:
body_x = frame_x
body_y = frame_y
pass #just do the whole frame
else:
body_x = self.get_bounding_box(frame_x, poi_joints, format="PIL")
body_y = self.get_bounding_box(frame_y, poi_joints, format="PIL")
if body_x.size == 0:
body_x = frame_x
body_y = frame_y
return [body_x, body_y]
def _sample_indices(self, record):
"""
:param record: VideoRecord
:return: list
"""
average_duration = (record.num_frames - self.new_length + 1) // self.num_segments
if average_duration > 0:
offsets = np.multiply(list(range(self.num_segments)), average_duration) + randint(average_duration, size=self.num_segments) # + (record.min_frame+1)
# print(record.num_frames, record.min_frame, record.max_frame)
elif record.num_frames > self.num_segments:
offsets = np.sort(randint(record.num_frames - self.new_length + 1, size=self.num_segments))
else:
offsets = np.zeros((self.num_segments,))
return offsets + 1
def _get_val_indices(self, record):
if record.num_frames > self.num_segments + self.new_length - 1:
tick = (record.num_frames - self.new_length + 1) / float(self.num_segments)
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
else:
offsets = np.zeros((self.num_segments,))
return offsets + 1
def _get_test_indices(self, record):
tick = (record.num_frames - self.new_length + 1) / float(self.num_segments)
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
return offsets + 1
def __getitem__(self, index):
sample = self.df.iloc[index]
fname = os.path.join(self.bold_path,"videos",self.df.iloc[index]["video"])
capture = cv2.VideoCapture(fname)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))-1
capture.release()
record_path = os.path.join(self.bold_path,"test_raw",sample["video"][4:-4])
record = VideoRecord([record_path, frame_count, sample["min_frame"], sample["max_frame"]])
if not self.test_mode:
segment_indices = self._sample_indices(record) if self.random_shift else self._get_val_indices(record)
else:
segment_indices = self._get_test_indices(record)
return self.get(record, segment_indices, index)
def get(self, record, indices, index):
images = list()
# print(indices)
for seg_ind in indices:
p = int(seg_ind)
for i in range(self.new_length):
seg_imgs = self._load_image(record.path, p, index, mode="body")
images.extend(seg_imgs)
if self.context:
seg_imgs = self._load_image(record.path, p, index, mode="context")
images.extend(seg_imgs)
if p < record.num_frames:
p += 1
if not self.test_mode:
categorical = self.df.iloc[index][self.categorical_emotions]
continuous = self.df.iloc[index][self.continuous_emotions]
continuous = continuous/10.0 # normalize to 0 - 1
if self.transform is None:
process_data = images
else:
process_data = self.transform(images)
return process_data, torch.tensor(self.embeddings).float(), torch.tensor(categorical).float(), torch.tensor(continuous).float(), self.df.iloc[index]["video"]
else:
process_data = self.transform(images)
return process_data, torch.tensor(self.embeddings).float()
def __len__(self):
return len(self.df)