-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
99 lines (89 loc) · 3.99 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
import cv2
import csv
import numpy as np
import pandas as pd
import sklearn
from sklearn.model_selection import train_test_split
from keras import regularizers
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential, Model
from keras.layers import Flatten, Dense, Lambda, Conv2D, MaxPooling2D, Cropping2D, Dropout, SpatialDropout2D
import matplotlib.pyplot as plt
lines = []
images = []
measurements = []
correction = 0.2 # For left and right cameras
with open('./data/driving_log.csv') as csvfile:
reader = csv.reader(csvfile)
next(reader, None) # skip the headers
for line in reader:
lines.append(line)
train_samples, validation_samples = train_test_split(lines, test_size=0.2)
def generator(samples, batch_size=32):
num_samples = len(lines)
while 1: # Loop forever so the generator never terminates
sklearn.utils.shuffle(lines)
for offset in range(0, num_samples, batch_size):
batch_samples = lines[offset:offset+batch_size]
images = []
angles = []
for batch_sample in batch_samples:
name = './data/IMG/'+batch_sample[0].split('/')[-1]
center_image = cv2.imread(name)
center_image = cv2.cvtColor(center_image, cv2.COLOR_BGR2RGB)
images.append(center_image)
images.append(cv2.flip(center_image,1))
name = './data/IMG/'+batch_sample[1].split('/')[-1]
left_image = cv2.imread(name)
left_image = cv2.cvtColor(left_image, cv2.COLOR_BGR2RGB)
images.append(left_image)
images.append(cv2.flip(left_image,1))
name = './data/IMG/'+batch_sample[2].split('/')[-1]
right_image = cv2.imread(name)
right_image = cv2.cvtColor(right_image, cv2.COLOR_BGR2RGB)
images.append(right_image)
images.append(cv2.flip(right_image,1))
center_angle = float(batch_sample[3])
angles.append(center_angle)
angles.append(center_angle*-1.0)
angles.append(center_angle+correction)
angles.append((center_angle+correction)*-1.0)
angles.append(center_angle-correction)
angles.append((center_angle-correction)*-1.0)
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
batch_size=32
train_generator = generator(train_samples, batch_size)
validation_generator = generator(validation_samples, batch_size)
model = Sequential()
model.add(Lambda(lambda x: x/255.0-0.5, input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((50,20), (0,0)), input_shape=(160,320,3)))
model.add(SpatialDropout2D(0.5))
model.add(Conv2D(24,(5,5),strides=(2,2),activation='relu'))
model.add(Conv2D(36,(5,5),strides=(2,2),activation='relu'))
model.add(Conv2D(48,(5,5),strides=(2,2),activation='relu'))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(Flatten())
model.add(Dense(100))
model.add(Dense(50,kernel_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l1(0.01)))
model.add(Dense(10))
model.add(Dense(1))
model.compile(loss='mse',optimizer='adam')
history_object = model.fit_generator(train_generator, steps_per_epoch=len(train_samples)/batch_size,
validation_data=validation_generator,validation_steps=len(validation_samples)/batch_size,
epochs=3, verbose=1)
model.save('model.h5')
### 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()