forked from scotthlee/hamlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_heatmaps.py
143 lines (132 loc) · 5.61 KB
/
generate_heatmaps.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
"""Writes a report with the models' predictions for a set of new images.
Notes:
1. 'img_dir' points to the directory holding the folder that holds the image
files for prediction. That folder should be named 'img/'.
"""
import numpy as np
import tensorflow as tf
import pickle
import argparse
import os
import saliency.core as saliency
from hamlet import attribution, models
from hamlet.tools import image as tim
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--image_dir',
type=str,
help='Path to the folder holding the images for \
prediction.')
parser.add_argument('--output_dir',
type=str,
default=None,
help='Path where the script should dump the output \
files. Writes to img_dir by default.')
parser.add_argument('--method',
type=str,
default='xrai',
help='Attribution method to use.',
choices=['gradient', 'blur', 'gig',
'ig', 'gradcam', 'xrai'])
parser.add_argument('--mod_dir',
type=str,
default='output/abnormal/checkpoints/training/',
help='Path to the folder holding the trained \
model.')
parser.add_argument('--model_flavor',
type=str,
default='EfficientNetV2M',
help='What pretrained model to use as the feature \
extractor.')
parser.add_argument('--no_augmentation',
action='store_true',
help='Specifies that the models should be built with \
the image augmentation layer.')
parser.add_argument('--image_dim',
type=int,
default=600,
help='Either dimension of the image to be passed \
to the model.')
parser.add_argument('--single_GPU',
action='store_true',
help='Turns off distributed (multi-GPU) training')
parser.add_argument('--prefix',
type=str,
default='',
help='Prefix for the predictions file.')
parser.add_argument('--scale',
type=float,
default=10.0,
help='Scaling parameter for figure size.')
parser.add_argument('--smooth',
action='store_true',
help='Whether to generated heatmaps with smooth \
activations as well as normal ones.')
parser.set_defaults(no_augmentation=False,
single_GPU=False,
smooth=False)
args = parser.parse_args()
# Setting things up
AUGMENT = not args.no_augmentation
METHOD = args.method
MOD_DIR = args.mod_dir
MODEL_FLAVOR = args.model_flavor
IMG_DIR = args.image_dir
IMG_DIM = args.image_dim
OUT_DIR = IMG_DIR
DISTRIBUTED = not args.single_GPU
PREFIX = args.prefix
SCALE = args.scale
SMOOTH = args.smooth
if args.output_dir is not None:
OUT_DIR = args.output_dir
# Setting training strategy
if DISTRIBUTED:
print('Using multiple GPUs.\n')
cdo = tf.distribute.HierarchicalCopyAllReduce()
strategy = tf.distribute.MirroredStrategy(cross_device_ops=cdo)
else:
strategy = tf.distribute.get_strategy()
# Loading the data
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
IMG_DIR[:-4],
labels=None,
shuffle=False,
image_size=(IMG_DIM, IMG_DIM)
)
# Loading the images
im_files = os.listdir(IMG_DIR)
im_ids = [f[:-4] for f in im_files]
im_paths = [IMG_DIR + s for s in im_files]
# Loading the trained model
with strategy.scope():
mod = models.EfficientNet(num_classes=1,
img_height=IMG_DIM,
img_width=IMG_DIM,
augmentation=AUGMENT,
model_flavor=MODEL_FLAVOR)
mod.load_weights(MOD_DIR)
conv_layer = mod.get_layer('top_conv')
mod = tf.keras.models.Model([mod.inputs],
[conv_layer.output, mod.output])
call_model_args = {'class_id': 0,
'model': mod}
# Trying the single-image plot
for i, im_path in enumerate(im_paths):
print('Making a ' + METHOD + ' heatmap for ' + im_files[i])
im = tim.load_image(im_path, (IMG_DIM, IMG_DIM))
mask, method_name = attribution.compute_masks(
image=im,
methods=[METHOD],
call_model_args=call_model_args,
batch_size=1,
smooth=SMOOTH)
attribution.panel_plot(images=[im],
masks=mask,
method_name=method_name[0],
save_dir=OUT_DIR,
image_id=im_files[i][:-4],
smoothed=SMOOTH,
show=False,
save=True,
scale=SCALE)