-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChinaRefinementAll.py
267 lines (216 loc) · 17.4 KB
/
ChinaRefinementAll.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
'''
Deep learning project (100Hz)
ChinaRefinementAll.py
Fine-tuned the original network after the fine-tuning of the classification layer using the China dataset train set.
Inputs:
--seed: random seed
--scenario: scenario simulated
--path: path to the directory in which to load the pre-trained model
--newpath: path to the directory in which to save the fine-tuned models
Authors: Daniele Baccega, Andrea Saglietto
Topic: Deep Learning applied to ECGs
Dataset: https://physionet.org/content/ptb-xl/1.0.1/
Description: The PTB-XL ECG dataset is a large dataset of 21837 clinical 12-lead ECGs from 18885 patients of 10 second length
where 52% are male and 48% are female with ages covering the whole range from 0 to 95 years (median 62 and interquantile range of 22).
The raw waveform data was annotated by up to two cardiologists, who assigned potentially multiple ECG statements to each record.
The in total 71 different ECG statements conform to the SCP-ECG standard and cover diagnostic, form, and rhythm statements.
To ensure comparability of machine learning algorithms trained on the dataset, we provide recommended splits into training and test sets.
'''
## Import the libraries
import argparse
import numpy as np
import pandas as pd
import pickle
import physionet_challenge_utility_script as pc
import tensorflow
from plotnine import *
from tensorflow import keras
from keras import models
from keras import Model
from keras.layers import Dense
from keras.losses import BinaryCrossentropy
from keras.metrics import BinaryAccuracy
from keras.optimizers import Adam
from skmultilearn.model_selection import iterative_train_test_split
## Import data generator and utils
from datagenerator import dataGenerator
from utils import interp1d
## Verify what tensorflow version is used
print("Using tensorflow version " + str(tensorflow.__version__))
print("Using keras version " + str(tensorflow.keras.__version__))
# This prevents tensorflow from allocating all memory on GPU - for TensorFlow 2.2+
gpus = tensorflow.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tensorflow.config.experimental.set_memory_growth(gpu, True)
## Parse the arguments
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default="123456789", help='Seed (default: 123456789)')
parser.add_argument('--scenario', type=str, default="D1", help='Scenario simulated, must be a directory name (default: D1)')
parser.add_argument('--path', type=str, default="ChinaRefinementLastLayer/D1/20Classes_0", help='Path to the run directory (default: ChinaRefinementLastLayer/D1/20Classes_0)')
parser.add_argument('--newpath', type=str, default="ChinaRefinementAll/D1/20Classes_0", help='Path to the directory in which to save the refined models (default: ChinaRefinementAll/D1/20Classes_0)')
args = parser.parse_args()
## Initialize some variables
path = 'China/'
activation_function = 'sigmoid'
sampling_rate = 100
test_proportion = 0.2
batch_size = 32
crop_window = 344
jitter_std = [0.01, 0.1]
amplitude_scale = [0.7, 1.3]
time_scale = [0.8, 1.2]
epochs = 10
leads_dict = {"D1": ["I"],
"D1-D2": ["I", "II"],
"D1-V1": ["I", "V1"],
"D1-V2": ["I", "V2"],
"D1-V3": ["I", "V3"],
"D1-V4": ["I", "V4"],
"D1-V5": ["I", "V5"],
"D1-V6": ["I", "V6"],
"8leads": ["I", "II", "V1", "V2", "V3", "V4", "V5", "V6"],
"12leads": ["I", "II", "III", "AVR", "AVL", "AVF", "V1", "V2", "V3", "V4", "V5", "V6"],
"12leads_WithoutDataAugmentation": ["I", "II", "III", "AVR", "AVL", "AVF", "V1", "V2", "V3", "V4", "V5", "V6"]}
## Load the scored diagnostic classes
_, _, labels, ecg_filenames = pc.import_key_data_China(path)
SNOMED_scored = pd.read_csv("SNOMED_mappings_scored_China.csv", sep=",")
SNOMED_unscored = pd.read_csv("SNOMED_mappings_unscored_China.csv", sep=",")
df_labels = pc.make_undefined_class(labels, SNOMED_unscored)
y, snomed_classes = pc.onehot_encode(df_labels)
SNOMED_dic = dict()
for _, row in SNOMED_scored.iterrows():
SNOMED_dic[str(row["SNOMED CT Code"])] = row["Abbreviation"]
classes_dic = dict()
i = 0
for value in SNOMED_dic.values():
if value not in classes_dic.keys():
classes_dic[value] = i
i = i + 1
num_classes = len(classes_dic)
y_data = np.zeros((len(ecg_filenames), num_classes), dtype=int)
for i in range(len(ecg_filenames)):
for j in range(len(snomed_classes)):
if y[i][j] == 1:
y_data[i][classes_dic[SNOMED_dic[snomed_classes[j]]]] = 1
data = []
for ecg in ecg_filenames:
signals, fields = pc.load_challenge_data(ecg)
init = np.random.randint(0, signals.shape[1] - 5000)
stop = init + 5000
signals = signals[:, init:stop]
signals = signals.reshape(signals.shape[0], signals.shape[1], 1)
signals = interp1d(signals, int(signals.shape[1] / 5))
signals = signals / 100
data.append(signals)
data = np.array(data)
y_data = np.array(y_data)
print("Data shape: ", data.shape)
print("Labels shape: ", y_data.shape)
leads = ["I", "II", "III", "AVR", "AVL", "AVF", "V1", "V2", "V3", "V4", "V5", "V6"]
# Select the leads
selected_leads_indeces = [i for i in range(0, len(leads)) if leads[i] in leads_dict[args.scenario]]
selected_leads_name = [leads[i] for i in selected_leads_indeces]
data = data[:, selected_leads_indeces, :]
data = data.reshape(data.shape[0], data.shape[1], data.shape[2], 1)
## Train/Test split
X_train, y_train, X_test, y_test = iterative_train_test_split(data, y_data, test_size=test_proportion)
# Load means and stds
with open('TrainedModels/' + args.scenario + '/means', 'rb') as means_file:
means = pickle.load(means_file)[selected_leads_indeces, :, 0]
with open('TrainedModels/' + args.scenario + '/stds', 'rb') as stds_file:
stds = pickle.load(stds_file)[selected_leads_indeces, :, 0]
# Load the model at the last epoch
model = models.load_model(args.path + '/checkpoints/model_last_epoch.h5')
model.trainable = True
# Specify the loss, optimizer, and metrics with `compile()`.
model.compile(
loss = BinaryCrossentropy(),
optimizer = Adam(learning_rate=1e-3),
metrics = [BinaryAccuracy()]
)
model.summary()
sample_weights_train = np.ones(X_train.shape[0])
sample_weights_test = np.ones(X_test.shape[0])
# Train the model
history = model.fit(dataGenerator(sampling_rate,
num_classes,
activation_function,
means,
stds,
sample_weights_train,
X_train,
y_train,
batch_size,
False,
False,
False,
False,
crop_window,
0,
jitter_std,
amplitude_scale,
time_scale),
steps_per_epoch = X_train.shape[0] // batch_size,
epochs = epochs,
validation_data = dataGenerator(sampling_rate,
num_classes,
activation_function,
means,
stds,
sample_weights_test,
X_test,
y_test,
batch_size,
False,
False,
False,
False,
crop_window,
0),
validation_steps = X_test.shape[0] // batch_size,
shuffle = True,
workers = 1,
verbose = 1)
# Save the model at the last epoch
model.save(args.newpath + "/checkpoints/model_last_epoch.h5")
# Plot losses and accuracies
history_df = pd.DataFrame(columns=['epoch', 'loss', 'type'])
history_df['epoch'] = np.concatenate((np.linspace(1, epochs, epochs, dtype=int), np.linspace(1, epochs, epochs, dtype=int)))
history_df['loss'] = np.concatenate((history.history['loss'], history.history['val_loss']))
history_df['type'] = np.concatenate((["Train" for e in range(epochs)], ["Val" for e in range(epochs)]))
my_plot_history_loss = (ggplot(history_df) \
+ aes(x='epoch', y = 'loss', color = 'type') \
+ geom_line() \
+ labs(title = "Loss", x = 'epoch', y = 'loss', color = 'Type')) \
+ scale_color_manual(values=['#FF0000', '#0000FF']) \
+ theme(plot_title = element_text(face="bold"), axis_title_x = element_text(face="bold"), axis_title_y = element_text(face="bold"), legend_title = element_text(face="bold"))
my_plot_history_loss.save(args.newpath + '/loss', dpi=600)
history_df = pd.DataFrame(columns=['epoch', 'accuracy', 'type'])
history_df['epoch'] = np.concatenate((np.linspace(1, epochs, epochs, dtype=int), np.linspace(1, epochs, epochs, dtype=int)))
history_df['accuracy'] = np.concatenate((history.history['binary_accuracy'], history.history['val_binary_accuracy']))
history_df['type'] = np.concatenate((["Train" for e in range(epochs)], ["Val" for e in range(epochs)]))
my_plot_history_accuracy = (ggplot(history_df) \
+ aes(x='epoch', y = 'accuracy', color = 'type') \
+ geom_line() \
+ labs(title = "Accuracy", x = 'epoch', y = 'accuracy', color = 'Type')) \
+ scale_color_manual(values=['#FF0000', '#0000FF']) \
+ theme(plot_title = element_text(face="bold"), axis_title_x = element_text(face="bold"), axis_title_y = element_text(face="bold"), legend_title = element_text(face="bold"))
my_plot_history_accuracy.save(args.newpath + '/accuracy', dpi=600)
# Predict the labels of the data inside the test set and save the predictions
y_pred = model.predict(dataGenerator(sampling_rate,
num_classes,
activation_function,
means,
stds,
sample_weights_test,
X_test,
y_test,
1),
steps = X_test.shape[0],
workers = 1,
verbose = 1)
# Save the predictions
with open(args.newpath + '/y_pred_China', 'wb') as y_pred_file:
pickle.dump(y_pred, y_pred_file)
with open(args.newpath + '/y_test_China', 'wb') as y_test_file:
pickle.dump(y_test, y_test_file)