Example #1
0
np.random.seed(1337)  # for reproducibility
from sklearn.metrics.classification import accuracy_score

from dbn.tensorflow import SupervisedDBNClassification
from Rafd import Rafd

# Splitting data
rafd = Rafd("entrenamiento/")
X_train, X_test, Y_train, Y_test = rafd.getData()

# Training
classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.001,
                                         n_epochs_rbm=15,
                                         n_iter_backprop=100,
                                         batch_size=32,
                                         activation_function='sigmoid',
                                         dropout_p=0.2)
classifier.fit(X_train, Y_train)

# Save the model
classifier.save('model.pkl')

# Restore it
classifier = SupervisedDBNClassification.load('model.pkl')

# Test
Y_pred = classifier.predict(X_test)
print('Done.\nAccuracy: %f' % accuracy_score(Y_test, Y_pred))
Example #2
0
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics.classification import accuracy_score
from dbn.tensorflow import SupervisedDBNClassification
from keras.utils import np_utils

labels = np.loadtxt('label_205.txt')
encoded_seq = np.loadtxt('encoded_seq_205.txt')

X_train, X_test, Y_train, Y_test = train_test_split(encoded_seq,
                                                    labels,
                                                    test_size=0.2)

classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.1,
                                         n_epochs_rbm=10,
                                         n_iter_backprop=100,
                                         batch_size=100,
                                         activation_function='sigmoid',
                                         dropout_p=0.2)
classifier.fit(X_train, Y_train)
classifier.save('dbn.pkl')

Y_pred = classifier.predict(X_test)
print('Done.\nAccuracy: %f' % accuracy_score(Y_test, Y_pred))
Example #3
0
print('x_test', x_test.shape)
print('y_test', y_test.shape)

#x_test = x_test[:2000]
#y_test = y_test[:2000]

#print('x_train: ', x_train.shape[0])
#print('x_test number: ', x_test.shape[0])

# Training
classifier = SupervisedDBNClassification(
    hidden_layers_structure=[2304, 256, 64],
    learning_rate_rbm=0.05,
    learning_rate=0.1,
    n_epochs_rbm=10,
    n_iter_backprop=60,
    batch_size=64,
    activation_function='relu',
    dropout_p=0.2)
classifier.fit(x_train, y_train)

# Save the model
classifier.save('models/model_mnist.pkl')

# Restore it
#classifier = SupervisedDBNClassification.load('model.pkl')

# Test
Y_pred = classifier.predict(x_test)
print('Done.\nAccuracy: %f' % accuracy_score(y_test, Y_pred))
                                         n_iter_backprop=100,
                                         batch_size=100,
                                         activation_function='relu',
                                         dropout_p=0.2)

# start counting time for training
time_train_start = time.clock()

# Training
classifier.fit(X_train, y_train)

# print training time
time_train_end = time.clock()
print("Training finished, training time: %g seconds \n" %
      (time_train_end - time_train_start))
'''
# Save the model
classifier.save('model.pkl')

# Restore it
classifier = SupervisedDBNClassification.load('model.pkl')
'''
# start counting time for testing
time_test_start = time.clock()

# Test
y_pred = classifier.predict(X_test)
print('Testing finished.\nAccuracy: %f' % accuracy_score(y_test, y_pred))

# print testing time
time_test_end = time.clock()
Example #5
0
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size = 0.22, random_state = 101)

#'''


classifier2 = SupervisedDBNClassification(hidden_layers_structure=[8, 50],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.1,
                                         n_epochs_rbm=30,
                                         n_iter_backprop=100,
                                         batch_size=64,
                                         activation_function='relu',
                                         dropout_p=0.2)
classifier2.fit(X_train, Y_train)
# Save the model
classifier2.save('models/breast_cancer_origin_3.pkl')

# Restore it
#classifier2 = SupervisedDBNClassification.load('models/breast_cancer_origin_2.pkl')

# Test
#X_test = min_max_scaler.transform(X_test)
Y_pred = classifier2.predict(X_test)
print('Accuracy: %f' % accuracy_score(Y_test, Y_pred))


'''


rfc = RandomForestClassifier(n_estimators=201)
rfc.fit(X_train,Y_train)
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

mlp = SupervisedDBNClassification(hidden_layers_structure=[19, 30, 19],
                                  learning_rate_rbm=0.05,
                                  learning_rate=0.1,
                                  n_epochs_rbm=10,
                                  n_iter_backprop=50,
                                  batch_size=32,
                                  activation_function='relu',
                                  dropout_p=0.2)

mlp.fit(X_train, y_train)
# Save the model
mlp.save('model.pkl')
# Restoreit
mlp = SupervisedDBNClassification.load('model.pkl')

predictions = mlp.predict(X_test)

RMSE_sum = 0

list = []
for x in range(0, len(X_test)):

    RMSE_sum = RMSE_sum + ((y_test[x] - predictions[x])**2)
    list.append(abs(y_test[x] - predictions[x]))

RMSE = math.sqrt(RMSE_sum / len(X_test))
print("RMSE :", RMSE)
Example #7
0
def run(params):

    # ##################### get parameters and define logger ################

    # device
    os.environ['CUDA_VISIBLE_DEVICES'] = str(params.gpu)

    # get parameters
    data_name = params.data.data_name
    data_dir = params.data.data_dir
    target_dir = params.data.target_dir
    train_prop = params.data.train_prop
    val_prop = params.data.val_prop

    train_params = params.train
    method_name = params.method_name
    result_dir = params.result_dir
    folder_level = params.folder_level

    train_prop = train_prop if train_prop < 1 else int(train_prop)
    val_prop = val_prop if val_prop < 1 else int(val_prop)

    result_root = result_dir
    local_v = locals()
    for s in folder_level:
        result_dir = check_path(os.path.join(result_dir, str(local_v[s])))

    # define output dirs
    acc_dir = os.path.join(result_root, 'accuracy.csv')
    log_dir = os.path.join(result_dir, 'train.log')
    model_dir = os.path.join(result_dir, 'weights.pkl')
    # soft_dir = os.path.join(result_dir, 'soft_label.mat')
    # loss_dir = os.path.join(result_dir, 'loss_curve.png')

    # define logger
    logger = define_logger(log_dir)

    # print parameters
    num1 = 25
    num2 = 100
    logger.info('%s begin a new training: %s %s' %
                ('#' * num1, method_name, '#' * num1))
    params_str = recur_str_dict_for_show(params, total_space=num2)
    logger.info('show parameters ... \n%s' % params_str)

    # ########################### get data, train ############################

    logger.info('get data ...')
    mask_dir = os.path.dirname(data_dir)
    data, target = read_data(data_dir, target_dir)
    train_mask, val_mask, test_mask = load_masks(mask_dir, target, train_prop,
                                                 val_prop)
    x_train, y_train = get_vector_samples(data, target, train_mask)

    logger.info('get model ...')
    from dbn.tensorflow import SupervisedDBNClassification
    classifier = SupervisedDBNClassification(**train_params)

    logger.info('begin to train ...')
    s = time.time()
    classifier.fit(x_train, y_train)
    e = time.time()
    train_time = e - s
    logger.info('training time: %.4fs' % train_time)

    logger.info('save model ...')
    classifier.save(model_dir)

    # ########################### predict, output ###########################

    all_data = data.reshape(-1, data.shape[1] * data.shape[2]).T

    classifier = SupervisedDBNClassification.load(model_dir)

    logger.info('begin to predict ...')
    s = time.time()
    pred = classifier.predict(all_data)
    pred = np.array(pred)
    pred = pred.reshape(target.shape) + 1
    e = time.time()
    pred_time = (e - s)
    logger.info('predicted time: %.4fs' % pred_time)

    # output predicted map(png/mat), accuracy table and other records
    logger.info('save classification maps etc. ...')
    train_records = {
        'train_time': '%.4f' % train_time,
        'pred_time': '%.4f' % pred_time
    }

    ro = ResultOutput(pred,
                      data,
                      target,
                      train_mask,
                      val_mask,
                      test_mask,
                      result_dir,
                      acc_dir,
                      hyper_params=params,
                      train_records=train_records)
    ro.output()
Example #8
0
        dropout_p=0.2)
    # Split Data
    X, Y = get_dataset(tz)
    X_train, X_test, Y_train, Y_test = train_test_split(X,
                                                        Y,
                                                        test_size=0.2,
                                                        random_state=0)
    print('Size of training set == {}, Size of testing set == {}\n'.format(
        len(X_train), len(X_test)))

    start_time = timer()
    tot_start = start_time
    Matt_Net.pre_train(X_train)
    print('Time to pretrain == {:5.3f} seconds\n'.format(timer() - start_time))

    start_time = timer()
    Matt_Net.fit(X_train, Y_train, False)
    print('Time to fit == {:5.3f} seconds\n'.format(timer() - start_time))
    print('Total time == {:5.3f} seconds\n'.format(timer() - tot_start))

    Matt_Net.save('train/Matt_Net_Zone_{}.pkl'.format(tz))

    Y_pred = Matt_Net.predict(X_test)
    start_time = timer()
    score = accuracy_score(Y_test, Y_pred)
    print(
        'Done, time to predict == {:5.3}\nAccuracy == {} for zone {}\n'.format(
            timer() - start_time, score, tz))

    del Matt_Net