-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
297 lines (240 loc) · 9.55 KB
/
model.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
# Import Libraries
import csv
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Convolution2D
from keras.layers import Dropout
from keras.callbacks import ModelCheckpoint, EarlyStopping
'''
#### DATA AUGMENTATION FUNCTIONS ####
'''
# Set Parameters
'''
Parameters for data manipulation
'''
centerAngles = 0.015
lowerBound = 0.2
lowerBound2 = 0.3
upperBound = 0.98
ANGLE_SHIFT = 0.006
def filterSamples(labels, threshold, delRate):
'''
Function to create mask to filter dataset with based upon the threshold
and the percentage to remove
'''
zero_idx = np.where(abs(labels)<=threshold)
zero_idx = zero_idx[0]
sizeDel = int(len(zero_idx) * delRate)
return np.random.choice(zero_idx, sizeDel, replace=False)
def flipImages(images, angles):
newImages = []
newAngles = []
for image, angle in zip(images, angles):
if lowerBound2 < abs(angle) < upperBound:
newImages.append(cv2.flip(image, 1))
newAngles.append(angle * -1)
if len(newAngles) > 0: # To protect against no image transformation
return np.append(images,np.array(newImages),axis=0), \
np.append(angles,np.array(newAngles),axis=0)
else:
return images, angles
def imageShift(images, angles, maxShift=10):
shiftedImages = []
newAngles = []
for image, angle in zip(images, angles):
if lowerBound2 < abs(angle) < upperBound:
shiftX = np.random.uniform(-maxShift, maxShift)
shiftY = np.random.uniform(-maxShift, maxShift)
M = np.float32([[1,0,shiftX],[0,1,shiftY]])
image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
shiftedImages.append(image)
angle = angle + ANGLE_SHIFT * shiftX
newAngles.append(angle)
if len(newAngles) > 0:
return np.append(images,np.array(shiftedImages),axis=0),\
np.append(angles,np.array(newAngles),axis=0)
else:
return images, angles
def brightnessAugmentation(images, angles):
newImages = []
newAngles = []
for image, angle in zip(images, angles):
newImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
brightnessAdjustment = np.random.uniform(0.1,1.1)
newImage[:,:,2] = newImage[:,:,2] * brightnessAdjustment
np.clip(newImage[:,:,2], 0, 255)
newImages.append(cv2.cvtColor(newImage, cv2.COLOR_HSV2BGR))
newAngles.append(angle)
if len(newAngles) > 0:
return np.array(newImages), np.array(newAngles)
else:
return images, angles
def randomShadow(images, angles):
shadowedImages = []
newAngles = []
for image, angle in zip(images,angles):
# Get indicies of mask
top_y = 320*np.random.uniform()
top_x = 0
bot_x = 160
bot_y = 320*np.random.uniform()
# convert image to hls color space
image_hls = cv2.cvtColor(image,cv2.COLOR_RGB2HLS)
# create mask
shadow_mask = 0*image_hls[:,:,1]
X_m = np.mgrid[0:image.shape[0],0:image.shape[1]][0]
Y_m = np.mgrid[0:image.shape[0],0:image.shape[1]][1]
shadow_mask[((X_m-top_x)*(bot_y-top_y) -(bot_x - top_x)*(Y_m-top_y) >=0)]=1
if np.random.randint(2)==1:
random_bright = .5
cond1 = shadow_mask==1
cond0 = shadow_mask==0
if np.random.randint(2)==1:
image_hls[:,:,1][cond1] = image_hls[:,:,1][cond1]*random_bright
else:
image_hls[:,:,1][cond0] = image_hls[:,:,1][cond0]*random_bright
image = cv2.cvtColor(image_hls,cv2.COLOR_HLS2RGB)
shadowedImages.append(image)
newAngles.append(angle)
if len(newAngles) > 0:
return np.array(shadowedImages), np.array(newAngles)
else:
return images, angles
'''
#### DATA PREPROCESSING FUNCTIONS ####
'''
# Parameters
carWidth = 1.0
recoverydist = 4.0
horizon = 60
bonnet = 140
def getLabels(samples):
y_labels = []
for sample in samples:
y_labels.append(float(sample[3]))
return np.array(y_labels)
def loadImages(sample):
image_center = cv2.imread(sample[0])
image_left = cv2.imread(sample[1].replace(" ",""))
image_right = cv2.imread(sample[2].replace(" ",""))
angle_center = float(sample[3])
angle_left = angle_center + (carWidth/recoverydist) / 25
angle_right = angle_center - (carWidth/recoverydist) / 25
return [image_center, image_left, image_right], [angle_center, angle_left, angle_right]
def resizeImages(images):
images = [cv2.resize(image,(200,66), interpolation=cv2.INTER_AREA) for image in images]
return images
def ChangeColourSpace(images):
images = [cv2.cvtColor(image, cv2.COLOR_BGR2YUV) for image in images]
return images
def cropImages(images, maxShift=10):
croppedImages = [image[horizon:bonnet,:] for image in images]
return croppedImages
def generator(samples, batch_size=32):
while 1:
samples = shuffle(samples)
# This augmentation is done here to make it random for each epoch
angles = getLabels(samples)
delIdx = filterSamples(np.array(angles), threshold=0.015, delRate=0.98)
samplesRemoved = np.delete(np.array(samples), delIdx, axis=0)
angles = getLabels(samplesRemoved)
delIdx = filterSamples(np.array(angles), threshold=0.1, delRate=0.60)
samplesRemoved = np.delete(np.array(samplesRemoved), delIdx, axis=0)
num_samples = len(samplesRemoved)
for offset in range(0, num_samples, batch_size):
batch_samples = samplesRemoved[offset:offset+batch_size]
images = []
angles = []
for batch_sample in batch_samples:
allImages, allAngles = loadImages(batch_sample)
images.extend(allImages)
angles.extend(allAngles)
# Augmentations
images, angles = brightnessAugmentation(images, angles)
images, angles = randomShadow(images, angles)
images, angles = flipImages(images, angles)
images, angles = imageShift(images, angles)
# Pre Processing
images = cropImages(np.array(images))
images = resizeImages(images)
images = ChangeColourSpace(images)
X_train = np.array(images)
y_train = np.array(angles)
yield shuffle(X_train, y_train)
def generator_valid(samples, batch_size=32):
while 1:
samples = shuffle(samples)
num_samples = len(samples)
for offset in range(0, num_samples, batch_size):
batch_samples = samples[offset:offset+batch_size]
images = []
angles = []
for batch_sample in batch_samples:
image = cv2.imread(batch_sample[0].replace(" ",""))
images.append(image)
angle = float(batch_sample[3])
angles.append(angle)
images = cropImages(np.array(images))
images = resizeImages(images)
images = ChangeColourSpace(images)
X_valid = np.array(images)
y_valid = np.array(angles)
yield shuffle(X_valid, y_valid)
'''
#### MODEL BUILDING AND TRAINING ####
'''
csv_directory = './data_udacity/driving_log.csv'
samples = []
with open(csv_directory) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
samples.append(line)
# Load the recovery data
csv_directory2 = './data_recovery/driving_log.csv'
with open(csv_directory2) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
samples.append(line)
samples = np.array(samples[1:])
train_samples, valid_samples = train_test_split(samples, test_size=0.2)
train_generator = generator(train_samples, batch_size=32)
valid_generator = generator_valid(valid_samples, batch_size=32)
# Model Selection, Hyperparameters
dropoutRate = 0.5
num_epochs = 20
num_samples_train = 26142
print('Number of samples to train on: {}'.format(num_samples_train))
model = Sequential()
model.add(Lambda(lambda x: (x/255.0)-0.5, input_shape=(66,200,3)))
model.add(Convolution2D(24,5,5, subsample=(2,2), init='he_normal',
activation='elu'))
model.add(Convolution2D(36,5,5, subsample=(2,2), init='he_normal',
activation='elu'))
model.add(Convolution2D(48,5,5, subsample=(2,2), init='he_normal',
activation='elu'))
model.add(Convolution2D(64,3,3, subsample=(1,1), init='he_normal',
activation='elu'))
model.add(Convolution2D(64,3,3, subsample=(1,1), init='he_normal',
activation='elu'))
model.add(Flatten())
model.add(Dropout(dropoutRate))
model.add(Dense(100, init='he_normal', activation='elu'))
model.add(Dropout(dropoutRate))
model.add(Dense(50, init='he_normal', activation='elu'))
model.add(Dense(10, init='he_normal', activation='elu'))
model.add(Dense(1))
# Compile model
model.compile(loss='mse', optimizer='adam')
# Set up training
filepath = './model-NVIDIA-{epoch:02d}-{val_loss:.5f}.h5'
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True)
stopper = EarlyStopping(monitor='val_loss', min_delta=.001, patience=5)
callbacks = [checkpoint, stopper]
history = model.fit_generator(train_generator, samples_per_epoch=num_samples_train,
validation_data=valid_generator,
nb_val_samples=len(valid_samples), nb_epoch=num_epochs,
callbacks=callbacks)
print('Done! Im Out...')