-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model_tf.py
107 lines (88 loc) · 3.26 KB
/
train_model_tf.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
__author__ = 'dl'
import csv
import cv2
import numpy as np
import random
#import tensorflow as tf
#from keras import optimizers
#from keras.callbacks import ModelCheckpoint
#from keras.models import Sequential
#from keras.layers import Flatten, Dense, Dropout, Lambda
#from keras.layers import Convolution2D, Cropping2D, MaxPooling2D
def read_log(filepath):
lines = []
with open(filepath) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
lines.append(line)
return lines
# Read original data
lines_twolaps = read_log('E:\\myproject\self-drive\IMG\driving_log.csv')
lines_curves = read_log('E:\\myproject\self-drive\IMG\driving_log.csv')
lines = np.array(lines_twolaps + lines_curves)
# Balance data
nbins = 2000
max_examples = 200
balanced = np.empty([0, lines.shape[1]], dtype=lines.dtype)
for i in range(0, nbins):
begin = i * (1.0 / nbins)
end = begin + 1.0 / nbins
extracted = lines[(abs(lines[:,3].astype(float)) >= begin) & (abs(lines[:,3].astype(float)) < end)]
np.random.shuffle(extracted)
extracted = extracted[0:max_examples, :]
balanced = np.concatenate((balanced, extracted), axis=0)
# Prepare and augment data
imgs, angles = [], []
offset = 0.2
correction = [0, offset, -offset] # center, left, right cameras
for line in balanced:
for i in range(3):
img_path = line[i]
img = cv2.imread(img_path)
imgs.append(img)
angle = float(line[3])
angles.append(angle + correction[i])
flip_imgs, flip_angles = [], []
for img, angle in zip(imgs, angles):
flip_imgs.append(cv2.flip(img, 1))
flip_angles.append(-1.0 * angle)
augmented_imgs = imgs + flip_imgs
augmented_angles = angles + flip_angles
X_test,Y_test = [],[]
random_index = list(range(len(augmented_angles)))
for i in range(int(len(augmented_angles)*0.1)):
random_index = list(range(len(augmented_angles)))
a = random.choice(random_index)
X_test.append(augmented_imgs[a])
Y_test.append(augmented_angles[a])
augmented_angles.pop(a)
augmented_imgs.pop(a)
random_index.pop(a)
X_train = np.array(augmented_imgs)
Y_train = np.array(augmented_angles)
X_test = np.array(X_test)
Y_test = np.array(Y_test)
# Build the 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(8, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Convolution2D(16, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mse', optimizer=optimizers.Adam(lr=0.0001), metrics=['accuracy'])
tbCallBack = keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=True,)
best_model = ModelCheckpoint('model_best.h5', verbose=2, save_best_only=True)
model.fit(X_train, y_train, validation_split=0.2, shuffle=True, epochs=30, callbacks=[best_model,tbCallBack])
model.save('model_last.h5')