-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_process.py
360 lines (275 loc) · 12.6 KB
/
data_process.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
import torch
import torch.nn.parallel
import torch.utils.data
import numpy as np
import os
import config
import suppor_lib
import pickle
import pickle as pck
'''
Description: data_process.py is used to obtain Ground-truth scanpaths
for model training and validation. We give an example of how we process
Sitzmann dataset (https://github.com/vsitzmann/vr-saliency).
We provide ready-to-use training & validation data in './Datasets/Sitzmann.pkl'
You can also follow the 3 steps to reproduce the dataset:
# 1. Rotate 360-degree images for data augmentation using:
suppor_lib.rotate_images(input_path, output_path)
# 2. Modify the configure in config.py
# 3. Modify and run data_process.py.
Data format:
[data]
['train']
['image1_name']
['image']: Tensor[3, 128, 256]
['scanpaths']: Tensor[n_scanpath, n_gaze_point, 3] # (x, y, z) for the 3-th dimension
['image2_name']
...
['test']
['imageN_name']
...
['info']
['train']: {
'num_image': int,
'num_scanpath': int,
'scanpath_length': int,
'max_scan_length': int
}
['test']: {
...
}
'''
def save_file(file_name, data):
with open(file_name, 'wb') as f:
pickle.dump(data, f)
f.close()
def load_logfile(path):
log = pck.load(open(path, 'rb'), encoding='latin1')
return log
def twoDict(pack, key_a, key_b, data):
if key_a in pack:
pack[key_a].update({key_b: data})
else:
pack.update({key_a: {key_b: data}})
return pack
def create_info():
info = {
'train': {
'num_image': 0,
'num_scanpath': 0,
'scanpath_length': 0,
'max_scan_length': 0
},
'test': {
'num_image': 0,
'num_scanpath': 0,
'scanpath_length': 0,
'max_scan_length': 0
}
}
return info
def summary(info):
print("\n============================================")
print("Train_set: {} images, {} scanpaths, length ={}".
format(info['train']['num_image'], info['train']['num_scanpath'], info['train']['scanpath_length']))
print("Test_set: {} images, {} scanpaths, length ={}".
format(info['test']['num_image'], info['test']['num_scanpath'], info['test']['scanpath_length']))
print("============================================\n")
def forward(database_name: str):
if not os.path.exists('Datasets'):
os.makedirs('Datasets')
print('\nBegin process {} database'.format(database_name))
if database_name == 'Sitzmann':
data = Sitzmann_Dataset()
dic = data.run()
save_file('./Datasets/Sitzmann.pkl', dic)
summary(dic['info'])
else:
print('\nYou need to prepare the code for {} data processing'.format(database_name))
class Sitzmann_Dataset():
def __init__(self):
super().__init__()
self.images_path = config.dic_Sitzmann['IMG_PATH']
self.gaze_path = config.dic_Sitzmann['GAZE_PATH']
self.test_set = config.dic_Sitzmann['TEST_SET']
self.duration = 30
self.info = create_info()
self.images_test_list = []
self.images_train_list = []
self.image_and_scanpath_dict = {}
def mod(self, a, b):
c = a // b
r = a - c * b
return r
def rotate(self, lat_lon, angle):
# We convert [-180, 180] to [0, 360], then compute the new longitude.
# We ``minus`` the angle here, which is different from what we do in rotating images,
# because ffmepeg has a different coordination. For example, set ``yaw=60`` in ffmepg
# equate to longitude = -60 + longitude
new_lon = self.mod(lat_lon[:, 1] + 180 - angle, 360) - 180
rotate_lat_lon = lat_lon
rotate_lat_lon[:, 1] = new_lon
return rotate_lat_lon
def handle_empty(self, sphere_coords):
empty_index = np.where(sphere_coords[:, 0] == -999)[0]
throw = False
for _index in range(empty_index.shape[0]):
# if not throw the scanpath of this user
if not throw:
# if the first one second is empty
if empty_index[_index] == 0:
# if the next second is not empty
if sphere_coords[empty_index[_index] + 1, 0] != -999:
sphere_coords[empty_index[_index], 0] = sphere_coords[empty_index[_index] + 1, 0]
sphere_coords[empty_index[_index], 1] = sphere_coords[empty_index[_index] + 1, 1]
else:
throw = True
# print(" Too many invalid gaze points !! {}".format(empty_index))
# if the last one second is empty
elif empty_index[_index] == (self.duration - 1):
sphere_coords[empty_index[_index], 0] = sphere_coords[empty_index[_index] - 1, 0]
sphere_coords[empty_index[_index], 1] = sphere_coords[empty_index[_index] - 1, 1]
else:
prev_x = sphere_coords[empty_index[_index] - 1, 1]
prev_y = sphere_coords[empty_index[_index] - 1, 0]
next_x = sphere_coords[empty_index[_index] + 1, 1]
next_y = sphere_coords[empty_index[_index] + 1, 0]
if prev_x == -999 or next_x == -999:
throw = True
# print(" Too many invalid gaze points !! {}".format(empty_index))
else:
" Interpolate on lat "
sphere_coords[empty_index[_index], 0] = 0.5 * (prev_y + next_y)
" Interpolate on lon "
# the maximum distance between two points on a sphere is pi
if np.abs(next_x - prev_x) <= 180:
sphere_coords[empty_index[_index], 1] = 0.5 * (prev_x + next_x)
# jump to another side
else:
true_distance = 360 - np.abs(next_x - prev_x)
if next_x > prev_x:
_temp = prev_x - true_distance / 2
if _temp < -180:
_temp = 360 + _temp
else:
_temp = prev_x + true_distance / 2
if _temp > 180:
_temp = _temp - 360
sphere_coords[empty_index[_index], 1] = _temp
return sphere_coords, throw
def sample_gaze_points(self, raw_data):
fixation_coords = []
samples_per_bin = raw_data.shape[0] // self.duration
bins = raw_data[:samples_per_bin * self.duration].reshape([self.duration, -1, 2])
for bin in range(self.duration):
" filter out invalid gaze points "
_fixation_coords = bins[bin, np.where((bins[bin, :, 0] != 0) & (bins[bin, :, 1] != 0))]
if _fixation_coords.shape[1] == 0:
" mark the empty set"
fixation_coords.append([-999, -999])
else:
" sample the first element in a set of one-second gaze points "
sample_vale = _fixation_coords[0, 0]
fixation_coords.append(sample_vale)
sphere_coords = np.vstack(fixation_coords) - [90, 180]
return sphere_coords
def get_train_set(self):
all_files = [os.path.join(self.gaze_path, self.images_train_list[i].split('/')[-1].split('.')[0][:-2] + '.pck')
for i in range(0, len(self.images_train_list), 6)]
runs_files = [load_logfile(logfile) for logfile in all_files]
image_id = 0
original_image_id = 0
for run in runs_files:
temple_gaze = np.zeros((len(run['data']), 30, 2))
scanpath_id = 0
" save original scanpath data to ``temple_gaze`` "
for data in run['data']:
relevant_fixations = data['gaze_lat_lon']
if len(relevant_fixations.shape) > 1:
sphere_coords = self.sample_gaze_points(relevant_fixations)
else:
continue
" handle invalid set"
sphere_coords, throw = self.handle_empty(sphere_coords)
if throw: # throw this scanpath if too many invalid values.
continue
else:
temple_gaze[scanpath_id] = torch.from_numpy(sphere_coords)
scanpath_id += 1
temple_gaze = temple_gaze[:scanpath_id]
original_image_id += 1
" rotate scanpaths "
for rotation_id in range(6):
image = suppor_lib.image_process(self.images_train_list[image_id])
gaze_ = np.zeros((temple_gaze.shape[0], 30, 3))
rotation_angle = rotation_id * 60 - 180
for scanpath_id in range(0, temple_gaze.shape[0]):
gaze_[scanpath_id] = suppor_lib.sphere2xyz(
torch.from_numpy(self.rotate(temple_gaze[scanpath_id], rotation_angle)))
self.info['train']['num_scanpath'] += 1
dic = {"image": image, "scanpaths": gaze_}
twoDict(self.image_and_scanpath_dict, "train",
self.images_train_list[image_id].split('/')[-1].split('.')[0],
dic)
print('Processing - {}. [Filter out {} scanpaths]'
.format(self.images_train_list[image_id].split('/')[-1],
len(run['data']) - scanpath_id - 1))
image_id += 1
self.info['train']['num_image'] = image_id
self.info['train']['scanpath_length'] = self.duration
def get_test_set(self):
all_files = [os.path.join(self.gaze_path, self.images_test_list[i].split('/')[-1].split('.')[0] + '.pck')
for i in range(len(self.images_test_list))]
runs_files = [load_logfile(logfile) for logfile in all_files]
image_id = 0
for run in runs_files:
scanpath_id = 0
gaze_ = np.zeros((len(run['data']), 30, 3))
image = suppor_lib.image_process(self.images_test_list[image_id])
for data in run['data']:
relevant_fixations = data['gaze_lat_lon']
if len(relevant_fixations.shape) > 1:
sphere_coords = self.sample_gaze_points(relevant_fixations)
else:
continue
" handle invalid set"
sphere_coords, throw = self.handle_empty(sphere_coords)
if throw: # throw this scanpath if too many invalid values.
continue
else:
sphere_coords = torch.from_numpy(sphere_coords.copy())
gaze_[scanpath_id] = suppor_lib.sphere2xyz(sphere_coords)
scanpath_id += 1
self.info['test']['num_scanpath'] += 1
gaze = gaze_[:scanpath_id]
dic = {"image": image, "scanpaths": gaze}
twoDict(self.image_and_scanpath_dict, "test",
self.images_test_list[image_id].split('/')[-1].split('.')[0],
dic)
print('Processing - {}. [Filter out {} scanpaths]'
.format(self.images_test_list[image_id].split('/')[-1], gaze_.shape[0] - scanpath_id))
image_id += 1
self.info['test']['num_image'] = image_id
self.info['test']['scanpath_length'] = self.duration
def run(self):
''
' PATH PREPARE '
for file_name in os.listdir(self.images_path):
if ".png" in file_name:
if file_name in self.test_set:
self.images_test_list.append(os.path.join(self.images_path, file_name))
else:
self.images_train_list.append(os.path.join(self.images_path, file_name))
' GET TRAINING SET '
print('\nProcessing [Training Set]\n')
self.get_train_set()
' GET TEST SET '
print('\nProcessing [Test Set]\n')
self.get_test_set()
' RECORD DATABASE INFORMATION '
self.image_and_scanpath_dict['info'] = self.info
return self.image_and_scanpath_dict
if __name__ == '__main__':
Datasets = ['Sitzmann']
for dataset in Datasets:
forward(dataset)