-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
281 lines (238 loc) · 8.52 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
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
import argparse
import sys
import os
import json
import numpy as np
import matplotlib.pyplot as plt
from tqdm.auto import tqdm
import traceback
sys.path.append(os.getcwd())
parser = argparse.ArgumentParser()
parser.add_argument("setting_keys", type=str, help="experiment setting keys", nargs="*")
parser.add_argument(
"-t",
"--target",
type=str,
help="target experiment of the settings",
choices=["sa"],
required=True,
)
parser.add_argument("-b", "--batch_size", type=int, help="batch size", default=None)
parser.add_argument("-stop", "--stop", type=float, help="stop", default=None)
parser.add_argument("-steps", "--steps", type=float, help="steps", default=None)
parser.add_argument("-mk", "--model_key", type=str, help="model key", required=True)
parser.add_argument("-dk", "--dataset_key", type=str, help="dataset key", required=True)
parser.add_argument(
"-c",
"--case",
type=str,
help="case of the experiment",
choices=[
"imagenet",
"isic",
"places365",
],
required=True,
)
mode_map = {
"xai": "xai",
"x": "xai",
"eval": "eval",
"e": "eval",
"sequence": "sequence",
"s": "sequence",
"auc": "auc",
"a": "auc",
"sn": "sanity",
"sanity": "sanity",
}
parser.add_argument(
"-m",
"--mode",
type=str,
help="xai execution of the given settings",
choices=mode_map.keys(),
)
args = parser.parse_args()
if __name__ == "__main__":
mode = mode_map.get(args.mode)
target_exp = args.target
model_key = args.model_key
dataset_key = args.dataset_key
setting_keys = args.setting_keys
batch_size = args.batch_size
stop = args.stop
steps = args.steps
case = args.case
if case in ["imagenet", "imagenet919"]:
from cases.imagenet_exp import settings
if case == "isic":
from cases.isic_exp import settings
if case == "places365":
from cases.places365_exp import settings
from cv_exp.pipe import Pipe
from cv_exp.eval import batch_rcap, batch_auc, batch_sanity
from cv_exp.utils import fix_seed, read_array
fix_seed(42)
pipe = Pipe()
device = pipe.device
if len(setting_keys) == 0:
setting_keys = settings.sa_settings.keys()
with tqdm(total=len(setting_keys), position=0, leave=True) as opbar:
for setting_key in setting_keys:
opbar.set_postfix({"key": setting_key})
print(f"Setting Key: {setting_key}")
# TODO: debug mode
debug = False
assert settings.sa_settings.get(setting_key) is not None
setting = settings.sa_settings[setting_key]
exts = [".npz", ".npy"]
# exp = setting['exp']
current_setting_key = setting_key
setting_save = os.path.join(
".",
"evaluation_result",
target_exp,
args.case,
model_key,
dataset_key,
current_setting_key,
)
print(setting_save)
name = setting.get("name")
description = setting.get("description")
func = setting.get("fu")
sa_args = setting.get("sa_args")
use_predicted_target = setting.get("use_predicted_target", False)
start = setting.get("start", 0)
end = setting.get("end", settings.dataset_len(dataset_key))
batch_size = (
batch_size if batch_size is not None else setting.get("batch_size", 100)
)
stop = stop if stop is not None else setting.get("stop", 0.5)
steps = steps if steps is not None else setting.get("steps", 0.1)
print(f"Setting:\n", setting, batch_size, start, end, stop, steps)
if mode in ["xai", "sequence"]:
print("\r\nXAI:")
# des_file = os.path.join(setting_save, 'des.txt')
# with open(des_file, 'w') as f:
# f.write(description)
# tracker = EmissionsTracker(
# project_name=f'{target_exp}_{current_setting_key}', tracking_mode='machine',
# output_dir=setting_save, log_level='critical')
try:
# tracker.start()
maps, tt0 = pipe.get_saliency_map(
settings,
model_key,
dataset_key,
start,
end,
batch_size,
target_exp,
func,
sa_args,
use_predicted_target,
debug=debug,
)
except Exception as e:
print(traceback.format_exc())
# or
print(sys.exc_info()[2])
raise e
finally:
# tracker.stop()
pass
if not debug:
task_save = os.path.join(setting_save)
if not os.path.exists(task_save):
os.makedirs(task_save, exist_ok=True)
if maps.shape[0] > 0:
np.save(os.path.join(task_save, f"maps"), maps)
stat = {
"n": maps.shape[0],
"sa_time": tt0,
"name": name,
"description": description,
"model_key": model_key,
"dataset_key": dataset_key,
"start": start,
"end": end,
"batch_size": batch_size,
"func": str(func),
"sa_args": sa_args,
}
with open(os.path.join(task_save, "stat.json"), "w") as f:
f.write(json.dumps(stat, indent=4, default=lambda o: str(o)))
else:
print(maps.shape)
plt.imshow(maps[0])
plt.show()
ext = ".npz"
for eee in exts:
if os.path.exists(os.path.join(setting_save, f"maps{eee}")):
ext = eee
break
if not os.path.exists(os.path.join(setting_save, f"maps{ext}")):
print(f"File not exist: {os.path.join(setting_save, f'maps{ext}')}")
continue
try:
os.remove(os.path.join(setting_save, f"eval_rs{ext}"))
except OSError:
pass
if mode in ["eval", "sequence"]:
print("\r\nRcap Eval:")
maps = read_array(setting_save, "maps")
rs = batch_rcap(
settings,
model_key,
dataset_key,
start,
end,
batch_size,
maps,
device,
stop,
steps,
)
for k, v in rs.items():
# print(k, v)
np.save(os.path.join(setting_save, f"{k}"), v)
if mode in ["auc"]:
print("\r\nD/I AUC Eval:")
maps = read_array(setting_save, "maps")
rs = batch_auc(
settings,
model_key,
dataset_key,
start,
end,
batch_size,
maps,
device,
)
for k, v in rs.items():
np.save(os.path.join(setting_save, f"{k}"), v)
if mode == "sanity":
print("\r\nSanity Check Eval:")
maps = read_array(setting_save, "maps")
rs = batch_sanity(
settings,
model_key,
dataset_key,
start,
end,
batch_size,
maps,
device,
func,
sa_args,
)
for k, v in rs.items():
np.save(os.path.join(setting_save, f"{k}"), v)
opbar.update(1)
# del unarys, mean, score_gd_mean, score_gd_max, all_mop
# del score_gd_var, score_ld_mean, score_ld_max, score_ld_var
# del maps
# torch.cuda.empty_cache()
# gc.collect()