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)
Ejemplo n.º 2
0
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)

# In[12]:

# Save your trained model weights
weight_file_name = 'model_weights'
Ejemplo n.º 3
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