forked from crocodoyle/deep-mri-qc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqc-ibis-2d.py
389 lines (280 loc) · 12.9 KB
/
qc-ibis-2d.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, BatchNormalization, Dropout
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
import numpy as np
import h5py
import os, csv, time
import nibabel as nib
from dltk.core.io.preprocessing import normalise_zero_one, resize_image_with_crop_or_pad
from custom_loss import sensitivity, specificity
from collections import defaultdict
import pickle as pkl
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn.model_selection import StratifiedShuffleSplit, StratifiedKFold
from sklearn.metrics import confusion_matrix
from vis.visualization import visualize_cam, overlay
from vis.utils import utils
from keras import activations
# from vis.utils import find_layer_idx
workdir = '/home/users/adoyle/deepqc/IBIS/'
datadir = '/data1/users/adoyle/IBIS/'
label_file = datadir + 't1_ibis_QC_labels.csv'
total_subjects = 2020
target_size = (168, 256, 244)
def make_ibis_qc():
f = h5py.File(workdir + 'ibis.hdf5', 'w')
f.create_dataset('ibis_t1', (total_subjects, target_size[0], target_size[1], target_size[2]), dtype='float32')
f.create_dataset('qc_label', (total_subjects, 2), dtype='float32')
dt = h5py.special_dtype(vlen=bytes)
f.create_dataset('filename', (total_subjects, ), dtype=dt)
index = 0
indices = []
labels = []
with open(label_file, 'r') as labels_csv:
qc_reader = csv.reader(labels_csv)
next(qc_reader)
for line in qc_reader:
try:
t1_filename = line[3][9:]
label = line[4]
if 'Pass' in label:
one_hot = [0.0, 1.0]
else:
one_hot = [1.0, 0.0]
f['qc_label'][index, :] = one_hot
t1_data = nib.load(datadir + t1_filename).get_data()
if not t1_data.shape == target_size:
# print('resizing from', t1_data.shape)
t1_data = resize_image_with_crop_or_pad(t1_data, img_size=target_size, mode='constant')
f['ibis_t1'][index, ...] = normalise_zero_one(t1_data)
f['filename'][index] = t1_filename.split('/')[-1]
# plt.imshow(t1_data[96, ...])
# plt.axis('off')
# plt.savefig(output_dir + t1_filename[:-4] + '.png', bbox_inches='tight', cmap='gray')
indices.append(index)
labels.append(np.argmax(one_hot))
index += 1
except Exception as e:
print('Error:', e)
print('Total subjects we actually have:', index+1)
f.close()
return indices, labels
def qc_model():
nb_classes = 2
conv_size = (3, 3)
model = Sequential()
model.add(Conv2D(16, conv_size, activation='relu', input_shape=(target_size[1], target_size[2], 1)))
model.add(BatchNormalization())
# model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.1))
model.add(Conv2D(32, conv_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.1))
model.add(Conv2D(32, conv_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, conv_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, conv_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Conv2D(128, conv_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Conv2D(256, conv_size, activation='relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax', name='predictions'))
sgd = SGD(lr=1e-3, momentum=0.9, decay=1e-6, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
#metrics=["accuracy", sensitivity, specificity])
metrics = ["accuracy"])
return model
def batch(indices, n, random_slice=False):
f = h5py.File(workdir + 'ibis.hdf5', 'r')
images = f['ibis_t1']
labels = f['qc_label']
x_train = np.zeros((n, target_size[1], target_size[2], 1), dtype=np.float32)
y_train = np.zeros((n, 2), dtype=np.int8)
while True:
np.random.shuffle(indices)
samples_this_batch = 0
for i, index in enumerate(indices):
if random_slice:
rn=np.random.randint(-4, 4)
else:
rn=0
x_train[i%n, :, :, 0] = images[index, target_size[0]//2+rn, :, :]
y_train[i%n, :] = labels[index, ...]
samples_this_batch += 1
if (i+1) % n == 0:
yield (x_train, y_train)
samples_this_batch = 0
elif i == len(indices)-1:
yield (x_train[0:samples_this_batch, ...], y_train[0:samples_this_batch, :])
def test_images(model, test_indices, save_imgs=True):
f = h5py.File(workdir + 'ibis.hdf5', 'r')
images = f['ibis_t1']
labels = f['qc_label']
filename_test = f['filenames']
predictions = np.zeros((len(test_indices)))
actual = np.zeros((len(test_indices)))
predict_batch = np.zeros((1, target_size[1], target_size[2], 1))
print("test indices:", len(test_indices))
print("test index max:", max(test_indices))
print("labels:", len(labels))
print("filenames:", len(filename_test))
for i, index in enumerate(test_indices):
predict_batch[0,:,:,0] = images[index,target_size[0]//2, :,:]
prediction = model.predict_on_batch(predict_batch)[0][0]
if prediction >= 0.5:
predictions[i] = 1
else:
predictions[i] = 0
actual[i] = labels[index,0]
if save_imgs:
plt.imshow(images[index,target_size[0]//2+10,:,:], cmap='gray')
if predictions[i] == 1 and actual[i] == 1:
plt.savefig(results_dir + 'fail_right_' + os.path.basename(filename_test[i]) + ".png")
elif predictions[i] == 0 and actual[i] == 0:
plt.savefig('/home/adoyle/images/pass_right_' + os.path.basename(filename_test[i]) + '.png')
elif predictions[i] == 1 and actual[i] == 0:
plt.savefig('/home/adoyle/images/pass_wrong_' + os.path.basename(filename_test[i]) + '.png')
elif predictions[i] == 0 and actual[i] == 1:
plt.savefig('/home/adoyle/images/fail_wrong_' + os.path.basename(filename_test[i]) + '.png')
plt.clf()
conf = confusion_matrix(actual, predictions)
print('Confusion Matrix')
print(conf)
print(np.shape(conf))
tp = conf[0][0]
tn = conf[1][1]
fp = conf[0][1]
fn = conf[1][0]
print('true negatives:', tn)
print('true positives:', tp)
print('false negatives:', fn)
print('false positives:', fp)
sensitivity = float(tp) / (float(tp) + float(fn))
specificity = float(tn) / (float(tn) + float(fp))
print('sens:', sensitivity)
print('spec:', specificity)
return sensitivity, specificity
def plot_graphs(hist, results_dir, fold_num):
epoch_num = range(len(hist.history['acc']))
plt.clf()
plt.plot(epoch_num, hist.history['acc'], label='Training Accuracy')
plt.plot(epoch_num, hist.history['val_acc'], label="Validation Accuracy")
# plt.plot(epoch_num, hist.history['sensitivity'], label='Training Sensitivity')
# plt.plot(epoch_num, hist.history['val_sensitivity'], label='Validation Sensitivity')
# plt.plot(epoch_num, hist.history['specificity'], label='Training Specificity')
# plt.plot(epoch_num, hist.history['val_specificity'], label='Validation Specificity')
plt.legend(shadow=True)
plt.xlabel("Training Epoch Number")
plt.ylabel("Metric Value")
plt.savefig(results_dir + 'training_metrics_fold' + str(fold_num) + '.png', bbox_inches='tight')
plt.close()
def predict_and_visualize(model, indices, results_dir):
f = h5py.File(workdir + 'ibis.hdf5', 'r')
images = f['ibis_t1']
labels = f['qc_label']
filenames = f['filename']
predictions = []
with open(results_dir + 'test_images.csv', 'w') as output_file:
output_writer = csv.writer(output_file)
output_writer.writerow(['Filename', 'Probability'])
for index in indices:
img = images[index, target_size[0]//2, ...][np.newaxis, ..., np.newaxis]
label = labels[index, ...]
prediction = model.predict(img, batch_size=1)
print('probs:', prediction[0])
output_writer.writerow([filenames[index, ...], prediction[0][0], np.argmax(label)])
predictions.append(np.argmax(prediction[0]))
for i, (index, prediction) in enumerate(zip(indices, predictions)):
layer_idx = utils.find_layer_idx(model, 'predictions')
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
grads = visualize_cam(model, layer_idx, filter_indices=prediction, seed_input=img[0, ...], backprop_modifier='guided')
heatmap = np.uint8(cm.jet(grads)[:,:,0,:3]*255)
gray = np.uint8(img[0, :, :, :]*255)
gray3 = np.dstack((gray,)*3)
print('image shape, heatmap shape', gray3.shape, heatmap.shape)
plt.imshow(overlay(heatmap, gray3, alpha=0.25))
actual = np.argmax(labels[index, ...])
if prediction == actual:
decision = '_right_'
else:
decision = '_wrong_'
if actual == 1:
qc_status = 'PASS'
else:
qc_status = 'FAIL'
# filename = qc_status + decision + filenames[index, ...][:-4] + '.png'
filename = str(i) + decision + qc_status + '.png'
plt.axis('off')
plt.savefig(results_dir + filename, bbox_inches='tight')
plt.clf()
f.close()
if __name__ == "__main__":
start_time = time.time()
batch_size = 32
try:
experiment_number = pkl.load(open(workdir + 'experiment_number.pkl', 'rb'))
experiment_number += 1
except:
print('Couldnt find the file to load experiment number')
experiment_number = 0
print('This is experiment number:', experiment_number)
results_dir = workdir + '/experiment-' + str(experiment_number) + '/'
os.makedirs(results_dir)
pkl.dump(experiment_number, open(workdir + 'experiment_number.pkl', 'wb'))
remake = False
if remake:
indices, labels = make_ibis_qc()
pkl.dump(indices, open(workdir + 'valid_indices.pkl', 'wb'))
pkl.dump(labels, open(workdir + 'qc_labels.pkl', 'wb'))
else:
indices = pkl.load(open(workdir + 'valid_indices.pkl', 'rb'))
labels = pkl.load(open(workdir + 'qc_labels.pkl', 'rb'))
print('indices', indices)
print('labels', labels)
skf = StratifiedKFold(n_splits=4)
model = qc_model()
model.summary()
scores = {}
for metric in model.metrics_names:
scores[metric] = []
for k, (train_indices, test_indices) in enumerate(skf.split(np.asarray(indices), np.asarray(labels))):
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.5, random_state=42)
result_indices = sss.split(np.asarray(test_indices), np.asarray(labels)[test_indices])
test_indices, validation_indices = next(result_indices)
print('train indices:', train_indices)
print('validation indices:', validation_indices)
print('test indices:', test_indices)
model_checkpoint = ModelCheckpoint(results_dir + "best_weights" + "_fold_" + str(k) + ".hdf5", monitor="val_acc", verbose=0, save_best_only=True, save_weights_only=False, mode='max')
hist = model.fit_generator(batch(train_indices, batch_size, True), len(train_indices)//batch_size, epochs=100, validation_data=batch(validation_indices, batch_size), validation_steps=len(validation_indices)//batch_size+1, callbacks=[model_checkpoint], class_weight = {0:.7, 1:.3})
model.load_weights(results_dir + "best_weights" + "_fold_" + str(k) + ".hdf5")
model.save(results_dir + 'ibis_qc_model' + str(k) + '.hdf5')
metrics = model.evaluate_generator(batch(test_indices, batch_size, True), len(test_indices)//32+1)
print(model.metrics_names)
print(metrics)
plot_graphs(hist, results_dir, k)
for metric_name, score in zip(model.metrics_names, metrics):
scores[metric_name].append(score)
predict_and_visualize(model, test_indices, results_dir)
print(metric, scores[metric])
for metric in model.metrics_names:
print(metric, np.mean(scores[metric]))
print(scores)
print('time taken:', (time.time() - start_time) / 60, 'minutes')
print('This experiment is brought to you by the number:', experiment_number)