File size: 5,982 Bytes
d37d90c |
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.
"""OK-VQA loading script."""
import csv
import json
import os
from pathlib import Path
import datasets
_CITATION = """\
@article{DBLP:journals/corr/abs-1906-00067,
author = {Kenneth Marino and
Mohammad Rastegari and
Ali Farhadi and
Roozbeh Mottaghi},
title = {{OK-VQA:} {A} Visual Question Answering Benchmark Requiring External
Knowledge},
journal = {CoRR},
volume = {abs/1906.00067},
year = {2019},
url = {http://arxiv.org/abs/1906.00067},
eprinttype = {arXiv},
eprint = {1906.00067},
timestamp = {Thu, 13 Jun 2019 13:36:00 +0200},
biburl = {https://dblp.org/rec/journals/corr/abs-1906-00067.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
"""
_DESCRIPTION = """\
OK-VQA is a new dataset for visual question answering that requires methods which can draw upon outside knowledge to answer questions.
- 14,055 open-ended questions
- 5 ground truth answers per question
- Manually filtered to ensure all questions require outside knowledge (e.g. from Wikipeida)
- Reduced questions with most common answers to reduce dataset bias
"""
_HOMEPAGE = "https://okvqa.allenai.org/"
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = "CC BY 4.0" # found in the zip files bellow - we show maybe ask for confirmation
_URLS = {
"annotations": {
"train": "https://okvqa.allenai.org/static/data/mscoco_train2014_annotations.json.zip",
"val": "https://okvqa.allenai.org/static/data/mscoco_val2014_annotations.json.zip",
},
"questions": {
"train": "https://okvqa.allenai.org/static/data/OpenEnded_mscoco_train2014_questions.json.zip",
"val": "https://okvqa.allenai.org/static/data/OpenEnded_mscoco_val2014_questions.json.zip",
},
"images": {
"train": "http://images.cocodataset.org/zips/train2014.zip",
"val": "http://images.cocodataset.org/zips/val2014.zip",
},
}
class OKVQADataset(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
def _info(self):
features = datasets.Features(
{
"image": datasets.Image(),
"question_type": datasets.Value('string'),
"confidence": datasets.Value('int32'),
"answers": [{
"answer": datasets.Value('string'),
"raw_answer": datasets.Value('string'),
"answer_confidence": datasets.Value('string'),
"answer_id": datasets.Value('int64'),
}],
"image_id": datasets.Value('int64'),
"answer_type": datasets.Value('string'),
"question_id": datasets.Value('int64'),
"question": datasets.Value('string'),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# urls = _URLS[self.config.name] # TODO later
data_dir = dl_manager.download_and_extract(_URLS)
gen_kwargs = {}
for split_name in ["train", "val"]:
gen_kwargs_per_split = {}
for dir_name in _URLS.keys():
if split_name in data_dir[dir_name]:
file_name = Path(_URLS[dir_name][split_name]).name[: -len(".zip")]
path = Path(data_dir[dir_name][split_name]) / file_name
gen_kwargs_per_split[f"{dir_name}_path"] = path
else:
gen_kwargs_per_split[f"{dir_name}_path"] = None
gen_kwargs[split_name] = gen_kwargs_per_split
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs=gen_kwargs["train"],
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs=gen_kwargs["val"],
),
]
def _generate_examples(self, questions_path, annotations_path, images_path):
dataset = json.load(open(annotations_path, "r"))
questions = json.load(open(questions_path, "r"))
qa = {ann["question_id"]: [] for ann in dataset["annotations"]}
for ann in dataset["annotations"]:
qa[ann["question_id"]] = ann
for question in questions["questions"]:
annotation = qa[question["question_id"]]
# some checks
assert len(set(question.keys()) ^ {"image_id", "question", "question_id"}) == 0
assert (
len(
set(annotation.keys())
^ {
"question_type",
"confidence",
"answers",
"image_id",
"answer_type",
"question_id",
}
)
== 0
)
# build record
record = question
record.update(annotation)
record["image"] = str(images_path / f"COCO_{images_path.name}_{record['image_id']:0>12}.jpg")
yield question["question_id"], record
|