-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
90 lines (77 loc) · 3.04 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
import cv2
import numpy as np
from sklearn.utils import shuffle
from keras.models import Sequential, load_model
from keras.layers import Dense, Flatten, Dropout, Lambda, Cropping2D, Convolution2D
"""
Load training and validation data sets generated by 'load_data.py'
"""
X_train = np.load('X_train.npy')
X_valid = np.load('X_valid.npy')
y_train = np.load('y_train.npy')
y_valid = np.load('y_valid.npy')
"""
Helper function
"""
def data_generator(X_samples, y_samples, batch_size=32):
"""
Generator for loading batches of image data into memory
"""
num_samples = len(X_samples)
while 1:
X_samples, y_samples = shuffle(X_samples, y_samples)
for offset in range(0, num_samples, batch_size):
X_batch = X_samples[offset:offset+batch_size]
y_batch = y_samples[offset:offset+batch_size]
images, steering_angles = [], []
for X_sample, y_sample in zip(X_batch, y_batch):
centercam_image = cv2.imread(X_sample) # BGR image
centercam_image = cv2.cvtColor(centercam_image, cv2.COLOR_BGR2RGB)
steering_angle = float(y_sample)
# Add base image/steering angle to batch
images.append(centercam_image)
steering_angles.append(steering_angle)
# Augment data by horizontal flipping
images.append(np.fliplr(centercam_image))
steering_angles.append(-1.0*steering_angle)
X_out = np.array(images)
y_out = np.array(steering_angles)
yield shuffle(X_out, y_out)
"""
Set number of epochs and data generators for training and validation
"""
EPOCHS = 10
train_generator = data_generator(X_train, y_train, batch_size=32)
validation_generator = data_generator(X_valid, y_valid, batch_size=32)
"""
Keras model architecture similar to Nvidia paper 'End to End Learning for Self-Driving Cars'
"""
model = Sequential()
model.add(Cropping2D(cropping=((50,20), (0,0)), input_shape=(160,320,3)))
model.add(Lambda(lambda x: (x / 255.0) - 0.5 ))
model.add(Convolution2D(nb_filter=24, nb_row=5, nb_col=5, subsample=(2, 2), activation="relu"))
model.add(Convolution2D(nb_filter=36, nb_row=5, nb_col=5, subsample=(2, 2), activation="relu"))
model.add(Convolution2D(nb_filter=48, nb_row=5, nb_col=5, subsample=(2, 2), activation="relu"))
model.add(Convolution2D(nb_filter=64, nb_row=3, nb_col=3, activation="relu"))
model.add(Convolution2D(nb_filter=64, nb_row=3, nb_col=3, activation="relu"))
model.add(Flatten())
model.add(Dense(100, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(50, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation="relu"))
model.add(Dense(1))
"""
Compile model using mean-squared error and ADAM optimizer with default learning rate parameters.
Run model fit training using generators and save model and loss history results.
"""
model.compile(loss='mse', optimizer='adam')
trained_history = model.fit_generator(train_generator,
samples_per_epoch=(2*len(X_train)),
validation_data=validation_generator,
nb_val_samples=(2*len(X_valid)),
nb_epoch=EPOCHS)
model.summary()
model.save('model.h5')
np.save("trained_history.npy", trained_history.history)
print('Model and history saved.')