-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel2.py
219 lines (163 loc) · 6.64 KB
/
model2.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import numpy as np
from keras.models import Model, Sequential
from keras.layers import Input, Cropping2D, Lambda, merge
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Dense, Activation, Flatten
from keras.layers import Dropout, BatchNormalization, ELU
from keras.optimizers import Adam
from utils import train, preprocess_img_size
"""
NVidia Model - see https://images.nvidia.com/content/tegra/automotive/images/2016/solutions/pdf/end-to-end-dl-using-px.pdf
NVidia uses 66x200 YUV image
We use 66x200 RGB images
"""
def create_image_branch(input_dim, conv_activation, fcn_activation, dropout_prob, use_bn):
image_input = Input(shape=input_dim, name='image_input')
x = image_input
# Normalization - centered around zero with small standard deviation
x = Lambda(lambda x: x / 255.0 - 0.5, name='image_normalization')(x)
# CONV1, # filters = 24, kernel size = 5x5, out = 24@31x98
x = conv_layer(x, 24, 5, 2, conv_activation, use_bn, dropout_prob)
# CONV2, # filters = 36, kernel size = 5x5, out = 36@14x47
x = conv_layer(x, 36, 5, 2, conv_activation, use_bn, dropout_prob)
# CONV3, # filters = 48, kernel size = 5x5, out = 48@5x22
x = conv_layer(x, 48, 5, 2, conv_activation, use_bn, dropout_prob)
# CONV4, # filters = 64, kernel size = 3x3, out = 64@3x20
x = conv_layer(x, 64, 3, 1, conv_activation, use_bn, dropout_prob)
# CONV5, # filters = 64, kernel size = 3x3, out = 64@1x18
x = conv_layer(x, 64, 3, 1, conv_activation, use_bn, dropout_prob)
# Flatten layer
x = Flatten()(x)
# FCN1
x = fc_layer(x, 1164, fcn_activation, use_bn, dropout_prob)
# FCN2
x = fc_layer(x, 100, fcn_activation, use_bn, dropout_prob)
# FCN3
x = fc_layer(x, 50, fcn_activation, use_bn, dropout_prob)
# FCN4
x = fc_layer(x, 10, fcn_activation, use_bn, dropout_prob)
return image_input, x#, si, sb
"""
Builds the model which have 2 outputs: steering and speed.
"""
def complex_model(input_dim, conv_activation='relu', fcn_activation='relu', dropout_prob=0.5, use_bn=False):
image_input, image_branch = create_image_branch(input_dim, conv_activation, fcn_activation, dropout_prob, use_bn)
# Readout Layers
steering_output = Dense(1, name='steering_output')(image_branch)
speed_output = Dense(1, name='speed_output')(image_branch)
model = Model(input=[image_input], output=[steering_output, speed_output])
return model
"""
Build Convolutional layer [CONV - BN - ACTIVATION - MAX_POOL - DROPOUT] depending on passed params.
"""
def conv_layer(x, nb_filter, filter_size, stride, activation, use_bn, dropout_prob, pool_size=0, pool_stride=2):
x = Convolution2D(nb_filter, filter_size, filter_size, subsample=(stride, stride))(x)
if use_bn:
x = BatchNormalization()(x)
x = Activation(activation)(x)
if pool_size > 0:
x = MaxPooling2D(pool_size=(pool_size, pool_size), strides=(pool_stride, pool_stride), border_mode='valid')(x)
if dropout_prob > 0:
x = Dropout(dropout_prob)(x)
return x
"""
Build FCN layer [FC - BN - ACTIVATION - DROPOUT] depending on passed params.
"""
def fc_layer(x, nb_hidden_units, activation, use_bn, dropout_prob):
x = Dense(nb_hidden_units)(x)
if use_bn:
x = BatchNormalization()(x)
x = Activation(activation)(x)
if dropout_prob > 0:
x = Dropout(dropout_prob)(x)
return x
"""
see utils.generator
"""
def x_generator(images, speeds):
return {'image_input': np.array(images)}
"""
see utils.generator
"""
def y_generator(steerings, speeds, throttles):
return {'steering_output': np.array(steerings), 'speed_output': normalize_speeds(np.array(speeds))}
def get_croppping_dim():
# crop from top and bottom
# return (70, 20)
return (40, 20)
def get_target_size():
return (66, 200)
def predict(model, image):
image = preprocess_img_size(image, get_croppping_dim(), get_target_size())
pred = model.predict(image[None, :, :, :], batch_size=1)
steering_angle = float(pred[0])
speed = unnormalize_speed(float(pred[1]))
return steering_angle, speed
"""
Convert speeds from the range [0, 30] to the range [-1, 1]
"""
def normalize_speeds(speeds):
return speeds / 15. - 1.
"""
Convert speed from the range [-1, 1] to the range [0, 30]
"""
def unnormalize_speed(normalized_speed):
return (normalized_speed + 1) * 15.
def main():
data_dirs = [
'data/udacity-origin-data/',
# 'data/gathering1/track1-lap1/',
# 'data/gathering1/track1-lap2-throttle/',
# 'data/gathering1/track2-lap1/',
# 'data/gathering1/track2-lap2/',
# 'data/gathering1/track2-lap3/',
# 'data/gathering1/track2-lap4-throttle/',
# 'data/gathering1/track2-recovery1/',
#
# 'data/gathering2/track1-lap1/',
# 'data/gathering2/track2-lap1/',
# 'data/gathering3/track1-lap1/',
# 'data/gathering3/track1-lap2-opposite/',
# 'data/gathering3/track1-lap3-recovery/',
# 'data/gathering3/track1-lap4/',
# 'data/gathering3/track1-lap5-recovery/',
# 'data/gathering3/track1-lap6-big/',
# 'data/gathering3/track1-lap7-recovery/',
'data/gathering4/track1-lap1',
'data/gathering4/track1-lap2-recovery',
'data/gathering4/track1-lap3-opposite',
'data/gathering4/track2-lap1',
'data/gathering4/track2-lap2-recovery',
'data/gathering4/track2-lap3-recovery',
'data/gathering5/track1-lap1',
'data/gathering5/track2-lap1',
'data/gathering6/track2-lap1',
'data/gathering6/track2-lap2-opposite',
'data/gathering6/track2-lap3',
'data/gathering6/track2-lap4-recovery',
'data/gathering6/track2-lap5-recovery',
]
steering_correction = 0.2
skip_steerings = [(0, 0.9)]#[(0, 0.98), (-1, 0.95), (1, 0.9)]
lr = 1e-3
activation = 'relu'
dropout_prob = 0.2
use_bn = False
nb_epoch = 50
use_side_cameras = False
cropping_dim = get_croppping_dim()
target_size = get_target_size()
generator_batch_size = 128
input_dim = (*target_size, 3)
model = complex_model(input_dim, conv_activation=activation, fcn_activation=activation, dropout_prob=dropout_prob, use_bn=use_bn)
print("Model: ")
model.summary()
model.compile(optimizer=Adam(lr=lr),
loss={'steering_output': 'mse', 'speed_output': 'mse'},
loss_weights={'steering_output': 1.0, 'speed_output': 1.0}
)
train(model, 'models/2/model2.h5', x_generator, y_generator,
data_dirs, skip_steerings, use_side_cameras, steering_correction,
generator_batch_size, nb_epoch)
if __name__ == '__main__':
main()