Ejemplo n.º 1
0
def train(x_train, y_train, x_test, y_test):
    # Init Keras Model here
    model = VDCNN(num_classes=y_train.shape[1],
                  depth=FLAGS.depth,
                  sequence_length=FLAGS.sequence_length,
                  shortcut=FLAGS.shortcut,
                  pool_type=FLAGS.pool_type,
                  sorted=FLAGS.sorted,
                  use_bias=FLAGS.use_bias)

    model.load_weights('./checkpoints/vdcnn_weights_val_acc_0.6634.h5',
                       by_name=True)

    model.compile(optimizer=SGD(lr=0.001, momentum=0.9),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    model_json = model.to_json()
    with open("vdcnn_model.json", "w") as json_file:
        json_file.write(model_json)  # Save model architecture
    time_str = datetime.datetime.now().isoformat()
    print("{}: Model saved as json.".format(time_str))
    print("")

    # Trainer
    # Tensorboard and extra callback to support steps history
    tensorboard = TensorBoard(log_dir='./logs',
                              histogram_freq=50,
                              batch_size=FLAGS.batch_size,
                              write_graph=True,
                              write_images=True)
    checkpointer = ModelCheckpoint(
        filepath="/checkpoints/vdcnn_weights_val_acc_0.5931.h5",
        period=1,
        verbose=1,
        save_best_only=True,
        mode='max',
        monitor='val_acc')
    loss_history = custom_callbacks.loss_history(model, tensorboard)
    evaluate_step = custom_callbacks.evaluate_step(model, checkpointer,
                                                   tensorboard,
                                                   FLAGS.evaluate_every,
                                                   FLAGS.batch_size, x_test,
                                                   y_test)

    # Fit model
    model.fit(
        x_train,
        y_train,
        batch_size=FLAGS.batch_size,
        epochs=FLAGS.num_epochs,
        validation_data=(x_test, y_test),
        verbose=1,
        callbacks=[checkpointer, tensorboard, loss_history, evaluate_step])
    print('-' * 30)
    time_str = datetime.datetime.now().isoformat()
    print("{}: Done training.".format(time_str))
    K.clear_session()
    print('-' * 30)
    print()
Ejemplo n.º 2
0
Archivo: train.py Proyecto: umer7/VDCNN
def train(x_train, y_train, x_test, y_test):
    # Init Keras Model here
    x_train, x_val, y_train, y_val = train_test_split(x_train,
                                                      y_train,
                                                      test_size=0.2,
                                                      random_state=1)

    model = VDCNN(num_classes=1,
                  depth=FLAGS.depth,
                  sequence_length=FLAGS.sequence_length,
                  shortcut=FLAGS.shortcut,
                  pool_type=FLAGS.pool_type,
                  sorted=FLAGS.sorted,
                  use_bias=FLAGS.use_bias)
    adam = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

    model.compile(optimizer=adam,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    model_json = model.to_json()
    with open("vdcnn_model.json", "w") as json_file:
        json_file.write(model_json)  # Save model architecture
    time_str = datetime.datetime.now().isoformat()
    print("{}: Model saved as json.".format(time_str))
    print("")

    # Trainer
    # Tensorboard and extra callback to support steps history
    tensorboard = TensorBoard(log_dir='./logs',
                              histogram_freq=50,
                              batch_size=FLAGS.batch_size,
                              write_graph=True,
                              write_images=True)
    checkpointer = ModelCheckpoint(
        filepath=
        "/home/nujud/Desktop/VDCNN-keras_version (1)/logs/checkpoints/vdcnn_weights_val_acc_{val_acc:.4f}.h5",
        period=1,
        verbose=1,
        save_best_only=True,
        mode='max',
        monitor='val_acc')
    x_train, x_val, y_train, y_val = train_test_split(x_train,
                                                      y_train,
                                                      test_size=0.3,
                                                      random_state=1)

    loss_history = custom_callbacks.loss_history(model, tensorboard)
    evaluate_step = custom_callbacks.evaluate_step(model, checkpointer,
                                                   tensorboard,
                                                   FLAGS.evaluate_every,
                                                   FLAGS.batch_size, x_val,
                                                   y_val)
    testcallback = custom_callbacks.TestCallback(model, (x_test, y_test))

    # Fit model
    model.fit(x_train,
              y_train,
              batch_size=FLAGS.batch_size,
              epochs=FLAGS.num_epochs,
              validation_data=(x_val, y_val),
              shuffle=True,
              verbose=1,
              callbacks=[
                  checkpointer, tensorboard, loss_history, evaluate_step,
                  testcallback
              ])
    print('-' * 30)
    time_str = datetime.datetime.now().isoformat()
    print("{}: Done training.".format(time_str))
    K.clear_session()
    print('-' * 30)
    print()
Ejemplo n.º 3
0
def train(x_train, y_train, x_test, y_test):
    # Init Keras Model here

    # class_weights = class_weight.compute_class_weight('balanced',
    #                                                   np.unique(y_train),
    #                                                   y_train)
    class_weight_dict = {0: 0.7, 1: 1.6}

    # class_weight_dict = dict(enumerate(class_weights))

    model = VDCNN(
        num_classes=y_train.shape[1],  #
        depth=FLAGS.depth,
        sequence_length=FLAGS.sequence_length,
        shortcut=FLAGS.shortcut,
        pool_type=FLAGS.pool_type,
        sorted=FLAGS.sorted,
        use_bias=FLAGS.use_bias)

    model.compile(optimizer=SGD(lr=0.01, momentum=0.9),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    model_json = model.to_json()
    with open("vdcnn_model.json", "w") as json_file:
        json_file.write(model_json)  # Save model architecture
    time_str = datetime.datetime.now().isoformat()
    print("{}: Model saved as json.".format(time_str))
    print("")

    # Trainer
    # Tensorboard and extra callback to support steps history
    tensorboard = TensorBoard(log_dir='./logs',
                              histogram_freq=50,
                              batch_size=FLAGS.batch_size,
                              write_graph=True,
                              write_images=True)
    checkpointer = ModelCheckpoint(
        filepath="./checkpoints/vdcnn_weights_val_acc_{val_acc:.4f}.h5",
        period=1,
        verbose=1,
        save_best_only=True,
        mode='max',
        monitor='val_acc')
    loss_history = custom_callbacks.loss_history(model, tensorboard)
    evaluate_step = custom_callbacks.evaluate_step(model, checkpointer,
                                                   tensorboard,
                                                   FLAGS.evaluate_every,
                                                   FLAGS.batch_size, x_test,
                                                   y_test)

    clr = CyclicLR(base_lr=0.004,
                   max_lr=0.01,
                   step_size=2000.,
                   mode='triangular',
                   gamma=0.99994)
    validation_data = (x_test, y_test)
    metrics = Metrics(validation_data)

    # Fit model
    model.fit(x_train,
              y_train,
              batch_size=FLAGS.batch_size,
              epochs=FLAGS.num_epochs,
              validation_data=(x_test, y_test),
              class_weight=class_weight_dict,
              verbose=1,
              callbacks=[
                  checkpointer, tensorboard, loss_history, evaluate_step, clr,
                  metrics
              ])
    print('-' * 30)
    time_str = datetime.datetime.now().isoformat()
    print("{}: Done training.".format(time_str))
    K.clear_session()
    print('-' * 30)
    print()