-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval.py
164 lines (132 loc) · 5.45 KB
/
eval.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
import argparse
import json
# import cPickle
import pickle as cPickle
from collections import defaultdict, Counter
from os.path import dirname, join
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import numpy as np
import os
# from new_dataset import Dictionary, VQAFeatureDataset
from dataset import Dictionary, VQAFeatureDataset
import base_model
from train import train
import utils
from vqa_debias_loss_functions import *
from tqdm import tqdm
from torch.autograd import Variable
def parse_args():
parser = argparse.ArgumentParser("Train the BottomUpTopDown model with a de-biasing method")
# Arguments we added
parser.add_argument(
'--cache_features', default=True,
help="Cache image features in RAM. Makes things much faster, "
"especially if the filesystem is slow, but requires at least 48gb of RAM")
parser.add_argument(
'--dataset', default='cpv2', help="Run on VQA-2.0 instead of VQA-CP 2.0")
parser.add_argument(
'-p', "--entropy_penalty", default=0.36, type=float,
help="Entropy regularizer weight for the learned_mixin model")
parser.add_argument(
'--debias', default="learned_mixin",
choices=["learned_mixin", "reweight", "bias_product", "none"],
help="Kind of ensemble loss to use")
# Arguments from the original model, we leave this default, except we
# set --epochs to 15 since the model maxes out its performance on VQA 2.0 well before then
parser.add_argument('--num_hid', type=int, default=1024)
parser.add_argument('--model', type=str, default='baseline0_newatt')
parser.add_argument('--batch_size', type=int, default=512)
parser.add_argument('--seed', type=int, default=1111, help='random seed')
parser.add_argument('--model_state', type=str, default='logs/exp0/model.pth')
args = parser.parse_args()
return args
def compute_score_with_logits(logits, labels):
# logits = torch.max(logits, 1)[1].data # argmax
logits = torch.argmax(logits,1)
one_hots = torch.zeros(*labels.size()).cuda()
one_hots.scatter_(1, logits.view(-1, 1), 1)
scores = (one_hots * labels)
return scores
def evaluate(model,dataloader,qid2type):
score = 0
upper_bound = 0
score_yesno = 0
score_number = 0
score_other = 0
total_yesno = 0
total_number = 0
total_other = 0
model.train(False)
# import pdb;pdb.set_trace()
for v, q, a, b,qids,hintscore in tqdm(dataloader, ncols=100, total=len(dataloader), desc="eval"):
v = Variable(v, requires_grad=False).cuda()
q = Variable(q, requires_grad=False).cuda()
pred, _ ,_= model(v, q, None, None,None)
batch_score= compute_score_with_logits(pred, a.cuda()).cpu().numpy().sum(1)
score += batch_score.sum()
upper_bound += (a.max(1)[0]).sum()
qids = qids.detach().cpu().int().numpy()
for j in range(len(qids)):
qid=qids[j]
typ = qid2type[str(qid)]
if typ == 'yes/no':
score_yesno += batch_score[j]
total_yesno += 1
elif typ == 'other':
score_other += batch_score[j]
total_other += 1
elif typ == 'number':
score_number += batch_score[j]
total_number += 1
else:
print('Hahahahahahahahahahaha')
score = score / len(dataloader.dataset)
upper_bound = upper_bound / len(dataloader.dataset)
score_yesno /= total_yesno
score_other /= total_other
score_number /= total_number
print('\teval overall score: %.2f' % (100 * score))
print('\teval up_bound score: %.2f' % (100 * upper_bound))
print('\teval y/n score: %.2f' % (100 * score_yesno))
print('\teval other score: %.2f' % (100 * score_other))
print('\teval number score: %.2f' % (100 * score_number))
def main():
args = parse_args()
dataset = args.dataset
with open('util/qid2type_%s.json'%args.dataset,'r') as f:
qid2type=json.load(f)
if dataset=='cpv1':
dictionary = Dictionary.load_from_file('data/dictionary_v1.pkl')
elif dataset=='cpv2' or dataset=='v2':
dictionary = Dictionary.load_from_file('data/dictionary.pkl')
print("Building test dataset...")
eval_dset = VQAFeatureDataset('val', dictionary, dataset=dataset,
cache_image_features=args.cache_features)
# Build the model using the original constructor
constructor = 'build_%s' % args.model
model = getattr(base_model, constructor)(eval_dset, args.num_hid).cuda()
if args.debias == "bias_product":
model.debias_loss_fn = BiasProduct()
elif args.debias == "none":
model.debias_loss_fn = Plain()
elif args.debias == "reweight":
model.debias_loss_fn = ReweightByInvBias()
elif args.debias == "learned_mixin":
model.debias_loss_fn = LearnedMixin(args.entropy_penalty)
else:
raise RuntimeError(args.mode)
model_state = torch.load(args.model_state)
model.load_state_dict(model_state)
model = model.cuda()
batch_size = args.batch_size
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.backends.cudnn.benchmark = True
# The original version uses multiple workers, but that just seems slower on my setup
eval_loader = DataLoader(eval_dset, batch_size, shuffle=False, num_workers=0)
print("Starting eval...")
evaluate(model,eval_loader,qid2type)
if __name__ == '__main__':
main()