def obtain_episode_data(folder_path, seg=False, delim=' ', header_names=None, columns_to_use=COLS_TO_USE): # Obtain images i.e x data rgb_path = folder_path + "/CameraRGB" rgb_images = [] seg_images = [] if os.path.isdir(rgb_path): rgb_images = image_extract(rgb_path) else: printc("Error:- folder '{}' not found".format(rgb_path)) if seg == True: seg_path = folder_path + "/CameraSemSeg" if os.path.isdir(seg_path): # files_seg = sum(os.path.isdir(i) for i in os.listdir(seg_path)) seg_images = image_extract(seg_path) else: printc("Error:- folder '{}' not found".format(seg_path)) # Obtain y data for filename in os.listdir(folder_path): if filename.endswith("_episode.txt"): dataframe = pd.read_csv(folder_path + "/" + filename, delimiter=delim, header=None, usecols=columns_to_use) dataset = dataframe.values control_data = dataset[:].astype(float) return np.asarray(rgb_images), np.asarray(control_data)
def train_with_all(data_path, val_path, model, target_model_name, nb_epochs=10, checkpoint_stage=10, callbacks=None, save_model=None): folders = os.listdir(data_path) checkpoint = 0 os.mkdir("../weights/" + target_model_name) history_file = open( "../weights/" + target_model_name + "/history_file_" + target_model_name + ".txt", 'a') val_history_file = open( "../weights/" + target_model_name + "/val_history_file_" + target_model_name + ".txt", 'a') x_val, y_val = obtain_data(val_path) for idx, folder in enumerate(folders): print("Obtaining data: {}/{}".format(idx, len(folders) - 1)) x_data, y_data = obtain_episode_data(data_path + "/" + folder, header_names=DATA_HEADERS) # time_start = time.process_time() history = model.fit([x_data], [y_data[..., 0], y_data[..., 1]], validation_data=([x_val], [y_val[..., 0], y_val[..., 1]]), epochs=nb_epochs, callbacks=callbacks) # elapsed = time.process_time() - time_start # print(elapsed) checkpoint += 1 history_file.write(str(history.history['loss']) + "\n") val_history_file.write(str(history.history['val_loss']) + "\n") if checkpoint == checkpoint_stage: printc( "Checkpoint: Saving model to " + "../weights/" + target_model_name + "/checkpoint_" + str(idx) + "_" + target_model_name + '.h5', 'okgreen') if save_model is not None: save_model.save("../weights/" + target_model_name + "/checkpoint_" + str(idx) + "_" + target_model_name + '.h5') checkpoint = 0 printc( "Saving model to " + "../weights/" + target_model_name + "/twa_final_" + target_model_name + '.h5', 'okgreen') save_model.save("../weights/" + target_model_name + "/twa_final_" + target_model_name + '.h5')
def main(): gpus = digit_counter(os.environ["CUDA_VISIBLE_DEVICES"])[0] params = [[0.001, 80, 10, 2, 0.6], [0.001, 320, 40, 8, 0.6], [0.001, 160, 20, 4, 0.6], [0.001, 640, 80, 16, 0.6], [0.001, 960, 120, 24, 0.6], [0.001, 1280, 160, 32, 0.6]] # [0.001, 1600, 200, 40, 0.6]] for id, param in enumerate(params): name = str(param) + str(datetime.now()).replace(" ", "_") # tensorboard_cb = keras.callbacks.TensorBoard(log_dir='./graph', histogram_freq=0, # write_graph=True, write_images=True) stop = EarlyStopping(monitor='val_loss', min_delta=0.001, patience=5, verbose=1, mode='auto') # checkpointer = ModelCheckpoint(filepath='../weights/checkpoint.hdf5', verbose=1, # save_best_only=True) history = LossHistory() callbacks = [history, stop] if USE_MULTI is True: parallel_model, model = network.create_model(model_params=param, multi_gpu=USE_MULTI, gpus=gpus) train_with_all(DATASET_PATH, VALSET_PATH, target_model_name=name, model=parallel_model, save_model=model, nb_epochs=20, callbacks=callbacks) else: model = network.create_model(model_params=param, multi_gpu=USE_MULTI, gpus=gpus) train_with_all(DATASET_PATH, VALSET_PATH, target_model_name=name, model=model, save_model=model, nb_epochs=20, callbacks=callbacks) printc("Param set: {} done".format(id)) printc("Params: {}".format(param)) printc("All done", 'okgreen')
def create_model(model_params=[0.001], multi_gpu=False, gpus=2): img = Input(shape=(IMG_WIDTH, IMG_HEIGHT, 3), name='img') mobilenet = MobileNetV2(input_shape=(IMG_WIDTH, IMG_HEIGHT, 3), alpha=1.0, depth_multiplier=1, include_top=False, weights='imagenet', pooling='max')(img) # mobilenet.trainable = False # Default params model = [] if len(model_params) == 1: x = Dense(640, input_shape=(1, 1280), activation='relu')(mobilenet) x = Dropout(0.2)(x) x = Dense(80, activation='relu')(x) x = Dropout(0.2)(x) x = Dense(16, activation='relu')(x) x = Dropout(0.2)(x) steer = Dense(1, activation='tanh', name='steer')(x) throttle = Dense(1, activation='tanh', name='throttle')(x) # brake = Dense(1, activation='tanh', name='brake')(x) model = Model(inputs=img, outputs=[steer, throttle]) elif len(model_params) == 5: print(" ") print("Params:") print("Learning rate:", end=''); printc(" {}".format(model_params[0]), 'okgreen') print("Dense_1:", end=''); printc(" {}".format(model_params[1]), 'okgreen') print("Dense_2:", end=''); printc(" {}".format(model_params[2]), 'okgreen') print("Dense_3:", end=''); printc(" {}".format(model_params[3]), 'okgreen') print("Dropout rate:", end=''); printc(" {}".format(model_params[4]), 'okgreen') print(" ") x = Dense(model_params[1], input_shape=(1, 1280), activation='relu')(mobilenet) x = Dropout(model_params[4])(x) x = Dense(model_params[2], activation='relu')(x) x = Dropout(model_params[4])(x) x = Dense(model_params[3], activation='relu')(x) x = Dropout(model_params[4])(x) steer = Dense(1, activation='tanh', name='steer')(x) throttle = Dense(1, activation='tanh', name='throttle')(x) # brake = Dense(1, activation='tanh', name='brake')(x) model = Model(inputs=img, outputs=[steer, throttle]) else: printc("Error: malformed model_params argument. Expected size 5 got size {}".format(len(model_params))) for layers in model.layers[:2]: layers.trainable = False adam = optimizers.Adam(lr=model_params[0]) print("Compiling model, will use ", end='') if multi_gpu is True: printc("{} GPUs".format(gpus), 'okgreen') parallel_model = multi_gpu_model(model, gpus=gpus) parallel_model.compile(optimizer=adam, loss=euclidean_distance_loss) else: printc("a single GPU") model.compile(optimizer=adam, loss=euclidean_distance_loss) model.summary() if multi_gpu is True: return parallel_model, model else: return model
parser = argparse.ArgumentParser(description='Plot loss history file') parser.add_argument('val_path', metavar='val_path', type=str, help='filepath of validation score file') # parser.add_argument('hist_path', metavar='hist_path', type=str, nargs='1', # help='filepath of history file') args = parser.parse_args() val_path = args.val_path # hist_path = args.hist_path if val_path is None: printc("Error: val_path == None, no val_path specified, cannot plot data") sys.exit(0) # elif hist_path is None: # printc("Error: hist_path == None, no hist_path specified, cannot plot data") # sys.exit(0) printc("Displaying val plot: {}".format(val_path), 'warn') fig = plt.figure(1, figsize=(20, 8)) fig.suptitle("Validation loss per episode in validation set") data = pd.read_csv(val_path, header=None, names=[i for i in range(5)]) objects = [str(i) for i in range(data.shape[0])] x = np.arange(len(objects)) plt.subplot(121)
import sys from coloured_print import printc parser = argparse.ArgumentParser(description='Plot loss history file') parser.add_argument('filepath', metavar='filepath', type=str, nargs='+', help='filepath of history file(s)') args = parser.parse_args() filepath = args.filepath if filepath is None: printc("Error: filepath == None, no filepath specified, cannot plot data") sys.exit(0) print("Displaying loss plot: {}".format(filepath)) nb_epochs = 20 data = [None] * len(filepath) # concatenated = [None] * len(filepath) fig = plt.figure(1, figsize=(12, 8)) fig.suptitle("Training loss") for idx, path in enumerate(filepath): data[idx] = pd.read_csv(path, header=None,