Reformat code

This commit is contained in:
Cyrille Nofficial 2020-02-17 19:11:29 +01:00
parent 84a8b11942
commit a5354e5653

View File

@ -22,7 +22,8 @@ def get_data(root_dir, filename):
print('load data from file ' + filename) print('load data from file ' + filename)
d = json.load(open(os.path.join(root_dir, filename))) d = json.load(open(os.path.join(root_dir, filename)))
if 'pilot/throttle' in d: if 'pilot/throttle' in d:
return [d['user/mode'], d['user/throttle'], d['user/angle'], root_dir, d['cam/image_array'], d['pilot/throttle'], d['pilot/angle']] return [d['user/mode'], d['user/throttle'], d['user/angle'], root_dir, d['cam/image_array'],
d['pilot/throttle'], d['pilot/angle']]
else: else:
return [d['user/mode'], d['user/throttle'], d['user/angle'], root_dir, d['cam/image_array']] return [d['user/mode'], d['user/throttle'], d['user/angle'], root_dir, d['cam/image_array']]
@ -54,7 +55,8 @@ def train():
unzip_file(root, f) unzip_file(root, f)
for root, dirs, files in os.walk('/opt/ml/input/data/train'): for root, dirs, files in os.walk('/opt/ml/input/data/train'):
data.extend([get_data(root,f) for f in sorted(files, key=str.lower) if f.startswith('record') and f.endswith('.json')]) data.extend(
[get_data(root, f) for f in sorted(files, key=str.lower) if f.startswith('record') and f.endswith('.json')])
# Normalize / correct data # Normalize / correct data
# data = [d for d in data if d[1] > 0.1] # data = [d for d in data if d[1] > 0.1]
@ -96,7 +98,8 @@ def train():
return arr return arr
logs = callbacks.TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True, write_images=True) logs = callbacks.TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True, write_images=True)
save_best = callbacks.ModelCheckpoint('/opt/ml/model/model_cat', monitor='angle_out_loss', verbose=1, save_best_only=True, mode='min') save_best = callbacks.ModelCheckpoint('/opt/ml/model/model_cat', monitor='angle_out_loss', verbose=1,
save_best_only=True, mode='min')
early_stop = callbacks.EarlyStopping(monitor='angle_out_loss', early_stop = callbacks.EarlyStopping(monitor='angle_out_loss',
min_delta=.0005, min_delta=.0005,
patience=10, patience=10,
@ -109,11 +112,16 @@ def train():
img_in = Input(shape=(128, 160, 3), img_in = Input(shape=(128, 160, 3),
name='img_in') # First layer, input layer, Shape comes from camera.py resolution, RGB name='img_in') # First layer, input layer, Shape comes from camera.py resolution, RGB
x = img_in x = img_in
x = Convolution2D(24, (5,5), strides=(2,2), activation='relu')(x) # 24 features, 5 pixel x 5 pixel kernel (convolution, feauture) window, 2wx2h stride, relu activation x = Convolution2D(24, (5, 5), strides=(2, 2), activation='relu')(
x = Convolution2D(32, (5,5), strides=(2,2), activation='relu')(x) # 32 features, 5px5p kernel window, 2wx2h stride, relu activatiion x) # 24 features, 5 pixel x 5 pixel kernel (convolution, feauture) window, 2wx2h stride, relu activation
x = Convolution2D(64, (5,5), strides=(2,2), activation='relu')(x) # 64 features, 5px5p kernal window, 2wx2h stride, relu x = Convolution2D(32, (5, 5), strides=(2, 2), activation='relu')(
x = Convolution2D(64, (3,3), strides=(2,2), activation='relu')(x) # 64 features, 3px3p kernal window, 2wx2h stride, relu x) # 32 features, 5px5p kernel window, 2wx2h stride, relu activatiion
x = Convolution2D(64, (3,3), strides=(1,1), activation='relu')(x) # 64 features, 3px3p kernal window, 1wx1h stride, relu x = Convolution2D(64, (5, 5), strides=(2, 2), activation='relu')(
x) # 64 features, 5px5p kernal window, 2wx2h stride, relu
x = Convolution2D(64, (3, 3), strides=(2, 2), activation='relu')(
x) # 64 features, 3px3p kernal window, 2wx2h stride, relu
x = Convolution2D(64, (3, 3), strides=(1, 1), activation='relu')(
x) # 64 features, 3px3p kernal window, 1wx1h stride, relu
# Possibly add MaxPooling (will make it less sensitive to position in image). Camera angle fixed, so may not to be needed # Possibly add MaxPooling (will make it less sensitive to position in image). Camera angle fixed, so may not to be needed
@ -124,7 +132,8 @@ def train():
x = Dropout(.1)(x) # Randomly drop out 10% of the neurons (Prevent overfitting) x = Dropout(.1)(x) # Randomly drop out 10% of the neurons (Prevent overfitting)
# categorical output of the angle # categorical output of the angle
callbacks_list = [save_best, early_stop, logs] callbacks_list = [save_best, early_stop, logs]
angle_out = Dense(15, activation='softmax', name='angle_out')(x) # Connect every input with every output and output 15 hidden units. Use Softmax to give percentage. 15 categories and find best one based off percentage 0.0-1.0 angle_out = Dense(15, activation='softmax', name='angle_out')(
x) # Connect every input with every output and output 15 hidden units. Use Softmax to give percentage. 15 categories and find best one based off percentage 0.0-1.0
# continous output of throttle # continous output of throttle
throttle_out = Dense(1, activation='relu', name='throttle_out')(x) # Reduce to 1 number, Positive number only throttle_out = Dense(1, activation='relu', name='throttle_out')(x) # Reduce to 1 number, Positive number only
@ -134,9 +143,11 @@ def train():
loss={'angle_out': 'categorical_crossentropy', loss={'angle_out': 'categorical_crossentropy',
'throttle_out': 'mean_absolute_error'}, 'throttle_out': 'mean_absolute_error'},
loss_weights={'angle_out': 0.9, 'throttle_out': .001}) loss_weights={'angle_out': 0.9, 'throttle_out': .001})
model.fit({'img_in':images},{'angle_out': angle_cat_array, 'throttle_out': throttle_array}, batch_size=32, epochs=100, verbose=1, validation_split=0.2, shuffle=True, callbacks=callbacks_list) model.fit({'img_in': images}, {'angle_out': angle_cat_array, 'throttle_out': throttle_array}, batch_size=32,
epochs=100, verbose=1, validation_split=0.2, shuffle=True, callbacks=callbacks_list)
# Save model for tensorflow using # Save model for tensorflow using
builder = tf.saved_model.builder.SavedModelBuilder("myModel") builder = tf.saved_model.builder.SavedModelBuilder("/opt/ml/model/tfModel")
# Tag the model, required for Go # Tag the model, required for Go
builder.add_meta_graph_and_variables(sess, ["myTag"]) builder.add_meta_graph_and_variables(sess, ["myTag"])