forked from OpenXAIProject/Lung-cancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D_subset.py
455 lines (397 loc) · 17.5 KB
/
3D_subset.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#Copyright 2018 (Institution) under XAI Project supported by Ministry of Science and ICT, Korea
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
# https://www.apache.org/licenses/LICENSE-2.0
#Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append(".")
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc, confusion_matrix
from modules.sequential import Sequential
from modules.linear import Linear
from modules.softmax import Softmax
from modules.relu import Relu
from modules.tanh import Tanh
from modules.convolution3D import Convolution3D
from modules.maxpool3D import MaxPool3D
from modules.convolution import Convolution
from modules.avgpool import AvgPool
from modules.maxpool import MaxPool
from modules.utils import Utils, Summaries, plot_relevances
import modules.render as render
import tensorflow as tf
import numpy as np
import pdb
from scipy.ndimage import rotate
import tflearn
import h5py
import itertools
from preprocessing import ImageDataGenerator
flags = tf.flags
logging = tf.logging
flags.DEFINE_integer("max_steps", 2000, 'Number of steps to run trainer.')
flags.DEFINE_integer("batch_size", 20, 'Number of steps to run trainer.')
flags.DEFINE_integer("test_every", 100, 'Number of steps to run trainer.')
flags.DEFINE_float("learning_rate", 0.0001, 'Initial learning rate')
flags.DEFINE_string("summaries_dir", 'convolutional_logs', 'Summaries directory')
flags.DEFINE_boolean("relevance", False, 'Compute relevances')
flags.DEFINE_string("relevance_method", 'alphabeta', 'relevance methods: simple/eps/w^2/alphabeta')
flags.DEFINE_boolean("save_model", True, 'Save the trained model')
flags.DEFINE_boolean("reload_model", True, 'Restore the trained model')
flags.DEFINE_integer("Class", 2, 'Number of class.')
FLAGS = flags.FLAGS
def nn(phase):
return Sequential(
[Convolution3D(kernel_size=3, output_depth=32, input_depth=1, batch_size=FLAGS.batch_size, input_dim=32,
act='lrelu', phase = phase, stride_size=1, pad='SAME'),
Convolution3D(kernel_size=3, output_depth=32, input_depth=32, batch_size=FLAGS.batch_size,
act='lrelu', phase = phase, stride_size=1, pad='SAME'),
MaxPool3D(),
Convolution3D(kernel_size=3, output_depth=64, input_depth=32, batch_size=FLAGS.batch_size,
act='lrelu', phase = phase, stride_size=1, pad='SAME'),
Convolution3D(kernel_size=3, output_depth=64, input_depth=64, batch_size=FLAGS.batch_size,
act='lrelu', phase = phase, stride_size=1, pad='SAME'),
MaxPool3D(),
Convolution3D(kernel_size=3, output_depth=128, input_depth=64, batch_size=FLAGS.batch_size,
act='lrelu', phase = phase, stride_size=1, pad='SAME'),
Convolution3D(kernel_size=3, output_depth=128, input_depth=64, batch_size=FLAGS.batch_size,
act='lrelu', phase = phase, stride_size=1, pad='SAME'),
MaxPool3D(),
Convolution3D(kernel_size=4, output_depth=128, stride_size=1, act='lrelu', phase = phase, pad='VALID'),
Convolution3D(kernel_size=1, output_depth=2, stride_size=1, phase = phase, final = True, pad='VALID')
])
def visualize(relevances, images_tensor):
n = FLAGS.batch_size
heatmap = relevances.reshape([n, 50, 50, 1])
input_images = images_tensor.reshape([n, 50, 50, 1])
heatmaps = []
for h, heat in enumerate(heatmap):
input_image = input_images[h]
maps = render.hm_to_rgb(heat, input_image, scaling=3, sigma=2, cmap='PuOr')
heatmaps.append(maps)
R = np.array(heatmaps)
with tf.name_scope('input_reshape'):
img = tf.summary.image('input', tf.cast(R, tf.float32), n)
return img.eval()
def next_batch(num, data, labels):
'''
Return a total of `num` random samples and labels.
'''
idx = np.arange(0, len(data))
np.random.shuffle(idx)
idx = idx[:num]
data_shuffle = [data[i] for i in idx]
labels_shuffle = [labels[i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
def format_image(image, num_images):
"""
Formats images
"""
idxs = np.random.choice(image.shape[0], num_images)
M = image.shape[1]
N = image.shape[2]
imagex = np.squeeze(image[idxs, :, :, :])
print(imagex.shape)
return imagex
def plot_roc_curve(fpr, tpr, roc_auc):
"""
Plots ROC curve
Args:
-----
FPR, TPR and AUC
"""
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
lw=lw, label='(AUC = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.axis('equal')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend(loc="lower right")
plt.savefig('roc1.png', bbox_inches='tight')
def create_mosaic(image, nrows, ncols):
"""
Tiles all the layers in nrows x ncols
Args:
------
image = 3d numpy array of M * N * number of filters dimensions
nrows = integer representing number of images in a row
ncol = integer representing number of images in a column
returns formatted image
"""
M = image.shape[1]
N = image.shape[2]
npad = ((0, 0), (1, 1), (1, 1))
image = np.pad(image, pad_width=npad, mode='constant', \
constant_values=0)
M += 2
N += 2
image = image.reshape(nrows, ncols, M, N)
image = np.transpose(image, (0, 2, 1, 3))
image = image.reshape(M * nrows, N * ncols)
return image
def plot_predictions(images, filename):
"""
Plots the predictions mosaic
"""
imagex = format_image(images, 4)
mosaic = create_mosaic(imagex, 2, 2)
plt.figure(figsize=(12, 12))
plt.imshow(mosaic, cmap='gray')
plt.axis('off')
plt.savefig(filename + '.png', bbox_inches='tight')
def plot_relevances(rel, img, writer):
img_summary = visualize(rel, img)
writer.add_summary(img_summary)
writer.flush()
def train(tag):
# Import data
tag = tag
sub = 'subset' + str(tag)
x_train_whole = []
y_train_whole = []
if tag==0 or tag==1 or tag==2:
tot = 8
elif tag==6:
tot = 14
elif tag==8:
tot = 15
else:
tot = 16
x_test_pos = []
x_test_neg = []
for num in range(tot):
h5f = h5py.File('./src/data/3D_data/' + sub + '_' + str(num) + '.h5', 'r')
y_tmp = np.asarray(h5f['Y'])
x_tmp = np.asarray(h5f['X'])
if max(y_tmp) != 0:
x_tmp_pos = x_tmp[np.where(y_tmp == 1)[0],:,:,:,:]
if x_test_pos == []:
x_test_pos = x_tmp_pos
else:
x_test_pos = np.concatenate([x_test_pos, x_tmp_pos])
negIndex = np.random.choice(np.where(y_tmp == 0)[0], len(x_tmp_pos) * 3, replace=False)
x_tmp_neg = x_tmp[negIndex, :, :, :, :]
if x_test_neg == []:
x_test_neg = x_tmp_neg
else:
x_test_neg = np.concatenate([x_test_neg, x_tmp_neg])
del x_tmp_pos
del x_tmp_neg
del negIndex
del x_tmp
del y_tmp
y_test_pos = np.ones(len(x_test_pos))
y_test_neg = np.zeros(len(x_test_neg))
x_test_tmp = np.concatenate([x_test_pos, x_test_neg])
y_test_tmp = np.concatenate([y_test_pos, y_test_neg])
idx = np.arange(0, len(y_test_tmp))
np.random.shuffle(idx)
x_test = np.asarray([x_test_tmp[i] for i in idx])
y_test = np.asarray([y_test_tmp[i] for i in idx])
del x_test_tmp
del y_test_tmp
del y_test_neg
del x_test_neg
del x_test_pos
del y_test_pos
print (len(x_test))
print (len(y_test))
sub = 'subset'
for i in range(10):
#for i in range(2):
subset = sub+str(i)
if i != tag:
if i == 0 or i == 1 or i == 2:
tot = 8
elif i == 6:
tot = 14
elif i == 8:
tot = 15
else:
tot = 16
x_train_pos = []
x_train_neg = []
for num in range(tot):
#for num in range(1):
h5f2 = h5py.File('./src/data/3D_data/' + subset + '_' + str(num) + '.h5', 'r')
x_tmp = np.asarray(h5f2['X'])
y_tmp = np.asarray(h5f2['Y'])
if max(y_tmp)!=0:
x_tmp_pos = x_tmp[np.where(y_tmp == 1)[0], :, :, :, :]
inp90 = np.zeros_like(x_tmp_pos)
inp180 = np.zeros_like(x_tmp_pos)
inp270 = np.zeros_like(x_tmp_pos)
inp45 = np.zeros_like(x_tmp_pos)
inp135 = np.zeros_like(x_tmp_pos)
inp225 = np.zeros_like(x_tmp_pos)
inp315 = np.zeros_like(x_tmp_pos)
for aug in range(len(x_tmp_pos)):
inp90[aug,:,:,:,:] = rotate(x_tmp_pos[aug,:,:,:,:], 90, reshape=False)
inp180[aug,:,:,:,:] = rotate(x_tmp_pos[aug,:,:,:,:], 180, reshape=False)
inp270[aug, :, :, :, :] = rotate(x_tmp_pos[aug, :, :, :, :], 270, reshape=False)
inp45[aug, :, :, :, :] = rotate(x_tmp_pos[aug, :, :, :, :], 45, reshape=False)
inp135[aug, :, :, :, :] = rotate(x_tmp_pos[aug, :, :, :, :], 135, reshape=False)
inp225[aug, :, :, :, :] = rotate(x_tmp_pos[aug, :, :, :, :], 225, reshape=False)
inp315[aug, :, :, :, :] = rotate(x_tmp_pos[aug, :, :, :, :], 315, reshape=False)
tmp = np.concatenate([np.concatenate([np.concatenate([np.concatenate([np.concatenate([np.concatenate([np.concatenate([x_tmp_pos, inp90]), inp180]), inp270]), inp45]), inp135]), inp225]), inp315])
idx2 = np.arange(0, len(tmp))
np.random.shuffle(idx2)
tmp2 = np.asarray([tmp[a] for a in idx2])
del inp90
del inp180
del inp270
del inp45
del inp135
del inp225
del inp315
if x_train_pos == []:
x_train_pos = tmp2[0:int(len(tmp)/4),:,:,:,:]
else:
x_train_pos = np.concatenate([x_train_pos, tmp2[0:int(len(tmp)/5),:,:,:,:]])
del tmp
negIndex = np.random.choice(np.where(y_tmp == 0)[0], len(x_tmp_pos) * 5, replace=False)
x_tmp_neg = x_tmp[negIndex, :, :, :, :]
if x_train_neg == []:
x_train_neg = x_tmp_neg
else:
x_train_neg = np.concatenate([x_train_neg, x_tmp_neg])
del tmp2
del x_tmp_neg
del x_tmp_pos
del negIndex
del x_tmp
del y_tmp
y_train_pos = np.ones(len(x_train_pos))
y_train_neg = np.zeros(len(x_train_neg))
x_train_tmp = np.concatenate([x_train_pos, x_train_neg])
y_train_tmp = np.concatenate([y_train_pos, y_train_neg])
del x_train_pos
del x_train_neg
del y_train_neg
del y_train_pos
idx = np.arange(0, len(y_train_tmp))
np.random.shuffle(idx)
x_train = np.asarray([x_train_tmp[a] for a in idx])
y_train = np.asarray([y_train_tmp[a] for a in idx])
del x_train_tmp
del y_train_tmp
if x_train_whole==[]:
x_train_whole = x_train
y_train_whole = y_train
else:
x_train_whole = np.concatenate([x_train_whole, x_train])
y_train_whole = np.concatenate([y_train_whole, y_train])
print (len(x_train_whole))
del x_train
del y_train
x_train = x_train_whole
y_train = y_train_whole
del x_train_whole
del y_train_whole
print (len(x_train))
print (len(y_train))
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# with tf.Session() as sess:
# Input placeholders
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, [None, 32, 32, 32, 1], name='x-input')
y_ = tf.placeholder(tf.float32, [None, 2], name='y-input')
phase = tf.placeholder(tf.bool, name='phase')
with tf.variable_scope('model'):
net = nn(phase)
# x_prep = prep_data_augment(x)
# x_input = data_augment(x_prep)
inp = tf.reshape(x, [FLAGS.batch_size, 32, 32, 32, 1])
op = net.forward(inp)
y = tf.reshape(op, [FLAGS.batch_size, 2])
soft = tf.nn.softmax(y)
trainer = net.fit(output=y, ground_truth=y_, loss='focal loss', optimizer='adam',
opt_params=[FLAGS.learning_rate])
with tf.variable_scope('relevance'):
if FLAGS.relevance:
LRP = net.lrp(y, FLAGS.relevance_method, 1)
# LRP layerwise
relevance_layerwise = []
# R = input_rel2
# for layer in net.modules[::-1]:
# R = net.lrp_layerwise(layer, R, FLAGS.relevance_method, 1e-8)
# relevance_layerwise.append(R)
else:
LRP = []
relevance_layerwise = []
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)), tf.float32))
# accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(tf.where(tf.greater(y,0),tf.ones_like(y, dtype=tf.float32), tf.zeros_like(y, dtype=tf.float32)), 2), tf.argmax(y_, 2)), tf.float32))
tf.summary.scalar('accuracy', accuracy)
# Merge all the summaries and write them out to /tmp/mnist_logs (by default)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('./conv_log/'+str(tag)+'_train', sess.graph)
test_writer = tf.summary.FileWriter('./conv_log/'+str(tag)+'_test')
tf.global_variables_initializer().run()
utils = Utils(sess, './3D_model/subset'+str(tag))
if FLAGS.reload_model:
utils.reload_model()
train_acc = []
test_acc = []
for i in range(FLAGS.max_steps):
if i % FLAGS.test_every == 0: # test-set accuracy
x_test_batch, y_test_batch = next_batch(FLAGS.batch_size, x_test, y_test)
tmp_y_batch = np.zeros([FLAGS.batch_size,2])
tmp_y_batch[:, 0] = np.ones([FLAGS.batch_size]) - y_test_batch
tmp_y_batch[:, 1] = np.zeros([FLAGS.batch_size]) + y_test_batch
y_test_batch = tmp_y_batch
test_inp = {x: x_test_batch, y_: y_test_batch, phase: False}
# pdb.set_trace()
summary, acc, relevance_test, op2, soft_val, rel_layer = sess.run([merged, accuracy, LRP, y, soft, relevance_layerwise],
feed_dict=test_inp)
test_writer.add_summary(summary, i)
test_acc.append(acc)
print('-----------')
for m in range(FLAGS.batch_size):
print(np.argmax(y_test_batch[m, :]),y_test_batch[m, :], end=" ")
print(np.argmax(op2[m, :]),op2[m,:], end=" ")
print(soft_val[m,:])
print("|")
print('Accuracy at step %s: %f' % (i, acc))
print(tag)
# print([np.sum(rel) for rel in rel_layer])
# print(np.sum(relevance_test))
# save model if required
if FLAGS.save_model:
utils.save_model()
else:
x_train_batch, y_train_batch = next_batch(FLAGS.batch_size, x_train, y_train)
tmp_y_batch = np.zeros([FLAGS.batch_size, 2])
tmp_y_batch[:, 0] = np.ones([FLAGS.batch_size]) - y_train_batch
tmp_y_batch[:, 1] = np.zeros([FLAGS.batch_size]) + y_train_batch
y_train_batch = tmp_y_batch
inp = {x: x_train_batch, y_: y_train_batch, phase: True}
summary, acc2, _, relevance_train, op2, soft_val, rel_layer = sess.run(
[merged, accuracy, trainer.train, LRP, y, soft, relevance_layerwise], feed_dict=inp)
train_writer.add_summary(summary, i)
#print(soft_val[0,:])
train_acc.append(acc2)
print(np.mean(train_acc), np.mean(test_acc))
# relevances plotted with visually pleasing color schemes
if FLAGS.relevance:
# plot test images with relevances overlaid
images = test_inp[test_inp.keys()[0]].reshape([FLAGS.batch_size, 32, 32, 32, 1])
# images = (images + 1)/2.0
plot_relevances(relevance_test.reshape([FLAGS.batch_size, 32, 32, 32, 1]),
images, test_writer)
train_writer.close()
test_writer.close()
def main(_):
tag = int(sys.argv[1])
#tag = 0
train(tag)
if __name__ == '__main__':
tf.app.run()