forked from hungntt/xai_thyroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
243 lines (224 loc) · 13.7 KB
/
main.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
import glob
import os
import warnings
from datetime import datetime
import cv2
import numpy as np
import tensorflow.compat.v1 as tf
from skimage import io, img_as_ubyte
from tqdm import tqdm
from utils import DeepExplain, get_config, get_model, get_info, save_image, gen_cam, GradientMethod, draw, create_file, get_parser
from xai.adasise import AdaSISE
from xai.density_map import DensityMap
from xai.drise import DRISE
from xai.gradcam import GradCAM, GradCAMPlusPlus
from xai.kde import KDE
from xai.lime_method import LIME
from xai.rise import RISE
warnings.filterwarnings('ignore')
start = datetime.now()
def main(args):
# ---------------------------------Parameters-------------------------------------
img_rs, output_tensor, last_conv_tensor, grads, num_sample, NMS = None, None, None, None, None, None
config_xAI = get_config(args.config_path)
config_models = get_config(config_xAI['Model']['file_config'])
image_dict = {}
sess, img_input, detection_boxes, detection_scores, num_detections, detection_classes = get_model(
config_models[0]['model_path'])
threshold = config_xAI['Model']['threshold']
# -------------------Create directory-------------------
create_file(args.output_path)
create_file(args.output_numpy)
# -------------------------eLRP-------------------------
if args.method in ['eLRP']:
img_rs = sess.graph.get_tensor_by_name(config_xAI['Gradient']['target'] + ':0')
output_tensor = sess.graph.get_tensor_by_name(config_xAI['Gradient']['output'] + ':0')
with DeepExplain(session=sess) as de:
explainer = de.get_explainer(args.method, np.sum(output_tensor[0, :, 1:2]), img_rs)
# ---------------------------------GradCAM, GradCAM++-------------------------------------
elif args.method in ['GradCAM', 'GradCAM++']:
last_conv_tensor = sess.graph.get_tensor_by_name(config_xAI['CAM'][args.stage]['target'] + ':0')
output_tensor = sess.graph.get_tensor_by_name(config_xAI['CAM'][args.stage]['output'] + ':0')
if args.stage == 'first_stage':
grads = tf.gradients(np.sum(output_tensor[0, :, 1:2]), last_conv_tensor)[0]
else:
NMS = sess.graph.get_tensor_by_name(config_xAI['CAM'][args.stage]['NMS'] + ':0')
elif args.method in ['KDE', 'DensityMap', 'AdaSISE']:
# These two methods do not need num_sample
pass
else:
num_sample = config_xAI[args.method]['num_sample']
# Run xAI for each image
for j in tqdm(sorted(glob.glob(f'{args.image_path}/*.jpg'))):
# Load image from input folder and extract ground-truth labels from xml file
image = cv2.imread(j)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = img.reshape(1, img.shape[0], img.shape[1], 3)
name_img = os.path.basename(j).split('.')[0]
try:
gr_truth_boxes = get_info(config_xAI['Model']['folder_xml'] + f'{name_img}.xml')
except FileNotFoundError:
gr_truth_boxes = None
# First stage of model: Extract 300 boxes
if args.stage == 'first_stage':
if args.method in ['eLRP']:
# Run epsilon-LRP explainer
image_rs = sess.run(img_rs, feed_dict={img_input: image})
baseline = np.zeros_like(image_rs)
gradient = GradientMethod(sess, img_rs, output_tensor, explainer, baseline)
image_dict[args.method] = gradient(image, args.method, img_input)
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), image_dict[args.method])
save_image(image_dict, os.path.basename(j), args.output_path, index='full_image')
elif args.method in ['GradCAM', 'GradCAM++']:
y_p_boxes, y_p_num_detections = sess.run([detection_boxes, num_detections],
feed_dict={img_input: image})
boxs = []
for i in range(int(y_p_num_detections[0])):
h_img, w_img = image.shape[1:3]
x1, x2 = int(y_p_boxes[0][i][1] * w_img), int(y_p_boxes[0][i][3] * w_img)
y1, y2 = int(y_p_boxes[0][i][0] * h_img), int(y_p_boxes[0][i][2] * h_img)
boxs.append([x1, y1, x2, y2])
if args.method == 'GradCAM':
# Run GradCAM for each class
gradcam = GradCAM(sess, last_conv_tensor, output_tensor)
mask = gradcam(image, grads, img_input, args.stage, y_p_boxes)
# Save image and heatmap
image_dict[args.method], _ = gen_cam(img, mask, gr_truth_boxes, threshold, boxs)
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), mask)
save_image(image_dict, os.path.basename(j), args.output_path,
index='gradcam_first_stage_full_image')
else:
# Run GradCAM++ for each class
grad_cam_plus_plus = GradCAMPlusPlus(sess, last_conv_tensor, output_tensor)
mask_plus_plus = grad_cam_plus_plus(image, grads, img_input, args.stage, y_p_boxes) # cam mask
# Save image and heatmap
image_dict[args.method], _ = gen_cam(img, mask_plus_plus, gr_truth_boxes, threshold, boxs)
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), mask_plus_plus)
save_image(image_dict, os.path.basename(j), args.output_path,
index='gradcam_plus_first_stage_full_image')
elif args.method == 'AdaSISE':
# Run AdaSISE and save results
adasise = AdaSISE(image=image, sess=sess)
image_cam = adasise.explain(image, img_input)
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), image_cam)
image_dict[args.method], _ = gen_cam(img, image_cam, gr_truth_boxes, threshold)
save_image(image_dict, os.path.basename(j), args.output_path, index=f'adasise_full_image')
else:
print('Method not supported for first stage')
pass
# Main part
# Second stage of model: Detect final boxes containing the nodule(s)
else:
# Extract boxes from session
y_p_boxes, y_p_scores, y_p_num_detections = sess.run([detection_boxes,
detection_scores,
num_detections],
feed_dict={img_input: image})
if args.method in ['RISE']:
boxs = []
grid_size = config_xAI['RISE']['grid_size']
prob = config_xAI['RISE']['prob']
index = config_xAI['RISE']['index']
assert y_p_scores[0][index] > args.threshold
for i in range(int(y_p_num_detections[0])):
h_img, w_img = image.shape[1:3]
x1, x2 = int(y_p_boxes[0][i][1] * w_img), int(y_p_boxes[0][i][3] * w_img)
y1, y2 = int(y_p_boxes[0][i][0] * h_img), int(y_p_boxes[0][i][2] * h_img)
boxs.append([x1, y1, x2, y2])
rise = RISE(image=image, sess=sess, grid_size=grid_size, prob=prob, num_samples=num_sample)
rs = rise.explain(image, index, img_input, detection_boxes, detection_scores, num_detections,
detection_classes)[0]
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), rs)
image_dict[args.method], _ = gen_cam(img, rs, gr_truth_boxes, threshold, boxs)
save_image(image_dict, os.path.basename(j), args.output_path, index=f'rise_box{index}')
elif args.method in ['LIME']:
index = config_xAI['LIME']['index']
num_features = config_xAI['LIME']['num_features']
feature_view = 1
lime = LIME(sess, img_input=img_input, detection_scores=detection_scores, image=img, indices=index, num_features=num_features)
image_dict[args.method] = lime.explain(feature_view, num_samples=num_sample)
# ------------saliency map for LIME --------------
cam_map = np.zeros(image.shape[1:3])
for k, v in image_dict[args.method].result.local_exp[0]:
cam_map[image_dict[args.method].segments == k] = v
# save results
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), cam_map)
save_image(image_dict, os.path.basename(j), args.output_path, index=f'rise_box{index}')
elif args.method in ['GradCAM', 'GradCAM++']:
index = config_xAI['CAM'][args.stage]['index']
assert y_p_scores[0][index] > args.threshold
NMS_tensor = sess.run(NMS, feed_dict={img_input: image})
indices = NMS_tensor[index]
grads = tf.gradients(output_tensor[0][indices][1], last_conv_tensor)[0]
if args.method == 'GradCAM':
# Run GradCAM and save results
gradcam = GradCAM(sess, last_conv_tensor, output_tensor)
mask, x1, y1, x2, y2 = gradcam(image,
grads,
args.stage,
y_p_boxes,
indices=indices,
index=index,
y_p_boxes=y_p_boxes) # cam mask
# Save image and heatmap
image_dict['predict_box'] = img[y1:y2, x1:x2] # [H, W, C]
image_dict[args.method], _ = gen_cam(img[y1:y2, x1:x2], mask, gr_truth_boxes, threshold)
save_image(image_dict, os.path.basename(j), args.output_path, index=f'gradcam_2th_stage_box{index}')
else:
# Run GradCAM++ and save results
grad_cam_plus_plus = GradCAMPlusPlus(sess, last_conv_tensor, output_tensor)
mask_plus_plus, x1, y1, x2, y2 = grad_cam_plus_plus(image,
grads,
args.stage,
y_p_boxes,
indices=indices,
index=index,
y_p_boxes=y_p_boxes) # cam mask
# Save image and heatmap
image_dict[args.method], _ = gen_cam(img[y1:y2, x1:x2], mask_plus_plus, gr_truth_boxes, threshold)
save_image(image_dict, os.path.basename(j), args.output_path,
index=f'gradcam_plus_2th_stage_box{index}')
elif args.method == 'DRISE':
# Run DRISE and save results
drise = DRISE(image=image, sess=sess, grid_size=8, prob=0.4, num_samples=100)
rs = drise.explain(image,
img_input,
y_p_boxes,
y_p_num_detections,
detection_boxes,
detection_scores,
num_detections,
detection_classes)
boxs = []
for i in range(int(y_p_num_detections[0])):
h_img, w_img = image.shape[1:3]
x1, x2 = int(y_p_boxes[0][i][1] * w_img), int(y_p_boxes[0][i][3] * w_img)
y1, y2 = int(y_p_boxes[0][i][0] * h_img), int(y_p_boxes[0][i][2] * h_img)
boxs.append([x1, y1, x2, y2])
rs[0] -= np.min(rs[0])
rs[0] /= (np.max(rs[0]) - np.min(rs[0]))
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), rs)
image_dict[args.method], _ = gen_cam(img, rs[0], gr_truth_boxes, threshold, boxs)
save_image(image_dict, os.path.basename(j), args.output_path, index='drise_result')
elif args.method == 'KDE':
# Run KDE and save results
all_box = None
kde = KDE(sess, image, j, y_p_num_detections, y_p_boxes)
box, box_predicted = kde.get_box_predicted(img_input)
kernel, f = kde.get_kde_map(box)
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), f.T)
kde_score = 1 / kde.get_kde_score(kernel, box_predicted) # Compute KDE score
print('kde_score:', kde_score)
for i in range(300):
all_box = draw(image, boxs=[box[i]])
kde.show_kde_map(box_predicted, f, save_file=args.output_path)
elif args.method == 'DensityMap':
# Run DensityMap and save results
density_map = DensityMap(sess, image, j)
heatmap = density_map.explain(img_input, y_p_num_detections, y_p_boxes)
np.save(os.path.join(args.output_numpy, f"{args.method}_{name_img}.npy"), heatmap)
cv2.imwrite(os.path.join("/results/", f'{name_img}.jpg'), img_as_ubyte(heatmap))
if __name__ == '__main__':
arguments = get_parser()
main(arguments)
print(f'Total training time: {datetime.now() - start}')