keras_model_name = description + '.h5'
file_name = description
seq_length = 2000
if num_channels < 2:
    x_shape = [seq_length, 1]
    input_shape = (seq_length, 1)
else:
    x_shape = [seq_length, 2]
    input_shape = (seq_length, num_channels)

y_shape = [1]

# Import Data:
# x_tt, y_tt = tfs.load_data_v2('data/rat_ecg_single_label', [seq_length, 2], y_shape, 'relevant_data',
#                               'Y', ind2vec=True)
x_tt, y_tt = tfs.load_data_v2('data/ptb_6class_single/lead_v2_all', [seq_length, 1], [6], 'X', 'Y')

if num_channels < 2:
    x_tt = np.reshape(x_tt[:, :, 0], [-1, seq_length, 1])

x_train, x_test, y_train, y_test = train_test_split(x_tt, y_tt, train_size=0.75, random_state=1)


def get_model():
    k_model = Sequential()
    k_model.add(Reshape((seq_length, num_channels), input_shape=input_shape))
    k_model.add(Conv1D(64, 16, strides=2, padding='same', activation='relu'))
    k_model.add(Conv1D(128, 16, strides=2, padding='same', activation='relu'))
    k_model.add(Conv1D(256, 16, strides=2, padding='same', activation='relu'))
    k_model.add(Flatten())
    # k_model.add(Dense(64, activation='relu', kernel_regularizer=regularizers.l2(l=0.01)))
Exemple #2
0
    keras_model_name = 'alt_' + keras_model_name
file_name = description
seq_length = 2000
if num_channels < 2:
    x_shape = [seq_length, 1]
    input_shape = seq_length
else:
    x_shape = [seq_length, 2]
    input_shape = (seq_length, num_channels)
y_shape = [seq_length, num_classes]

# Start Timer:
start_time_ms = tfs.current_time_ms()

# Import Data:
x_tt, y_tt = tfs.load_data_v2('data/extended_5_class/mit_bih_tlabeled_w8s_fixed_all', [seq_length, 2], y_shape,
                              'relevant_data', 'Y')
if num_channels < 2:
    x_tt = np.reshape(x_tt[:, :, 0], [-1, seq_length, 1])
xx_flex, y_flex = tfs.load_data_v2('data/flexEcg_8s_normal', [seq_length, 1], [1], 'relevant_data', 'Y')
x_train, x_test, y_train, y_test = train_test_split(x_tt, y_tt, train_size=0.75, random_state=1)  # 0.66

ptb_data_lead_ii = tfs.load_mat('_adversarial/data/lead_ii_all/all_y.mat', key='Y', shape=[seq_length, 1])


def get_model_alt():
    k_model = Sequential()
    k_model.add(Reshape((seq_length, num_channels), input_shape=(input_shape, 1)))
    k_model.add(k.layers.Conv1D(128, 8, strides=2, padding='same', activation='relu'))
    k_model.add(k.layers.Conv1D(256, 8, strides=2, padding='same', activation='relu'))
    k_model.add(Reshape(target_shape=(seq_length, 64)))
    k_model.add(Bidirectional(LSTM(LSTM_UNITS, return_sequences=True)))
from keras import backend as K
from keras.layers import (Activation, Conv2D, Dense, Dropout, Flatten, Lambda,
                          Reshape, UpSampling2D)
from keras.models import Sequential
from keras.optimizers import Adam

import tf_shared as tfs

x_shape = (28, 28)
z_dim = 128

x_tt, y_tt = tfs.load_data_v2('../data/ptb_6class_single/lead_v2_all',
                              [2000, 1], [6], 'X', 'Y')


def build_conv():
    model = Sequential(name='conv')
    model.add(Reshape(x_shape + (1, ), input_shape=x_shape))
    model.add(Conv2D(64, (5, 5), strides=(2, 2), activation='relu'))
    model.add(Dropout(0.3))
    model.add(Conv2D(128, (5, 5), strides=(2, 2), activation='relu'))
    model.add(Dropout(0.3))
    model.add(Flatten())
    model.add(Dense(10))
    return model


def build_classifier(conv):
    model = Sequential(name='classifier')
    model.add(conv)
    model.add(Activation('softmax'))
keras_model_name = description + '.h5'
file_name = description
seq_length = 2000
if num_channels < 2:
    x_shape = [seq_length, 1]
    input_shape = seq_length
else:
    x_shape = [seq_length, 2]
    input_shape = (seq_length, num_channels)

y_shape = [1]

# Import Data:
x_tt, y_tt = tfs.load_data_v2('data/rat_ecg_single_label', [seq_length, 2],
                              y_shape,
                              'relevant_data',
                              'Y',
                              ind2vec=True)

if num_channels < 2:
    x_tt = np.reshape(x_tt[:, :, 0], [-1, seq_length, 1])

x_train, x_test, y_train, y_test = train_test_split(x_tt,
                                                    y_tt,
                                                    train_size=0.75,
                                                    random_state=1)


def get_model():
    k_model = Sequential()
    k_model.add(Reshape((seq_length, num_channels), input_shape=input_shape))
Exemple #5
0
learn_rate = 0.01
description = 'rat_2cnn.c1d_lstm_' + str(LSTM_UNITS) + 'lr' + str(learn_rate) + 'ep' + str(epochs) + '_v1'
keras_model_name = description + '.h5'
file_name = description
seq_length = 2000
if num_channels < 2:
    x_shape = [seq_length, 1]
    input_shape = seq_length
else:
    x_shape = [seq_length, 2]
    input_shape = (seq_length, num_channels)

y_shape = [seq_length, 2]

# Import Data:
x_tt, y_tt = tfs.load_data_v2('data/rat_ecg', [seq_length, 2], y_shape, 'relevant_data', 'Y')

if num_channels < 2:
    x_tt = np.reshape(x_tt[:, :, 0], [-1, seq_length, 1])

x_train, x_test, y_train, y_test = train_test_split(x_tt, y_tt, train_size=0.75, random_state=1)


def get_model_conv1d_bilstm(export=False):
    k_model = Sequential()
    k_model.add(Reshape((seq_length, num_channels), input_shape=input_shape))
    k_model.add(k.layers.Conv1D(128, 8, strides=2, padding='same', activation='relu'))
    k_model.add(k.layers.Conv1D(256, 8, strides=2, padding='same', activation='relu'))
    k_model.add(Reshape(target_shape=(seq_length, 64)))
    if export:
        k_model.add(LSTM(LSTM_UNITS, return_sequences=True))  # TODO: Removed Bidirectional
Exemple #6
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:
Exemple #7
0
epochs = 301
num_channels = 1
num_classes = 1
label = 'ecg_cycle_gan_v1_r3'
model_dir = "model_exports/" + label + '/'
output_folder = 'outputs/' + label + '/'
version_num = 0
learn_rate = 0.0005
description = label + '_' + 'lr' + str(learn_rate)
seq_length = 2000
x_shape = [seq_length, 1]
input_length = seq_length
y_shape = [seq_length, num_classes]

# Import Data:
x_tt, y_tt = tfs.load_data_v2('data/flex_overlap', [seq_length, 1], [1],
                              'relevant_data', 'Y')  # Ignore Y.

if num_channels < 2:
    x_tt = np.reshape(x_tt[:, :, 0], [-1, seq_length, 1])
xx_br, y_br = tfs.load_data_v2('data/br_overlap', [seq_length, 1], [1],
                               'relevant_data', 'Y')
x_train, x_test, y_train, y_test = train_test_split(x_tt,
                                                    xx_br,
                                                    train_size=0.75,
                                                    random_state=1)  # 0.66


def build_generator():
    def conv_layer(layer_input, filters, kernel_size=5, strides=2):
        d = Conv1D(filters, kernel_size, strides=strides,
                   padding='same')(layer_input)
Exemple #8
0
seq_length = 2000

if num_channels < 2:
    x_shape = [seq_length, 1]
    input_shape = (seq_length, num_channels)
else:
    x_shape = [seq_length, 2]
    input_shape = (seq_length, num_channels)

if IND_TO_VEC:
    y_shape = [1]
else:
    y_shape = [num_classes]

# Import Data:
x_tt, y_tt = tfs.load_data_v2('data/ptb_ecg_1ch_single_labels/lead_v2_all', x_shape, y_shape, 'X', 'Y',
                              ind2vec=IND_TO_VEC)

if num_channels < 2:
    x_tt = np.reshape(x_tt[:, :, 0], [-1, seq_length, 1])

x_train, x_test, y_train, y_test = train_test_split(x_tt, y_tt, train_size=0.75, random_state=1)


def get_model():
    k_model = Sequential()
    k_model.add(Reshape((seq_length, num_channels), input_shape=input_shape))
    k_model.add(Conv1D(128, 8, strides=2, padding='same', activation='relu'))
    k_model.add(Conv1D(256, 8, strides=2, padding='same', activation='relu'))
    k_model.add(Conv1D(512, 8, strides=2, padding='same', activation='relu'))
    k_model.add(Dense(128, activation='relu', kernel_regularizer=regularizers.l2(l=0.01)))
    k_model.add(Reshape(target_shape=(1, seq_length * 16)))
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
        })