Jinchen Ge
commited on
Commit
·
810aa11
1
Parent(s):
675179f
Add test set
Browse files- gqa-lxmert.py +29 -9
gqa-lxmert.py
CHANGED
@@ -44,12 +44,15 @@ seeking to address key shortcomings of previous visual question answering (VQA)
|
|
44 |
|
45 |
_URLS = {
|
46 |
"train": "https://nlp.cs.unc.edu/data/lxmert_data/gqa/train.json",
|
47 |
-
"
|
48 |
-
"
|
|
|
|
|
49 |
"ans2label": "https://raw.githubusercontent.com/airsplay/lxmert/master/data/gqa/trainval_ans2label.json",
|
50 |
}
|
51 |
|
52 |
-
|
|
|
53 |
|
54 |
FIELDNAMES = [
|
55 |
"img_id", "img_h", "img_w", "objects_id", "objects_conf", "attrs_id", "attrs_conf", "num_boxes", "boxes", "features"
|
@@ -89,16 +92,21 @@ class GqaLxmert(datasets.GeneratorBasedBuilder):
|
|
89 |
"""Returns SplitGenerators."""
|
90 |
dl_dir = dl_manager.download_and_extract(_URLS)
|
91 |
self.ans2label = json.load(open(dl_dir["ans2label"]))
|
92 |
-
self.
|
|
|
93 |
|
94 |
return [
|
95 |
datasets.SplitGenerator(
|
96 |
name=datasets.Split.TRAIN,
|
97 |
-
gen_kwargs={"filepath": dl_dir["train"]},
|
98 |
),
|
99 |
datasets.SplitGenerator(
|
100 |
name=datasets.Split.VALIDATION,
|
101 |
-
gen_kwargs={"filepath": dl_dir["
|
|
|
|
|
|
|
|
|
102 |
),
|
103 |
]
|
104 |
|
@@ -127,13 +135,25 @@ class GqaLxmert(datasets.GeneratorBasedBuilder):
|
|
127 |
normalized_boxes[:, (1, 3)] /= img_h
|
128 |
return normalized_boxes
|
129 |
|
130 |
-
def _generate_examples(self, filepath):
|
131 |
""" Yields examples as (key, example) tuples."""
|
132 |
with open(filepath, encoding="utf-8") as f:
|
133 |
gqa = json.load(f)
|
134 |
for id_, d in enumerate(gqa):
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
yield id_, {
|
138 |
"question": d["sent"],
|
139 |
"question_id": d["question_id"],
|
|
|
44 |
|
45 |
_URLS = {
|
46 |
"train": "https://nlp.cs.unc.edu/data/lxmert_data/gqa/train.json",
|
47 |
+
"val": "https://nlp.cs.unc.edu/data/lxmert_data/gqa/valid.json",
|
48 |
+
"trainval_feat": "https://nlp.cs.unc.edu/data/lxmert_data/vg_gqa_imgfeat/vg_gqa_obj36.zip",
|
49 |
+
"test": "https://nlp.cs.unc.edu/data/lxmert_data/gqa/testdev.json",
|
50 |
+
"test_feat": "https://nlp.cs.unc.edu/data/lxmert_data/vg_gqa_imgfeat/gqa_testdev_obj36.zip",
|
51 |
"ans2label": "https://raw.githubusercontent.com/airsplay/lxmert/master/data/gqa/trainval_ans2label.json",
|
52 |
}
|
53 |
|
54 |
+
TRAINVAL_FEAT_PATH = "vg_gqa_imgfeat/vg_gqa_obj36.tsv"
|
55 |
+
TEST_FEAT_PATH = "vg_gqa_imgfeat/gqa_testdev_obj36.tsv"
|
56 |
|
57 |
FIELDNAMES = [
|
58 |
"img_id", "img_h", "img_w", "objects_id", "objects_conf", "attrs_id", "attrs_conf", "num_boxes", "boxes", "features"
|
|
|
92 |
"""Returns SplitGenerators."""
|
93 |
dl_dir = dl_manager.download_and_extract(_URLS)
|
94 |
self.ans2label = json.load(open(dl_dir["ans2label"]))
|
95 |
+
self.trainval_id2features = self._load_features(os.path.join(dl_dir["trainval_feat"], TRAINVAL_FEAT_PATH))
|
96 |
+
self.test_id2features = self._load_features(os.path.join(dl_dir["test_feat"], TEST_FEAT_PATH))
|
97 |
|
98 |
return [
|
99 |
datasets.SplitGenerator(
|
100 |
name=datasets.Split.TRAIN,
|
101 |
+
gen_kwargs={"filepath": dl_dir["train"], "testset": False},
|
102 |
),
|
103 |
datasets.SplitGenerator(
|
104 |
name=datasets.Split.VALIDATION,
|
105 |
+
gen_kwargs={"filepath": dl_dir["val"], "testset": False},
|
106 |
+
),
|
107 |
+
datasets.SplitGenerator(
|
108 |
+
name=datasets.Split.TEST,
|
109 |
+
gen_kwargs={"filepath": dl_dir["test"], "testset": True},
|
110 |
),
|
111 |
]
|
112 |
|
|
|
135 |
normalized_boxes[:, (1, 3)] /= img_h
|
136 |
return normalized_boxes
|
137 |
|
138 |
+
def _generate_examples(self, filepath, testset=False):
|
139 |
""" Yields examples as (key, example) tuples."""
|
140 |
with open(filepath, encoding="utf-8") as f:
|
141 |
gqa = json.load(f)
|
142 |
for id_, d in enumerate(gqa):
|
143 |
+
# test_id2features only contains features of a subset of samples in testdev.json
|
144 |
+
if testset:
|
145 |
+
try:
|
146 |
+
img_features = self.test_id2features[d["img_id"]]
|
147 |
+
except KeyError:
|
148 |
+
continue
|
149 |
+
label_key = next(iter(d["label"]))
|
150 |
+
if label_key not in self.ans2label:
|
151 |
+
print ("Ignored one sample because of unseen label.")
|
152 |
+
continue
|
153 |
+
label = self.ans2label[label_key]
|
154 |
+
else:
|
155 |
+
img_features = self.trainval_id2features[d["img_id"]]
|
156 |
+
label = self.ans2label[next(iter(d["label"]))]
|
157 |
yield id_, {
|
158 |
"question": d["sent"],
|
159 |
"question_id": d["question_id"],
|