def train(model: Model, x_train, x_test, y_train, y_test):
    """
    Trains the model with the modified MNIST dataset and loads it with the best weights
    :param model: Model to be trained
    :param x_train: Training X data from modified MNIST
    :param x_test: Test X data from modified MNIST
    :param y_train: Training Y data from modified MNIST
    :param y_test: Test Y data from modified MNIST
    """
    model_path = os.path.join(models_path, "UNPROCESSED_" + "fold" + str(FOLD_NUMBER) + "_" + MODEL + ".h5")
    callbacks = [ModelCheckpoint(model_path, monitor='val_acc', mode='max', verbose=1, save_best_only=True), ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=3, verbose=1, min_lr=0.00001)]
    if GENERATE_TEMP_PREDICTIONS:
        callbacks.append(ProduceTempPredictions(model))

    # Make the y inputs categorical
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)

    # Define the data generator to perform data augmentation
    datagen = ImageDataGenerator(rotation_range=10, zoom_range=0.1, width_shift_range=0.1, height_shift_range=0.1)
    datagen.fit(x_train)

    print("Training unprocessed data with " + MODEL)
    batch_size = 32
    history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), validation_data=(x_test, y_test), epochs=EPOCH, steps_per_epoch=int(x_train.shape[0]/batch_size), verbose=2, callbacks=callbacks)

    # Save the training history
    save_training_history(history.history, os.path.join(results_path, "UNPROCESSED_" + "fold" + str(FOLD_NUMBER) + "_" + MODEL + "_acc.png"), os.path.join(results_path, "UNPROCESSED_" + "fold" + str(FOLD_NUMBER) + "_" + MODEL + "_loss.png"))
    dictionary_to_json(os.path.join(results_path, "UNPROCESSED_" + "fold" + str(FOLD_NUMBER) + "_" + MODEL + "_results.json"), history.history)

    # Load the model with the best weights
    load_model(model_path, model)
def run():
    print("Evaluating predictions with model " + MODEL + " on unprocessed dataset. Fold number:" + str(FOLD_NUMBER))
    # Instantiate the appropriate model
    model = get_model(MODEL, input_shape=(MOD_MNIST_PIXEL, MOD_MNIST_PIXEL, 1),
                      num_categories=NUM_CATEGORIES)
    model_path = os.path.join(models_path, "UNPROCESSED_" + "fold" + str(FOLD_NUMBER) + "_" + MODEL + ".h5")

    if FOLD_NUMBER < 0 or FOLD_NUMBER > 4:
        raise Exception("The fold number must be between 0 and 4")

    print("Loading modified MNIST train dataset")
    x_train, y_train = load_modified_MNIST_training()

    x_train = prepare_for_model_training(x_train)
    i = 0
    for train_index, test_index in KFold(n_splits=5, random_state=None, shuffle=False).split(x_train, y_train):
        if not i == FOLD_NUMBER:
            i += 1
            continue
        x_train, x_test = x_train[train_index], x_train[test_index]
        y_train, y_test = y_train[train_index], y_train[test_index]
        break

    if not retrain_models:
        try:
            # Try to load the weights if we do not want to retrain
            load_model(model_path, model)
        except:
            print("\tThe model file cannot be found at " + model_path + " so it will be retrained.")
            train(model, x_train, x_test, y_train, y_test)
    else:
        if transfer_learning:
            try:
                load_model(model_path, model)
                print("Transfer learning enabled, loaded old weights")
            except:
                print("Transfer learning enabled but no old weights exist")
        train(model, x_train, x_test, y_train, y_test)

    print("Predicting training data...")
    # predict the output
    y_pred = model.predict(x_test).argmax(axis=1)

    # Save a confusion matrix
    conf_mat_file_path = os.path.join(results_path, "UNPROCESSED_" + "fold" + str(FOLD_NUMBER) + "_" + MODEL + "_confusion.png")
    save_confusion_matrix(confusion_matrix(y_test, y_pred), list(map(lambda x: str(x), range(10))),
                          conf_mat_file_path,
                          title="Predictions with model " + MODEL)

    print("Validation accuracy: ", accuracy_score(y_test, y_pred))

    produce_kaggle_results(model)
예제 #3
0
def train(model: Model, x_triplet, y_triplet, split: float):
    """
    Trains the model with the triplet MNIST dataset and loads it with the best weights
    :param model: Model to be trained
    :param x_triplet: X triplet dataset
    :param y_triplet: Y triplet dataset
    :param split: Percentage of the data to be used for the training
    """
    model_path = os.path.join(
        models_path, "TRIPLET_" + MODEL + "_removeback" +
        str(REMOVE_BACKGROUND_TRIO) + ".h5")
    mc = ModelCheckpoint(model_path,
                         monitor='val_acc',
                         mode='max',
                         verbose=1,
                         save_best_only=True)

    split = int(x_triplet.shape[0] * split)
    # Split into training ad testing set
    x_train = x_triplet[:split]
    y_train = y_triplet[:split]
    x_test = x_triplet[split:]
    y_test = y_triplet[split:]

    print("Training Triplet " + MODEL + " on with background removed as " +
          str(REMOVE_BACKGROUND_TRIO))
    history = model.fit(x=x_train,
                        y=to_categorical(y_train),
                        batch_size=128,
                        epochs=EPOCH,
                        verbose=2,
                        callbacks=[mc],
                        validation_data=(x_test, to_categorical(y_test)))

    # Save the training history
    save_training_history(
        history.history,
        os.path.join(
            results_path, "TRIPLET_" + MODEL + "_removeback" +
            str(REMOVE_BACKGROUND_TRIO) + "_acc.png"),
        os.path.join(
            results_path, "TRIPLET_" + MODEL + "_removeback" +
            str(REMOVE_BACKGROUND_TRIO) + "_loss.png"))
    dictionary_to_json(
        os.path.join(
            results_path, "TRIPLET_" + MODEL + "_removeback" +
            str(REMOVE_BACKGROUND_TRIO) + "_results.json"), history.history)

    # Load the model with the best weights
    load_model(model_path, model)
예제 #4
0
def train(model: Model):
    """
    Trains the model with the correct MNIST dataset and loads it with the best weights
    :param model: Model to be trained
    """
    model_path = os.path.join(models_path, "ISOLATED_" + MODEL + "_" + ISOLATED_PRED_DATASET + ".h5")
    mc = ModelCheckpoint(model_path, monitor='val_acc', mode='max', verbose=1,
                         save_best_only=True)

    (x_train, y_train), (x_test, y_test) = get_MNIST(ISOLATED_PRED_DATASET)

    print("Training " + MODEL + " on " + ISOLATED_PRED_DATASET + " dataset")
    history = model.fit(x=x_train, y=to_categorical(y_train), batch_size=128, epochs=EPOCH, verbose=2, callbacks=[mc], validation_data=(x_test, to_categorical(y_test)))

    # Save the training history
    save_training_history(history.history, os.path.join(results_path, "ISOLATED_" + MODEL + "_" + ISOLATED_PRED_DATASET + "_acc.png"), os.path.join(results_path, "ISOLATED_" + MODEL + "_" + ISOLATED_PRED_DATASET + "_loss.png"))
    dictionary_to_json(os.path.join(results_path, "ISOLATED_" + MODEL + "_" + ISOLATED_PRED_DATASET + "_results.json"), history.history)

    # Load the model with the best weights
    load_model(model_path, model)
예제 #5
0
def run():
    if MODEL == "ResNet":
        raise Exception("The triplet predictions can only be done using the CNN, please change the MODEL parameter in the config file")

    print("Evaluating Independent predictions with model " + MODEL + " with dataset " + ISOLATED_PRED_DATASET)
    # Instantiate the appropriate model
    model = get_model(MODEL, input_shape=(MNIST_PIXEL, MNIST_PIXEL, 1), num_categories=NUM_CATEGORIES)
    model_path = os.path.join(models_path, "ISOLATED_" + MODEL + "_" + ISOLATED_PRED_DATASET + ".h5")
    if not retrain_models:
        try:
            # Try to load the weights if we do not want to retrain
            load_model(model_path, model)
        except:
            print("\tThe model file cannot be found at " + model_path + " so it will be retrained.")
            train(model)
    else:
        if transfer_learning:
            try:
                load_model(model_path, model)
                print("Transfer learning enabled, loaded old weights")
            except:
                print("Transfer learning enabled but no old weights exist")
        train(model)

    print("Loading modified MNIST training dataset...")
    x_test, y_test = load_modified_MNIST_training()

    print("Predicting training data...")
    # predict the output
    max_predictor = MaxMNISTPredictor(model)
    y_pred = max_predictor.predict_max_num(x_test)

    # Save a confusion matrix
    conf_mat_file_path = os.path.join(results_path, "ISOLATED_" + MODEL + "_" + ISOLATED_PRED_DATASET + "_confusion.png")
    save_confusion_matrix(confusion_matrix(y_test, y_pred), list(map(lambda x: str(x), range(10))),
                          conf_mat_file_path,
                          title="Isolated predictions with inner model " + MODEL + " trained on dataset " + ISOLATED_PRED_DATASET)

    print("Validation accuracy: ", accuracy_score(y_test, y_pred))

    produce_kaggle_results(model)
예제 #6
0
def run():
    if MODEL == "ResNet":
        raise Exception(
            "The triplet predictions can only be done using the CNN, please change the MODEL parameter in the config file"
        )

    print(
        "Evaluating Triplet predictions with model " + MODEL +
        " and with background removal", REMOVE_BACKGROUND_TRIO)

    print("Loading modified MNIST train dataset")
    x_train, y_train = load_modified_MNIST_training()

    print("Transforming training set to Triplet set")
    x_triplet, y_triplet = transform_to_trio_MNIST(x_train, y_train)
    x_triplet = prepare_for_model_training(x_triplet)
    del x_train
    del y_train

    # Instantiate the appropriate model
    model = get_model(MODEL,
                      input_shape=(MNIST_PIXEL,
                                   NUMBERS_PER_PICTURE * MNIST_PIXEL, 1),
                      num_categories=NUM_CATEGORIES)
    model_path = os.path.join(
        models_path, "TRIPLET_" + MODEL + "_removeback" +
        str(REMOVE_BACKGROUND_TRIO) + ".h5")

    split = 0.8

    if not retrain_models:
        try:
            # Try to load the weights if we do not want to retrain
            load_model(model_path, model)
        except:
            print("\tThe model file cannot be found at " + model_path +
                  " so it will be retrained.")
            train(model, x_triplet, y_triplet, split)
    else:
        if transfer_learning:
            try:
                load_model(model_path, model)
                print("Transfer learning enabled, loaded old weights")
            except:
                print("Transfer learning enabled but no old weights exist")
        train(model, x_triplet, y_triplet, split)

    print("Predicting training data...")
    # predict the output
    y_pred = model.predict(x_triplet[int(x_triplet.shape[0] *
                                         split):]).argmax(axis=1)
    y_true = y_triplet[int(y_triplet.shape[0] * split):]

    # Save a confusion matrix
    conf_mat_file_path = os.path.join(
        results_path, "TRIPLET_" + MODEL + "_removeback" +
        str(REMOVE_BACKGROUND_TRIO) + "_confusion.png")
    save_confusion_matrix(confusion_matrix(y_true, y_pred),
                          list(map(lambda x: str(x), range(10))),
                          conf_mat_file_path,
                          title="Triplet predictions with model " + MODEL +
                          ", removed background: " +
                          str(REMOVE_BACKGROUND_TRIO))

    print("Validation accuracy: ", accuracy_score(y_true, y_pred))

    produce_kaggle_results(model)