-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.py
113 lines (85 loc) · 3.24 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
"""
This code is copied from SSL-VQA's repository.
https://github.com/CrossmodalGroup/SSL-VQA
"""
import argparse
import json
import progressbar
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import numpy as np
from dataset_vqacp import Dictionary, VQAFeatureDataset
from UpDn_and_DVQA import Model
import utils
import opts
def get_question(q, dataloader):
str = []
dictionary = dataloader.dataset.dictionary
for i in range(q.size(0)):
str.append(dictionary.idx2word[q[i]] if q[i] < len(dictionary.idx2word) else '_')
return ' '.join(str)
def get_answer(p, dataloader):
_m, idx = p.max(0)
return dataloader.dataset.label2ans[idx.item()]
@torch.no_grad()
def get_logits(model, dataloader):
N = len(dataloader.dataset)
M = dataloader.dataset.num_ans_candidates
K = 36
pred = torch.FloatTensor(N, M).zero_()
qIds = torch.IntTensor(N).zero_()
idx = 0
bar = progressbar.ProgressBar(maxval=N or None).start()
for v, b, q, a, i in iter(dataloader):
bar.update(idx)
batch_size = v.size(0)
v = v.cuda()
b = b.cuda()
q = q.cuda()
logits, att = model(q,v,False)
pred[idx:idx+batch_size,:].copy_(logits['logits'].data)
qIds[idx:idx+batch_size].copy_(i)
idx += batch_size
bar.update(idx)
return pred, qIds
def make_json(logits, qIds, dataloader):
utils.assert_eq(logits.size(0), len(qIds))
results = []
for i in range(logits.size(0)):
result = {}
result['question_id'] = qIds[i].item()
result['answer'] = get_answer(logits[i], dataloader)
results.append(result)
return results
if __name__ == '__main__':
opt = opts.parse_opt()
torch.backends.cudnn.benchmark = True
dictionary = Dictionary.load_from_file(opt.dataroot + 'dictionary.pkl')
opt.ntokens = dictionary.ntoken
eval_dset = VQAFeatureDataset('test', dictionary, opt.dataroot, opt.img_root, 1.0, adaptive=False)
n_device = torch.cuda.device_count()
batch_size = opt.batch_size * n_device
model = Model(opt)
model = model.cuda()
eval_loader = DataLoader(eval_dset, batch_size, shuffle=False, num_workers=1, collate_fn=utils.trim_collate)
def process(args, model, eval_loader):
print('loading %s' % opt.checkpoint_path)
model_data = torch.load(opt.checkpoint_path)
model = nn.DataParallel(model).cuda()
model.load_state_dict(model_data.get('model_state', model_data))
opt.s_epoch = model_data['epoch'] + 1
model.train(False)
logits, qIds = get_logits(model, eval_loader)
results = make_json(logits, qIds, eval_loader)
model_label = opt.label
if opt.logits:
utils.create_dir('logits/'+model_label)
torch.save(logits, 'logits/'+model_label+'/logits%d.pth' % opt.s_epoch)
utils.create_dir(opt.output)
if 0 <= opt.s_epoch:
model_label += '_epoch%d' % opt.s_epoch
with open(opt.output+'/test_%s.json' \
% (model_label), 'w') as f:
json.dump(results, f)
process(opt, model, eval_loader)