model.save(keras_model_name)
    else:
        exit(-1)

score, acc = model.evaluate(x_test, y_test, batch_size=128, verbose=1)
print('Test score: {} , Test accuracy: {}'.format(score, acc))

# Print confusion matrix:
y_prob = model.predict(x_test)
print_confusion_matrix(y_prob, y_test)

# predict
yy_probabilities = model.predict(x_test)

# https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer

if SAVE_HIDDEN:
    layers_of_interest = ['conv1d_1', 'conv1d_2', 'reshape_2', 'lstm_1', 'dense_1', 'dense_2']
    np.random.seed(0)
    rand_indices = np.random.randint(0, x_test.shape[0], 250)
    print('Saving hidden layers: ', layers_of_interest)
    tfs.get_keras_layers(model, layers_of_interest, x_test[rand_indices], y_test[rand_indices],
                         output_dir=tfs.prep_dir('I:/_ecg_data_backup/classification/hidden_layers'),
                         fname='rat_hidden_all_' + file_name + '.mat')

print('Elapsed Time (ms): ', tfs.current_time_ms() - start_time_ms)
print('Elapsed Time (min): ', (tfs.current_time_ms() - start_time_ms) / 60000)

data_dict = {'x_val': x_test, 'y_val': y_test, 'y_prob': yy_probabilities}
savemat(tfs.prep_dir(output_folder) + file_name + '.mat', mdict=data_dict)
예제 #2
0
        print("Model Not Found!")
        if not TRAIN:
            exit(-1)

    if SAVE_PREDICTIONS:
        # predict
        yy_probabilities = model.predict(x_test)
        yy_predicted = tfs.maximize_output_probabilities(yy_probabilities)  # Maximize probabilities of prediction.

        # Evaluate other dataset:
        yy_probabilities_f = model.predict(xx_flex)
        yy_predicted_f = tfs.maximize_output_probabilities(yy_probabilities_f)  # Maximize probabilities of prediction.

        data_dict = {'x_val': x_test, 'y_val': y_test, 'y_out': yy_predicted, 'y_prob': yy_probabilities,
                     'x_flex': xx_flex, 'y_prob_flex': yy_probabilities_f, 'y_out_flex': yy_predicted_f}
        savemat(tfs.prep_dir(output_folder) + file_name + '.mat', mdict=data_dict)

    if SAVE_HIDDEN:
        # Evaluate hidden layers: # 'conv1d_3'
        # https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer
        layers_of_interest = ['conv1d_1', 'conv1d_2', 'reshape_2', 'bidirectional_1', 'dense_1', 'dense_2']
        np.random.seed(0)
        rand_indices = np.random.randint(0, x_test.shape[0], 250)
        print('Saving hidden layers: ', layers_of_interest)
        tfs.get_keras_layers(model, layers_of_interest, x_test[rand_indices], y_test[rand_indices],
                             output_dir='I:/_gan_data_backup/hidden_layers/', fname='hidden_all_' + file_name + '.mat')

    if CUSTOM_EVALUATION:
        rand_indices = np.random.randint(0, ptb_data_lead_ii.shape[0], 3500)
        ptb_sampling = ptb_data_lead_ii[rand_indices]
        yy_probabilities = model.predict(ptb_sampling)
from scipy.io import savemat, loadmat

from tf_shared import load_data_v2, prep_dir, ind2vec

num_classes = 1
seq_length = 2000
input_length = seq_length
dir_x = 'ptb_ecg_1ch_temporal_labels/lead_v2'
# x_data, y_data = load_data_v2('data/' + dir_x, [seq_length, 1], [seq_length, 1], 'relevant_data', 'Y')
key_x = 'X'
key_y = 'Y'
x_data = loadmat(prep_dir('data/' + dir_x + '_all/') + 'all_x.mat').get(key_x)
y_data = loadmat(prep_dir('data/' + dir_x + '_all/') + 'all_x.mat').get(key_y)
print("Loaded Data Shape: X:", x_data.shape, " Y: ", y_data.shape)

y_data = ind2vec(y_data, dimensions=3)

savemat(prep_dir('data/' + dir_x + '_all/') + 'all_x.mat', mdict={key_x: x_data, key_y: y_data})


예제 #4
0
    # Save blank, untrained model.
    # k_model_untrained_no_cuda = get_model_conv1d_bilstm(export=True)
    # k_model_untrained_no_cuda.save('alt_' + keras_model_name)

    # predict
    yy_probabilities = model.predict(x_test)
    yy_predicted = tfs.maximize_output_probabilities(yy_probabilities)  # Maximize probabilities of prediction.

    # Evaluate hidden layers: # 'conv1d_3'
    # https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer

    if SAVE_HIDDEN:
        layers_of_interest = ['conv1d_1', 'conv1d_2', 'reshape_2', 'lstm_1', 'dense_1', 'dense_2']
        np.random.seed(0)
        rand_indices = np.random.randint(0, x_test.shape[0], 250)
        print('Saving hidden layers: ', layers_of_interest)
        tfs.get_keras_layers(model, layers_of_interest, x_test[rand_indices], y_test[rand_indices],
                             output_dir=tfs.prep_dir('I:/_ecg_data_backup/classification/hidden_layers'),
                             fname='rat_hidden_all_' + file_name + '.mat')

    print('Elapsed Time (ms): ', tfs.current_time_ms() - start_time_ms)
    print('Elapsed Time (min): ', (tfs.current_time_ms() - start_time_ms) / 60000)

    data_dict = {'x_val': x_test, 'y_val': y_test, 'y_out': yy_predicted, 'y_prob': yy_probabilities}
    savemat(tfs.prep_dir(output_folder) + file_name + '.mat', mdict=data_dict)
tf_backend.set_learning_phase(0)
tfs.export_model_keras(keras_model_name, tfs.prep_dir("graph_rat"), model_name=description)

# Results:
예제 #5
0
from scipy.io import savemat

from tf_shared import load_data_v2, prep_dir, ind2vec

data_directory_name = 'ptb_6class_single/lead_v2'
x_data, y_data = load_data_v2('data/' + data_directory_name, [2000, 1], [1],
                              'relevant_data',
                              'Y',
                              ind2vec=True)

# New Keys:
key_x = 'X'
key_y = 'Y'

# y_data = ind2vec(y_data)
# print("Updated Data Shape: X:", x_data.shape, " Y: ", y_data.shape)

savemat(prep_dir('data/' + data_directory_name + '_all/') + 'all_data.mat',
        mdict={
            key_x: x_data,
            key_y: y_data
        })

# modify labels:
예제 #6
0
        fake_B = g_AB.predict(x_train)
        fake_A = g_BA.predict(y_train)
        # translate back to original domain:
        reconstr_A = g_BA.predict(fake_B)
        reconstr_B = g_AB.predict(fake_A)

        # gen_imgs = generator.predict(x_train)
        md = {
            'x_val': x_train,
            'y_true': y_train,
            'fake_A': fake_A,
            'fake_B': fake_B,
            'reconstr_A': reconstr_A,
            'reconstr_B': reconstr_B
        }
        savemat(tfs.prep_dir(output_folder) + description + "_%d.mat" % epoch,
                mdict=md)

# TODO: Evaluate Test Samples

# Save and restore models:
keras_combined_model_location = tfs.prep_dir(
    "model_exports/") + description + 'combined_model.h5'
keras_d_A_location = tfs.prep_dir("model_exports/") + description + 'd_A.h5'
keras_d_B_location = tfs.prep_dir("model_exports/") + description + 'd_B.h5'
keras_g_BA_location = tfs.prep_dir("model_exports/") + description + 'g_BA.h5'
keras_g_AB_location = tfs.prep_dir("model_exports/") + description + 'g_AB.h5'

combined_model.save(keras_combined_model_location)
g_AB.save(keras_g_AB_location)
g_BA.save(keras_g_BA_location)
from scipy.io import savemat, loadmat
import numpy as np
from tf_shared import load_data_v2, prep_dir, ind2vec

num_classes = 5
seq_length = 2000
input_length = seq_length
suffix = '_train'
dir_1 = 'incartdb_5c_shift' + suffix
dir_2 = 'ptb_5c_shift' + suffix
dir_out = 'incart_ptb_5c'
key_x = 'X'
key_y = 'Y'
x_shape = []
y_shape = []
x_data, y_data = load_data_v2(dir_1, [seq_length, 1],
                              [seq_length, num_classes], key_x, key_y)
x_data2, y_data2 = load_data_v2(dir_2, [seq_length, 1],
                                [seq_length, num_classes], key_x, key_y)

x_data = np.concatenate([x_data, x_data2], axis=0)
y_data = np.concatenate([y_data, y_data2], axis=0)
print("Loaded Data Shape Final: X:", x_data.shape, " Y: ", y_data.shape)

savemat(prep_dir(dir_out + '_all' + suffix + '/') + 'all_data.mat',
        mdict={
            key_x: x_data,
            key_y: y_data
        })