-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
353 lines (246 loc) · 10.4 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
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
# coding: utf-8
# # Read files
#
# In[1]:
'''
import csv
import cv2
import numpy as np
lines = []
#csv_file = '../data/driving_log.csv'
csv_file = '../data_2/driving_log.csv'
with open(csv_file ) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
lines.append(line)
images = []
measurements = []
for line in lines:
for i in range(3):#left center right camera
source_path = line[0]
filename = source_path.split('/')[-1]
current_path = '../data_2/IMG/' + filename
image = cv2.imread(current_path)
images.append(image)
measurement = float(line[3])
measurements.append(measurement)
#X_train = np.array(images)
#y_train = np.array(measurements)
'''
# # data augmentation
# In[2]:
def data_aug(image, measurement):
augmented_images = []
augmented_measurements = []
#for image, measurement in zip (images, measurements):
augmented_images.append(image)
augmented_measurements.append(measurement)
augmented_images.append(cv2.flip(image,1))
augmented_measurements.append(measurement*-1.0)
return augmented_images, augmented_measurements
#X_train = np.array(augmented_images)
#y_train = np.array(augmented_measurements)
# In[12]:
import os
import csv
import numpy as np
samples = []
csv_file = '../data_2/driving_log.csv'
with open(csv_file) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
samples.append(line)
from sklearn.model_selection import train_test_split
train_samples, validation_samples = train_test_split(samples, test_size=0.2)
import sklearn
import csv
import cv2
from random import shuffle
#img_new_shape = (80,160,3)
def generator(samples, batch_size=32):
num_samples = len(samples)
while 1: # Loop forever so the generator never terminates
shuffle(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:
name_c = '../data_2/IMG/'+batch_sample[0].split('/')[-1]#center
name_l = '../data_2/IMG/'+batch_sample[1].split('/')[-1]#left
name_r = '../data_2/IMG/'+batch_sample[2].split('/')[-1]#right
center_image = cv2.imread(name_c)
left_image = cv2.imread(name_l)
right_image = cv2.imread(name_r)
#Reorder BGR to RGB
#CV2 import BGR but we infere the steering in RGB
center_image = center_image[:, :, (2, 1, 0)]
left_image = left_image[:, :, (2, 1, 0)]
right_image = right_image[:, :, (2, 1, 0)]
correction = 0.2 # this is a parameter to tune
center_angle = float(batch_sample[3])
left_angle = center_angle + correction
right_angle = center_angle - correction
aug_center_image = []
aug_center_angle = []
aug_right_image = []
aug_right_angle = []
aug_left_image = []
aug_left_angle = []
aug_center_image, aug_center_angle = data_aug(center_image, center_angle)
aug_right_image, aug_right_angle = data_aug(right_image, right_angle)
aug_left_image, aug_left_angle = data_aug(left_image, left_angle)
#aug_center_image = cv2.flip(center_image,1)
#aug_center_angle = center_angle*-1.0
#images.append(center_image)
#angles.append(center_angle)
#images.append(aug_center_image)
#angles.append(aug_center_angle)
# add images and angles to data set
images.append(left_image)
images.append(cv2.flip(left_image,1))
images.append( center_image)
images.append(cv2.flip(center_image,1))
images.append(right_image)
images.append(cv2.flip(right_image,1))
angles.append(left_angle)
angles.append(left_angle*-1.0)
angles.append( center_angle)
angles.append( center_angle*-1.0)
angles.append(right_angle)
angles.append(right_angle*-1.0)
#images.extend(aug_left_image, aug_center_image, aug_right_image)
#angles.extend(aug_left_angle, aug_center_angle, aug_right_angle)
# trim image to only see section with road
X_train = np.array(images)
y_train = np.array(angles)
yield sklearn.utils.shuffle(X_train, y_train)
# compile and train the model using the generator function
train_generator = generator(train_samples, batch_size=32)
validation_generator = generator(validation_samples, batch_size=32)
#X_train_shuffle = np.zeros(160*320*3*32)
#y_train_shuffle = np.zeros(160*320*3*32)
#X_train_shuffle, y_train_shuffle = train_generator
#print (X_train_shuffle)
# # Generator
# In[13]:
'''
import os
import csv
import numpy as np
samples = []
csv_file = '../data_2/driving_log.csv'
with open(csv_file) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
samples.append(line)
from sklearn.model_selection import train_test_split
train_samples, validation_samples = train_test_split(samples, test_size=0.2)
import sklearn
import csv
import cv2
from random import shuffle
#img_new_shape = (80,160,3)
def generator(samples, batch_size=32):
num_samples = len(samples)
while 1: # Loop forever so the generator never terminates
shuffle(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:
name = '../data_2/IMG/'+batch_sample[0].split('/')[-1]
center_image = cv2.imread(name)
#Reorder BGR to RGB
#CV2 import BGR but we infere the steering in RGB
center_image = center_image[:, :, (2, 1, 0)]
#downsample the picture
#input_shape=(160,320,3)
#center_image = cv2.resize(center_image, img_new_shape)
center_angle = float(batch_sample[3])
#real batch_size = batch_size * 2
# center_image, center_angle = data_aug(center_image, center_angle)
#aug_center_image = []
#aug_center_angle = []
#aug_center_image, aug_center_angle = data_aug(center_image, center_angle)
#print(aug_center_image)
#images.append(center_image)
#angles.append(center_angle)
aug_center_image = cv2.flip(center_image,1)
aug_center_angle = center_angle*-1.0
images.append(center_image)
angles.append(center_angle)
images.append(aug_center_image)
angles.append(aug_center_angle)
# trim image to only see section with road
X_train = np.array(images)
y_train = np.array(angles)
yield sklearn.utils.shuffle(X_train, y_train)
# compile and train the model using the generator function
train_generator = generator(train_samples, batch_size=32)
validation_generator = generator(validation_samples, batch_size=32)
#X_train_shuffle = np.zeros(160*320*3*32)
#y_train_shuffle = np.zeros(160*320*3*32)
#X_train_shuffle, y_train_shuffle = train_generator
#print (X_train_shuffle)
'''
# # data augmentation
# In[14]:
'''
augmented_images, augmented_measurements = [], []
for image, measurement in zip (images, measurements):
augmented_images.append(image)
augmented_measurements.append(measurement)
augmented_images.append(cv2.flip(image,1))
augmented_measurements.append(measurement*-1.0)
X_train = np.array(augmented_images)
y_train = np.array(augmented_measurements)
'''
# # build model
# In[15]:
import keras
from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D
model = Sequential()
model.add(Lambda(lambda x: x/ 255.0 - 0.5, input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((70,25),(0,0))))
model.add(Convolution2D(24,5,5, subsample=(2,2), activation='relu'))
#model.add(MaxPooling2D())
model.add(Convolution2D(36,5,5, subsample=(2,2), activation='relu'))
#model.add(Convolution2D(6,5,5,activation='relu'))
model.add(Convolution2D(48,5,5, subsample=(2,2), activation='relu'))
#model.add(MaxPooling2D())
model.add(Convolution2D(64,3,3, activation='relu'))
model.add(Convolution2D(64,3,3, activation='relu'))
model.add(Flatten())
model.add(Dense(100))
model.add(Dense(50))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
#model.fit(X_train, y_train, validation_split = 0.2, shuffle=True, nb_epoch=5)
history_object = model.fit_generator(train_generator, samples_per_epoch= 6*len(train_samples),
validation_data=validation_generator,
nb_val_samples=6*len(validation_samples),
nb_epoch=3)
model.save('model.h5')
# # plot MSE loss
# In[ ]:
import matplotlib.pyplot as plt
#history_object = model.fit_generator(train_generator, samples_per_epoch =
# len(train_samples), validation_data =
# validation_generator,
# nb_val_samples = len(validation_samples),
# nb_epoch=5, verbose=1)
### print the keys contained in the history object
print(history_object.history.keys())
### plot the training and validation loss for each epoch
plt.plot(history_object.history['loss'])
plt.plot(history_object.history['val_loss'])
plt.title('model mean squared error loss')
plt.ylabel('mean squared error loss')
plt.xlabel('epoch')
plt.legend(['training set', 'validation set'], loc='upper right')
plt.show()