forked from udacity/CarND-Behavioral-Cloning-P3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
203 lines (167 loc) · 5.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
import csv
import cv2
from keras.models import Sequential
from keras.layers import (
BatchNormalization,
Cropping2D,
Dense,
Dropout,
Convolution2D,
Flatten,
Lambda,
MaxPooling2D)
from keras.callbacks import ModelCheckpoint
import numpy as np
import random
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
BATCH_SIZE = 32
def get_image(path):
'''
Given an image path, return the image
'''
image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
class Frame(object):
'''
A single frame data which corresponds to a single line in the CSV file
'''
img_data = None
def __init__(self, path, angle, flip=False):
'''
Converts an array of data into the object
'''
self.image_path = path
self.angle = angle
self.flip = flip
@property
def img(self):
'''
Returns the center image
'''
# if self.c_img_data is None:
# self.c_img_data = get_image(self.c_img_path)
# return self.c_img_data
image = get_image(self.image_path)
if self.flip:
image = np.fliplr(image)
return image
def __repr__(self):
'''
String representation of a CVS data frame
'''
return '<Frame {image_path} {angle}'.format(
image_path=self.image_path,
angle=self.angle)
def get_frames():
'''
Returns the training data
'''
frames = []
with open('./recorded_data/driving_log.csv') as csvfile:
print('Opening file')
reader = csv.reader(csvfile)
print('Creating frames')
for line in reader:
if not line:
continue
c_path, l_path, r_path, steering, throttle, brake, speed = line
steering = float(steering)
# Perform data augmentation!
# Center image
frames.append(Frame(c_path, steering))
frames.append(Frame(c_path, steering * -1, flip=True))
# Left image
l_steering = steering + 0.2
frames.append(Frame(l_path, l_steering))
frames.append(Frame(l_path, l_steering * -1, flip=True))
# Center image
r_steering = steering - 0.2
frames.append(Frame(r_path, r_steering))
frames.append(Frame(r_path, r_steering * -1, flip=True))
random.shuffle(frames)
return frames
def frames_to_data(frames, augment=True):
'''
Given frames, return features and labels
'''
input_data = []
labels = []
for frame in frames:
input_data.append(frame.img)
labels.append(frame.angle)
input_data = np.asarray(input_data)
labels = np.asarray(labels)
return input_data, labels
def generator_training_data(frames, batch_size=BATCH_SIZE):
'''
Returns the Frames as training data split between test and validation
'''
print('Getting training data')
index = 0
while True:
start = index % len(frames)
end = min(start + batch_size, len(frames) - 1)
input_data, labels = frames_to_data(frames[start: end])
index += batch_size
yield shuffle(input_data, labels)
def train():
'''
Trains and creates a neural network to mimic my (not-so-hot) driving
'''
# split data into training, validation, and testing
training_frames, test_frames = train_test_split(get_frames(),
test_size=0.2)
validation_frames, test_frames = train_test_split(test_frames,
test_size=0.5)
print('Number of training frames: #{}'.format(len(training_frames)))
print('Number of validation frames: #{}'.format(len(validation_frames)))
print('Number of test frames: #{}'.format(len(test_frames)))
training_generator = generator_training_data(training_frames)
validation_generator = generator_training_data(validation_frames)
test_generator = generator_training_data(test_frames)
# create model
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(10, (7, 7), activation='relu'))
model.add(MaxPooling2D())
model.add(BatchNormalization())
model.add(Convolution2D(20, (7, 7), activation='relu'))
model.add(BatchNormalization())
# model.add(MaxPooling2D())
model.add(Convolution2D(48, (7, 7), activation='relu'))
model.add(Convolution2D(60, (7, 7), activation='relu'))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(120))
model.add(BatchNormalization())
# model.add(Dropout(0.2))
model.add(Dense(100))
model.add(BatchNormalization())
# model.add(Dropout(0.2))
model.add(Dense(1))
# Compile model
model.compile(loss='mse', optimizer='adam')
model.summary()
callbacks = [
ModelCheckpoint('models/mimic_{epoch}.h5', period=1)
]
validation_input, validation_labels = frames_to_data(validation_frames)
# Fit the model
model.fit_generator(
training_generator,
epochs=20,
steps_per_epoch=64,
callbacks=callbacks,
validation_data=validation_generator,
validation_steps=3,
verbose=1)
# evaluate the model
scores = model.evaluate_generator(test_generator, steps=5)
print('Score: {}'.format(scores))
model.save('model.h5')
if __name__ == '__main__':
train()