Exemplo n.º 1
0
    def full_train(self,
                   lr_func,
                   save_model_path,
                   batch_size=1,
                   nb_epoch=150,
                   patience=10):

        # prepare data for training
        folded_data = prepare_full_train_data_from_multiple_datasets(
            self.datasets, self.add_extra_atom_attribute,
            self.add_extra_bond_attribute, self.differentiate_atom_type,
            self.differentiate_bond_type, self.padding,
            self.padding_final_size, self.prediction_task)

        X_test, y_test, X_train, y_train = folded_data

        losses = []
        inner_val_losses = []
        test_losses = []
        data = split_inner_val_from_train_data(X_train, y_train)

        X_train, X_inner_val, y_train, y_inner_val = data

        # execute train_model
        logging.info('\nStart full training...')
        logging.info('Training data: {} points'.format(len(X_train)))
        logging.info('Inner val data: {} points'.format(len(X_inner_val)))
        logging.info('Test data: {} points'.format(len(X_test)))
        train_model_output = train_model(self.model,
                                         X_train,
                                         y_train,
                                         X_inner_val,
                                         y_inner_val,
                                         X_test,
                                         y_test,
                                         X_outer_val=None,
                                         y_outer_val=None,
                                         nb_epoch=nb_epoch,
                                         batch_size=batch_size,
                                         lr_func=lr_func,
                                         patience=patience)

        model, loss, inner_val_loss, mean_outer_val_loss, mean_test_loss = train_model_output

        # loss and inner_val_loss each is a list
        # containing loss for each epoch
        losses.append(loss)
        inner_val_losses.append(inner_val_loss)
        test_losses.append(mean_test_loss)

        # save model and write report
        fpath = os.path.join(save_model_path, 'full_train')
        self.save_model(loss, inner_val_loss, mean_outer_val_loss,
                        mean_test_loss, fpath)
def cnn_train_simulation(trainSet, testSet):
    latent_dim, epochs, batch_size = ask_for_hyperparameters()

    check = input("Do you want to load a pretrained cnn model? [y/n]: ")
    if (check == 'y'):
        modelPath = input(
            "> Give the path in which the cnn model is located: ")
        if (os.path.isfile(modelPath) == False):
            die("\n[+] Error: File \"{}\" does not exist!\n".format(modelPath),
                -1)
        model = keras.models.load_model(modelPath)
    else:
        autoencoder = Autoencoder(trainSet, testSet, epochs, batch_size,
                                  latent_dim)
        input_img = keras.Input(shape=(rows, cols, 1))
        encoded = autoencoder.encoder(input_img)
        decoded = autoencoder.decoder(encoded)
        model = autoencoder.compile_model(input_img, decoded)

    train_model(model, trainSet, testSet, epochs, batch_size)

    return model
Exemplo n.º 3
0
num_filters_per_hidden_layer = [12, 25, 50, 100, 200]
patience = 20

model_name = "MODEL_NAME_HERE"
model_save_path = parent_cwd + "/Saved_Models/" + model_name
#model = cnn.load_model_(model_save_path)

model = cnn.create_model(
    input_shape, num_filters_per_hidden_layer,
    map(int, list(np.array(filter_size_per_hidden_layer) * fs)),
    int(filter_size_output_layer * fs))
model, history = cnn.train_model(model=model,
                                 train_inputs=train_noisy,
                                 train_labels=train_clean,
                                 epochs=epochs,
                                 batch_size=batch_size,
                                 validation_inputs=validation_noisy,
                                 validation_labels=validation_clean,
                                 filepath=model_save_path,
                                 patience=patience)

print("Getting CNN output for noisy test set input...")
test_filtered_frames = (
    train_std * cnn.get_output_multiple_batches(model, test_noisy)) + train_mu

print("Perfectly reconstructing filtered test set audio & saving to memory...")
test_filtered = ap.rebuild_audio(test_filtered_frames)
scipy.io.wavfile.write(filename=parent_cwd + "/Audio_Files/Test_Files/" +
                       model_name + "_FilteredTest_" + str(snr_db) +
                       "dB_5min.wav",
                       rate=fs,
Exemplo n.º 4
0
    def kfcv_train(self,
                   folds,
                   lr_func,
                   save_model_path,
                   batch_size=1,
                   nb_epoch=150,
                   patience=10):

        # prepare data for training
        folded_data = prepare_folded_data_from_multiple_datasets(
            self.datasets, folds, self.add_extra_atom_attribute,
            self.add_extra_bond_attribute, self.differentiate_atom_type,
            self.differentiate_bond_type, self.padding,
            self.padding_final_size, self.prediction_task)

        X_test, y_test, folded_Xs, folded_ys = folded_data

        losses = []
        inner_val_losses = []
        outer_val_losses = []
        test_losses = []
        for fold in range(folds):
            data = prepare_data_one_fold(folded_Xs,
                                         folded_ys,
                                         current_fold=fold,
                                         shuffle_seed=4)

            # execute train_model
            X_train, X_inner_val, X_outer_val, y_train, y_inner_val, y_outer_val = data
            train_model_output = train_model(self.model,
                                             X_train,
                                             y_train,
                                             X_inner_val,
                                             y_inner_val,
                                             X_test,
                                             y_test,
                                             X_outer_val,
                                             y_outer_val,
                                             nb_epoch=nb_epoch,
                                             batch_size=batch_size,
                                             lr_func=lr_func,
                                             patience=patience)

            model, loss, inner_val_loss, mean_outer_val_loss, mean_test_loss = train_model_output

            # loss and inner_val_loss each is a list
            # containing loss for each epoch
            losses.append(loss)
            inner_val_losses.append(inner_val_loss)
            outer_val_losses.append(mean_outer_val_loss)
            test_losses.append(mean_test_loss)

            # save model and write fold report
            fpath = os.path.join(save_model_path, 'fold_{0}'.format(fold))
            self.save_model(loss, inner_val_loss, mean_outer_val_loss,
                            mean_test_loss, fpath)

            # once finish training one fold, reset the model
            self.reset_model()

        # mean inner_val_loss and outer_val_loss used for selecting parameters,
        # e.g., lr, epoch, attributes, etc
        full_folds_mean_loss = np.mean([l[-1] for l in losses if len(l) > 0])
        full_folds_mean_inner_val_loss = np.mean(
            [l[-1] for v_l in inner_val_losses if len(l) > 0])
        full_folds_mean_outer_val_loss = np.mean(outer_val_losses)
        full_folds_mean_test_loss = np.mean(test_losses)

        full_folds_loss_report_path = os.path.join(
            save_model_path, 'full_folds_loss_report.txt')

        write_loss_report(full_folds_mean_loss, full_folds_mean_inner_val_loss,
                          full_folds_mean_outer_val_loss,
                          full_folds_mean_test_loss,
                          full_folds_loss_report_path)
Exemplo n.º 5
0
from cnn_model import train_model, cnn_model1, cnn_model2, alex

if __name__ == '__main__':
    model = cnn_model2()
    train_model(model)