Ejemplo n.º 1
0
def train_model(test, inputs, output_layer,image_shape):
    #load variables for the test
    learning_rate = test['learning_rate']
    batch_size = test['batch_size']
    steps_per_epoch = test['steps_per_epoch']
    num_epochs = test['num_epochs']
    validation_steps = test['validation_steps']
    workers = test['workers']

    # Define the Keras model and compile it for training
    model = models.Model(inputs=inputs, outputs=output_layer)

    model.compile(optimizer=keras.optimizers.Adam(learning_rate), loss='categorical_crossentropy')

    # Data iterators for loading the training and validation data
    train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                   data_folder=os.path.join('..', 'data', 'train'),
                                                   image_shape=image_shape,
                                                   shift_aug=True)

    val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                 data_folder=os.path.join('..', 'data', 'validation'),
                                                 image_shape=image_shape)

    #logger_cb = plotting_tools.LoggerPlotter()
    #callbacks = [logger_cb]

    model.fit_generator(train_iter,
                        steps_per_epoch = steps_per_epoch, # the number of batches per epoch,
                        epochs = num_epochs, # the number of epochs to train for,
                        validation_data = val_iter, # validation iterator
                        validation_steps = validation_steps, # the number of batches to validate on

                        workers = workers)
    return model
def train_model(learning_rate, batch_size, num_epochs, steps_per_epoch,
                validation_steps, workers, optimizer, weight_file_name):
    image_hw = 160
    image_shape = (image_hw, image_hw, 3)
    inputs = layers.Input(image_shape)
    num_classes = 3

    # Call fcn_model()
    output_layer = fcn_model(inputs, num_classes)

    # Define the Keras model and compile it for training
    model = models.Model(inputs=inputs, outputs=output_layer)

    if optimizer == 'Nadam':
        model.compile(optimizer=keras.optimizers.Adam(learning_rate),
                      loss='categorical_crossentropy')
    elif optimizer == 'Adam':
        model.compile(optimizer=keras.optimizers.Nadam(lr=learning_rate,
                                                       beta_1=0.9,
                                                       beta_2=0.999,
                                                       epsilon=1e-08,
                                                       schedule_decay=0.004),
                      loss='categorical_crossentropy')
    else:
        raise RuntimeError('Invalid optimizer type "{}"'.format(optimizer))

    # Data iterators for loading the training and validation data
    train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                   data_folder=os.path.join(
                                                       '..', 'data', 'train'),
                                                   image_shape=image_shape,
                                                   shift_aug=True)

    val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                 data_folder=os.path.join(
                                                     '..', 'data',
                                                     'validation'),
                                                 image_shape=image_shape)

    logger_cb = plotting_tools.LoggerPlotter(save_graphs=True)
    callbacks = [logger_cb]

    model.fit_generator(
        train_iter,
        steps_per_epoch=steps_per_epoch,  # the number of batches per epoch,
        epochs=num_epochs,  # the number of epochs to train for,
        validation_data=val_iter,  # validation iterator
        validation_steps=
        validation_steps,  # the number of batches to validate on
        callbacks=callbacks,
        workers=workers)

    # Save your trained model weights
    model_tools.save_network(model, weight_file_name)
    batch_size = int(run_number['batch_size'])
    print("Batch Size: " + str(batch_size))
    num_epochs = int(run_number['num_epochs'])
    print("Epochs: " + str(num_epochs))
    steps_per_epoch = int(run_number['steps_per_epoch'])
    print("Steps per Epoch: " + str(steps_per_epoch))
    validation_steps = int(run_number['validation_steps'])
    print("Validation Steps: " + str(validation_steps))
    workers = int(run_number['workers'])
    print("Workers: " + str(workers))
    print(
        "--------------------------------------------------------------------------------"
    )

    # Define the Keras model and compile it for training
    model = models.Model(inputs=inputs, outputs=output_layer)

    model.compile(optimizer=keras.optimizers.Nadam(learning_rate),
                  loss='categorical_crossentropy')

    # Data iterators for loading the training and validation data
    train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                   data_folder=os.path.join(
                                                       '..', 'data', 'train'),
                                                   image_shape=image_shape,
                                                   shift_aug=True)

    val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                 data_folder=os.path.join(
                                                     '..', 'data',
                                                     'validation'),
Ejemplo n.º 4
0
def train_model(fcn_func, filter_set, learning_params=None):
    """
    DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
    """

    print("Training: ", fcn_func.__name__, str(filter_set))

    image_hw = 160
    image_shape = (image_hw, image_hw, 3)
    inputs = layers.Input(image_shape)
    num_classes = 3

    output_layer = fcn_func(inputs, num_classes, filter_set)

    if learning_params is None:
        learning_rate = 0.002
        batch_size = 40
        num_epochs = 15
        steps_per_epoch = 200
        validation_steps = 50
        workers = 4
    else:
        learning_rate = learning_params["learning_rate"]
        batch_size = learning_params["batch_size"]
        num_epochs = learning_params["num_epochs"]
        steps_per_epoch = learning_params["steps_per_epoch"]
        validation_steps = learning_params["validation_steps"]
        workers = learning_params["workers"]

    ############# Training code ##############
    """
    DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
    """
    # Define the Keras model and compile it for training
    model = models.Model(inputs=inputs, outputs=output_layer)
    model.compile(optimizer=keras.optimizers.Adam(learning_rate),
                  loss='categorical_crossentropy')

    # Data iterators for loading the training and validation data
    train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                   data_folder=os.path.join(
                                                       '..', 'data', 'train'),
                                                   image_shape=image_shape,
                                                   shift_aug=True)

    val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                 data_folder=os.path.join(
                                                     '..', 'data',
                                                     'validation'),
                                                 image_shape=image_shape)

    logger_cb = plotting_tools.LoggerPlotter()
    callbacks = [logger_cb]

    model.fit_generator(
        train_iter,
        steps_per_epoch=steps_per_epoch,  # the number of batches per epoch,
        epochs=num_epochs,  # the number of epochs to train for,
        validation_data=val_iter,  # validation iterator
        validation_steps=
        validation_steps,  # the number of batches to validate on
        # callbacks=callbacks,
        workers=workers)

    print("Done training", fcn_func.__name__, str(filter_set))

    return model
def train_net(x):
    """Trains the network with the suggested hyper-parameters passed in x
    argument. Returns -final_score as a result to let ski-opt library find the
    desired minimum, corresponding to the maximum final_score"""

    learning_rate = x[0]  # 0.001
    batch_size = x[1]  # 32
    num_epochs = x[2]  # 12
    layers_num = x[3]  #2
    conv_layers_num = x[4]  #1
    external_features = x[5]  #128
    internal_features = x[6]  #16
    conv_features = x[7]  #16

    print()
    print("learning_rate", learning_rate)
    print("batch_size", batch_size)
    print("num_epochs", num_epochs)
    print("layers_num", layers_num)
    print("conv_layers_num", conv_layers_num)
    print("external_features", external_features)
    print("internal_features", internal_features)
    print("conv_features", conv_features)

    image_hw = 160
    image_shape = (image_hw, image_hw, 3)
    inputs = layers.Input(image_shape)
    num_classes = 3

    # Call fcn_model()
    output_layer = fcn_model(inputs, num_classes, layers_num,
                             external_features, internal_features,
                             conv_layers_num, conv_features)

    # Define the Keras model and compile it for training
    model = models.Model(inputs=inputs, outputs=output_layer)

    model.compile(optimizer=keras.optimizers.Adam(learning_rate),
                  loss='categorical_crossentropy')

    # Data iterators for loading the training and validation data
    train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                   data_folder=os.path.join(
                                                       '..', 'data',
                                                       'train_combined'),
                                                   image_shape=image_shape,
                                                   shift_aug=True)

    val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                                 data_folder=os.path.join(
                                                     '..', 'data',
                                                     'validation'),
                                                 image_shape=image_shape)

    model.fit_generator(
        train_iter,
        steps_per_epoch=steps_per_epoch,  # the number of batches per epoch,
        epochs=num_epochs,  # the number of epochs to train for,
        validation_data=val_iter,  # validation iterator
        validation_steps=
        validation_steps,  # the number of batches to validate on
        workers=workers)

    run_num = 'run_1'

    val_with_targ, pred_with_targ = model_tools.write_predictions_grade_set(
        model, run_num, 'patrol_with_targ', 'sample_evaluation_data')

    val_no_targ, pred_no_targ = model_tools.write_predictions_grade_set(
        model, run_num, 'patrol_non_targ', 'sample_evaluation_data')

    val_following, pred_following = model_tools.write_predictions_grade_set(
        model, run_num, 'following_images', 'sample_evaluation_data')

    # Scores for while the quad is following behind the target.
    true_pos1, false_pos1, false_neg1, iou1 = scoring_utils.score_run_iou(
        val_following, pred_following)

    # Scores for images while the quad is on patrol and the target is not
    # visible
    true_pos2, false_pos2, false_neg2, iou2 = scoring_utils.score_run_iou(
        val_no_targ, pred_no_targ)

    # This score measures how well the neural network can detect the target
    # from far away
    true_pos3, false_pos3, false_neg3, iou3 = scoring_utils.score_run_iou(
        val_with_targ, pred_with_targ)

    # Sum all the true positives, etc from the three datasets to get a weight
    # for the score
    true_pos = true_pos1 + true_pos2 + true_pos3
    false_pos = false_pos1 + false_pos2 + false_pos3
    false_neg = false_neg1 + false_neg2 + false_neg3

    weight = true_pos / (true_pos + false_neg + false_pos)

    # The IoU for the dataset that never includes the hero is excluded from
    # grading
    final_IoU = (iou1 + iou3) / 2

    # And the final grade score is
    final_score = final_IoU * weight

    weight_file_name = 'model_weights_' + str(final_score)
    model_tools.save_network(model, weight_file_name)
    print("Saved", weight_file_name)

    print("final_score", final_score)
    print()

    return -final_score