-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
353 lines (296 loc) · 14.1 KB
/
train.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Lambda, ELU
from tensorflow.keras.layers import Conv2D, Convolution2D, Conv2DTranspose, Cropping2D
from tensorflow.keras.layers import TimeDistributed
from tensorflow.keras.utils import Sequence
from tensorflow.keras import optimizers, Model, Input
from tensorflow.keras import backend as K
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard
from utils import Sample, load_data_from_samples
# Global variable
# OUT_SHAPE = 5
# OUT_SHAPE = 16
OUT_SHAPE = 1 # Change to number of variables used in output
INPUT_SHAPE = (Sample.IMG_H, Sample.IMG_W, Sample.IMG_D)
def customized_loss(y_true, y_pred, loss='euclidean'):
# Simply a mean squared error that penalizes large joystick summed values
if loss == 'L2':
L2_norm_cost = 0.001
val = K.mean(K.square((y_pred - y_true)), axis=-1) \
+ K.sum(K.square(y_pred), axis=-1)/2 * L2_norm_cost
# euclidean distance loss
elif loss == 'euclidean':
val = K.sqrt(K.sum(K.square(y_pred-y_true), axis=-1))
return val
def create_model(keep_prob = 0.1):
model = Sequential()
# crop
model.add(Cropping2D(cropping=((75, 40), (0, 0)), input_shape=(135, 240, 3)))
# NVIDIA's model
model.add(Conv2D(24, kernel_size=(5, 5), strides=(2, 2), activation='relu', input_shape= INPUT_SHAPE))
model.add(Conv2D(36, kernel_size=(5, 5), strides=(2, 2), activation='relu'))
# model.add(Conv2D(48, kernel_size=(5, 5), strides=(2, 2), activation='relu'))
# model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
# model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(1164, activation='relu'))
drop_out = 1 - keep_prob
model.add(Dropout(drop_out))
model.add(Dense(100, activation='relu'))
model.add(Dropout(drop_out))
model.add(Dense(50, activation='relu'))
model.add(Dropout(drop_out))
model.add(Dense(10, activation='relu'))
model.add(Dropout(drop_out))
model.add(Dense(OUT_SHAPE, activation='softsign'))
return model
def create_new_model(keep_prob = 0.7):
model = Sequential()
# NVIDIA's model
# model.add(Lambda(lambda z: z / 127.5 - 1., input_shape=INPUT_SHAPE, output_shape=INPUT_SHAPE))
# model.add(RandomBrightness(0.2, input_shape=INPUT_SHAPE))
# model.add(RandomContrast(0.2, input_shape=INPUT_SHAPE))
model.add(Conv2D(24, kernel_size=(5, 5), strides=(2, 2), activation='elu', input_shape= INPUT_SHAPE))
model.add(Conv2D(36, kernel_size=(5, 5), strides=(2, 2), activation='elu'))
model.add(Conv2D(48, kernel_size=(5, 5), strides=(2, 2), activation='elu'))
model.add(Conv2D(64, kernel_size=(3, 3), activation='elu'))
model.add(Conv2D(64, kernel_size=(3, 3), activation='elu'))
model.add(Flatten())
model.add(Dense(1164, activation='elu'))
drop_out = 1 - keep_prob
model.add(Dropout(drop_out))
model.add(Dense(100, activation='elu'))
model.add(Dropout(drop_out))
model.add(Dense(50, activation='elu'))
model.add(Dropout(drop_out))
model.add(Dense(10, activation='elu'))
model.add(Dropout(drop_out))
model.add(Dense(OUT_SHAPE, activation='tanh'))
return model
def commaai_model(keep_prob):
model = Sequential()
model.add(Lambda(lambda x: x/127.5 - 1.,
input_shape=INPUT_SHAPE,
output_shape=INPUT_SHAPE))
model.add(Convolution2D(16, (8, 8), strides=(4, 4), padding="same"))
model.add(ELU())
model.add(Convolution2D(32, (5, 5), strides=(2, 2), padding="same"))
model.add(ELU())
model.add(Convolution2D(64, (5, 5), strides=(2, 2), padding="same"))
model.add(Flatten())
model.add(Dropout(.2))
model.add(ELU())
model.add(Dense(512))
model.add(Dropout(.5))
model.add(ELU())
model.add(Dense(OUT_SHAPE))
return model
def categorical_model(keep_prob=0.8):
drop = 1 - keep_prob
img_in = Input(shape=INPUT_SHAPE, name='img_in')
x = Conv2D(24, 5, 2, padding='same')(img_in) # Update this line
x = Dropout(drop)(x)
x = Conv2D(32, 5, 2, padding='same')(x)
x = Dropout(drop)(x)
x = Conv2D(64, 5, 2, padding='same')(x)
x = Dropout(drop)(x)
x = Conv2D(64, 3, 2, padding='same')(x)
x = Dropout(drop)(x)
x = Conv2D(64, 3, 1, padding='same')(x)
x = Dropout(drop)(x)
x = Flatten(name='flattened')(x)
x = Dense(100, activation='relu', name="dense_1")(x)
x = Dropout(drop)(x)
x = Dense(50, activation='relu', name="dense_2")(x)
x = Dropout(drop)(x)
# Categorical output of the angle into 15 bins
angle_out = Dense(15, activation='softmax', name='angle_out')(x)
model = Model(inputs=[img_in], outputs=[angle_out], name='categorical')
return model
def sequence_categorical_model(keep_prob=0.8, seq_len=5):
drop = 1 - keep_prob
seq_input_shape = (seq_len, INPUT_SHAPE[0], INPUT_SHAPE[1], INPUT_SHAPE[2])
seq_in = Input(shape=seq_input_shape, name='img_seq_in')
x = TimeDistributed(Conv2D(24, 5, 2, padding='same'))(seq_in)
x = TimeDistributed(Dropout(drop))(x)
x = TimeDistributed(Conv2D(32, 5, 2, padding='same'))(x)
x = TimeDistributed(Dropout(drop))(x)
x = TimeDistributed(Conv2D(64, 5, 2, padding='same'))(x)
x = TimeDistributed(Dropout(drop))(x)
x = TimeDistributed(Conv2D(64, 3, 2, padding='same'))(x)
x = TimeDistributed(Dropout(drop))(x)
x = TimeDistributed(Flatten())(x)
x = TimeDistributed(Dense(100, activation='relu'))(x)
x = TimeDistributed(Dropout(drop))(x)
x = TimeDistributed(Dense(50, activation='relu'))(x)
x = TimeDistributed(Dropout(drop))(x)
# Categorical output of the angle into 15 bins
x = TimeDistributed(Dense(15, activation='softmax'))(x)
# Take the last time distributed output
angle_out = Lambda(lambda x: x[:, -1], output_shape=(15,))(x)
model = Model(inputs=[seq_in], outputs=[angle_out], name='seq_categorical')
return model
class CustomDataGenerator(Sequence):
def __init__(self, x_train, y_train, batch_size=32, seq_length=5):
self.x_train = x_train
self.y_train = y_train
self.batch_size = batch_size
self.seq_length = seq_length
self.num_samples = x_train.shape[0]
self.num_classes = y_train.shape[1]
self.indexes = np.arange(self.seq_length, self.num_samples)
def __len__(self):
return int(np.ceil((self.num_samples - self.seq_length + 1) / self.batch_size))
def __getitem__(self, index):
start_idx = index * self.batch_size
end_idx = (index + 1) * self.batch_size
batch_indexes = self.indexes[start_idx:end_idx]
x_batch = []
y_batch = []
for i in batch_indexes:
sequence_x = self.x_train[i - self.seq_length + 1:i + 1]
x_batch.append(sequence_x)
y_batch.append(self.y_train[i])
return np.array(x_batch), np.array(y_batch)
def train_sequence_categorical_model(x_train, y_train, _model=sequence_categorical_model, batch_size=128, epochs=10):
# Binning and one-hot encoding
# Inputs are sequences of images (5 images each)
print("Binning and one-hot encoding")
num_bins = 15
bin_edges = np.linspace(-1, 1, num_bins)
y_binned = np.digitize(y_train, bin_edges)
print(f"[DEBUG] bin_edges: {bin_edges}]")
y = np.eye(num_bins)[y_binned - 1]
print("y_train shape: ", y_train.shape)
# Reshape X to sequences of images
seq_len = 5
# get 20% of data for validation
y_train = y[:int(len(y)*0.8)]
y_val = y[int(len(y)*0.8):]
x_train = x_train[:int(len(x_train)*0.8)]
x_val = x_train[int(len(x_train)*0.8):]
# use TF Dataset API to create generator (no duplicate data)
train_generator= CustomDataGenerator(x_train, y_train, batch_size=batch_size, seq_length=seq_len)
val_generator = CustomDataGenerator(x_val, y_val, batch_size=batch_size, seq_length=seq_len)
# x_train = x_train_new
# print("x_train shape: ", x_train.shape)
# Train model
model = _model()
checkpoint = ModelCheckpoint("model_weights_seq_c1.h5", monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# default lr=0.001, make it smaller
model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=0.00002))
print(model.summary())
model.fit(train_generator, epochs=epochs, shuffle=True, validation_data=val_generator, callbacks=callbacks_list)
def autoencoder_model():
drop = 0.2
img_in = Input(shape=INPUT_SHAPE, name='img_in')
x = img_in
x = Convolution2D(24, 5, strides=2, activation='relu', name="conv2d_1")(x)
x = Dropout(drop)(x)
x = Convolution2D(32, 5, strides=2, activation='relu', name="conv2d_2")(x)
x = Dropout(drop)(x)
x = Convolution2D(32, 5, strides=2, activation='relu', name="conv2d_3")(x)
x = Dropout(drop)(x)
x = Convolution2D(32, 3, strides=1, activation='relu', name="conv2d_4")(x)
x = Dropout(drop)(x)
x = Convolution2D(32, 3, strides=1, activation='relu', name="conv2d_5")(x)
x = Dropout(drop)(x)
x = Convolution2D(64, 3, strides=2, activation='relu', name="conv2d_6")(x)
x = Dropout(drop)(x)
x = Convolution2D(64, 3, strides=2, activation='relu', name="conv2d_7")(x)
x = Dropout(drop)(x)
x = Convolution2D(64, 1, strides=2, activation='relu', name="latent")(x)
y = Conv2DTranspose(filters=64, kernel_size=3, strides=2,
name="deconv2d_1")(x)
y = Conv2DTranspose(filters=64, kernel_size=3, strides=2,
name="deconv2d_2")(y)
y = Conv2DTranspose(filters=32, kernel_size=3, strides=2,
name="deconv2d_3")(y)
y = Conv2DTranspose(filters=32, kernel_size=3, strides=2,
name="deconv2d_4")(y)
y = Conv2DTranspose(filters=32, kernel_size=3, strides=2,
name="deconv2d_5")(y)
y = Conv2DTranspose(filters=1, kernel_size=3, strides=2, name="img_out")(y)
x = Flatten(name='flattened')(x)
x = Dense(256, activation='relu')(x)
x = Dropout(drop)(x)
x = Dense(100, activation='relu')(x)
x = Dropout(drop)(x)
x = Dense(50, activation='relu')(x)
x = Dropout(drop)(x)
angle_out = Dense(15, activation='softmax', name='angle_out')(x)
model = Model(inputs=[img_in], outputs=[angle_out], name="latent")
return model
def categorical_model_predict(loaded_model, input):
pred = loaded_model.predict(input)
# print(pred)
# onehot --> -1 to 1 (15 bins)
return (np.argmax(pred) / 7) - 1.0 # -1 to 1
def train_model(x_train, y_train, _model=create_model, batch_size=128, epochs=10):
model = _model()
checkpoint = ModelCheckpoint("model_weights_bal.h5", monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# model.compile(loss=customized_loss, optimizer="adam")
# model.compile(loss="mean_squared_error", optimizer="adam")
model.compile(loss="mean_squared_error", optimizer=optimizers.SGD(lr=0.01))
print(model.summary())
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, shuffle=True, validation_split=0.2, callbacks=callbacks_list)
def train_categorical_model(x_train, y_train, _model=categorical_model, batch_size=128, epochs=10):
# Binning and one-hot encoding
print("Binning and one-hot encoding")
num_bins = 15
# bin_edges = np.linspace(-1, 1, num_bins + 1)
# y_binned = np.digitize(y_train, bin_edges)
# y_train = np.eye(num_bins)[y_binned - 1]
# print("y_train shape: ", y_train.shape)
y_train = tf.keras.utils.to_categorical(y_train, num_bins)
model = _model()
checkpoint = ModelCheckpoint("model_weights_c2a.h5", monitor='val_loss', verbose=1, save_best_only=True, mode='min')
tensorboard = TensorBoard(log_dir="logs_C/", histogram_freq=0, write_graph=True, write_images=True)
callbacks_list = [checkpoint, tensorboard]
# default lr=0.001, make it smaller
model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=0.00002))
print(model.summary())
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, shuffle=True, validation_split=0.2, callbacks=callbacks_list)
def train_autoencoder_model(x_train, y_train, _model=autoencoder_model, batch_size=128, epochs=10):
print("Binning and one-hot encoding")
num_bins = 15
bin_edges = np.linspace(-1, 1, num_bins + 1)
y_binned = np.digitize(y_train, bin_edges)
y_train = np.eye(num_bins)[y_binned - 1]
print("y_train shape: ", y_train.shape)
model = _model()
checkpoint = ModelCheckpoint("model_weights_A0.h5", monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# default lr=0.001, make it smaller
model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=0.00002))
print(model.summary())
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, shuffle=True, validation_split=0.2, callbacks=callbacks_list)
if __name__ == '__main__':
#Set GPU options
gpus = tf.config.list_physical_devices("GPU")
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu,True)
# model summary picture
# tf.keras.utils.plot_model(create_model(), to_file='model.png', show_shapes=True)
# exit()
# Load Training Data
print("loading training data")
# x_train = np.load("data/x_sbal.npy")
# y_train = np.load("data/y_sbal.npy")
samples = ['samples/forza4003', 'samples/forza4004', 'samples/forza4005']
# samples = ['samples/forza4003']
x_train, y_train = load_data_from_samples(samples, debug=False, augment=False)
print(x_train.shape[0], 'train samples')
# Training loop variables
epochs = 200
batch_size = 192
# Train model
train_model(x_train, y_train, _model=create_model, batch_size=batch_size, epochs=epochs)
# train_categorical_model(x_train, y_train, _model=categorical_model, batch_size=batch_size, epochs=epochs)
# train_autoencoder_model(x_train, y_train, _model=autoencoder_model, batch_size=batch_size, epochs=epochs)
# train_sequence_categorical_model(x_train, y_train, _model=sequence_categorical_model, batch_size=batch_size, epochs=epochs)