File size: 11,376 Bytes
4c05985 d3202fb 4c05985 d3202fb 4c05985 d3202fb 4c05985 d3202fb 4c05985 d3202fb 4c05985 |
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# coding=utf-8
# Copyright 2022 The PolyAI and HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
import os
import json
import numpy as np
import datasets
logger = datasets.logging.get_logger(__name__)
datasets.Image()
""" VTQA Dataset"""
_CITATION = """\
@inproceedings{chen2024vtqa,
title={VTQA: Visual Text Question Answering via Entity Alignment and Cross-Media Reasoning},
author={Chen, Kang and Wu, Xiangqian},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={27218--27227},
year={2024}
}
"""
_DESCRIPTION = """\
VTQA is a new dataset containing open-ended questions about image-text pairs.
These questions require multimedia entity alignment, multi-step reasoning and open-ended answer generation.
"""
_HOMEPAGE_URL = "https://visual-text-qa.github.io/"
_LICENSE = "The annotations in this dataset belong to the VTQA Consortium and are licensed under a Creative Commons Attribution NonCommercial NoDerivs 4.0 International License"
_ALL_CONFIGS = sorted(
[
"zh-image",
"zh-region",
"zh-grid",
"en-image",
"en-region",
"en-grid",
"en",
"zh",
"image",
"region",
"grid",
]
)
_BASE_IMAGE_FEATURES = {
"image": datasets.Image(),
"region": datasets.Value("string"),
"grid": datasets.Value("string"),
}
_BASE_TEXT_FEATURES = {
"raw": {
"en": datasets.Value("string"),
"zh": datasets.Value("string"),
},
"cws": {
"en": [datasets.Value("string")],
"zh": [datasets.Value("string")],
},
}
_BASE_ANSWER_FEATURES = {
"answer_type": datasets.Value("string"),
"answer": {
"en": datasets.Value("string"),
"zh": datasets.Value("string"),
},
}
_DATA_URL = "data"
class VTQAConfig(datasets.BuilderConfig):
"""BuilderConfig for VTQA."""
def __init__(
self, data_url: str = None, use_cws=False, local_url=None, get_test_split=False, **kwargs
):
super(VTQAConfig, self).__init__(
version=datasets.Version("1.0.0", ""),
description=self.description,
**kwargs,
)
self.data_url = _DATA_URL if data_url is None else data_url
self.use_cws = use_cws
self.local_url = local_url
self.get_test_split = get_test_split
@property
def features(self):
if self.name == "all":
lang, image_type = "all", "all"
elif "-" in self.name:
lang, image_type = self.name.split("-")
elif self.name in ["en", "zh"]:
lang, image_type = self.name, "all"
elif self.name in ["image", "region", "grid"]:
lang, image_type = "all", self.name
self.lang, self.image_type = lang, image_type
btf = _BASE_TEXT_FEATURES["cws"] if self.use_cws else _BASE_TEXT_FEATURES["raw"]
baf = {
"answer_type": _BASE_ANSWER_FEATURES["answer_type"],
"answer": (
_BASE_ANSWER_FEATURES["answer"]
if lang == "all"
else _BASE_ANSWER_FEATURES["answer"][lang]
),
}
dataset_features = datasets.Features(
{
"question": (btf if lang == "all" else btf[lang]),
"question_id": datasets.Value("int64"),
"context": (btf if lang == "all" else btf[lang]),
"image_id": datasets.Value("int64"),
"image_path": (
_BASE_IMAGE_FEATURES
if image_type == "all"
else _BASE_IMAGE_FEATURES[image_type]
),
"answers": [baf],
}
)
return dataset_features
def _build_config(name, use_cws=False, local_url=None):
return VTQAConfig(
name=name,
data_url=_DATA_URL,
use_cws=use_cws,
local_url=local_url,
)
class VTQA(datasets.GeneratorBasedBuilder):
BUILDER_CONFIG_CLASS = VTQAConfig
DEFAULT_WRITER_BATCH_SIZE = 1000
BUILDER_CONFIGS = [_build_config(name) for name in _ALL_CONFIGS + ["all"]]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=self.config.features,
homepage=_HOMEPAGE_URL,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
lang, image_type = self.config.lang, self.config.image_type
def _get_url(file_name):
if self.config.local_url is None:
return dl_manager.download_and_extract(
os.path.join(self.config.data_url, f"{file_name}.zip")
)
else:
return os.path.join(self.config.local_url, f"{file_name}")
annotation_dir = _get_url("annotations")
image_dir, region_dir, grid_dir = None, None, None
if image_type in ["image", "all"]:
image_dir = _get_url("image")
if image_type in ["region", "all"]:
region_dir = _get_url("region")
if image_type in ["grid", "all"]:
grid_dir = _get_url("grid")
if self.config.use_cws:
cws_supp_dir = _get_url("cws_supp")
self.cws_supp_dir = cws_supp_dir
datasets_split = [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": os.path.join(annotation_dir, "train.json"),
"image_dir": (
os.path.join(image_dir, "train") if image_dir else None
),
"region_dir": (
os.path.join(region_dir, "train") if region_dir else None
),
"grid_dir": os.path.join(grid_dir, "train") if grid_dir else None,
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath": os.path.join(annotation_dir, "val.json"),
"image_dir": (
os.path.join(image_dir, "val") if image_dir else None
),
"region_dir": (
os.path.join(region_dir, "val") if region_dir else None
),
"grid_dir": os.path.join(grid_dir, "val") if grid_dir else None,
},
),
datasets.SplitGenerator(
name=datasets.Split("test_dev"),
gen_kwargs={
"filepath": os.path.join(annotation_dir, "test_dev.json"),
"image_dir": (
os.path.join(image_dir, "test_dev") if image_dir else None
),
"region_dir": (
os.path.join(region_dir, "test_dev") if region_dir else None
),
"grid_dir": (
os.path.join(grid_dir, "test_dev") if grid_dir else None
),
"labeled": False,
},
),
]
if self.config.get_test_split:
return datasets_split + [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": os.path.join(annotation_dir, "test.json"),
"image_dir": (
os.path.join(image_dir, "test") if image_dir else None
),
"region_dir": (
os.path.join(region_dir, "test") if region_dir else None
),
"grid_dir": (
os.path.join(grid_dir, "test") if grid_dir else None
),
"labeled": False,
},
)
]
else:
return datasets_split
def _generate_examples(
self, filepath, image_dir=None, region_dir=None, grid_dir=None, labeled=True
):
lang, image_type = self.config.lang, self.config.image_type
use_cws = "cws" if self.config.use_cws else "raw"
"""Yields examples as (key, example) tuples."""
with open(filepath, encoding="utf-8") as f:
vtqa = json.load(f)
for id_, d in enumerate(vtqa):
text_dict = {
"question": (
d["question"][use_cws]
if lang == "all"
else d["question"][use_cws][lang]
),
"context": (
d["context"][use_cws]
if lang == "all"
else d["context"][use_cws][lang]
),
}
image_dict = {}
if image_dir is not None:
image_dict["image"] = os.path.join(
image_dir, d["image_name"]["image"]
)
if region_dir is not None:
image_dict["region"] = os.path.join(
region_dir, d["image_name"]["region"]
)
if grid_dir is not None:
image_dict["grid"] = os.path.join(grid_dir, d["image_name"]["grid"])
if labeled:
yield id_, {
"question_id": d["question_id"],
"image_id": d["image_id"],
"answers": [
{
"answer_type": a["answer_type"],
"answer": (
a["answer"] if lang == "all" else a["answer"][lang]
),
}
for a in d["answers"]
],
**text_dict,
"image_path": (
image_dict
if image_type == "all"
else image_dict[image_type]
),
}
else:
yield id_, {
"question_id": d["question_id"],
"image_id": d["image_id"],
"answers": None,
**text_dict,
"image_path": (
image_dict
if image_type == "all"
else image_dict[image_type]
),
}
|