-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsimulation_data.py
462 lines (345 loc) · 16.8 KB
/
simulation_data.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
450
451
452
453
454
455
456
457
458
459
460
461
462
import csv
import scipy.misc
from random import shuffle
import cv2
from skimage.util import random_noise
from numpy.random import uniform as random
import numpy as np
class data_handler(object):
def __init__(self, validation_split = 0.2, batch_size = 128, left_and_right_images = False, root_path = '', left_right_offset = 0.2, test_root_path = '', test_left_and_right_images = False):
# Name of file where metadata is present
filename = 'driving_log.csv'
test_filename = 'test_driving_log.csv'
self.left_and_right_images = left_and_right_images
self.left_right_offset = left_right_offset
self.metadata = []
# loading metadata
with open(filename, 'r') as f:
reader = csv.reader(f)
i = 0
for row in reader:
self.metadata.append(row)
# removing first row if it has column names
if(self.metadata[0][0]=='center'):
self.metadata.reverse()
self.metadata.pop()
self.metadata.reverse()
# shuffle the training data
shuffle(self.metadata)
self.test_metadata = []
# loading metadata
with open(test_filename, 'r') as f:
reader = csv.reader(f)
i = 0
for row in reader:
self.test_metadata.append(row)
# removing first row if it has column names
if(self.test_metadata[0][0]=='center'):
self.test_metadata.reverse()
self.test_metadata.pop()
self.test_metadata.reverse()
# splitting into training and validation set
if(validation_split<1.0):
self.metadata_train = self.metadata[0:int((1-validation_split)*len(self.metadata))]
if(not validation_split==0):
self.metadata_val = self.metadata[int((1-validation_split)*len(self.metadata)):]
else:
print("Validation split can't be 1.")
raise Exception("Validation split not valid.")
# setting batch size
self.batch_size = batch_size
# setting current training step (in the beginning we are at the 0th step)
self.step_train = 0
# setting current validation step (in the beginning we are at the 0th step)
self.step_val = 0
# setting current validation step (in the beginning we are at the 0th test step)
self.step_test = 0
# root path of images
self.root_path = root_path
# root path of test images
self.test_root_path = test_root_path
# left and right images for
self.test_left_and_right_images = test_left_and_right_images
def generate_train_batch(self):
while 1:
X_train = []
y_train = []
# start and end of current batch
start = self.step_train*self.batch_size
end = (self.step_train+1)*self.batch_size
# if number of training samples are not a multiple of batch size
if(end>=len(self.metadata_train)):
end = len(self.metadata_train)
# restart from the beginning
self.step_train = 0
shuffle(self.metadata_train)
# load images and steering angles for current batch
for j in range(start,end,1):
if(not self.metadata_train[j][0][0] == 'C'):
center_path = self.root_path+self.metadata_train[j][0]
else:
center_path = self.metadata_train[j][0]
center_steer = [float(self.metadata_train[j][3])]
# X_train.append(self.get_image(self.root_path+self.metadata_train[j][0]))
# y_train.append([float(self.metadata_train[j][3])])
center_image, center_steer[0] = self.get_image_and_steering(center_path,center_steer[0])
X_train.append(center_image)
y_train.append(center_steer)
if(self.left_and_right_images):
if(self.metadata_train[j][1][0] == ' ' and not self.metadata_train[j][1][1]=='C'):
left_path = self.root_path+self.metadata_train[j][1][1:]
elif(self.metadata_train[j][1][0] == ' ' and self.metadata_train[j][1][1]=='C'):
left_path = self.metadata_train[j][1][1:]
elif(self.metadata_train[j][1][0] == 'C'):
left_path = self.metadata_train[j][1]
else:
left_path = self.root_path + self.metadata_train[j][1]
left_steer = [float(self.metadata_train[j][3])+self.left_right_offset]
if(self.metadata_train[j][2][0] == ' ' and not self.metadata_train[j][2][1]=='C'):
right_path = self.root_path+self.metadata_train[j][2][1:]
elif(self.metadata_train[j][2][0] == ' ' and self.metadata_train[j][2][1]=='C'):
right_path = self.metadata_train[j][2][1:]
elif(self.metadata_train[j][2][0] == 'C'):
right_path = self.metadata_train[j][2]
else:
right_path = self.root_path + self.metadata_train[j][2]
right_steer = [float(self.metadata_train[j][3])-self.left_right_offset]
left_image, left_steer[0] = self.get_image_and_steering(left_path, left_steer[0])
right_image, right_steer[0] = self.get_image_and_steering(right_path, right_steer[0])
X_train.append(left_image)
y_train.append(left_steer)
X_train.append(right_image)
y_train.append(right_steer)
# X_train.append(self.get_image(self.root_path+self.metadata_train[j][1][1:]))
# y_train.append([float(self.metadata_train[j][3])+self.left_right_offset])
# X_train.append(self.get_image(self.root_path+self.metadata_train[j][2][1:]))
# y_train.append([float(self.metadata_train[j][3])-self.left_right_offset])
# incrementing step
self.step_train = self.step_train + 1
yield (X_train, y_train)
def generate_validation_batch(self):
while 1:
X_val = []
y_val = []
# start and end of current batch
start = self.step_val*self.batch_size
end = (self.step_val+1)*self.batch_size
# if number of validation samples are not a multiple of batch size
if(end>=len(self.metadata_val)):
end = len(self.metadata_val)
# restart from the beginning
self.step_val = 0
shuffle(self.metadata_val)
# laod images and steering angles for current batch
for j in range(start,end):
if(not self.metadata_val[j][0][0] == 'C'):
center_path = self.root_path+self.metadata_val[j][0]
else:
center_path = self.metadata_val[j][0]
center_steer = [float(self.metadata_val[j][3])]
# X_val.append(self.get_image(self.root_path+self.metadata_val[j][0]))
# y_val.append([float(self.metadata_val[j][3])])
center_image, center_steer[0] = self.get_image_and_steering(center_path, center_steer[0])
X_val.append(center_image)
y_val.append(center_steer)
if(self.left_and_right_images):
if(self.metadata_val[j][1][0]==' ' and not self.metadata_val[j][1][1] == 'C'):
path_left = self.root_path + self.metadata_val[j][1][1:]
elif(self.metadata_val[j][1][0]==' ' and self.metadata_val[j][1][1] == 'C'):
path_left = self.metadata_val[j][1][1:]
elif(self.metadata_val[j][1][0] == 'C'):
path_left = self.metadata_val[j][1]
else:
path_left = self.root_path + self.metadata_val[j][1]
steer_left = [float(self.metadata_val[j][3])+self.left_right_offset]
if(self.metadata_val[j][2][0] == ' ' and not self.metadata_val[j][2][1] == 'C'):
path_right = self.root_path+self.metadata_val[j][2][1:]
elif(self.metadata_val[j][2][0] == ' ' and self.metadata_val[j][2][1] == 'C'):
path_right = self.metadata_val[j][2][1:]
elif(self.metadata_val[j][2][0] == 'C'):
path_right = self.metadata_val[j][2]
else:
path_right = self.root_path+self.metadata_val[j][2]
steer_right = [float(self.metadata_val[j][3])-self.left_right_offset]
image_left, steer_left[0] = self.get_image_and_steering(path_left,steer_left[0])
image_right, steer_right[0] = self.get_image_and_steering(path_right, steer_right[0])
X_val.append(image_left)
y_val.append(steer_left)
X_val.append(image_right)
y_val.append(steer_right)
#
# X_val.append(self.get_image(self.root_path+self.metadata_train[j][1][1:]))
# y_val.append([float(self.metadata_train[j][3])+self.left_right_offset])
# X_val.append(self.get_image(self.root_path+self.metadata_train[j][2][1:]))
# y_val.append([float(self.metadata_train[j][3])-self.left_right_offset])
# incrementing step
self.step_val = self.step_val + 1
yield (X_val, y_val)
def generate_test_batch(self):
while 1:
X_test = []
y_test = []
start = self.step_test*self.batch_size
end = (self.step_test+1)*self.batch_size
if(end >= len(self.test_metadata)):
end = len(self.test_metadata)
self.step_test = 0
shuffle(self.test_metadata)
for j in range(start,end):
center_path = self.root_path +self.test_metadata[j][0]
center_steer = [float(self.test_metadata[j][3])]
# X_val.append(self.get_image(self.root_path+self.metadata_val[j][0]))
# y_val.append([float(self.metadata_val[j][3])])
center_image, center_steer[0] = self.get_image_and_steering(center_path, center_steer[0])
X_test.append(center_image)
y_test.append(center_steer)
if(self.test_left_and_right_images):
path_left = self.test_root_path + self.test_metadata[j][1][1:]
steer_left = [float(self.test_metadata[j][3])+self.left_right_offset]
path_right = self.test_root_path + self.test_metadata[j][2][1:]
steer_right = [float(self.test_metadata[j][3])-self.left_right_offset]
image_left, steer_left[0] = self.get_image_and_steering(path_left,steer_left[0])
image_right, steer_right[0] = self.get_image_and_steering(path_right, steer_right[0])
X_test.append(image_left)
y_test.append(steer_left)
X_test.append(image_right)
y_test.append(steer_right)
self.step_test = self.step_test + 1
yield X_test, y_test, int(len(self.test_metadata)/self.batch_size)
def set_root_image_path(self,path):
self.root_path = path
def move_to_start_train(self):
self.step_train = 0
def move_to_start_val(self):
self.step_val = 0
def num_train_batches(self):
return int(len(self.metadata_train) / self.batch_size)
def num_val_batches(self):
return int(len(self.metadata_val) / self.batch_size)
def add_noise(self,x):
return random_noise(x, mode='gaussian')
def get_image_and_steering(self,path,steering):
image = scipy.misc.imresize(scipy.misc.imread(path)[25:135], [66, 200])
if(self.coin_flip()):
image = self.random_saturation_change(image)
if(self.coin_flip()):
image = self.random_lightness_change(image)
if(self.coin_flip()):
image = self.invert_image(image)
image = self.random_shadow(image)
image, steering = self.random_translation(image,steering)
if(self.coin_flip()):
image, steering = self.horizontal_flip_image(image,steering)
image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
return (image/255.0)-0.5, steering
def coin_flip(self):
return random()<0.5
def make_yuv_grey_scale(self,x):
x = np.array(x)
x[:,:,1] = 0
x[:,:,2] = 0
return x
def random_gamma_correction_rgb(self,x):
# Partially taken from http://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
gamma = 0.4 + random() * 1.2
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(x, table)
def random_brightness_change_rgb(self,x):
brightness_change = 0.4 + random()*1.2
x = np.array(x)
x = cv2.cvtColor(x,cv2.COLOR_RGB2HSV)
x[:,:,2] = x[:,:,2]*brightness_change
return cv2.cvtColor(x,cv2.COLOR_HSV2RGB)
def random_saturation_change(self,x):
saturation_change = 1.5*random()
x = np.array(x)
x = cv2.cvtColor(x,cv2.COLOR_RGB2HSV)
x[:,:,1] = x[:,:,1]*saturation_change
return cv2.cvtColor(x,cv2.COLOR_HSV2RGB)
def invert_image(self,x):
return -x+255
def random_lightness_change(self,x):
lightness_change = 0.2 + 1.4*random()
x = np.array(x)
x = cv2.cvtColor(x,cv2.COLOR_RGB2HLS)
x[:,:,1] = x[:,:,1]*lightness_change
return cv2.cvtColor(x,cv2.COLOR_HLS2RGB)
def random_translation(self,x,steer):
x = np.array(x)
rows,cols,rgb = x.shape
rand_for_x = random()
translate_y = -10 + random()*20
translate_x = -30 + rand_for_x*60
M = np.float32([[1,0,translate_x],[0,1,translate_y]])
return cv2.warpAffine(x,M,(cols,rows)), (steer+(rand_for_x-0.5)*0.4)
# def random_translation(self,x,steer):
# x = np.array(x)wwwwwwwwwwwwwwwwwwwwww
# rows,cols,rgb = x.shape
#
# rand_for_x = random()
# rand_for_y = random()
#
# translate_y = -15 + rand_for_y*30
# translate_x = -30 + rand_for_x*60
#
# M = np.float32([[1,0,translate_x],[0,1,translate_y]])
#
# return cv2.warpAffine(x,M,(cols,rows)), ((steer+(rand_for_x-0.5)*0.27))
def random_rotation_image(self,x):
x = np.array(x)
rows,cols,rgb = x.shape
rand_angle = 3*(random()-0.5)
M = cv2.getRotationMatrix2D((cols/2,rows/2),rand_angle,1)
x = cv2.warpAffine(x,M,(cols,rows))
return x
def horizontal_flip_image(self,x,steer):
steer = -steer
x = np.array(x)
return cv2.flip(x,1), steer
def random_shadow(self,x):
x = cv2.cvtColor(x,cv2.COLOR_RGB2HSV)
max_x = 200
max_y = 66
if(self.coin_flip()):
i_1 = (0,0)
i_2 = (0,max_y)
i_3 = (random()*max_x,max_y)
i_4 = (random()*max_x,0)
else:
i_1 = (random()*max_x,0)
i_2 = (random()*max_x,max_y)
i_3 = (max_x,max_y)
i_4 = (max_x,0)
vertices = np.array([[i_1,i_2,i_3,i_4]], dtype = np.int32)
x = self.region_of_interest(x,vertices)
x = cv2.cvtColor(x,cv2.COLOR_HSV2RGB)
return x
def random_blur(self,x):
kernel_size = 1+int(random()*9)
if(kernel_size%2 == 0):
kernel_size = kernel_size + 1
x = cv2.GaussianBlur(x,(kernel_size,kernel_size),0)
return x
def region_of_interest(self,x, vertices):
random_brightness = 0.13
mask = np.zeros_like(x)
ignore_mask_color = [0,0,255]
cv2.fillPoly(mask, vertices, ignore_mask_color)
indices = mask[:,:,2] == 255
x[:,:,2][indices] = x[:,:,2][indices]*random_brightness
return x
def cut_top(self,x):
x = cv2.cvtColor(x,cv2.COLOR_RGB2HSV)
vertices = np.array([[(0,0),(200,0),(200,33),(0,33)]],np.int32)
random_brightness = 0
mask = np.zeros_like(x)
ignore_mask_color = [0,0,255]
cv2.fillPoly(mask, vertices, ignore_mask_color)
indices = mask[:,:,2] == 255
x[:,:,2][indices] = x[:,:,2][indices]*random_brightness
x = cv2.cvtColor(x,cv2.COLOR_HSV2RGB)
return x