-
Notifications
You must be signed in to change notification settings - Fork 49
/
dataset.py
449 lines (383 loc) · 20.1 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
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
import torch
from torch.utils.data.dataset import Dataset
from torchvision import transforms
import torchvision.transforms.functional as TF
import numpy as np
from PIL import Image, ImageFilter, ImageDraw
import pandas as pd
import matplotlib as mpl
mpl.use('Agg')
from matplotlib import cm
import matplotlib.pyplot as plt
from scipy.misc import imresize
import os
import glob
import csv
from utils import imutils
from utils import myutils
from config import *
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
class GazeFollow(Dataset):
def __init__(self, data_dir, csv_path, transform, input_size=input_resolution, output_size=output_resolution,
test=False, imshow=False):
if test:
column_names = ['path', 'idx', 'body_bbox_x', 'body_bbox_y', 'body_bbox_w', 'body_bbox_h', 'eye_x', 'eye_y',
'gaze_x', 'gaze_y', 'bbox_x_min', 'bbox_y_min', 'bbox_x_max', 'bbox_y_max', 'meta']
df = pd.read_csv(csv_path, sep=',', names=column_names, index_col=False, encoding="utf-8-sig")
df = df[['path', 'eye_x', 'eye_y', 'gaze_x', 'gaze_y', 'bbox_x_min', 'bbox_y_min', 'bbox_x_max',
'bbox_y_max']].groupby(['path', 'eye_x'])
self.keys = list(df.groups.keys())
self.X_test = df
self.length = len(self.keys)
else:
column_names = ['path', 'idx', 'body_bbox_x', 'body_bbox_y', 'body_bbox_w', 'body_bbox_h', 'eye_x', 'eye_y',
'gaze_x', 'gaze_y', 'bbox_x_min', 'bbox_y_min', 'bbox_x_max', 'bbox_y_max', 'inout', 'meta']
df = pd.read_csv(csv_path, sep=',', names=column_names, index_col=False, encoding="utf-8-sig")
df = df[df['inout'] != -1] # only use "in" or "out "gaze. (-1 is invalid, 0 is out gaze)
df.reset_index(inplace=True)
self.y_train = df[['bbox_x_min', 'bbox_y_min', 'bbox_x_max', 'bbox_y_max', 'eye_x', 'eye_y', 'gaze_x',
'gaze_y', 'inout']]
self.X_train = df['path']
self.length = len(df)
self.data_dir = data_dir
self.transform = transform
self.test = test
self.input_size = input_size
self.output_size = output_size
self.imshow = imshow
def __getitem__(self, index):
if self.test:
g = self.X_test.get_group(self.keys[index])
cont_gaze = []
for i, row in g.iterrows():
path = row['path']
x_min = row['bbox_x_min']
y_min = row['bbox_y_min']
x_max = row['bbox_x_max']
y_max = row['bbox_y_max']
eye_x = row['eye_x']
eye_y = row['eye_y']
gaze_x = row['gaze_x']
gaze_y = row['gaze_y']
cont_gaze.append([gaze_x, gaze_y]) # all ground truth gaze are stacked up
for j in range(len(cont_gaze), 20):
cont_gaze.append([-1, -1]) # pad dummy gaze to match size for batch processing
cont_gaze = torch.FloatTensor(cont_gaze)
gaze_inside = True # always consider test samples as inside
else:
path = self.X_train.iloc[index]
x_min, y_min, x_max, y_max, eye_x, eye_y, gaze_x, gaze_y, inout = self.y_train.iloc[index]
gaze_inside = bool(inout)
# expand face bbox a bit
k = 0.1
x_min -= k * abs(x_max - x_min)
y_min -= k * abs(y_max - y_min)
x_max += k * abs(x_max - x_min)
y_max += k * abs(y_max - y_min)
img = Image.open(os.path.join(self.data_dir, path))
img = img.convert('RGB')
width, height = img.size
x_min, y_min, x_max, y_max = map(float, [x_min, y_min, x_max, y_max])
if self.imshow:
img.save("origin_img.jpg")
if self.test:
imsize = torch.IntTensor([width, height])
else:
## data augmentation
# Jitter (expansion-only) bounding box size
if np.random.random_sample() <= 0.5:
k = np.random.random_sample() * 0.2
x_min -= k * abs(x_max - x_min)
y_min -= k * abs(y_max - y_min)
x_max += k * abs(x_max - x_min)
y_max += k * abs(y_max - y_min)
# Random Crop
if np.random.random_sample() <= 0.5:
# Calculate the minimum valid range of the crop that doesn't exclude the face and the gaze target
crop_x_min = np.min([gaze_x * width, x_min, x_max])
crop_y_min = np.min([gaze_y * height, y_min, y_max])
crop_x_max = np.max([gaze_x * width, x_min, x_max])
crop_y_max = np.max([gaze_y * height, y_min, y_max])
# Randomly select a random top left corner
if crop_x_min >= 0:
crop_x_min = np.random.uniform(0, crop_x_min)
if crop_y_min >= 0:
crop_y_min = np.random.uniform(0, crop_y_min)
# Find the range of valid crop width and height starting from the (crop_x_min, crop_y_min)
crop_width_min = crop_x_max - crop_x_min
crop_height_min = crop_y_max - crop_y_min
crop_width_max = width - crop_x_min
crop_height_max = height - crop_y_min
# Randomly select a width and a height
crop_width = np.random.uniform(crop_width_min, crop_width_max)
crop_height = np.random.uniform(crop_height_min, crop_height_max)
# Crop it
img = TF.crop(img, crop_y_min, crop_x_min, crop_height, crop_width)
# Record the crop's (x, y) offset
offset_x, offset_y = crop_x_min, crop_y_min
# convert coordinates into the cropped frame
x_min, y_min, x_max, y_max = x_min - offset_x, y_min - offset_y, x_max - offset_x, y_max - offset_y
# if gaze_inside:
gaze_x, gaze_y = (gaze_x * width - offset_x) / float(crop_width), \
(gaze_y * height - offset_y) / float(crop_height)
# else:
# gaze_x = -1; gaze_y = -1
width, height = crop_width, crop_height
# Random flip
if np.random.random_sample() <= 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
x_max_2 = width - x_min
x_min_2 = width - x_max
x_max = x_max_2
x_min = x_min_2
gaze_x = 1 - gaze_x
# Random color change
if np.random.random_sample() <= 0.5:
img = TF.adjust_brightness(img, brightness_factor=np.random.uniform(0.5, 1.5))
img = TF.adjust_contrast(img, contrast_factor=np.random.uniform(0.5, 1.5))
img = TF.adjust_saturation(img, saturation_factor=np.random.uniform(0, 1.5))
head_channel = imutils.get_head_box_channel(x_min, y_min, x_max, y_max, width, height,
resolution=self.input_size, coordconv=False).unsqueeze(0)
# Crop the face
face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))
if self.imshow:
img.save("img_aug.jpg")
face.save('face_aug.jpg')
if self.transform is not None:
img = self.transform(img)
face = self.transform(face)
# generate the heat map used for deconv prediction
gaze_heatmap = torch.zeros(self.output_size, self.output_size) # set the size of the output
if self.test: # aggregated heatmap
num_valid = 0
for gaze_x, gaze_y in cont_gaze:
if gaze_x != -1:
num_valid += 1
gaze_heatmap = imutils.draw_labelmap(gaze_heatmap, [gaze_x * self.output_size, gaze_y * self.output_size],
3,
type='Gaussian')
gaze_heatmap /= num_valid
else:
# if gaze_inside:
gaze_heatmap = imutils.draw_labelmap(gaze_heatmap, [gaze_x * self.output_size, gaze_y * self.output_size],
3,
type='Gaussian')
if self.imshow:
fig = plt.figure(111)
img = 255 - imutils.unnorm(img.numpy()) * 255
img = np.clip(img, 0, 255)
plt.imshow(np.transpose(img, (1, 2, 0)))
plt.imshow(imresize(gaze_heatmap, (self.input_size, self.input_size)), cmap='jet', alpha=0.3)
plt.imshow(imresize(1 - head_channel.squeeze(0), (self.input_size, self.input_size)), alpha=0.2)
plt.savefig('viz_aug.png')
if self.test:
return img, face, head_channel, gaze_heatmap, cont_gaze, imsize, path
else:
return img, face, head_channel, gaze_heatmap, path, gaze_inside
def __len__(self):
return self.length
class VideoAttTarget_video(Dataset):
def __init__(self, data_dir, annotation_dir, transform, input_size=input_resolution, output_size=output_resolution,
test=False, imshow=False, seq_len_limit=400):
shows = glob.glob(os.path.join(annotation_dir, '*'))
self.all_sequence_paths = []
for s in shows:
sequence_annotations = glob.glob(os.path.join(s, '*', '*.txt'))
self.all_sequence_paths.extend(sequence_annotations)
self.data_dir = data_dir
self.transform = transform
self.input_size = input_size
self.output_size = output_size
self.test = test
self.imshow = imshow
self.length = len(self.all_sequence_paths)
self.seq_len_limit = seq_len_limit
def __getitem__(self, index):
sequence_path = self.all_sequence_paths[index]
df = pd.read_csv(sequence_path, header=None, index_col=False,
names=['path', 'xmin', 'ymin', 'xmax', 'ymax', 'gazex', 'gazey'])
show_name = sequence_path.split('/')[-3]
clip = sequence_path.split('/')[-2]
seq_len = len(df.index)
# moving-avg smoothing
window_size = 11 # should be odd number
df['xmin'] = myutils.smooth_by_conv(window_size, df, 'xmin')
df['ymin'] = myutils.smooth_by_conv(window_size, df, 'ymin')
df['xmax'] = myutils.smooth_by_conv(window_size, df, 'xmax')
df['ymax'] = myutils.smooth_by_conv(window_size, df, 'ymax')
if not self.test:
# cond for data augmentation
cond_jitter = np.random.random_sample()
cond_flip = np.random.random_sample()
cond_color = np.random.random_sample()
if cond_color < 0.5:
n1 = np.random.uniform(0.5, 1.5)
n2 = np.random.uniform(0.5, 1.5)
n3 = np.random.uniform(0.5, 1.5)
cond_crop = np.random.random_sample()
# if longer than seq_len_limit, cut it down to the limit with the init index randomly sampled
if seq_len > self.seq_len_limit:
sampled_ind = np.random.randint(0, seq_len - self.seq_len_limit)
seq_len = self.seq_len_limit
else:
sampled_ind = 0
if cond_crop < 0.5:
sliced_x_min = df['xmin'].iloc[sampled_ind:sampled_ind+seq_len]
sliced_x_max = df['xmax'].iloc[sampled_ind:sampled_ind+seq_len]
sliced_y_min = df['ymin'].iloc[sampled_ind:sampled_ind+seq_len]
sliced_y_max = df['ymax'].iloc[sampled_ind:sampled_ind+seq_len]
sliced_gaze_x = df['gazex'].iloc[sampled_ind:sampled_ind+seq_len]
sliced_gaze_y = df['gazey'].iloc[sampled_ind:sampled_ind+seq_len]
check_sum = sliced_gaze_x.sum() + sliced_gaze_y.sum()
all_outside = check_sum == -2*seq_len
# Calculate the minimum valid range of the crop that doesn't exclude the face and the gaze target
if all_outside:
crop_x_min = np.min([sliced_x_min.min(), sliced_x_max.min()])
crop_y_min = np.min([sliced_y_min.min(), sliced_y_max.min()])
crop_x_max = np.max([sliced_x_min.max(), sliced_x_max.max()])
crop_y_max = np.max([sliced_y_min.max(), sliced_y_max.max()])
else:
crop_x_min = np.min([sliced_gaze_x.min(), sliced_x_min.min(), sliced_x_max.min()])
crop_y_min = np.min([sliced_gaze_y.min(), sliced_y_min.min(), sliced_y_max.min()])
crop_x_max = np.max([sliced_gaze_x.max(), sliced_x_min.max(), sliced_x_max.max()])
crop_y_max = np.max([sliced_gaze_y.max(), sliced_y_min.max(), sliced_y_max.max()])
# Randomly select a random top left corner
if crop_x_min >= 0:
crop_x_min = np.random.uniform(0, crop_x_min)
if crop_y_min >= 0:
crop_y_min = np.random.uniform(0, crop_y_min)
# Get image size
path = os.path.join(self.data_dir, show_name, clip, df['path'].iloc[0])
img = Image.open(path)
img = img.convert('RGB')
width, height = img.size
# Find the range of valid crop width and height starting from the (crop_x_min, crop_y_min)
crop_width_min = crop_x_max - crop_x_min
crop_height_min = crop_y_max - crop_y_min
crop_width_max = width - crop_x_min
crop_height_max = height - crop_y_min
# Randomly select a width and a height
crop_width = np.random.uniform(crop_width_min, crop_width_max)
crop_height = np.random.uniform(crop_height_min, crop_height_max)
else:
sampled_ind = 0
faces, images, head_channels, heatmaps, paths, gazes, imsizes, gaze_inouts = [], [], [], [], [], [], [], []
index_tracker = -1
for i, row in df.iterrows():
index_tracker = index_tracker+1
if not self.test:
if index_tracker < sampled_ind or index_tracker >= (sampled_ind + self.seq_len_limit):
continue
face_x1 = row['xmin'] # note: Already in image coordinates
face_y1 = row['ymin'] # note: Already in image coordinates
face_x2 = row['xmax'] # note: Already in image coordinates
face_y2 = row['ymax'] # note: Already in image coordinates
gaze_x = row['gazex'] # note: Already in image coordinates
gaze_y = row['gazey'] # note: Already in image coordinates
impath = os.path.join(self.data_dir, show_name, clip, row['path'])
img = Image.open(impath)
img = img.convert('RGB')
width, height = img.size
imsize = torch.FloatTensor([width, height])
# imsizes.append(imsize)
face_x1, face_y1, face_x2, face_y2 = map(float, [face_x1, face_y1, face_x2, face_y2])
gaze_x, gaze_y = map(float, [gaze_x, gaze_y])
if gaze_x == -1 and gaze_y == -1:
gaze_inside = False
else:
if gaze_x < 0: # move gaze point that was sliglty outside the image back in
gaze_x = 0
if gaze_y < 0:
gaze_y = 0
gaze_inside = True
if not self.test:
## data augmentation
# Jitter (expansion-only) bounding box size.
if cond_jitter < 0.5:
k = cond_jitter * 0.1
face_x1 -= k * abs(face_x2 - face_x1)
face_y1 -= k * abs(face_y2 - face_y1)
face_x2 += k * abs(face_x2 - face_x1)
face_y2 += k * abs(face_y2 - face_y1)
face_x1 = np.clip(face_x1, 0, width)
face_x2 = np.clip(face_x2, 0, width)
face_y1 = np.clip(face_y1, 0, height)
face_y2 = np.clip(face_y2, 0, height)
# Random Crop
if cond_crop < 0.5:
# Crop it
img = TF.crop(img, crop_y_min, crop_x_min, crop_height, crop_width)
# Record the crop's (x, y) offset
offset_x, offset_y = crop_x_min, crop_y_min
# convert coordinates into the cropped frame
face_x1, face_y1, face_x2, face_y2 = face_x1 - offset_x, face_y1 - offset_y, face_x2 - offset_x, face_y2 - offset_y
if gaze_inside:
gaze_x, gaze_y = (gaze_x- offset_x), \
(gaze_y - offset_y)
else:
gaze_x = -1; gaze_y = -1
width, height = crop_width, crop_height
# Flip?
if cond_flip < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
x_max_2 = width - face_x1
x_min_2 = width - face_x2
face_x2 = x_max_2
face_x1 = x_min_2
if gaze_x != -1 and gaze_y != -1:
gaze_x = width - gaze_x
# Random color change
if cond_color < 0.5:
img = TF.adjust_brightness(img, brightness_factor=n1)
img = TF.adjust_contrast(img, contrast_factor=n2)
img = TF.adjust_saturation(img, saturation_factor=n3)
# Face crop
face = img.copy().crop((int(face_x1), int(face_y1), int(face_x2), int(face_y2)))
# Head channel image
head_channel = imutils.get_head_box_channel(face_x1, face_y1, face_x2, face_y2, width, height,
resolution=self.input_size, coordconv=False).unsqueeze(0)
if self.transform is not None:
img = self.transform(img)
face = self.transform(face)
# Deconv output
if gaze_inside:
gaze_x /= float(width) # fractional gaze
gaze_y /= float(height)
gaze_heatmap = torch.zeros(self.output_size, self.output_size) # set the size of the output
gaze_map = imutils.draw_labelmap(gaze_heatmap, [gaze_x * self.output_size, gaze_y * self.output_size],
3,
type='Gaussian')
gazes.append(torch.FloatTensor([gaze_x, gaze_y]))
else:
gaze_map = torch.zeros(self.output_size, self.output_size)
gazes.append(torch.FloatTensor([-1, -1]))
faces.append(face)
images.append(img)
head_channels.append(head_channel)
heatmaps.append(gaze_map)
gaze_inouts.append(torch.FloatTensor([int(gaze_inside)]))
if self.imshow:
for i in range(len(faces)):
fig = plt.figure(111)
img = 255 - imutils.unnorm(images[i].numpy()) * 255
img = np.clip(img, 0, 255)
plt.imshow(np.transpose(img, (1, 2, 0)))
plt.imshow(imresize(heatmaps[i], (self.input_size, self.input_size)), cmap='jet', alpha=0.3)
plt.imshow(imresize(1 - head_channels[i].squeeze(0), (self.input_size, self.input_size)), alpha=0.2)
plt.savefig(os.path.join('debug', 'viz_%d_inout=%d.png' % (i, gaze_inouts[i])))
plt.close('all')
faces = torch.stack(faces)
images = torch.stack(images)
head_channels = torch.stack(head_channels)
heatmaps = torch.stack(heatmaps)
gazes = torch.stack(gazes)
gaze_inouts = torch.stack(gaze_inouts)
# imsizes = torch.stack(imsizes)
# print(faces.shape, images.shape, head_channels.shape, heatmaps.shape)
if self.test:
return images, faces, head_channels, heatmaps, gazes, gaze_inouts
else: # train
return images, faces, head_channels, heatmaps, gaze_inouts
def __len__(self):
return self.length