-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
345 lines (205 loc) · 9.98 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from dataread import all_leads_process
from utility import storeWeights, check_units, precision, auroc, f1
import tensorflow as tf
import numpy as np
import cv2
import matplotlib.pyplot as plt
import ast
import json
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN,Dense, Flatten,Dropout, LSTM, BatchNormalization,SimpleRNN,Input,Conv1D,Conv2D,Concatenate,Activation,MaxPooling1D,Masking,GRU,Bidirectional
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import scale
from tensorflow.keras import backend as K
from tensorflow.keras import layers
from sklearn.utils import class_weight
from sklearn.metrics import roc_auc_score
import sklearn.metrics as metrics
import tensorflow_addons as tfa
from tensorflow.keras.callbacks import LambdaCallback
from sklearn import preprocessing
import keras
gpu_fraction = 1
gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
from keras import backend as K
def _bn_relu(layer, params,dropout=0):
layer = BatchNormalization()(layer)
layer = Activation(params["conv_activation"])(layer)
if dropout > 0:
from keras.layers import Dropout
layer = Dropout(params["conv_dropout"])(layer)
return layer
def add_conv_weight(
layer,
filter_length,
num_filters,
params,
subsample_length=1):
layer = Conv1D(
filters=num_filters,
kernel_size=filter_length,
strides=subsample_length,
padding='same',
kernel_initializer="he_normal")(layer)
return layer
def add_conv_layers(layer,num_layers, params):
for subsample_length in num_layers:
layer = add_conv_weight(
layer,
params["conv_filter_length"],
params["conv_num_filters_start"],
params,
subsample_length=subsample_length)
layer = _bn_relu(layer, params)
return layer
gpu_fraction = 1
gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
def buildModel(time_steps=1691, n_features=157):
"""The branch dealing with the EHR Data"""
demo_in =Input(shape=(n_features,), name='Demo 1')
dense1 = Dense(n_features, activation='relu')(demo_in)
dense2 = Dropout(0.25)(dense1)
dense2 = Dense(128, activation='relu')(dense2)
dense2 = Dropout(0.25)(dense2)
"""The branch dealing with ECG Data"""
params = json.load(open('config.json', 'r'))
inputs = Input(shape=(1691,6),
dtype='float32',
name='inputs')
layer = add_conv_layers(inputs, params['conv_subsample_lengths'],params)
layer= BatchNormalization()(layer)
layer = add_conv_layers(layer, params['conv_subsample_lengths'],params)
layer= BatchNormalization()(layer)
layer = layers.MaxPooling1D(pool_size=4)(layer)
layer = layers.MaxPooling1D(pool_size=4)(layer)
layer = layers.MaxPooling1D(pool_size=4)(layer)
layer = Flatten()(layer)
d2 = Dense(256,activation = 'relu')(layer)
d2 = Dropout(0.25)(d2)
d2= BatchNormalization()(d2)
d2 = Dense(16,activation = 'relu')(d2)
d2 = Dropout(0.25)(d2)
"""Combining both the branches"""
merge = Concatenate()(
[dense2,d2]
)
hidden = Dense(128, activation='relu',name='dense1')(merge)
drop1 = Dropout(0.25)(hidden)
hidden2 = Dense(1, activation='sigmoid',name='output2')(drop1)
#Building the Model
model = tf.keras.Model(
inputs = [demo_in, inputs],
outputs=hidden2)
print("Compiling the Model with optimizers and Metrics")
opt = tf.keras.optimizers.Adam(learning_rate = 1e-4)
metrics = [
keras.metrics.FalseNegatives(name="fn"),
keras.metrics.FalsePositives(name="fp"),
keras.metrics.TrueNegatives(name="tn"),
keras.metrics.TruePositives(name="tp"),
keras.metrics.Precision(name="precision"),
keras.metrics.Recall(name="recall"),
keras.metrics.AUC(curve= 'ROC')
]
model.compile(loss=tfa.losses.SigmoidFocalCrossEntropy(from_logits= False),metrics = metrics,optimizer = opt)
return model
def scheduler(epoch, lr):
if epoch < 10:
return lr
else:
return lr * tf.math.exp(-0.1)
def trainModel(data):
# Storing the outcomes
outcomes = [ 'Time from PCI to Stroke_1yr', 'Time from PCI to CHF Hospitalization_1yr',
'Mortality 1 yr\nYes:1\nNo:0']
true_variables= []
pred_variables =[]
models=[]
"""
true_variables: an array containing the labels
pred_variables: an array containing the predicted results for the outcomes
models: A list of the models trained. """
"""If using the 6 months data as label, remove the comment for the code below"""
# outcomes2 = ['Time from PCI to Stroke_6mo','Time from PCI to CHF Hospitalization_6mo','Mortality 6 months\nYes:1\nNo:0']
for outcome in outcomes:
print("Generating results for: ", outcome)
""" If using the 6 months labels remove the comments from the lines below """
# if outcome == 'Time from PCI to Stroke_1yr':
# outcome2 = 'Time from PCI to Stroke_6mo'
# elif outcome == 'Time from PCI to CHF Hospitalization_1yr':
# outcome2 = 'Time from PCI to CHF Hospitalization_6mo'
# elif outcome == 'Mortality 1 yr\nYes:1\nNo:0':
# outcome2 = 'Mortality 6 months\nYes:1\nNo:0'
ind_var, demo, labels,image_name = all_leads_process(data, outcome)
"""Readint the Train and Test Images and storing the corresponding labels and information into arrays"""
with open('image_train1yr.txt') as f:
image_train_info = f.read()
image_train_info = ast.literal_eval(image_train_info)
with open('image_test1yr.txt') as f:
image_test_info = f.read()
image_test_info = ast.literal_eval(image_test_info)
""""Code for storing the labels corresponding to the outcomes"""
my_file = open(image_train_info[outcome], "r")
# reading the file
image_data = my_file.read()
image_name_train = image_data.split("\n")
my_file = open(image_test_info[outcome], "r")
# reading the file
image_data = my_file.read()
image_name_test = image_data.split("\n")
for img in range(len(image_name_train)):
if image_name_train[img][-4:] == '.png' or image_name_train[img][-4:] == '.PNG':
image_name_train[img] = image_name_train[img][:-4]
for img in range(len(image_name_test)):
if image_name_test[img][-4:] == '.png' or image_name_test[img][-4:] == '.PNG':
image_name_test[img] = image_name_test[img][:-4]
ecg_train =[]
ecg_test =[]
tab_train =[]
tab_test = []
labels_train=[]
labels_test = []
"""ecg_train/test: array containing ECG data
tab_train/test: array containing EHR data
labels_train/test: array containing the corresponding labels"""
for image in image_name_train:
if image in image_name:
ecg_train.append(ind_var[image_name.index(image)])
tab_train.append(demo[image_name.index(image)])
labels_train.append(labels[image_name.index(image)])
for image in image_name_test:
if image in image_name:
ecg_test.append(ind_var[image_name.index(image)])
tab_test.append(demo[image_name.index(image)])
labels_test.append(labels[image_name.index(image)])
tab_train = np.array(tab_train)
tab_test= np.array(tab_test)
ecg_train = np.array(ecg_train)
ecg_test = np.array(ecg_test)
labels_train = np.array(labels_train)
labels_test = np.array(labels_test)
print(labels_train)
model = buildModel(time_steps = 1691, n_features=157)
"""Taking the class_weights since its imbalanced dataset"""
class_weights = class_weight.compute_class_weight('balanced',
np.unique(labels_train),
labels_train)
"""Defining the callbacks"""
callback_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
callback_scheduler = tf.keras.callbacks.LearningRateScheduler(scheduler)
tf.config.run_functions_eagerly(True)
"""Training the model"""
weight = {i : class_weights[i] for i in range(2)}
history = model.fit([tab_train,ecg_train], labels_train, batch_size=64, validation_split = 0.2, epochs = 100,
callbacks=[callback_stopping, callback_scheduler],shuffle=True,class_weight=weight)
#Storing the actual and predicted labels for the outcomes
preds = model.predict([tab_test,ecg_test])
"""Storing the labels"""
true_variables.append(labels_test)
pred_variables.append(preds)
"""Storing the model"""
models.append(model)
return true_variables, pred_variables, outcomes, models