-
Notifications
You must be signed in to change notification settings - Fork 2
/
cross_validate.py
248 lines (203 loc) · 12.5 KB
/
cross_validate.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
#!/usr/bin/env python
# Edit this script to add your team's code. Some functions are *required*, but you can edit most parts of the required functions,
# change or remove non-required functions, and add your own functions.
################################################################################
#
# Import libraries and functions. You can change or remove them.
#
################################################################################
from curses import meta
from gc import callbacks
from random import shuffle
from termios import VLNEXT
from helper_code import *
from team_code import base_model, load_challenge_model, build_murmur_model, build_clinical_model, scheduler, scheduler_2, get_murmur_locations, pad_array, calculating_class_weights
import numpy as np, scipy as sp, scipy.stats, os, sys, joblib
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils.class_weight import compute_class_weight
from sklearn.metrics import accuracy_score
import os
import tqdm
import numpy as np
import tensorflow as tf
from scipy import signal
from sklearn.model_selection import StratifiedKFold
from sklearn.utils.class_weight import compute_class_weight
################################################################################
#
# Required functions. Edit these functions to add your code, but do not change the arguments.
#
################################################################################
# Cross validate model.
def cv_challenge_model(data_folder, result_folder, n_epochs_1, n_epochs_2, n_folds, pre_train):
NEW_FREQUENCY = 100
batch_size = 30
# Find the patient data files.
patient_files = find_patient_files(data_folder)
num_patient_files = len(patient_files)
if num_patient_files==0:
raise Exception('No data was provided.')
# Create a folder for the model if it does not already exist.
os.makedirs(result_folder, exist_ok=True)
#TODO: remove this:
#classes = ['Present', 'Unknown', 'Absent']
#num_classes = len(classes)
murmur_classes = ['Present', 'Unknown', 'Absent']
num_murmur_classes = len(murmur_classes)
outcome_classes = ['Abnormal', 'Normal']
num_outcome_classes = len(outcome_classes)
#Make CV folds
cv_murmur = []
cv_outcome = []
max_len = 0
for i in tqdm.tqdm(range(len(patient_files))):
# Load the current patient data and recordings.
current_patient_data = load_patient_data(patient_files[i])
cv_murmur.append(get_murmur(current_patient_data))
cv_outcome.append(get_outcome(current_patient_data))
current_recordings, freq = load_recordings(data_folder, current_patient_data, get_frequencies=True)
for indx, rec in enumerate(current_recordings):
rec_len = int(len(rec)/freq[indx]*NEW_FREQUENCY)
if rec_len > max_len:
max_len = rec_len
cv_outcome = np.asarray(cv_outcome)
cv_murmur = np.asarray(cv_murmur)
patient_files = np.asarray(patient_files)
skf = StratifiedKFold(n_splits=n_folds)
murmur_probas = []
outcome_probas = []
murmur_trues = []
outcome_trues = []
clinical_history = []
murmur_history = []
patient_labels = []
val_murmur_patient_clf_cv = []
val_outcome_patient_clf_cv = []
lr_schedule = tf.keras.callbacks.LearningRateScheduler(scheduler_2, verbose=0)
for train_index, val_index in skf.split(cv_murmur, cv_outcome):
train_data, train_murmurs, train_outcomes, _ = get_data(patient_files[train_index], data_folder, NEW_FREQUENCY, num_murmur_classes, num_outcome_classes,outcome_classes,max_len)
print(f"Number of signals in training data = {train_data.shape[0]}")
print("Murmurs prevalence:")
print(f"Present = {np.where(np.argmax(train_murmurs,axis=1)==0)[0].shape[0]}, Unknown = {np.where(np.argmax(train_murmurs,axis=1)==1)[0].shape[0]}, Absent = {np.where(np.argmax(train_murmurs,axis=1)==2)[0].shape[0]}")
print("Outcomes prevalence:")
print(f"Abnormal = {len(np.where(train_outcomes==0)[0])}, Normal = {len(np.where(train_outcomes==1)[0])}")
val_data, val_murmurs, val_outcomes, val_patient_labels= get_data(patient_files[val_index], data_folder, NEW_FREQUENCY, num_murmur_classes, num_outcome_classes,outcome_classes, max_len)
print(f"Number of signals in validation data = {val_data.shape[0]}")
print("Murmurs prevalence:")
print(f"Present = {np.where(np.argmax(val_murmurs,axis=1)==0)[0].shape[0]}, Unknown = {np.where(np.argmax(val_murmurs,axis=1)==1)[0].shape[0]}, Absent = {np.where(np.argmax(val_murmurs,axis=1)==2)[0].shape[0]}")
print("Outcomes prevalence:")
print(f"Abnormal = {len(np.where(val_outcomes==0)[0])}, Normal = {len(np.where(val_outcomes==1)[0])}")
val_murmur_patient_clf = cv_murmur[val_index]
val_outcome_patient_clf = cv_outcome[val_index]
# Calculate weights
new_weights_murmur=calculating_class_weights(train_murmurs)
keys = np.arange(0,len(murmur_classes),1)
murmur_weight_dictionary = dict(zip(keys, new_weights_murmur.T[1]))
weight_outcome = np.unique(train_outcomes, return_counts=True)[1][0]/np.unique(train_outcomes, return_counts=True)[1][1]
outcome_weight_dictionary = {0: 1.0, 1:weight_outcome}
gpus = tf.config.list_logical_devices('GPU')
strategy = tf.distribute.MirroredStrategy(gpus)
with strategy.scope():
if pre_train == False:
# Initiate the model.
clinical_model = build_clinical_model(train_data.shape[1],train_data.shape[2])
murmur_model = build_murmur_model(train_data.shape[1],train_data.shape[2])
print("Train murmur model..")
temp_murmur_history = murmur_model.fit(x=train_data, y=train_murmurs, epochs=n_epochs_1, batch_size=batch_size,
verbose=1, validation_data = (val_data,val_murmurs),
class_weight=murmur_weight_dictionary, shuffle = True,
callbacks=[lr_schedule]
)
print("Train clinical model..")
temp_clinical_history = clinical_model.fit(x=train_data, y=train_outcomes, epochs=n_epochs_2, batch_size=batch_size,
verbose=1, validation_data = (val_data,val_outcomes),
class_weight=outcome_weight_dictionary, shuffle = True,
callbacks=[lr_schedule]
)
elif pre_train == True:
print("Train murmur model..")
model = base_model(train_data.shape[1],train_data.shape[2])
model.load_weights("./pretrained_model.h5")
murmur_layer = tf.keras.layers.Dense(3, "softmax", name="murmur_output")(model.layers[-2].output)
murmur_model = tf.keras.Model(inputs=model.layers[0].output, outputs=[murmur_layer])
for layer in murmur_model.layers[:-2]:
layer.trainable = False
murmur_model.compile(loss="categorical_crossentropy", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics = [tf.keras.metrics.CategoricalAccuracy(), tf.keras.metrics.AUC(curve='ROC')])
murmur_model.fit(x=train_data, y=train_murmurs, epochs=5, batch_size=batch_size,
verbose=1, validation_data = (val_data,val_murmurs),
class_weight=murmur_weight_dictionary, shuffle = True)
for layer in murmur_model.layers[:-2]:
layer.trainable = True
murmur_model.compile(loss="categorical_crossentropy", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics = [tf.keras.metrics.CategoricalAccuracy(), tf.keras.metrics.AUC(curve='ROC')])
temp_murmur_history = murmur_model.fit(x=train_data, y=train_murmurs, epochs=n_epochs_1, batch_size=batch_size,
verbose=1, validation_data = (val_data,val_murmurs),
class_weight=murmur_weight_dictionary, shuffle = True, callbacks=[lr_schedule])
print("Train clinical model..")
outcome_layer = tf.keras.layers.Dense(1, "sigmoid", name="clinical_output")(model.layers[-2].output)
clinical_model = tf.keras.Model(inputs=model.layers[0].output, outputs=[outcome_layer])
for layer in clinical_model.layers[:-2]:
layer.trainable = False
clinical_model.compile(loss="binary_crossentropy", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics = [tf.keras.metrics.BinaryAccuracy(),tf.keras.metrics.AUC(curve='ROC')])
clinical_model.fit(x=train_data, y=train_outcomes, epochs=5, batch_size=batch_size,
verbose=1, validation_data = (val_data,val_outcomes),
class_weight=outcome_weight_dictionary, shuffle = True)
for layer in clinical_model.layers[:-2]:
layer.trainable = True
clinical_model.compile(loss="binary_crossentropy", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics = [tf.keras.metrics.BinaryAccuracy(),tf.keras.metrics.AUC(curve='ROC')])
temp_clinical_history = clinical_model.fit(x=train_data, y=train_outcomes, epochs=n_epochs_2, batch_size=batch_size,
verbose=1, validation_data = (val_data,val_outcomes),
class_weight=outcome_weight_dictionary, shuffle = True, callbacks=[lr_schedule])
murmur_probabilities = murmur_model.predict(val_data)
outcome_probabilities = clinical_model.predict(val_data)
clinical_history.append(temp_clinical_history)
murmur_history.append(temp_murmur_history)
murmur_probas.append(murmur_probabilities)
outcome_probas.append(outcome_probabilities)
murmur_trues.append(val_murmurs)
outcome_trues.append(val_outcomes)
patient_labels.append(val_patient_labels)
val_murmur_patient_clf_cv.append(val_murmur_patient_clf)
val_outcome_patient_clf_cv.append(val_outcome_patient_clf)
return murmur_model, clinical_model, murmur_probas, outcome_probas, murmur_trues, outcome_trues, murmur_history, clinical_history, val_data, patient_labels, val_murmur_patient_clf_cv, val_outcome_patient_clf_cv
def get_data(patient_files, data_folder, new_frequenzy, num_murmur_classes, num_outcome_classes,outcome_classes, max_length):
data = []
murmurs = list()
outcomes = list()
patient_label = list()
for i in tqdm.tqdm(range(len(patient_files))):
# Load the current patient data and recordings.
current_patient_data = load_patient_data(patient_files[i])
current_recordings, freq = load_recordings(data_folder, current_patient_data, get_frequencies=True)
for j in range(len(current_recordings)):
data.append(signal.resample(current_recordings[j], int((len(current_recordings[j])/freq[j]) * new_frequenzy)))
current_auscultation_location = current_patient_data.split('\n')[1:len(current_recordings)+1][j].split(" ")[0]
all_murmur_locations = get_murmur_locations(current_patient_data).split("+")
current_murmur = np.zeros(num_murmur_classes, dtype=int)
if get_murmur(current_patient_data) == "Present":
if current_auscultation_location in all_murmur_locations:
current_murmur[0] = 1
else:
pass
elif get_murmur(current_patient_data) == "Unknown":
current_murmur[1] = 1
elif get_murmur(current_patient_data) == "Absent":
current_murmur[2] = 1
murmurs.append(current_murmur)
current_outcome = np.zeros(num_outcome_classes, dtype=int)
outcome = get_outcome(current_patient_data)
if outcome in outcome_classes:
j = outcome_classes.index(outcome)
current_outcome[j] = 1
outcomes.append(current_outcome)
patient_label.append(i)
data_padded = pad_array(data, max_length)
data_padded = np.expand_dims(data_padded,2)
murmurs = np.vstack(murmurs)
outcomes = np.argmax(np.vstack(outcomes),axis=1)
patient_label = np.asarray(patient_label)
return data_padded, murmurs, outcomes, patient_label