forked from udacity/CarND-Behavioral-Cloning-P3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_trainer.py
130 lines (98 loc) · 4.87 KB
/
model_trainer.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
import csv
import os
import random
from datetime import datetime
from math import ceil
from random import shuffle
import cv2
import numpy as np
import sklearn
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.callbacks import EarlyStopping, TensorBoard
from tensorflow.python.keras.layers import Flatten, Dense, Lambda, Convolution2D, Cropping2D, Dropout
from tensorflow.python.keras.models import Sequential
def create_model():
model = Sequential()
model.add(Lambda(lambda x: (x / 255.0) - .5, input_shape=(160, 320, 3)))
model.add(Cropping2D(cropping=((60, 25), (0, 0))))
model.add(Convolution2D(24, 5, 2, activation='relu', kernel_initializer='he_uniform'))
model.add(Convolution2D(36, 5, 2, activation='relu', kernel_initializer='he_uniform'))
model.add(Convolution2D(48, 5, 2, activation='relu', kernel_initializer='he_uniform'))
model.add(Convolution2D(64, 3, activation='relu', kernel_initializer='he_uniform'))
model.add(Convolution2D(64, 3, activation='relu', kernel_initializer='he_uniform'))
model.add(Flatten())
model.add(Dense(1164, activation='relu', kernel_initializer='he_uniform'))
model.add(Dropout(.5))
model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
model.add(Dropout(.5))
model.add(Dense(50, activation='relu', kernel_initializer='he_uniform'))
model.add(Dropout(.5))
model.add(Dense(10, activation='relu', kernel_initializer='he_uniform'))
model.add(Dropout(.5))
model.add(Dense(1))
return model
def generator(samples, driving_log_file, batch_size=64):
num_samples = len(samples)
image_dir = driving_log_file.split('/')[0] + "/IMG/"
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 = image_dir + batch_sample[0].split(os.sep)[-1]
center_image = cv2.imread(name)
center_image = cv2.cvtColor(center_image, cv2.COLOR_RGB2YUV)
center_angle = batch_sample[1]
if batch_sample[2]:
center_image = cv2.flip(center_image, 1)
images.append(center_image)
angles.append(center_angle)
# trim image to only see section with road
X_train = np.array(images)
y_train = np.array(angles)
yield tuple(sklearn.utils.shuffle(X_train, y_train, random_state=42))
def load_images(driving_log_file, batch_size=64, correction=.25):
lines = []
with open(driving_log_file) as csv_file:
reader = csv.reader(csv_file)
next(reader, None)
for line in reader:
if float(line[6]) >= 25:
steering_center = float(line[3])
steering_right = steering_center - correction
steering_left = steering_center + correction
# 0 means do not flip, 1 means flip camera image
lines.append((line[0], steering_center, 0))
lines.append((line[0], -steering_center, 1))
lines.append((line[1], steering_left, 0))
lines.append((line[1], -steering_left, 1))
lines.append((line[2], steering_right, 0))
lines.append((line[2], -steering_right, 1))
train_samples, validation_samples = train_test_split(lines, test_size=0.2)
# compile and train the model using the generator function
train_generator = generator(train_samples, driving_log_file, batch_size=batch_size)
validation_generator = generator(validation_samples, driving_log_file, batch_size=batch_size)
return train_generator, validation_generator, len(train_samples), len(validation_samples)
if __name__ == '__main__':
tf.random.set_seed(42)
random.seed(42)
image_file = 'data/driving_log.csv'
early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, restore_best_weights=True, patience=5)
model = create_model()
model.summary()
model.compile(loss='mse', optimizer='adam')
date_time = datetime.now().strftime("%Y%m%d-%H%M%S")
logdir = "logs/fit/" + date_time
tensorboard_callback = TensorBoard(log_dir=logdir, histogram_freq=1)
batch_size = 64
train_generator, validation_generator, l_train_samples, l_validation_samples = load_images(image_file, batch_size)
print(l_train_samples, l_validation_samples)
model.fit(train_generator,
steps_per_epoch=ceil(l_train_samples / batch_size),
validation_data=validation_generator,
validation_steps=ceil(l_validation_samples / batch_size),
epochs=5, verbose=1, callbacks=[early_stop, tensorboard_callback])
model.save(f'model{date_time}.h5')