-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathensemble.py
76 lines (59 loc) · 2.45 KB
/
ensemble.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
import argparse
import glob
import json
import os
import random
import torch
from torch.nn import functional as F
from dense_coattn.data import DataLoader, VQADataset
def main(opt):
"""
Generating answers for (image, question) pair in the dataset.
"""
random.seed(opt.seed)
print("Constructing the dataset...")
testset = VQADataset(opt.data_path, opt.data_name, "test", opt.img_path, opt.img_type, "test")
testLoader = DataLoader(testset, batch_size=opt.batch_size, shuffle=False, drop_last=False,
num_workers=opt.num_workers, pin_memory=True)
idx2ans = testset.idx2ans
num_batches = len(testLoader)
file_name = glob.glob(os.path.join(opt.result_path, "DCN*.pt"))
score = 0
answers = []
chosen_file = random.sample(file_name, opt.num_model)
for name in chosen_file:
score += F.sigmoid(torch.load(name))
print("Number of results:", len(chosen_file))
_, inds = torch.sort(score, dim=1, descending=True)
for i, batch in enumerate(testLoader):
ques_idx = batch[-1]
for j in range(ques_idx.size(0)):
answers.append({"question_id": ques_idx[j], "answer": idx2ans[inds[i*ques_idx.size(0) + j, 0]]})
if i % 10 == 0:
print("processing %i / %i" % (i, num_batches))
with open("%s.json" % (os.path.join(opt.result_path, opt.save_file)), "w") as file:
json.dump(answers, file)
with open("{}.txt".format(chosen_file), "w") as file:
json.dump(chosen_file, file)
print("Done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--num_workers", type=int, default=8)
parser.add_argument("--batch", type=int, default=1000)
parser.add_argument("--seq_per_img", type=int, default=1)
parser.add_argument("--img_name", type=str, default="cocoimages")
parser.add_argument("--data_path", type=str, default="/home/duykien/storage/vqa/dataset")
parser.add_argument("--data_name", type=str, default="cocotrainval")
parser.add_argument("--result_path", type=str, default="/home/duykien/storage/vqa/result")
parser.add_argument("--num_model", type=int, default=9)
parser.add_argument("--save_file", type=str, default="")
parser.add_argument("--use_h5py", action="store_true")
parser.add_argument("--use_thread", action="store_true")
parser.add_argument("--use_rcnn", action="store_true")
parser.add_argument("--size_scale", default=(448, 448))
parser.add_argument("--seed", type=int, default=1234)
args = parser.parse_args()
params = vars(args)
print("Parsed input parameters:")
print(json.dumps(params, indent=2))
main(args)