-
Notifications
You must be signed in to change notification settings - Fork 24
/
dataset_tools.py
317 lines (244 loc) · 9.7 KB
/
dataset_tools.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
import os
import sys
import os.path
import shutil
import sys
import cv2
import h5py
import random
from random import randrange
import numpy as np
from config import raw_path, dataset_path, dataset_img_size, processed_path, dataset_max_serie_length
from video_tools import get_blank_frame_diff, get_difference_frame
#Padding for LSTM as it appears that padding images is not working in TFLearn
#Not useful if sequences are manually padded (sliding window etc.)
def pad_lstm(sequences, maxlen=None, dtype='int32', padding='post', truncating='post', value=0.):
lengths = [len(s) for s in sequences]
nb_samples = len(sequences)
if maxlen is None:
maxlen = np.max(lengths)
#x = (np.ones((nb_samples, maxlen)) * value).astype(dtype)
x = (np.ones((nb_samples, maxlen, dataset_img_size, dataset_img_size, 1)) * value).astype(dtype)
for idx, s in enumerate(sequences):
if len(s) == 0:
continue # empty list was found
if truncating == 'pre':
trunc = s[-maxlen:]
elif truncating == 'post':
trunc = s[:maxlen]
else:
raise ValueError("Truncating type '%s' not understood" % padding)
if padding == 'post':
x[idx, :len(trunc)] = trunc
elif padding == 'pre':
x[idx, -len(trunc):] = trunc
else:
raise ValueError("Padding type '%s' not understood" % padding)
return x
def process(eye):
"""Processes the image"""
eye = cv2.resize(eye,(dataset_img_size, dataset_img_size), interpolation = cv2.INTER_CUBIC)
eye = cv2.equalizeHist(eye)
return eye
def save(eye, step, eyeNb, motion, destination_path):
"""Writes image at desired path"""
name = "{}_{}_{}.png".format(motion,eyeNb,step)
cv2.imwrite(destination_path + name,eye)
####### GENERATE PROCESSED & AUGMENTED DATASET FROM RAW #######
def generate_processed_imgs():
"""Reads from RAW and writes to PROCESSED"""
print("Generating processed images...")
folders = os.listdir(raw_path)
folders = [folder for folder in folders if os.path.isdir(raw_path+folder)]
for index,folder in enumerate(folders):
os.mkdir(processed_path + folder, exist_ok=True)
folder_nb = folder.split('_')[-1]
print("Processing folder {}/{}...".format(index+1,len(folders)))
if index+1 < len(folders): sys.stdout.write("\033[F")
#Copy skips file
skip_path = raw_path + folder + '/a_skips.txt'
newskip_path = processed_path + folder + '/a_skips.txt'
shutil.copy(skip_path,newskip_path)
files = os.listdir(raw_path + folder)
files = [file for file in files if file.endswith(".png")]
for filename in files:
motion_name, eyeNb, step = filename[:-4].split('_')
#Open and process image
eye = cv2.imread(raw_path+folder+"/"+filename,0)
processedEye = process(eye)
savePath = processed_path+"{}_{}/".format(motion_name,folder_nb)
#Write processed image on disk
save(processedEye,step,eyeNb,motion_name,savePath)
####### GENERATE (X,y) DATASET #########
def get_time_warped_dataset(X0, X1, y):
new_X0, new_X1, new_Y = [], [], []
#Foreach sample in the dataset
for i in range(len(y)):
label = y[i]
#Grab copie of series
X0_serie = list(X0[i])
X1_serie = list(X1[i])
#Keep initial samples
new_X0.append(np.array(X0_serie))
new_X1.append(np.array(X1_serie))
new_Y.append(label)
if 25 <= len(X0_serie) <= 49:
#Slowdown x0.5
X0_serie = [x for pair in zip(X0_serie, X0_serie) for x in pair]
X1_serie = [x for pair in zip(X1_serie, X1_serie) for x in pair]
#Add to dataset
new_X0.append(np.array(X0_serie))
new_X1.append(np.array(X1_serie))
new_Y.append(label)
elif 74 <= len(X0_serie) <= 100:
#Speedup x2
X0_serie = [X0_serie[speed_index] for speed_index in range(len(X0_serie)) if speed_index%2 == 0]
X1_serie = [X1_serie[speed_index] for speed_index in range(len(X1_serie)) if speed_index%2 == 0]
#Add to dataset
new_X0.append(np.array(X0_serie))
new_X1.append(np.array(X1_serie))
new_Y.append(label)
return new_X0, new_X1, new_Y
def get_sliding_padded_dataset(X0, X1, y, missing_paddings):
"""Pad beginning/end/both to max length"""
blank_frame = get_blank_frame_diff()
#We don't keep old samples
new_X0, new_X1, new_Y = [], [], []
#Foreach sample in the dataset
for i in range(len(y)):
label = y[i]
missing_frames = dataset_max_serie_length - len(X0[i])
shift_size = float(missing_frames) / missing_paddings
#Add N sliding padded examples
for shift_index in range(missing_paddings + 1):
#Initialize blank full size series
X0_serie = [get_blank_frame_diff() for _ in range(dataset_max_serie_length)]
X1_serie = [get_blank_frame_diff() for _ in range(dataset_max_serie_length)]
#Change slice of serie
X0_serie[int(shift_index * shift_size):int(shift_index * shift_size) + len(X0[i])] = X0[i]
X1_serie[int(shift_index * shift_size):int(shift_index * shift_size) + len(X0[i])] = X1[i]
#Add to final dataset
new_X0.append(np.array(X0_serie))
new_X1.append(np.array(X1_serie))
new_Y.append(label)
return new_X0, new_X1, new_Y
def get_random_padded_dataset(X0, X1, y, padded_examples=2):
blank_frame = get_blank_frame_diff()
#We don't keep old samples
new_X0, new_X1, new_Y = [], [], []
#Foreach sample in the dataset
for i in range(len(y)):
label = y[i]
#Add N padded examples
for _ in range(padded_examples):
#Grab copie of series
X0_serie = list(X0[i])
X1_serie = list(X1[i])
#Add blank frames until reaching padded length
while len(X0_serie) < dataset_max_serie_length:
random_index = randrange(0, len(X0_serie))
X0_serie.insert(random_index, blank_frame)
X1_serie.insert(random_index, blank_frame)
#Add to final dataset
new_X0.append(np.array(X0_serie))
new_X1.append(np.array(X1_serie))
new_Y.append(label)
return new_X0, new_X1, new_Y
#Creates pickle/HDF5 dataset (X,y)
def generate_dataset(motions, random_padding=False, cropLength=False, speedUp=False):
print("Generating dataset...")
X0, X1, y = [], [], []
#List processed folders
folders = os.listdir(processed_path)
folders = [folder for folder in folders if os.path.isdir(processed_path + folder)]
for index, folder in enumerate(folders):
#Extract ordered stop points
with open(processed_path + folder + "/a_skips.txt") as f:
lines = [line.strip() for line in f.readlines()]
stop_points = [int(line.split('_')[-1]) for line in lines]
seen = set()
seen_add = seen.add
stop_points = [x for x in stop_points if not (x in seen or seen_add(x)) and x != 0]
print("Extracting data for folder {}/{}...".format(index+1,len(folders)))
if index + 1 < len(folders): sys.stdout.write("\033[F")
motion_name, folder_nb = folder.split('_')
label = [1. if motion_name == motion else 0. for motion in motions]
files = os.listdir(processed_path+folder)
files = [file for file in files if file.endswith(".png")]
max_step = max([int(filename[:-4].split('_')[-1]) for filename in files])
X0_serie, X1_serie = [], []
for step in range(max_step + 1):
eye_0_path = processed_path+folder+"/{}_{}_{}.png".format(motion_name,0,step)
eye_1_path = processed_path+folder+"/{}_{}_{}.png".format(motion_name,1,step)
#Add first eye image
eye_0_img = cv2.imread(eye_0_path,0).astype(float)
eye_0Data = np.reshape(eye_0_img,[dataset_img_size,dataset_img_size,1])
X0_serie.append(eye_0Data)
#Add second eye image
eye_1_img = cv2.imread(eye_1_path,0).astype(float)
eye_1Data = np.reshape(eye_1_img,[dataset_img_size,dataset_img_size,1])
X1_serie.append(eye_1Data)
#Compute frame difference instead of frames
X0_serie_diff = []
X1_serie_diff = []
#Compute diff
for step in range(1,len(X0_serie)):
#Avoid shifts of bounding box
if step not in stop_points:
eye_0_t0 = X0_serie[step-1]
eye_0_t1 = X0_serie[step]
diff0 = get_difference_frame(eye_0_t1, eye_0_t0);
X0_serie_diff.append(diff0)
eye_1_t0 = X1_serie[step-1]
eye_1_t1 = X1_serie[step]
diff1 = get_difference_frame(eye_1_t1, eye_1_t0);
X1_serie_diff.append(diff1)
#Pretty display -- no computation here --
# from video_tools import show_difference
# current = np.hstack((eye_0_t1.astype(np.uint8),eye_1_t1.astype(np.uint8)))
# last = np.hstack((eye_0_t0.astype(np.uint8),eye_1_t0.astype(np.uint8)))
# show_difference(current,last)
# cv2.waitKey(0)
#Add to dataset
X0.append(np.array(X0_serie_diff))
X1.append(np.array(X1_serie_diff))
y.append(label)
#Now we have loaded all images diffs into X0, X1
#Manual time warping
print(len(X0), "examples of shape", X0[0].shape)
print("Time warping dataset...")
X0, X1, y = get_time_warped_dataset(X0, X1, y)
print("Generated dataset of size {} x {}".format(len(X0),X0[0].shape))
print(len(X0), "examples of shape", X0[0].shape)
print("Padding dataset (sliding)...")
X0, X1, y = get_sliding_padded_dataset(X0, X1, y, 8)
print("Generated dataset of size {} x {}".format(len(X0),X0[0].shape))
#Shuffle dataset
data = zip(X0,X1,y)
random.shuffle(data)
X0, X1, y = map(list,zip(*data))
# print("Saving dataset with pickle... - this may take a while")
# print("Saving X0...")
# pickle.dump(X0, open(dataset_path+"X0_hd_slide.p", "wb" ))
# print("Saving X1...")
# pickle.dump(X1, open(dataset_path+"X1_hd_slide.p", "wb" ))
# print("Saving y...")
# pickle.dump(y, open(dataset_path+"y_hd_slide.p", "wb" ))
#Save at hdf5 format
print("Saving dataset in hdf5 format... - this may take a while")
h5f = h5py.File(dataset_path+"data.h5", 'w')
h5f.create_dataset('eye_X0', data=X0)
h5f.create_dataset('eye_X1', data=X1)
h5f.create_dataset('eye_Y', data=y)
h5f.close()
if __name__ == "__main__":
motions = ["gamma","z","idle"]
arg = sys.argv[1] if len(sys.argv) == 2 else None
if arg is None:
print "Need argument"
elif arg == "process":
generate_processed_imgs()
elif arg == "dataset":
generate_dataset(motions, random_padding=True, cropLength=False, speedUp=False)
else:
print "Wrong argument"