forked from udacity/CarND-Behavioral-Cloning-P3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
222 lines (185 loc) · 6.84 KB
/
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
import scipy.misc
import random
import cv2
import numpy as np
from sklearn.utils import shuffle
#from keras.preprocessing.image import img_to_array, load_img
from PIL import Image, ImageEnhance, ImageOps
# some settings to tweak the way the training data is loaded
# do we want to load left/right images?
leftright = True
# 0.15 seemed to work as the best offset for the right/left cameras
steering_camera_offset = 0.15
#steering_camera_offset = 0.27
#steering_camera_offset = 0.10
#steering_camera_offset = 0.08
#
# this function opens the training data, and returns two
# lists, one with the image paths, and one with the steering angles
#
def loadTraining():
xs = []
ys = []
path = '/data1/udacity/simulator/data'
img_path = path +'/IMG'
csv_file = path +'/driving_log.csv'
# open the CSV file and loop through each line
# load the CSV so we can have labels
csv_data=np.recfromcsv(csv_file, delimiter=',', filling_values=np.nan, case_sensitive=True, deletechars='', replace_space=' ')
i = 0
remove = 0
for line in csv_data:
# remove 90% of straight scenes
i = i + 1
if (abs(float(line[3])) < 1e-5):
remove=remove+1
if (remove==10):
remove=0
# add 10% of the frames where the steering angle is close to 0
xs.append( path+'/'+line[0].decode('UTF-8').strip())
ys.append(float(line[3]))
if leftright:
# add the left image
xs.append( path+'/'+line[1].decode('UTF-8').strip())
ys.append(float(line[3])+steering_camera_offset)
# add the right image
xs.append( path+'/'+line[0].decode('UTF-8').strip())
ys.append(float(line[3])-steering_camera_offset)
else:
# add all non straight frames
# add center image
xs.append( path+'/'+line[0].decode('UTF-8').strip())
ys.append(float(line[3]))
if leftright:
# add the left image
xs.append( path+'/'+line[1].decode('UTF-8').strip())
ys.append(float(line[3])+steering_camera_offset)
# add the right image
xs.append( path+'/'+line[0].decode('UTF-8').strip())
ys.append(float(line[3])-steering_camera_offset)
# xs now has the image paths
# ys now has the steering angles
#shuffle list of images
c = list(zip(xs, ys))
random.shuffle(c)
xs, ys = zip(*c)
return xs,ys
rotation = True
#cropImage = False
#
# this brightness function taken from:
# https://medium.com/@vivek.yadav/improved-performance-of-deep-learning-neural-network-models-on-traffic-sign-classification-using-6355346da2dc
def augment_brightness_camera_images(image):
'''
:param image: Input image
:return: output image with reduced brightness
'''
# convert to HSV so that its easy to adjust brightness
image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
# randomly generate the brightness reduction factor
# Add a constant so that it prevents the image from being completely dark
random_bright = .25+np.random.uniform()
# Apply the brightness reduction to the V channel
image1[:,:,2] = image1[:,:,2]*random_bright
# convert to RGB again
image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB)
return image1
#
# This function crops the image - it didn't seem to help so it isn't used
#
def cropImage(image):
if cropImage:
top_crop = 55
bottom_crop = 135
image= image[top_crop:bottom_crop, :, :]
return image
#
# this function rotates and scales the image
#
# it randomly scales it +/- 1.02 and +/- 1 degree
# from http://docs.opencv.org/trunk/da/d6e/tutorial_py_geometric_transformations.html
def rotateAndScaleImage(image):
(rows, cols) = image.shape[:2]
rotation_degrees = 1
scale = 0.02
scale = random.uniform(1.0 - scale, 1.0 + scale)
rotation = random.uniform(-rotation_degrees, rotation_degrees)
M = cv2.getRotationMatrix2D((cols/2,rows/2),rotation,scale)
image = cv2.warpAffine(image,M,(cols,rows))
return image
#
# This function translates the image randmonly between -2 and 2 pixels in each direction
#
def translateImage(image):
trans = 2
transx= random.randint (-trans, trans);
transy= random.randint (-trans, trans);
(rows, cols) = image.shape[:2]
M = np.float32([[1,0,transx],[0,1,transy]])
image = cv2.warpAffine(image,M,(cols,rows))
return image
def processImagePixels(image):
# cropping the image didn't help
#image = cropImage(image)
#randomize brightness
image = augment_brightness_camera_images(image)
#rotation and scaling
image = rotateAndScaleImage(image)
image = translateImage(image)
# resize for the nvidia model 200x66
image = cv2.resize(image, (200, 66) )
return image
# open image image from disk
def openImage(name):
image = np.array(Image.open(name))
return image
# open the image, and call the image pipeline
def processImage(name):
image = openImage(name)
return processImagePixels(image)
# open the image, resize, don't augment
def processImageValidation(name):
image = openImage(name)
# resize for the nvidia model 200x66
image = cv2.resize(image, (200, 66) )
return image
# adjust the steering angle if needed
# this is adding some random noise
def yFunc(y):
y= y+ np.random.normal (0, 0.005)
return y
#
# the generator takes the image paths, steering angles, and two functions
# the functions process the steering angle, and the image pipeline
#
# because the X_items are image paths, this function will open and augment
# the image each time it needs to fill the returning batch array with an image
# we might be able to speed this up by passing in an array of preloaded images
# but that would use more memory
def generator(X_items,y_items,batch_size,x_func=processImage,y_func=yFunc):
#print("inside generator")
num_items = len(X_items) -1
while 1:
y = []
X = []
for i in range (batch_size):
# grab a random image, and run it through the augmentation
# to get a unique image
this_image_index = random.randint (0, num_items)
image = x_func(X_items[this_image_index])
steering = y_func(y_items[this_image_index])
# flip the image and steering angle half the time
if (random.uniform (0, 1) > 0.5):
image = cv2.flip (image, 1)
steering = - steering
y.append(steering)
X.append(image)
yield np.asarray(X), np.asarray(y)
#
# This function returns an array of images and steering angles. It is passed in
# an array of image paths and steering angles
# it probably should call the steering function to alter it if needed - this code
# doesn't alter the steering angle much
def getValidationDataset(val_xs,val_ys,func=processImageValidation):
images= [func(x) for x in val_xs]
return np.array(images), np.array(val_ys)