-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
676 lines (580 loc) · 23.6 KB
/
test.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import argparse
import copy
import json
import os
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from scipy.stats import hmean
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm
from utils import *
from loss import loss_calu
from parameters import parser, YML_PATH
from dataset import CompositionDataset
from model.rapr_model import RAPR
cudnn.benchmark = True
device = "cuda" if torch.cuda.is_available() else "cpu"
class Evaluator:
"""
Evaluator class, adapted from:
https://github.com/Tushar-N/attributes-as-operators
With modifications from:
https://github.com/ExplainableML/czsl
"""
def __init__(self, dset, model):
self.dset = dset
# Convert text pairs to idx tensors: [('sliced', 'apple'), ('ripe',
# 'apple'), ...] --> torch.LongTensor([[0,1],[1,1], ...])
pairs = [(dset.attr2idx[attr], dset.obj2idx[obj])
for attr, obj in dset.pairs]
self.train_pairs = [(dset.attr2idx[attr], dset.obj2idx[obj])
for attr, obj in dset.train_pairs]
self.pairs = torch.LongTensor(pairs)
# Mask over pairs that occur in closed world
# Select set based on phase
if dset.phase == 'train':
print('Evaluating with train pairs')
test_pair_set = set(dset.train_pairs)
test_pair_gt = set(dset.train_pairs)
elif dset.phase == 'val':
print('Evaluating with validation pairs')
test_pair_set = set(dset.val_pairs + dset.train_pairs)
test_pair_gt = set(dset.val_pairs)
else:
print('Evaluating with test pairs')
test_pair_set = set(dset.test_pairs + dset.train_pairs)
test_pair_gt = set(dset.test_pairs)
self.test_pair_dict = [
(dset.attr2idx[attr],
dset.obj2idx[obj]) for attr,
obj in test_pair_gt]
self.test_pair_dict = dict.fromkeys(self.test_pair_dict, 0)
# dict values are pair val, score, total
for attr, obj in test_pair_gt:
pair_val = dset.pair2idx[(attr, obj)]
key = (dset.attr2idx[attr], dset.obj2idx[obj])
self.test_pair_dict[key] = [pair_val, 0, 0]
# open world
if dset.open_world:
masks = [1 for _ in dset.pairs]
else:
masks = [1 if pair in test_pair_set else 0 for pair in dset.pairs]
# masks = [1 if pair in test_pair_set else 0 for pair in dset.pairs]
self.closed_mask = torch.BoolTensor(masks)
# Mask of seen concepts
seen_pair_set = set(dset.train_pairs)
mask = [1 if pair in seen_pair_set else 0 for pair in dset.pairs]
self.seen_mask = torch.BoolTensor(mask)
# Object specific mask over which pairs occur in the object oracle
# setting
oracle_obj_mask = []
for _obj in dset.objs:
mask = [1 if _obj == obj else 0 for attr, obj in dset.pairs]
oracle_obj_mask.append(torch.BoolTensor(mask))
self.oracle_obj_mask = torch.stack(oracle_obj_mask, 0)
# Decide if the model under evaluation is a manifold model or not
self.score_model = self.score_manifold_model
# Generate mask for each settings, mask scores, and get prediction labels
def generate_predictions(self, scores, obj_truth, bias=0.0, topk=1): # (Batch, #pairs)
'''
Inputs
scores: Output scores
obj_truth: Ground truth object
Returns
results: dict of results in 3 settings
'''
def get_pred_from_scores(_scores, topk):
"""
Given list of scores, returns top 10 attr and obj predictions
Check later
"""
_, pair_pred = _scores.topk(
topk, dim=1) # sort returns indices of k largest values
pair_pred = pair_pred.contiguous().view(-1)
attr_pred, obj_pred = self.pairs[pair_pred][:, 0].view(
-1, topk
), self.pairs[pair_pred][:, 1].view(-1, topk)
return (attr_pred, obj_pred)
results = {}
orig_scores = scores.clone()
mask = self.seen_mask.repeat(
scores.shape[0], 1
) # Repeat mask along pairs dimension
scores[~mask] += bias # Add bias to test pairs
# Unbiased setting
# Open world setting --no mask, all pairs of the dataset
results.update({"open": get_pred_from_scores(scores, topk)})
results.update(
{"unbiased_open": get_pred_from_scores(orig_scores, topk)}
)
# Closed world setting - set the score for all Non test pairs to -1e10,
# this excludes the pairs from set not in evaluation
mask = self.closed_mask.repeat(scores.shape[0], 1)
closed_scores = scores.clone()
closed_scores[~mask] = -1e10
closed_orig_scores = orig_scores.clone()
closed_orig_scores[~mask] = -1e10
results.update({"closed": get_pred_from_scores(closed_scores, topk)})
results.update(
{"unbiased_closed": get_pred_from_scores(closed_orig_scores, topk)}
)
return results
def score_clf_model(self, scores, obj_truth, topk=1):
'''
Wrapper function to call generate_predictions for CLF models
'''
attr_pred, obj_pred = scores
# Go to CPU
attr_pred, obj_pred, obj_truth = attr_pred.to(
'cpu'), obj_pred.to('cpu'), obj_truth.to('cpu')
# Gather scores (P(a), P(o)) for all relevant (a,o) pairs
# Multiply P(a) * P(o) to get P(pair)
# Return only attributes that are in our pairs
attr_subset = attr_pred.index_select(1, self.pairs[:, 0])
obj_subset = obj_pred.index_select(1, self.pairs[:, 1])
scores = (attr_subset * obj_subset) # (Batch, #pairs)
results = self.generate_predictions(scores, obj_truth)
results['biased_scores'] = scores
return results
def score_manifold_model(self, scores, obj_truth, bias=0.0, topk=1):
'''
Wrapper function to call generate_predictions for manifold models
'''
# Go to CPU
scores = {k: v.to('cpu') for k, v in scores.items()}
obj_truth = obj_truth.to(device)
# Gather scores for all relevant (a,o) pairs
scores = torch.stack(
[scores[(attr, obj)] for attr, obj in self.dset.pairs], 1
) # (Batch, #pairs)
orig_scores = scores.clone()
results = self.generate_predictions(scores, obj_truth, bias, topk)
results['scores'] = orig_scores
return results
def score_fast_model(self, scores, obj_truth, bias=0.0, topk=1):
'''
Wrapper function to call generate_predictions for manifold models
'''
results = {}
# Repeat mask along pairs dimension
mask = self.seen_mask.repeat(scores.shape[0], 1)
scores[~mask] += bias # Add bias to test pairs
mask = self.closed_mask.repeat(scores.shape[0], 1)
closed_scores = scores.clone()
closed_scores[~mask] = -1e10
# sort returns indices of k largest values
_, pair_pred = closed_scores.topk(topk, dim=1)
# _, pair_pred = scores.topk(topk, dim=1) # sort returns indices of k
# largest values
pair_pred = pair_pred.contiguous().view(-1)
attr_pred, obj_pred = self.pairs[pair_pred][:, 0].view(-1, topk), \
self.pairs[pair_pred][:, 1].view(-1, topk)
results.update({'closed': (attr_pred, obj_pred)})
return results
def evaluate_predictions(
self,
predictions,
attr_truth,
obj_truth,
pair_truth,
allpred,
topk=1):
# Go to CPU
attr_truth, obj_truth, pair_truth = (
attr_truth.to("cpu"),
obj_truth.to("cpu"),
pair_truth.to("cpu"),
)
pairs = list(zip(list(attr_truth.numpy()), list(obj_truth.numpy())))
seen_ind, unseen_ind = [], []
for i in range(len(attr_truth)):
if pairs[i] in self.train_pairs:
seen_ind.append(i)
else:
unseen_ind.append(i)
seen_ind, unseen_ind = torch.LongTensor(seen_ind), torch.LongTensor(
unseen_ind
)
def _process(_scores):
# Top k pair accuracy
# Attribute, object and pair
attr_match = (
attr_truth.unsqueeze(1).repeat(1, topk) == _scores[0][:, :topk]
)
obj_match = (
obj_truth.unsqueeze(1).repeat(1, topk) == _scores[1][:, :topk]
)
# Match of object pair
match = (attr_match * obj_match).any(1).float()
attr_match = attr_match.any(1).float()
obj_match = obj_match.any(1).float()
# Match of seen and unseen pairs
seen_match = match[seen_ind]
unseen_match = match[unseen_ind]
# Calculating class average accuracy
seen_score, unseen_score = torch.ones(512, 5), torch.ones(512, 5)
return attr_match, obj_match, match, seen_match, unseen_match, torch.Tensor(
seen_score + unseen_score), torch.Tensor(seen_score), torch.Tensor(unseen_score)
def _add_to_dict(_scores, type_name, stats):
base = [
"_attr_match",
"_obj_match",
"_match",
"_seen_match",
"_unseen_match",
"_ca",
"_seen_ca",
"_unseen_ca",
]
for val, name in zip(_scores, base):
stats[type_name + name] = val
stats = dict()
# Closed world
closed_scores = _process(predictions["closed"])
unbiased_closed = _process(predictions["unbiased_closed"])
_add_to_dict(closed_scores, "closed", stats)
_add_to_dict(unbiased_closed, "closed_ub", stats)
# Calculating AUC
scores = predictions["scores"]
# getting score for each ground truth class
correct_scores = scores[torch.arange(scores.shape[0]), pair_truth][
unseen_ind
]
# Getting top predicted score for these unseen classes
max_seen_scores = predictions['scores'][unseen_ind][:, self.seen_mask].topk(topk, dim=1)[
0][:, topk - 1]
# Getting difference between these scores
unseen_score_diff = max_seen_scores - correct_scores
# Getting matched classes at max bias for diff
unseen_matches = stats["closed_unseen_match"].bool()
correct_unseen_score_diff = unseen_score_diff[unseen_matches] - 1e-4
# sorting these diffs
correct_unseen_score_diff = torch.sort(correct_unseen_score_diff)[0]
magic_binsize = 20
# getting step size for these bias values
bias_skip = max(len(correct_unseen_score_diff) // magic_binsize, 1)
# Getting list
biaslist = correct_unseen_score_diff[::bias_skip]
seen_match_max = float(stats["closed_seen_match"].mean())
unseen_match_max = float(stats["closed_unseen_match"].mean())
seen_accuracy, unseen_accuracy = [], []
# Go to CPU
base_scores = {k: v.to("cpu") for k, v in allpred.items()}
obj_truth = obj_truth.to("cpu")
# Gather scores for all relevant (a,o) pairs
base_scores = torch.stack(
[allpred[(attr, obj)] for attr, obj in self.dset.pairs], 1
) # (Batch, #pairs)
for bias in biaslist:
scores = base_scores.clone()
results = self.score_fast_model(
scores, obj_truth, bias=bias, topk=topk)
results = results['closed'] # we only need biased
results = _process(results)
seen_match = float(results[3].mean())
unseen_match = float(results[4].mean())
seen_accuracy.append(seen_match)
unseen_accuracy.append(unseen_match)
seen_accuracy.append(seen_match_max)
unseen_accuracy.append(unseen_match_max)
seen_accuracy, unseen_accuracy = np.array(seen_accuracy), np.array(
unseen_accuracy
)
area = np.trapz(seen_accuracy, unseen_accuracy)
for key in stats:
stats[key] = float(stats[key].mean())
try:
harmonic_mean = hmean([seen_accuracy, unseen_accuracy], axis=0)
except BaseException:
harmonic_mean = 0
max_hm = np.max(harmonic_mean)
idx = np.argmax(harmonic_mean)
if idx == len(biaslist):
bias_term = 1e3
else:
bias_term = biaslist[idx]
stats["biasterm"] = float(bias_term)
stats["best_unseen"] = np.max(unseen_accuracy)
stats["best_seen"] = np.max(seen_accuracy)
stats["AUC"] = area
stats["hm_unseen"] = unseen_accuracy[idx]
stats["hm_seen"] = seen_accuracy[idx]
stats["best_hm"] = max_hm
return stats
def predict_logits(model, dataset, config):
"""Function to predict the cosine similarities between the
images and the attribute-object representations. The function
also returns the ground truth for attributes, objects, and pair
of attribute-objects.
Args:
model (nn.Module): the model
text_rep (nn.Tensor): the attribute-object representations.
dataset (CompositionDataset): the composition dataset (validation/test)
device (str): the device (either cpu/cuda:0)
config (argparse.ArgumentParser): config/args
Returns:
tuple: the logits, attribute labels, object labels,
pair attribute-object labels
"""
model.eval()
all_attr_gt, all_obj_gt, all_pair_gt = (
[],
[],
[],
)
attr2idx = dataset.attr2idx
obj2idx = dataset.obj2idx
pairs_dataset = dataset.pairs
pairs = torch.tensor([(attr2idx[attr], obj2idx[obj])
for attr, obj in pairs_dataset]).cuda()
dataloader = DataLoader(
dataset,
batch_size=config.eval_batch_size,
shuffle=False)
all_logits = torch.Tensor()
loss = 0
with torch.no_grad():
for idx, data in tqdm(
enumerate(dataloader), total=len(dataloader), desc="Testing",ncols=150
):
batch_img = data[0].cuda()
predict = model(batch_img, pairs)
logits = predict[0]
l = loss_calu(predict, data, config)
loss += l
attr_truth, obj_truth, pair_truth = data[1], data[2], data[3]
logits = logits.cpu()
all_logits = torch.cat([all_logits, logits], dim=0)
all_attr_gt.append(attr_truth)
all_obj_gt.append(obj_truth)
all_pair_gt.append(pair_truth)
all_attr_gt, all_obj_gt, all_pair_gt = (
torch.cat(all_attr_gt).to("cpu"),
torch.cat(all_obj_gt).to("cpu"),
torch.cat(all_pair_gt).to("cpu"),
)
return all_logits, all_attr_gt, all_obj_gt, all_pair_gt, loss / len(dataloader)
def threshold_with_feasibility(
logits,
seen_mask,
threshold=None,
feasiblity=None):
"""Function to remove infeasible compositions.
Args:
logits (torch.Tensor): the cosine similarities between
the images and the attribute-object pairs.
seen_mask (torch.tensor): the seen mask with binary
threshold (float, optional): the threshold value.
Defaults to None.
feasiblity (torch.Tensor, optional): the feasibility.
Defaults to None.
Returns:
torch.Tensor: the logits after filtering out the
infeasible compositions.
"""
score = copy.deepcopy(logits)
# Note: Pairs are already aligned here
mask = (feasiblity >= threshold).float()
# score = score*mask + (1.-mask)*(-1.)
score = score * (mask + seen_mask)
return score
def test(
test_dataset,
evaluator,
all_logits,
all_attr_gt,
all_obj_gt,
all_pair_gt,
config):
"""Function computes accuracy on the validation and
test dataset.
Args:
test_dataset (CompositionDataset): the validation/test
dataset
evaluator (Evaluator): the evaluator object
all_logits (torch.Tensor): the cosine similarities between
the images and the attribute-object pairs.
all_attr_gt (torch.tensor): the attribute ground truth
all_obj_gt (torch.tensor): the object ground truth
all_pair_gt (torch.tensor): the attribute-object pair ground
truth
config (argparse.ArgumentParser): the config
Returns:
dict: the result with all the metrics
"""
predictions = {
pair_name: all_logits[:, i]
for i, pair_name in enumerate(test_dataset.pairs)
}
all_pred = [predictions]
all_pred_dict = {}
for k in all_pred[0].keys():
all_pred_dict[k] = torch.cat(
[all_pred[i][k] for i in range(len(all_pred))]
).float()
results = evaluator.score_model(
all_pred_dict, all_obj_gt, bias=1e3, topk=1
)
attr_acc = float(torch.mean(
(results['unbiased_closed'][0].squeeze(-1) == all_attr_gt).float()))
obj_acc = float(torch.mean(
(results['unbiased_closed'][1].squeeze(-1) == all_obj_gt).float()))
stats = evaluator.evaluate_predictions(
results,
all_attr_gt,
all_obj_gt,
all_pair_gt,
all_pred_dict,
topk=1,
)
stats['attr_acc'] = attr_acc
stats['obj_acc'] = obj_acc
return stats
if __name__ == "__main__":
config = parser.parse_args()
load_args(YML_PATH[config.dataset], config)
config.load_model = 'saved_models/' + config.dataset + '/' + config.model_name + config.exp_id + '/' + config.test_epoch + '.pt'
print("evaluation details")
print("----")
print(f"dataset: {config.dataset}")
dataset_path = config.dataset_path
if config.open_world:
print('loading validation dataset')
val_dataset = CompositionDataset(dataset_path,
phase='val',
open_world=config.open_world)
print('loading test dataset')
test_dataset = CompositionDataset(dataset_path,
phase='test',
open_world=config.open_world)
allattrs = test_dataset.attrs
allobj = test_dataset.objs
classes = [cla.replace(".", " ").lower() for cla in allobj]
attributes = [attr.replace(".", " ").lower() for attr in allattrs]
offset = len(attributes)
model = RAPR(config, attributes=attributes, classes=classes, offset=offset).cuda()
loaded_data = torch.load(config.load_model)
trained_model = loaded_data['model']
model.db = loaded_data['database']
model_dict = model.state_dict()
trained_state = {k:v for k,v in trained_model.items() if k in model_dict.keys()}
model_dict.update(trained_state)
model.load_state_dict(model_dict)
model.train_pairs = test_dataset.train_pairs
model.attr2idx = test_dataset.attr2idx
model.obj2idx = test_dataset.obj2idx
model.idx2attr = {v:k for k,v in model.attr2idx.items()}
model.idx2obj = {v:k for k,v in model.obj2idx.items()}
if config.open_world:
print('evaluating on the validation set')
if config.open_world and config.threshold is None:
evaluator = Evaluator(val_dataset, model=None)
feasibility_path = os.path.join(
DIR_PATH, f'data/feasibility/feasibility_{config.dataset}.pt')
unseen_scores = torch.load(
feasibility_path,
map_location='cpu')['feasibility']
seen_mask = val_dataset.seen_mask.to('cpu')
min_feasibility = (unseen_scores + seen_mask * 10.).min()
max_feasibility = (unseen_scores - seen_mask * 10.).max()
thresholds = np.linspace(
min_feasibility,
max_feasibility,
num=config.threshold_trials)
best_auc = 0.
best_th = -10
val_stats = None
with torch.no_grad():
all_logits, all_attr_gt, all_obj_gt, all_pair_gt, loss_avg = predict_logits(
model, val_dataset, device, config)
for th in thresholds:
temp_logits = threshold_with_feasibility(
all_logits, val_dataset.seen_mask, threshold=th, feasiblity=unseen_scores)
results = test(
val_dataset,
evaluator,
temp_logits,
all_attr_gt,
all_obj_gt,
all_pair_gt,
config
)
auc = results['AUC']
if auc > best_auc:
best_auc = auc
best_th = th
print('New best AUC', best_auc)
print('Threshold', best_th)
val_stats = copy.deepcopy(results)
else:
best_th = config.threshold
evaluator = Evaluator(val_dataset, model=None)
feasibility_path = os.path.join(
DIR_PATH, f'data/feasibility/feasibility_{config.dataset}.pt')
unseen_scores = torch.load(
feasibility_path,
map_location='cpu')['feasibility']
with torch.no_grad():
all_logits, all_attr_gt, all_obj_gt, all_pair_gt, loss_avg = predict_logits(
model, val_dataset, config)
if config.open_world:
print('using threshold: ', best_th)
all_logits = threshold_with_feasibility(
all_logits, val_dataset.seen_mask, threshold=best_th, feasiblity=unseen_scores)
results = test(
val_dataset,
evaluator,
all_logits,
all_attr_gt,
all_obj_gt,
all_pair_gt,
config
)
val_stats = copy.deepcopy(results)
result = ""
for key in val_stats:
result = result + key + " " + str(round(val_stats[key], 4)) + "| "
print(result)
else:
val_stats = None
best_th = None
print('evaluating on the test set')
with torch.no_grad():
evaluator = Evaluator(test_dataset, model=None)
all_logits, all_attr_gt, all_obj_gt, all_pair_gt, loss_avg = predict_logits(
model, test_dataset, config)
if config.open_world and best_th is not None:
print('using threshold: ', best_th)
all_logits = threshold_with_feasibility(
all_logits,
test_dataset.seen_mask,
threshold=best_th,
feasiblity=unseen_scores)
test_stats = test(
test_dataset,
evaluator,
all_logits,
all_attr_gt,
all_obj_gt,
all_pair_gt,
config
)
result = ""
for key in test_stats:
result = result + key + " " + \
str(round(test_stats[key], 4)) + "| "
print(result)
results = {
'val': val_stats,
'test': test_stats,
}
if best_th is not None:
results['best_threshold'] = best_th
if config.open_world:
result_path = config.load_model[:-2] + "test.open.calibrated.json"
else:
result_path = config.load_model[:-2] + "test.closed.json"
with open(result_path, 'w+') as fp:
json.dump(results, fp)
print("done!")