clevr / clevr.py
aps's picture
Support classification
d2a0cc5
# 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