Add gqa dataset script
Browse files
gqa.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""The GQA dataset."""
|
16 |
+
|
17 |
+
import json
|
18 |
+
import os
|
19 |
+
|
20 |
+
import datasets
|
21 |
+
|
22 |
+
|
23 |
+
_CITATION = """\
|
24 |
+
@inproceedings{hudson2019gqa,
|
25 |
+
title={Gqa: A new dataset for real-world visual reasoning and compositional question answering},
|
26 |
+
author={Hudson, Drew A and Manning, Christopher D},
|
27 |
+
booktitle={Proceedings of the IEEE/CVF conference on computer vision and pattern recognition},
|
28 |
+
pages={6700--6709},
|
29 |
+
year={2019}
|
30 |
+
}
|
31 |
+
"""
|
32 |
+
|
33 |
+
_DESCRIPTION = """\
|
34 |
+
GQA is a new dataset for real-world visual reasoning and compositional question answering,
|
35 |
+
seeking to address key shortcomings of previous visual question answering (VQA) datasets.
|
36 |
+
"""
|
37 |
+
|
38 |
+
_URLS = {
|
39 |
+
"train": "https://nlp.cs.unc.edu/data/lxmert_data/gqa/train.json",
|
40 |
+
"dev": "https://nlp.cs.unc.edu/data/lxmert_data/gqa/valid.json",
|
41 |
+
"img": "https://downloads.cs.stanford.edu/nlp/data/gqa/images.zip",
|
42 |
+
"ans2label": "https://raw.githubusercontent.com/airsplay/lxmert/master/data/gqa/trainval_ans2label.json",
|
43 |
+
}
|
44 |
+
|
45 |
+
_IMG_DIR = "images"
|
46 |
+
|
47 |
+
|
48 |
+
class Gqa(datasets.GeneratorBasedBuilder):
|
49 |
+
"""The GQA dataset."""
|
50 |
+
|
51 |
+
BUILDER_CONFIGS = [
|
52 |
+
datasets.BuilderConfig(name="gqa", version=datasets.Version("1.0.0"), description="GQA dataset."),
|
53 |
+
]
|
54 |
+
|
55 |
+
def _info(self):
|
56 |
+
features = datasets.Features(
|
57 |
+
{
|
58 |
+
"question": datasets.Value("string"),
|
59 |
+
"question_id": datasets.Value("int32"),
|
60 |
+
"image_id": datasets.Value("string"),
|
61 |
+
"label": datasets.Value("int32"),
|
62 |
+
}
|
63 |
+
)
|
64 |
+
return datasets.DatasetInfo(
|
65 |
+
description=_DESCRIPTION,
|
66 |
+
features=features,
|
67 |
+
supervised_keys=None,
|
68 |
+
citation=_CITATION,
|
69 |
+
)
|
70 |
+
|
71 |
+
def _split_generators(self, dl_manager):
|
72 |
+
"""Returns SplitGenerators."""
|
73 |
+
dl_dir = dl_manager.download_and_extract(_URLS)
|
74 |
+
self.ans2label = json.load(open(dl_dir["ans2label"]))
|
75 |
+
|
76 |
+
return [
|
77 |
+
datasets.SplitGenerator(
|
78 |
+
name=datasets.Split.TRAIN,
|
79 |
+
gen_kwargs={"filepath": dl_dir["train"], "img_dir": os.path.join(dl_dir["img"], _IMG_DIR)},
|
80 |
+
),
|
81 |
+
datasets.SplitGenerator(
|
82 |
+
name=datasets.Split.VALIDATION,
|
83 |
+
gen_kwargs={"filepath": dl_dir["dev"], "img_dir": os.path.join(dl_dir["img"], _IMG_DIR)},
|
84 |
+
),
|
85 |
+
]
|
86 |
+
|
87 |
+
def _generate_examples(self, filepath, img_dir):
|
88 |
+
""" Yields examples as (key, example) tuples. """
|
89 |
+
with open(filepath, encoding="utf-8") as f:
|
90 |
+
gqa = json.load(f)
|
91 |
+
for id_, d in enumerate(gqa):
|
92 |
+
img_id = os.path.join(img_dir, d["img_id"] + ".jpg")
|
93 |
+
label = self.ans2label[next(iter(d["label"]))]
|
94 |
+
yield id_, {
|
95 |
+
"question": d["sent"],
|
96 |
+
"question_id": d["question_id"],
|
97 |
+
"image_id": img_id,
|
98 |
+
"label": label,
|
99 |
+
}
|