-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_imagenet_hpo.py
More file actions
343 lines (289 loc) · 11 KB
/
Copy pathanalyze_imagenet_hpo.py
File metadata and controls
343 lines (289 loc) · 11 KB
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
from collections import defaultdict
import argparse
import os
from torchvision.transforms import InterpolationMode, Resize
import captum
import torch
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from pnpxai.core.modality.modality import ImageModality
from pnpxai.explainers import LRPUniformEpsilon, IntegratedGradients, KernelShap
from pnpxai.explainers.utils.feature_masks import FeatureMaskFunction
from pnpxai.explainers.utils.function_selectors import FunctionSelector
from pnpxai.explainers.utils.postprocess import PostProcessor
from pnpxai.evaluator.metrics import MoRF, LeRF, AbPC
from pnpxai.evaluator.optimizer import Objective, optimize
from experiments.utils import (
set_seed,
get_torchvision_model,
get_imagenet_sample_from_hf,
denormalize_image,
save_pickle_data,
load_pickle_data,
)
# plot settings
plt.rcParams['font.family'] = 'Times New Roman'
# configs
TARGET_EXPLAINERS = {
'lrpe': {
'pnpxai': LRPUniformEpsilon,
'captum': captum.attr.LRP,
'dname': r'LRP-Uniform$\varepsilon$',
},
'ig': {
'pnpxai': IntegratedGradients,
'captum': captum.attr.IntegratedGradients,
'dname': 'Integrated Gradients'
},
'ks': {
'pnpxai': KernelShap,
'captum': captum.attr.KernelShap,
'dname': 'KernelSHAP',
}
}
TARGET_METRICS = {
'morf': {
'cls': MoRF,
'dname': r'MoRF$\downarrow$',
},
'lerf': {
'cls': LeRF,
'dname': r'LeRF$\uparrow$',
},
'abpc': {
'cls': AbPC,
'dname': r'AbPC$\uparrow$',
},
}
# a custom feature mask function
class Checkerboard(FeatureMaskFunction):
def __init__(
self,
size=[20, 20],
):
assert len(size) == 2
self.size = size
self._n_checkers = size[0] * size[1]
def __call__(self, inputs: torch.Tensor):
assert inputs.dim() == 4
bsz, c, h, w = inputs.size()
# print(input_size)
resize = Resize([h, w], interpolation=InterpolationMode.NEAREST)
patch_masks = []
for i in range(self._n_checkers):
mask = np.zeros(self._n_checkers)
mask[i] = i
mask = resize(
torch.Tensor(mask).reshape(-
1,self.size[0], self.size[1])).unsqueeze(1)
patch_masks.append(mask.numpy())
return torch.from_numpy(sum(patch_masks)).squeeze(1).repeat(
bsz, 1, 1).long().to(inputs.device)
def analyze(args, fp_data):
"""
Performs HPO and saves raw data for a single data instance.
"""
# Setup
set_seed(args.seed)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model, transform = get_torchvision_model('resnet18')
model = model.to(device)
model.eval()
img, label = get_imagenet_sample_from_hf(
transform,
hf_repo_id="geonhyeongkim/imagenet-samples-for-pnpxai-experiments",
indices=[args.data_id],
)
imgs = img.unsqueeze(0).to(device)
targets = torch.tensor(args.data_id).unsqueeze(0).to(device)
# Get explanations and evaluations
fm_selector = FunctionSelector(
{'checkerboard': Checkerboard})
modality = ImageModality( # set data modality
channel_dim=1,
feature_mask_fn_selector=fm_selector,
)
plot_data = { # container for collecting plot data
'img': denormalize_image(img, mean=transform.mean, std=transform.std),
'label': label,
'heatmaps': defaultdict(dict),
'values': defaultdict(lambda: defaultdict(dict)),
}
for explainer_key in TARGET_EXPLAINERS:
# --- pnpxai -----------------------------------------------------------
# Create explainer
pnp_explainer = TARGET_EXPLAINERS[explainer_key]['pnpxai'](model)
metric = TARGET_METRICS['abpc']['cls'](
model=model, explainer=pnp_explainer,
) # metric to be used as objective: AbPC
# Get default postprocessor to initialize optimization: (SumPos, MinMax)
default_pp = modality.get_default_postprocessors()[0]
# Optimize explainer
obj = Objective(
explainer=pnp_explainer,
postprocessor=default_pp,
metric=metric,
modality=modality,
inputs=imgs,
targets=targets,
)
study = optimize(
obj,
direction='maximize',
n_trials=args.n_trials,
sampler='tpe',
seed=args.seed,
)
opt_explainer, opt_pp = study.best_trial.user_attrs.values()
# Get explanation of the optimized explainer
if explainer_key == 'ks':
setattr(opt_explainer, 'n_samples', 300)
opt_attrs = opt_explainer.attribute(imgs, targets)
opt_attrs_pp = opt_pp(opt_attrs)
plot_data['heatmaps']['pnpxai'][explainer_key] = (
opt_attrs_pp.squeeze().detach().cpu().numpy())
# --- captum -----------------------------------------------------------
# Create explainer
captum_kwargs = {}
if explainer_key == 'ks':
# Use same feature mask with pnpxai
captum_kwargs['feature_mask'] = Checkerboard()(imgs)
captum_kwargs['n_samples'] = 300
captum_explainer = TARGET_EXPLAINERS[explainer_key]['captum'](model)
# Get explanation of captum explainer
captum_attrs = captum_explainer.attribute(
inputs=imgs, target=targets, **captum_kwargs)
captum_attrs_pp = default_pp(captum_attrs)
plot_data['heatmaps']['captum'][explainer_key] = (
captum_attrs_pp.squeeze().detach().cpu().numpy())
# --- evaluation -------------------------------------------------------
for metric_key in TARGET_METRICS:
# Create metric and evaluate
pnp_value = TARGET_METRICS[metric_key]['cls'](
model=model, explainer=pnp_explainer).evaluate(
inputs=imgs, targets=targets, attributions=opt_attrs_pp).item()
plot_data['values']['pnpxai'][explainer_key][metric_key] = pnp_value
captum_value = TARGET_METRICS[metric_key]['cls'](
model=model, explainer=captum_explainer).evaluate(
inputs=imgs, targets=targets, attributions=captum_attrs_pp).item()
plot_data['values']['captum'][explainer_key][metric_key] = captum_value
# Save the data
os.makedirs(os.path.dirname(fp_data), exist_ok=True)
save_pickle_data(data=plot_data, filepath=fp_data)
DEFAULT_SELECTED_SAMPLE_INDICES = [
75, 358, 367, 852,
]
def visualize(args, fp_data, fp_fig):
"""
Loads saved data for a single instance and generates visualization.
"""
# Load the data
plot_data = load_pickle_data(fp_data)
# Set layout
edge_size = 2.5
fig = plt.figure(figsize=(edge_size*3, edge_size*3))
outer = gridspec.GridSpec(
2, 1,
height_ratios=[1.0, 2.4],
)
row1 = gridspec.GridSpecFromSubplotSpec(
1, 3,
subplot_spec=outer[0],
)
ax_img = fig.add_subplot(row1[0, 0])
ax_bar = fig.add_subplot(row1[0, 1:3])
row23 = gridspec.GridSpecFromSubplotSpec(
2, 3,
subplot_spec=outer[1],
hspace=0.04,
wspace=0.04
)
axes_heatmaps = [[
fig.add_subplot(row23[i, j]) for j in range(3)] for i in range(2)]
# Plot sample img
ax_img.imshow(plot_data['img'])
ax_img.set_xticks([]); ax_img.set_yticks([])
ax_img.set_aspect('equal')
ax_img.set_title(plot_data['label'].replace('_', ' ').title(), fontsize=15)
# Plot differences in evaluations between pnpxai and captum
bar_data = defaultdict(list)
for explainer_key in TARGET_EXPLAINERS:
for metric_key in TARGET_METRICS:
diff = (
plot_data['values']['pnpxai'][explainer_key][metric_key]
- plot_data['values']['captum'][explainer_key][metric_key]
)
bar_data[explainer_key].append(diff)
x = np.arange(len(TARGET_METRICS))
width = 0.25
for i, (explainer_key, evals) in enumerate(bar_data.items()):
ax_bar.bar(
x + i*width, evals, width,
label=TARGET_EXPLAINERS[explainer_key]['dname'])
ax_bar.set_ylim(-.2, .8)
ax_bar.set_title('PnPXAI - Captum', fontsize=15)
ax_bar.set_xticks(
x + width,
[TARGET_METRICS[nm]['dname'] for nm in TARGET_METRICS],
fontsize=10,
)
ax_bar.grid(axis='y')
ax_bar.margins(x=0.01)
# Plot heatmaps
for c, explainer_key in enumerate(TARGET_EXPLAINERS):
alpha = 1.
if explainer_key == 'ks':
axes_heatmaps[0][c].imshow(plot_data['img'])
axes_heatmaps[1][c].imshow(plot_data['img'])
alpha *= .75
axes_heatmaps[0][c].imshow(
plot_data['heatmaps']['captum'][explainer_key],
cmap='Reds', alpha=alpha,
)
axes_heatmaps[1][c].imshow(
plot_data['heatmaps']['pnpxai'][explainer_key],
cmap='Reds', alpha=alpha,
)
axes_heatmaps[0][c].set_title(
TARGET_EXPLAINERS[explainer_key]['dname'], fontsize=15)
if c == 0:
axes_heatmaps[0][c].set_ylabel('Captum', fontsize=15)
axes_heatmaps[1][c].set_ylabel(
'PnPXAI (Ours)', fontsize=15, fontweight='bold')
axes_heatmaps[0][c].set_xticks([]); axes_heatmaps[0][c].set_yticks([])
axes_heatmaps[1][c].set_xticks([]); axes_heatmaps[1][c].set_yticks([])
axes_heatmaps[0][c].set_aspect('equal')
axes_heatmaps[1][c].set_aspect('equal')
# Save figure
fig.legend(
loc='upper center', ncols=3,
bbox_to_anchor=(.5, 1.0), frameon=False, fontsize=12)
os.makedirs(os.path.dirname(fp_fig), exist_ok=True)
fig.savefig(fp_fig, bbox_inches='tight', pad_inches=0.02, dpi=300)
print(f"Visualization saved for Data ID {args.data_id}: {fp_fig}")
def main():
"""Main execution function"""
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('--data_id', type=int, required=True, help='The specific ID of the data instance to process.')
parser.add_argument('--save_dir', type=str, default='results/analyze_imagenet_hpo/')
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--n_trials', default=100, type=int)
parser.add_argument('--analyze', action='store_true')
parser.add_argument('--visualize', action='store_true')
args = parser.parse_args()
# Set result filepaths
os.makedirs(args.save_dir, exist_ok=True)
fp_data = os.path.join(args.save_dir, 'raw', f'{args.data_id}.pkl')
fp_fig = os.path.join(args.save_dir, 'figures', f'{args.data_id}.pdf')
# Run experiment
if args.analyze:
analyze(args, fp_data)
if args.visualize:
if not os.path.exists(fp_data):
raise Exception(
f'{fp_data} not found. Try again with the following flag: --analyze')
visualize(args, fp_data, fp_fig)
if __name__ == '__main__':
main()