-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_saliency_maps.py
275 lines (230 loc) · 8.61 KB
/
get_saliency_maps.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
import argparse
import os
import pickle
import copy
import numpy as np
import torch
import matplotlib.pyplot as plt
from PIL import Image
from typing import Literal
from concepts import ConceptBank
from data import get_dataset
from models import SaliencyModel, get_model
from utils.saliency.gradients import SmoothGrad, VanillaGrad
from utils.saliency.image_utils import save_as_gray_image
from matplotlib.colors import PowerNorm
def config():
parser = argparse.ArgumentParser()
parser.add_argument(
"--concept-bank1", required=True, type=str, help="Path to the concept bank"
)
parser.add_argument(
"--concept-bank2", required=True, type=str, help="Path to the concept bank"
)
parser.add_argument(
"--out-dir",
required=True,
type=str,
default="artifacts/outdir",
help="Output folder for model/run info.",
)
# For the above: Please make sure to output the COCO-Stuff results in "outdir/coco-stuff"
parser.add_argument("--dataset", default="cub", type=str)
parser.add_argument("--backbone-name", default="resnet18_cub", type=str)
parser.add_argument("--device", default="cuda", type=str)
parser.add_argument("--seed", default=42, type=int, help="Random seed")
parser.add_argument("--batch-size", default=64, type=int)
parser.add_argument("--num-workers", default=4, type=int)
parser.add_argument(
"--img-path", default=None, help="img to compute saliency map for"
)
parser.add_argument("--concept-names1", nargs="+", type=str, default=None)
parser.add_argument("--concept-names2", nargs="+", type=str, default=None)
parser.add_argument("--targetclass", default="bicycle", type=str)
parser.add_argument("--concept-ix", type=int)
parser.add_argument("--method", default="smoothgrad", type=str)
return parser.parse_args()
def saliencyv2(
input_img: Image.Image,
input: torch.Tensor,
model: torch.nn.Module,
out_dir: str,
concept_ix: int,
method: Literal["vanilla", "smoothgrad"] = "smoothgrad",
) -> np.ndarray:
"""
input_img: raw image
input: preprocessed image
model: torch.nn.Module
"""
for param in model.parameters():
param.requires_grad = False
# set model in eval mode
model.eval()
input = input.unsqueeze(0)
input.requires_grad = True
if method == "vanilla":
vanilla_grad = VanillaGrad(pretrained_model=model, cuda=True)
vanilla_saliency = vanilla_grad(input, index=concept_ix)
img = save_as_gray_image(
vanilla_saliency, os.path.join(out_dir, "vanilla_grad.jpg")
)
return img
elif method == "smoothgrad":
N_SAMPLES_SMOOTHGRAD: int = 25 # 25
smooth_grad = SmoothGrad(
pretrained_model=model,
cuda=True,
n_samples=N_SAMPLES_SMOOTHGRAD,
magnitude=True,
)
smooth_saliency = smooth_grad(input, index=concept_ix)
img = save_as_gray_image(
smooth_saliency, os.path.join(out_dir, "smooth_grad.jpg")
)
print("Saved smooth gradient image")
return img
else:
raise NotImplementedError(f"Method {method} not implemented")
def saliency(input_img, input, model, out_dir):
# we don't need gradients w.r.t. weights for a trained model
for param in model.parameters():
param.requires_grad = False
# set model in eval mode
model.eval()
# transoform input PIL image to torch.Tensor and normalize
input.unsqueeze_(0)
# we want to calculate gradient of higest score w.r.t. input
# so set requires_grad to True for input
input.requires_grad = True
# forward pass to calculate predictions
preds = model(input)
score, indices = torch.max(preds, 1)
# backward pass to get gradients of score predicted class w.r.t. input image
score.backward()
# get max along channel axis
slc, _ = torch.max(torch.abs(input.grad[0]), dim=0)
# normalize to [0..1]
slc = (slc - slc.min()) / (slc.max() - slc.min())
return slc.cpu().numpy()
def plot_maps(img, maps1, maps2, concept_names1, concept_names2):
plt.figure(figsize=(15, 5))
plt.subplot(2, len(concept_names1) + 1, 1)
# plt.imshow(np.transpose(input_img.numpy(), (1, 2, 0)))\
plt.imshow(img)
plt.title("Input image")
plt.xticks([])
plt.yticks([])
for i in range(len(concept_names1)):
plt.subplot(2, len(concept_names1) + 1, i + 2)
if i == 0:
plt.ylabel("CLIP concepts")
plt.title(concept_names1[i])
plt.imshow(
maps1[i], cmap=plt.cm.hot, norm=PowerNorm(gamma=0.6, vmin=0.5)
) # maybe I should make this greyscale instead idk
plt.xticks([])
plt.yticks([])
for i in range(len(concept_names1)):
plt.subplot(2, len(concept_names1) + 1, len(concept_names1) + i + 3)
if i == 0:
plt.ylabel("CAV concepts")
plt.title(concept_names2[i])
plt.imshow(
maps2[i], cmap=plt.cm.hot, norm=PowerNorm(gamma=0.6, vmin=0.5)
) # maybe I should make this greyscale instead idk
plt.xticks([])
plt.yticks([])
plt.savefig(f"{args.out_dir}/saliency.png")
plt.show()
print(f"figure save in {args.out_dir}/saliency.png")
if __name__ == "__main__":
args = config()
# get concepts for the first concept bank
all_concepts1 = pickle.load(open(args.concept_bank1, "rb"))
all_concept_names1 = list(all_concepts1.keys())
print(
f"Bank path: {args.concept_bank1}. {len(all_concept_names1)} concepts will be used."
)
concept_bank1 = ConceptBank(all_concepts1, args.device)
# get concepts for the second concept bank
all_concepts2 = pickle.load(open(args.concept_bank2, "rb"))
all_concept_names2 = list(all_concepts2.keys())
print(
f"Bank path: {args.concept_bank2}. {len(all_concept_names2)} concepts will be used."
)
concept_bank2 = ConceptBank(all_concepts2, args.device)
# Get the backbone from the model zoo.
backbone, preprocess = get_model(args, backbone_name=args.backbone_name)
backbone = backbone.to(args.device)
# initialize the saliency model
(input, label), (input_img, input_label), class_name = get_dataset(
args, preprocess, single_image=True
)
input = input.to(args.device)
print(input)
print(input_img)
print("saliency map for image with label", label, input_label)
print("which is class_name", class_name)
# get a single preprocessed and non-preprossed image from the dataloader
if args.img_path is not None:
input_img = Image.open(args.img_path).convert("RGB")
input = preprocess(input_img)
input.to(args.device)
"""
saliency_model = SaliencyModel(
concept_bank=concept_bank,
backbone=backbone,
backbone_name=args.backbone_name,
concept_names=args.concept_names,
)
saliency(input_img, input, saliency_model, args.out_dir)
"""
maps1 = []
maps2 = []
for concept_name1, concept_name2 in zip(args.concept_names1, args.concept_names2):
# get the map for both the first concept bank
saliency_model1 = SaliencyModel(
concept_bank=concept_bank1,
backbone=backbone,
backbone_name=args.backbone_name,
concept_names=[concept_name1],
)
saliency_model1 = saliency_model1.to(args.device)
# second bank
saliency_model2 = SaliencyModel(
concept_bank=concept_bank2,
backbone=backbone,
backbone_name=args.backbone_name,
concept_names=[concept_name2],
)
saliency_model2 = saliency_model2.to(args.device)
if args.method in ["smoothgrad", "vanilla"]:
map1 = saliencyv2(
input_img,
input,
saliency_model1,
args.out_dir,
concept_ix=args.concept_ix,
method=args.method,
)
map2 = saliencyv2(
input_img,
input,
saliency_model2,
args.out_dir,
concept_ix=args.concept_ix,
method=args.method,
)
else:
map1 = saliency(input_img, copy.copy(input), saliency_model1, args.out_dir)
map2 = saliency(input_img, copy.copy(input), saliency_model2, args.out_dir)
maps1.append(map1)
maps2.append(map2)
plot_maps(
img=input_img,
maps1=maps1,
maps2=maps2,
concept_names1=args.concept_names1,
concept_names2=args.concept_names2,
)