-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
342 lines (306 loc) · 16.7 KB
/
metrics.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
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 __future__ import print_function
import os
import time
import pickle
import json
import random
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from scipy.special import softmax
from sklearn.metrics import average_precision_score
from components import feature_impt
def generate_mask(inputs, mask_type, percentage):
## generate mask to keep or remove top k% of feats
inputs_sort, inputs_ind = inputs.sort(1, descending=True)
cut_off_index = int(inputs.size(1)*percentage)
cut_off_values = inputs_sort[:, cut_off_index : cut_off_index+1] # [Batch, 1]
if mask_type == 'keep_top':
mask_cutoff = (inputs>cut_off_values).float() # [Batch, num_objs]
mask_positive = (inputs>0).float() # [Batch, num_objs]
mask = mask_positive * mask_cutoff # only keep positive FI (AND)
elif mask_type == 'remove_top':
mask_cutoff = (inputs<=cut_off_values).float() # [Batch, num_objs]
mask_negative = (inputs<=0).float() # [Batch, num_objs]
mask = torch.logical_or(mask_negative, mask_cutoff).float() # only keep positive FI (OR)
mask = mask.unsqueeze(-1) #[Batch, #objs, 1]
return mask
def create_csv(opt, metrics):
## create .pkl file of a pandas dataframe for all metrics
# get qtype
# if opt.dataset == 'xaicp':
# # get qtype for xai
# _path = './data/xaicp/questions/test-id_annotations.json'
# test_id_anns = json.load(open(_path))['annotations']
# _path = './data/xaicp/questions/test-ood_annotations.json'
# test_ood_anns = json.load(open(_path))['annotations']
# qid2qtype = {}
# for ann in test_id_anns:
# qid2qtype[ann['question_id']] = ann['question_type']
# for ann in test_ood_anns:
# qid2qtype[ann['question_id']] = ann['question_type']
# elif opt.dataset == 'hatcp':
# # get qtype for hat
# _path = './data/hatcp/questions/test-id_annotations.json'
# test_id_anns = json.load(open(_path))['annotations']
# _path = './data/hatcp/questions/test-ood_annotations.json'
# test_ood_anns = json.load(open(_path))['annotations']
# qid2qtype = {}
# for ann in test_id_anns:
# qid2qtype[ann['question_id']] = ann['answer_type']
# for ann in test_ood_anns:
# qid2qtype[ann['question_id']] = ann['answer_type']
# csv columns
column_names = ["dataset", "split", "model_type", "model_name", "seed",
"qid", "qtype", "gt_answers", "output_pred", "output_gt",
"human_impt_max", "human_impt_min", "acc",
"RRR_suff", "RRR_inv", "RRR_unc_pred", "RRR_unc_gt",
"FI_method", "suff_model", "comp_model", "plau_rank_corr", "plau_iou"]
# init variables
if opt.FI_predicted_class:
FI_method = opt.model_importance+'_pred'
else:
FI_method = opt.model_importance+'_gt'
dataset = opt.dataset
model_type = opt.model_type
split = opt.split_test
model_name = opt.checkpoint_path
seed = opt.seed
# iter through metrics
all_data = []
for qid in metrics["gt_answers"]:
# qtype
# if dataset in ['xaicp', 'hatcp']:
# _qtype = qid2qtype[qid]
# else:
# _qtype = ""
_qtype = ""
# ans
_gt_answer = metrics["gt_answers"][qid].argmax()
if not opt.ACC_only:
# suff/comp/unc/plau
_suff_model = metrics["suff_model"][qid][0.1] + metrics["suff_model"][qid][0.25] + metrics["suff_model"][qid][0.5]
_suff_model /= 3
_comp_model = metrics["comp_model"][qid][0.1] + metrics["comp_model"][qid][0.25] + metrics["comp_model"][qid][0.5]
_comp_model /= 3
_plau_iou = metrics["plau_iou"][qid][0.1] + metrics["plau_iou"][qid][0.25] + metrics["plau_iou"][qid][0.5]
_plau_iou /= 3
_unc_prob = softmax(metrics["RRR_unc"][qid])
_unc_prob_pred = _unc_prob.max()
_unc_prob_gt = (_unc_prob * metrics["gt_answers"][qid]).sum()
# probability output
_output_prob = softmax(metrics["model_outputs"][qid])
_prob_pred = _output_prob.max()
_prob_gt = (_output_prob * metrics["gt_answers"][qid]).sum()
# human impt
_human_impt_max = metrics["human_impt"][qid].max()
_human_impt_min = metrics["human_impt"][qid].min()
column_names = ["dataset", "split", "model_type", "model_name", "seed",
"qid", "qtype", "gt_answers", "output_pred", "output_gt",
"human_impt_max", "human_impt_min", "acc",
"RRR_suff", "RRR_inv", "RRR_unc_pred", "RRR_unc_gt",
"FI_method", "suff_model", "comp_model", "plau_rank_corr", "plau_iou"]
new_row = [dataset, split, model_type, model_name, seed,
qid, _qtype, int(_gt_answer), float(_prob_pred), float(_prob_gt),
float(_human_impt_max), float(_human_impt_min), float(metrics["accuracy"][qid]),
float(metrics["RRR_suff"][qid]), float(metrics["RRR_inv"][qid]),
float(_unc_prob_pred), float(_unc_prob_gt), FI_method,
float(_suff_model), float(_comp_model), float(metrics["plau_rank_corr"][qid]),
float(_plau_iou)]
else:
column_names = ["dataset", "split", "model_type", "model_name", "seed",
"qid", "qtype", "gt_answers", "pred_answers", "acc"]
new_row = [dataset, split, model_type, model_name, seed,
qid, _qtype, int(_gt_answer), int(metrics["model_outputs"][qid].argmax()),
float(metrics["accuracy"][qid])]
all_data.append(new_row)
# save
df = pd.DataFrame(all_data, columns = column_names)
_path = os.path.join(opt.checkpoint_path,
opt.saved_model_prefix+opt.split_test+'_'+opt.filter_mode+'_'+opt.filter_objects+
'_'+opt.model_importance+"_gt_metrics.pkl")
df.to_pickle(_path)
def calc_dp_level_metrics(model, tokenizer, dataloader, opt, log_file):
model.eval()
if opt.vqa_loss_type == 'softmax':
to_prob_func = nn.Softmax(dim=1)
else:
to_prob_func = nn.Sigmoid()
## results to record
# PART 1
acc_counter = 0
gt_answers = {} # dict(qid, array)
model_outputs = {} # dict(qid, array)
human_impt = {} # dict(qid, array)
accuracy = {} # dict(qid, binary acc) boolean
# PART 2
RRR_suff = {} # dict (qid, binary acc) boolean
RRR_inv = {} # dict (qid, avg acc) float
RRR_unc = {} # dict (qid, array) model output array(28,)
# PART 3
model_impt_results = {} # dict(qid, array)
suff_model = {} # dict(qid, dict), dict(percentage, float)
comp_model = {} # dict(qid, dict), dict(percentage, float)
plau_rank_corr = {} # dict(qid, float)
plau_iou = {} # dict(qid, float)
# iter batches
for objs, qns, answers, hint_scores, question_ids, image_ids, hint_flags, q_ori, a_ori in tqdm(iter(dataloader)):
cur_batch_size = objs.size(0)
# prep data
objs = objs.cuda().float().requires_grad_()
qns = qns.cuda().long()
answers = answers.cuda() # true labels
hint_scores = hint_scores.cuda().float() # B x num_objs x 1
### PART 1: original forward
with torch.no_grad():
_, logits, _, ans_idxs = feature_impt.forward(opt, model, tokenizer, objs, qns, q_ori)
prob_original = to_prob_func(logits)
predicted_ans = prob_original.ge(prob_original.max(-1, keepdim=True)[0]) # stritly greater
# RECORD: gt_answer, model_outputs
for index, qid in enumerate(question_ids): # iter through index
# qid = int(qid)
# assert(qid not in model_outputs)
gt_answers[qid] = answers[index].cpu().detach().numpy()
# if not opt.ACC_only:
model_outputs[qid] = logits[index].cpu().detach().numpy()
human_impt[qid] = hint_scores[index].squeeze().cpu().detach().numpy()
accuracy[qid] = answers[index][logits[index].cpu().detach().numpy().argmax()].cpu().detach().numpy()
acc_counter += accuracy[qid]
### PART 2: impt & nonimpt forward
if not opt.ACC_only:
qns_new = qns.repeat(2 + 3, 1) # 1 for suff, 1 for unc, 3 for inv
objs_new = objs.repeat(2 + 3, 1, 1)
q_ori_new = q_ori * (2+3)
## get masks
mask_impt = (hint_scores >opt.impt_threshold).float() # Impt mask
mask_nonimpt = (hint_scores <=opt.impt_threshold).float() # NonImpt mask
batch_mask_1 = (mask_impt.sum(dim=(1, 2)) > 0).float()
batch_mask_2 = (mask_nonimpt.sum(dim=(1, 2)) > 0).float()
# inv mask * 3
masks_inv = []
batch_masks_inv = []
for index_inv in range(3):
# randomly select nonimpt objs - uniform over num of selected objs
mask_count = mask_nonimpt.sum(dim=(1,2))
mask_uniform_nonimpt = torch.zeros(hint_scores.size()).cuda()
for index, count in enumerate(mask_count):
prob = torch.randint(int(count)+1, (1,)).cuda() / count
mask_uniform_nonimpt[index] = (torch.rand((opt.num_objects,1)).cuda() <= prob).float().cuda()
# combine with impt objs
mask = torch.logical_or(mask_impt, mask_uniform_nonimpt)
# if all objs are non-impt, ignore
mask_ignore = (mask_nonimpt.sum(dim=(1, 2)) != mask_nonimpt.size(1)).float()
mask = mask * mask_ignore.unsqueeze(-1).unsqueeze(-1)
masks_inv.append(mask)
batch_mask = (mask.sum(dim=(1, 2)) > 0).float()
batch_masks_inv.append(batch_mask)
# apply mask
objs_new[ : cur_batch_size] = feature_impt.apply_mask(opt, objs, mask_impt)
objs_new[cur_batch_size: cur_batch_size * 2] = feature_impt.apply_mask(opt, objs, mask_nonimpt)
for index_inv in range(3):
objs_new[cur_batch_size*(2+index_inv):cur_batch_size*(3+index_inv)] = feature_impt.apply_mask(opt, objs, masks_inv[index_inv])
# forward
with torch.no_grad():
_, logits_new, _, ans_idxs_new = feature_impt.forward(opt, model, tokenizer, objs_new, qns_new, q_ori_new)
# RECORD: RRR_suff, RRR_unc, RRR_inv
logits_impt_only = logits_new.split(cur_batch_size)[0]
logits_nonimpt_only = logits_new.split(cur_batch_size)[1]
logits_inv_0 = logits_new.split(cur_batch_size)[2]
logits_inv_1 = logits_new.split(cur_batch_size)[3]
logits_inv_2 = logits_new.split(cur_batch_size)[4]
for index, qid in enumerate(question_ids): # iter through index
# qid = int(qid)
# assert(qid not in RRR_suff)
# RRR_inv & RRR_suff & RRR_unc
RRR_unc[qid] = logits_nonimpt_only[index].cpu().detach().numpy()
RRR_suff[qid] = answers[index][logits_impt_only[index].argmax()].cpu().detach().numpy()
acc_0 = (logits_impt_only[index].argmax() == logits_inv_0[index].argmax()).float()
acc_1 = (logits_impt_only[index].argmax() == logits_inv_1[index].argmax()).float()
acc_2 = (logits_impt_only[index].argmax() == logits_inv_2[index].argmax()).float()
RRR_inv[qid] = ((acc_0 + acc_1 + acc_2) / 3.0).cpu().detach().numpy()
# del objs_new, qns_new, logits_new, ans_idxs_new, _
# del logits_impt_only, logits_nonimpt_only, logits_inv_0, logits_inv_1, logits_inv_2
# del masks_inv, batch_masks_inv
### PART 3: model impt with budget=1000
# get model impt - select from ['gradcam', 'expected_gradient', 'LOO', 'KOI', 'SHAP', 'avg_effect']
# gradcam, LOO, KOI does not care about budget (1, 15/36, 15/36 respectively)
# SHAP, avg_effect can have no gradient
# forward
if not opt.ACC_only and not opt.RRR_only:
_, _, model_impt, _ = feature_impt.FI_forward(opt, model, tokenizer, objs, qns, answers, hint_scores, hint_flags, q_ori)
# RECORD: model_outputs
for index, qid in enumerate(question_ids): # iter through index
# qid = int(qid)
model_impt_results[qid] = model_impt[index].cpu().detach().numpy()
# for each percentage cut-off
for percentage in opt.percentages:
# forward with masks
with torch.no_grad():
## based on model exp - suff - keep top
assert(model_impt.size() == (cur_batch_size, opt.num_objects))
mask_model = generate_mask(model_impt, 'keep_top', percentage)
objs_model = feature_impt.apply_mask(opt, objs, mask_model)
_, logits_model_exp_suff, _, _ = feature_impt.forward(opt, model, tokenizer, objs_model, qns, q_ori)
prob_model_exp_suff = to_prob_func(logits_model_exp_suff)
## based on model exp - comp - remove top
mask_model = generate_mask(model_impt, 'remove_top', percentage)
objs_model = feature_impt.apply_mask(opt, objs, mask_model)
_, logits_model_exp_comp, _, _ = feature_impt.forward(opt, model, tokenizer, objs_model, qns, q_ori)
prob_model_exp_comp = to_prob_func(logits_model_exp_comp)
# RECORD: suff_model, comp_model
mask_model = generate_mask(model_impt, 'keep_top', percentage).squeeze()
mask_human = (hint_scores>opt.impt_threshold).squeeze()
for index, qid in enumerate(question_ids): # iter through index
# qid = int(qid)
# init suff_model/comp_model for this FI method
if qid not in suff_model:
suff_model[qid] = {}
comp_model[qid] = {}
plau_iou[qid] = {}
# record suff_model/comp_model
suff_model[qid][percentage] = ((prob_original[index] - prob_model_exp_suff[index])*(predicted_ans[index]>0).float()).sum().cpu().detach().numpy()
comp_model[qid][percentage] = ((prob_original[index] - prob_model_exp_comp[index])*(predicted_ans[index]>0).float()).sum().cpu().detach().numpy()
# record plau_rank_corr
plau_rank_corr[qid] = spearmanr(hint_scores[index].squeeze().cpu().numpy(),
model_impt[index].detach().cpu().numpy())[0]
# record plau_iou
intersection = torch.logical_and(mask_model[index], mask_human[index]).cpu().numpy()
union = torch.logical_or(mask_model[index], mask_human[index]).cpu().numpy()
plau_iou[qid][percentage] = intersection.sum()/union.sum()
results = {'gt_answers': gt_answers,
'model_outputs': model_outputs,
'human_impt': human_impt,
'accuracy': accuracy,
'RRR_suff': RRR_suff,
'RRR_inv': RRR_inv,
'RRR_unc': RRR_unc,
'model_impt_results': model_impt_results,
'suff_model': suff_model,
'comp_model': comp_model,
'plau_rank_corr': plau_rank_corr,
'plau_iou': plau_iou}
print(f"{opt.checkpoint_path} {opt.split_test} acc: %.4f" % (acc_counter / len(results['accuracy'])))
## save all metrics
create_csv(opt, results)
# if opt.ACC_only:
# _path = os.path.join(opt.checkpoint_path, "../",
# opt.saved_model_prefix+opt.split_test+"_ACC_metrics.pth")
# elif opt.RRR_only:
# _path = os.path.join(opt.checkpoint_path,
# opt.saved_model_prefix+opt.split_test+"_RRR_metrics.pth")
# else: # full
# if opt.FI_predicted_class:
# _path = os.path.join(opt.checkpoint_path,
# opt.saved_model_prefix+opt.split_test+
# '_'+opt.model_importance+"_pred_metrics.pth")
# else:
# _path = os.path.join(opt.checkpoint_path,
# opt.saved_model_prefix+opt.split_test+
# '_'+opt.model_importance+"_gt_metrics.pth")
# with open(_path, 'wb') as file:
# pickle.dump(results, file, protocol=pickle.HIGHEST_PROTOCOL)
# return