-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
90 lines (75 loc) · 2.76 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
__author__ = 'dl'
import csv
import cv2
import numpy as np
import keras
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_train = np.array(augmented_imgs)
y_train = np.array(augmented_angles)
# 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')