File size: 5,359 Bytes
f5998fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2a0cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5998fb
 
 
 
d2a0cc5
 
 
 
 
 
 
 
 
 
 
 
f5998fb
d2a0cc5
f5998fb
d2a0cc5
 
 
 
f5998fb
 
 
 
 
 
 
d2a0cc5
f5998fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2a0cc5
f5998fb
 
 
 
 
 
 
 
 
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
# Copyright 2020 The HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""VQA v2 loading script."""


import json
from pathlib import Path
import datasets


_CITATION = """\
@inproceedings{johnson2017clevr,
  title={Clevr: A diagnostic dataset for compositional language and elementary visual reasoning},
  author={Johnson, Justin and Hariharan, Bharath and Van Der Maaten, Laurens and Fei-Fei, Li and Lawrence Zitnick, C and Girshick, Ross},
  booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition},
  pages={2901--2910},
  year={2017}
}
"""

_DESCRIPTION = """\
CLEVR is a diagnostic dataset that tests a range of visual reasoning abilities. It contains minimal biases and has detailed annotations describing the kind of reasoning each question requires. We use this dataset to analyze a variety of modern visual reasoning systems, providing novel insights into their abilities and limitations.
"""

_HOMEPAGE = "https://cs.stanford.edu/people/jcjohns/clevr/"

_LICENSE = "CC BY 4.0"  # TODO need to credit both ms coco and vqa authors!

_URLS = "https://dl.fbaipublicfiles.com/clevr/CLEVR_v1.0.zip"

CLASSES = [
    "0",
    "gray",
    "cube",
    "purple",
    "yes",
    "small",
    "brown",
    "red",
    "blue",
    "7",
    "5",
    "8",
    "metal",
    "6",
    "rubber",
    "1",
    "sphere",
    "cylinder",
    "3",
    "10",
    "2",
    "yellow",
    "cyan",
    "green",
    "9",
    "large",
    "no",
    "4",
]


class ClevrDataset(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")
    DEFAULT_BUILD_CONFIG_NAME = "default"
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="default",
            version=VERSION,
            description="This config returns answers as plain text",
        ),
        datasets.BuilderConfig(
            name="classification",
            version=VERSION,
            description="This config returns answers as class labels",
        )

    ]
    def _info(self):
        if self.config.name == "classification":
            answer_feature = datasets.ClassLabel(names=CLASSES)
        else:
            answer_feature = datasets.Value("string")
        features = datasets.Features(
            {
                "question_index": datasets.Value("int64"),
                "question_family_index": datasets.Value("int64"),
                "image_filename": datasets.Value("string"),
                "split": datasets.Value("string"),
                "question": datasets.Value("string"),
                "answer": answer_feature,
                "image": datasets.Image(),
                "image_index": datasets.Value("int64"),
                "program": datasets.Sequence({
                    "inputs": datasets.Sequence(datasets.Value("int64")),
                    "function": datasets.Value("string"),
                    "value_inputs": datasets.Sequence(datasets.Value("string")),
                }),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract(_URLS)
        gen_kwargs = {
            split_name: {
                "split": split_name,
                "questions_path": Path(data_dir) / "CLEVR_v1.0" / "questions" / f"CLEVR_{split_name}_questions.json",
                "image_folder": Path(data_dir) / "CLEVR_v1.0" / "images" / f"{split_name}",
            }
            for split_name in ["train", "val", "test"]
        }
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs=gen_kwargs["train"],
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs=gen_kwargs["val"],
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs=gen_kwargs["test"],
            ),
        ]

    def _generate_examples(self, split, questions_path, image_folder):
        questions = json.load(open(questions_path, "r"))

        for idx, question in enumerate(questions["questions"]):
            question["image"] = str(image_folder / f"{question['image_filename']}")
            if split == "test":
                question["question_family_index"] = -1
                question["answer"] = -1 if self.config.name == "classification" else ""
                question["program"] = [
                    {
                        "inputs": [],
                        "function": "scene",
                        "value_inputs": [],
                    }
                ]
            yield idx, question