Ejemplo n.º 1
0
def train_network(weights_file="weights.hdf5", classpath="Preproc/Train/",
    epochs=50, batch_size=20, val_split=0.2, tile=False, max_per_class=0,
    k_fold=1):

    np.random.seed(1)  # fix a number to get reproducibility; comment out for random behavior

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(path=classpath,
        batch_size=batch_size, tile=tile, max_per_class=max_per_class)

    save_best_only = (val_split > 1e-6)
    assert k_fold  <= 1/val_split   # make sure we don't repeat folds

    for k in range(k_fold):

        # Instantiate the model
        model, serial_model = setup_model(X_train, class_names,
            weights_file=weights_file, quiet=(k!=0))

        # Split between Training and Validation Set, val_split = percentage to use for val
        split_index = int(X_train.shape[0]*(1-val_split))   # Train first, Val second
        X_val, Y_val = X_train[split_index:], Y_train[split_index:]
        X_train, Y_train = X_train[:split_index], Y_train[:split_index]

        # if we're doing k-folding cross-val, don't overwrite the weights file until the last time
        callbacks = None if (k < k_fold-1) else [MultiGPUModelCheckpoint(filepath=weights_file, verbose=1, save_best_only=save_best_only,
              serial_model=serial_model, period=1, class_names=class_names)]

        steps_per_epoch = X_train.shape[0] // batch_size
        if False and ((len(class_names) > 2) or (steps_per_epoch > 1)):
            training_generator = MixupGenerator(X_train, Y_train, batch_size=batch_size, alpha=0.25)()
            model.fit_generator(generator=training_generator, steps_per_epoch=steps_per_epoch,
                  epochs=epochs, shuffle=True,
                  verbose=1, callbacks=callbacks, validation_data=(X_val, Y_val))
        else:
            model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, shuffle=True,
                  verbose=1, callbacks=callbacks, #validation_split=val_split)
                  validation_data=(X_val, Y_val))

        if k < k_fold-1: # reconstitute and re-split the data for the next loop
            print("\n\n------ Starting another round of cross-validation, for k =",k+2,"/",k_fold,"------")
            X_train = np.concatenate((X_val, X_train))  # stick the val at the front this time
            Y_train = np.concatenate((Y_val, Y_train))


    # overwrite text file class_names.txt  - does not put a newline after last class name
    with open('class_names.txt', 'w') as outfile:
        outfile.write("\n".join(class_names))

    # Score the model against Test dataset
    X_test, Y_test, paths_test, class_names_test  = build_dataset(path=classpath+"../Test/", tile=tile)
    assert( class_names == class_names_test )
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Ejemplo n.º 2
0
def train_network(weights_file="weights.hdf5",
                  classpath="Preproc/Train/",
                  epochs=50,
                  batch_size=20,
                  val_split=0.25,
                  tile=False):
    np.random.seed(1)
    from keras import backend as K
    # prevent TF from consuming whole memory in GPU
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.8
    sess = tf.Session(config=config)
    K.set_session(sess)
    K.set_image_data_format('channels_last')  #make sure we use current

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(
        path=classpath, batch_size=batch_size, tile=tile)

    # Instantiate the model
    model, serial_model = setup_model(X_train,
                                      class_names,
                                      weights_file=weights_file)

    save_best_only = (val_split > 1e-6)
    checkpointer = MultiGPUModelCheckpoint(filepath=weights_file,
                                           verbose=1,
                                           save_best_only=save_best_only,
                                           serial_model=serial_model,
                                           period=1,
                                           class_names=class_names)
    #earlystopping = EarlyStopping(patience=12)

    model.fit(X_train,
              Y_train,
              batch_size=batch_size,
              epochs=epochs,
              shuffle=True,
              verbose=1,
              callbacks=[checkpointer],
              validation_split=val_split)  # validation_data=(X_val, Y_val),

    # Score the model against Test dataset
    X_test, Y_test, paths_test, class_names_test = build_dataset(
        path=classpath + "../Test/", tile=tile)
    assert (class_names == class_names_test)
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Ejemplo n.º 3
0
def train_network(
    weights_file="weights.hdf5",
    classpath="Preproc/Train/",
    epochs=50,
    batch_size=20,
    val_split=0.25,
    tile=False,
):

    np.random.seed(1)

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(
        path=classpath, batch_size=batch_size, tile=tile)

    # Instantiate the model
    model, serial_model = setup_model(X_train,
                                      class_names,
                                      weights_file=weights_file)

    save_best_only = val_split > 1e-6
    checkpointer = MultiGPUModelCheckpoint(
        filepath=weights_file,
        verbose=1,
        save_best_only=save_best_only,
        serial_model=serial_model,
        period=1,
        class_names=class_names,
    )
    # earlystopping = EarlyStopping(patience=12)

    model.fit(
        X_train,
        Y_train,
        batch_size=batch_size,
        epochs=epochs,
        shuffle=True,
        verbose=1,
        callbacks=[checkpointer],
        validation_split=val_split,
    )

    # Score the model against Test dataset
    X_test, Y_test, paths_test, class_names_test = build_dataset(
        path=classpath + "../Test/", tile=tile)
    assert class_names == class_names_test
    score = model.evaluate(X_test, Y_test, verbose=0)
    print("Test loss:", score[0])
    print("Test accuracy:", score[1])
Ejemplo n.º 4
0
def train_network(weights_file="weights.hdf5",
                  classpath="Preproc/Train/",
                  epochs=50,
                  batch_size=20,
                  val_split=0.25,
                  tile=False):
    np.random.seed(1)

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(
        path=classpath, batch_size=batch_size, tile=tile)

    # Instantiate the model
    model, serial_model = setup_model(X_train,
                                      class_names,
                                      weights_file=weights_file)

    save_best_only = (val_split > 1e-6)
    checkpointer = MultiGPUModelCheckpoint(filepath=weights_file,
                                           verbose=1,
                                           save_best_only=save_best_only,
                                           serial_model=serial_model,
                                           period=1,
                                           class_names=class_names)
    #earlystopping = EarlyStopping(patience=12)

    model.fit(X_train,
              Y_train,
              batch_size=batch_size,
              epochs=epochs,
              shuffle=True,
              verbose=1,
              callbacks=[checkpointer],
              validation_split=val_split)  # validation_data=(X_val, Y_val),

    # overwrite text file class_names.txt  - does not put a newline after last class name
    with open('class_names.txt', 'w') as outfile:
        outfile.write("\n".join(class_names))

    # Score the model against Test dataset
    X_test, Y_test, paths_test, class_names_test = build_dataset(
        path=classpath + "../Test/", tile=tile)
    assert (class_names == class_names_test)
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Ejemplo n.º 5
0
def train_network(weights_file,
                  classpath,
                  epochs,
                  batch_size,
                  val_split,
                  tile=False):
    # Instantiate a random seed for reproducible results
    np.random.seed(1)

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(
        path=classpath, batch_size=batch_size, tile=tile)

    # Instantiate the model
    model, serial_model = setup_model(X_train,
                                      class_names,
                                      weights_file=weights_file)

    save_best_only = (val_split > 1e-6)
    checkpointer = MultiGPUModelCheckpoint(filepath=weights_file,
                                           verbose=1,
                                           save_best_only=save_best_only,
                                           serial_model=serial_model,
                                           period=1,
                                           class_names=class_names)

    # Train the network
    model.fit(X_train,
              Y_train,
              batch_size=batch_size,
              epochs=epochs,
              shuffle=True,
              verbose=1,
              callbacks=[checkpointer],
              validation_split=val_split)

    # Score the model against test dataset
    X_test, Y_test, paths_test, class_names_test = build_dataset(
        path=classpath + '../Test/', tile=tile)
    assert (class_names == class_names_test)
    # Evaluate the model
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Ejemplo n.º 6
0
def train_network(weights_file="weights.hdf5",
                  classpath="Preproc/Train/",
                  epochs=50,
                  batch_size=20,
                  val_split=0.2,
                  tile=False,
                  max_per_class=0):

    np.random.seed(
        1
    )  # fix a number to get reproducibility; comment out for random behavior

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(
        path=classpath,
        batch_size=batch_size,
        tile=tile,
        max_per_class=max_per_class)

    # Instantiate the model
    model, serial_model = setup_model(X_train,
                                      class_names,
                                      weights_file=weights_file)

    save_best_only = (val_split > 1e-6)

    split_index = int(X_train.shape[0] * (1 - val_split))
    X_val, Y_val = X_train[split_index:], Y_train[split_index:]
    X_train, Y_train = X_train[:split_index - 1], Y_train[:split_index - 1]

    checkpointer = MultiGPUModelCheckpoint(filepath=weights_file,
                                           verbose=1,
                                           save_best_only=save_best_only,
                                           serial_model=serial_model,
                                           period=1,
                                           class_names=class_names)

    steps_per_epoch = X_train.shape[0] // batch_size
    if False and ((len(class_names) > 2) or (steps_per_epoch > 1)):
        training_generator = MixupGenerator(X_train,
                                            Y_train,
                                            batch_size=batch_size,
                                            alpha=0.25)()
        model.fit_generator(generator=training_generator,
                            steps_per_epoch=steps_per_epoch,
                            epochs=epochs,
                            shuffle=True,
                            verbose=1,
                            callbacks=[checkpointer],
                            validation_data=(X_val, Y_val))
    else:
        model.fit(
            X_train,
            Y_train,
            batch_size=batch_size,
            epochs=epochs,
            shuffle=True,
            verbose=1,
            callbacks=[checkpointer],  #validation_split=val_split)
            validation_data=(X_val, Y_val))

    # overwrite text file class_names.txt  - does not put a newline after last class name
    with open('class_names.txt', 'w') as outfile:
        outfile.write("\n".join(class_names))

    # Score the model against Test dataset
    X_test, Y_test, paths_test, class_names_test = build_dataset(
        path=classpath + "../Test/", tile=tile)
    assert (class_names == class_names_test)
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Ejemplo n.º 7
0
def train_network(weights_file="weights.hdf5",
                  classpath="Preproc/Train/",
                  epochs=50,
                  batch_size=20,
                  val_split=0.25):
    np.random.seed(1)

    # Get the data
    X_train, Y_train, paths_train, class_names = build_dataset(
        path=classpath, batch_size=batch_size)

    # Instantiate the model
    model, serial_model = make_model(X_train,
                                     class_names,
                                     weights_file=weights_file)

    save_best_only = (val_split > 1e-6)
    checkpointer = MultiGPUModelCheckpoint(filepath=weights_file,
                                           verbose=1,
                                           save_best_only=save_best_only,
                                           serial_model=serial_model,
                                           period=2)
    #earlystopping = EarlyStopping(patience=12)
    logthis = keras.callbacks.TensorBoard(log_dir='./logs/O-.15windows',
                                          histogram_freq=50,
                                          batch_size=32,
                                          write_graph=False,
                                          write_grads=True,
                                          write_images=True,
                                          embeddings_freq=0,
                                          embeddings_layer_names=None,
                                          embeddings_metadata=None)

    def step_decay(epoch):
        initial_lrate = .000035

        drop = .5
        epochs_drop = 500

        lrate = initial_lrate * math.pow(drop,
                                         math.floor((1 + epoch) / epochs_drop))

        return lrate

    change_lr = keras.callbacks.LearningRateScheduler(step_decay)

    model.fit(X_train,
              Y_train,
              batch_size=batch_size,
              epochs=epochs,
              shuffle=True,
              verbose=1,
              callbacks=[checkpointer, logthis, change_lr],
              validation_split=val_split)  # validation_data=(X_val, Y_val),

    # Score the model against Test dataset
    X_test, Y_test, paths_test, class_names_test = build_dataset(
        path=classpath + "../Test/")
    assert (class_names == class_names_test)
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])