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ed 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 |
+
|
| 16 |
+
import os
|
| 17 |
+
import json
|
| 18 |
+
import numpy as np
|
| 19 |
+
import datasets
|
| 20 |
+
|
| 21 |
+
logger = datasets.logging.get_logger(__name__)
|
| 22 |
+
|
| 23 |
+
datasets.Image()
|
| 24 |
+
""" VTQA Dataset"""
|
| 25 |
+
|
| 26 |
+
_CITATION = """\
|
| 27 |
+
@inproceedings{Chen2023VTQA,
|
| 28 |
+
title={VTQA: Visual Text Question Answering via Entity Alignment and Cross-Media Reasoning},
|
| 29 |
+
author={Kang Chen and Xiangqian Wu},
|
| 30 |
+
year={2023}
|
| 31 |
+
}
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
_DESCRIPTION = """\
|
| 35 |
+
VTQA is a new dataset containing open-ended questions about image-text pairs.
|
| 36 |
+
These questions require multimedia entity alignment, multi-step reasoning and open-ended answer generation.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
_HOMEPAGE_URL = "https://visual-text-qa.github.io/"
|
| 40 |
+
|
| 41 |
+
_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"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
_ALL_CONFIGS = sorted(
|
| 45 |
+
[
|
| 46 |
+
"zh-image",
|
| 47 |
+
"zh-region",
|
| 48 |
+
"zh-grid",
|
| 49 |
+
"en-image",
|
| 50 |
+
"en-region",
|
| 51 |
+
"en-grid",
|
| 52 |
+
"en",
|
| 53 |
+
"zh",
|
| 54 |
+
"image",
|
| 55 |
+
"region",
|
| 56 |
+
"grid",
|
| 57 |
+
]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
_BASE_IMAGE_FEATURES = {
|
| 61 |
+
"image": datasets.Image(),
|
| 62 |
+
"region": datasets.Value("string"),
|
| 63 |
+
"grid": datasets.Value("string"),
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
_BASE_TEXT_FEATURES = {
|
| 67 |
+
"raw": {
|
| 68 |
+
"en": datasets.Value("string"),
|
| 69 |
+
"zh": datasets.Value("string"),
|
| 70 |
+
},
|
| 71 |
+
"cws": {
|
| 72 |
+
"en": [datasets.Value("string")],
|
| 73 |
+
"zh": [datasets.Value("string")],
|
| 74 |
+
},
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
_BASE_ANSWER_FEATURES = {
|
| 78 |
+
"answer_type": datasets.Value("string"),
|
| 79 |
+
"answer": {
|
| 80 |
+
"en": datasets.Value("string"),
|
| 81 |
+
"zh": datasets.Value("string"),
|
| 82 |
+
},
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
_DATA_URL = "data"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class VTQAConfig(datasets.BuilderConfig):
|
| 90 |
+
"""BuilderConfig for VTQA."""
|
| 91 |
+
|
| 92 |
+
def __init__(self, data_url: str = None, use_cws=False, local_url=None, **kwargs):
|
| 93 |
+
super(VTQAConfig, self).__init__(
|
| 94 |
+
version=datasets.Version("1.0.0", ""),
|
| 95 |
+
description=self.description,
|
| 96 |
+
**kwargs,
|
| 97 |
+
)
|
| 98 |
+
self.data_url = _DATA_URL if data_url is None else data_url
|
| 99 |
+
self.use_cws = use_cws
|
| 100 |
+
self.local_url = local_url
|
| 101 |
+
|
| 102 |
+
@property
|
| 103 |
+
def features(self):
|
| 104 |
+
if self.name == "all":
|
| 105 |
+
lang, image_type = "all", "all"
|
| 106 |
+
elif "-" in self.name:
|
| 107 |
+
lang, image_type = self.name.split("-")
|
| 108 |
+
elif self.name in ["en", "zh"]:
|
| 109 |
+
lang, image_type = self.name, "all"
|
| 110 |
+
elif self.name in ["image", "region", "grid"]:
|
| 111 |
+
lang, image_type = "all", self.name
|
| 112 |
+
self.lang, self.image_type = lang, image_type
|
| 113 |
+
|
| 114 |
+
btf = _BASE_TEXT_FEATURES["cws"] if self.use_cws else _BASE_TEXT_FEATURES["raw"]
|
| 115 |
+
baf = {
|
| 116 |
+
"answer_type": _BASE_ANSWER_FEATURES["answer_type"],
|
| 117 |
+
"answer": (
|
| 118 |
+
_BASE_ANSWER_FEATURES["answer"]
|
| 119 |
+
if lang == "all"
|
| 120 |
+
else _BASE_ANSWER_FEATURES["answer"][lang]
|
| 121 |
+
),
|
| 122 |
+
}
|
| 123 |
+
dataset_features = datasets.Features(
|
| 124 |
+
{
|
| 125 |
+
"question": (btf if lang == "all" else btf[lang]),
|
| 126 |
+
"question_id": datasets.Value("int64"),
|
| 127 |
+
"context": (btf if lang == "all" else btf[lang]),
|
| 128 |
+
"image_id": datasets.Value("int64"),
|
| 129 |
+
"image_path": (
|
| 130 |
+
_BASE_IMAGE_FEATURES
|
| 131 |
+
if image_type == "all"
|
| 132 |
+
else _BASE_IMAGE_FEATURES[image_type]
|
| 133 |
+
),
|
| 134 |
+
"answers": [baf],
|
| 135 |
+
}
|
| 136 |
+
)
|
| 137 |
+
return dataset_features
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def _build_config(name, use_cws=False, local_url=None):
|
| 141 |
+
return VTQAConfig(
|
| 142 |
+
name=name,
|
| 143 |
+
data_url=_DATA_URL,
|
| 144 |
+
use_cws=use_cws,
|
| 145 |
+
local_url=local_url,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class VTQA(datasets.GeneratorBasedBuilder):
|
| 150 |
+
|
| 151 |
+
BUILDER_CONFIG_CLASS = VTQAConfig
|
| 152 |
+
DEFAULT_WRITER_BATCH_SIZE = 1000
|
| 153 |
+
BUILDER_CONFIGS = [_build_config(name) for name in _ALL_CONFIGS + ["all"]]
|
| 154 |
+
|
| 155 |
+
def _info(self):
|
| 156 |
+
|
| 157 |
+
return datasets.DatasetInfo(
|
| 158 |
+
description=_DESCRIPTION,
|
| 159 |
+
features=self.config.features,
|
| 160 |
+
homepage=_HOMEPAGE_URL,
|
| 161 |
+
license=_LICENSE,
|
| 162 |
+
citation=_CITATION,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def _split_generators(self, dl_manager):
|
| 166 |
+
lang, image_type = self.config.lang, self.config.image_type
|
| 167 |
+
|
| 168 |
+
def _get_url(file_name):
|
| 169 |
+
if self.config.local_url is None:
|
| 170 |
+
return dl_manager.download_and_extract(
|
| 171 |
+
os.path.join(self.config.data_url, f"{file_name}.zip")
|
| 172 |
+
)
|
| 173 |
+
else:
|
| 174 |
+
return os.path.join(self.config.local_url, f"{file_name}")
|
| 175 |
+
|
| 176 |
+
annotation_dir = _get_url("annotations")
|
| 177 |
+
image_dir, region_dir, grid_dir = None, None, None
|
| 178 |
+
if image_type in ["image", "all"]:
|
| 179 |
+
image_dir = _get_url("image")
|
| 180 |
+
if image_type in ["region", "all"]:
|
| 181 |
+
region_dir = _get_url("region")
|
| 182 |
+
if image_type in ["grid", "all"]:
|
| 183 |
+
grid_dir = _get_url("grid")
|
| 184 |
+
|
| 185 |
+
if self.config.use_cws:
|
| 186 |
+
cws_supp_dir = _get_url("cws_supp")
|
| 187 |
+
self.cws_supp_dir = cws_supp_dir
|
| 188 |
+
|
| 189 |
+
return [
|
| 190 |
+
datasets.SplitGenerator(
|
| 191 |
+
name=datasets.Split.TRAIN,
|
| 192 |
+
gen_kwargs={
|
| 193 |
+
"filepath": os.path.join(annotation_dir, "train.json"),
|
| 194 |
+
"image_dir": (
|
| 195 |
+
os.path.join(image_dir, "train") if image_dir else None
|
| 196 |
+
),
|
| 197 |
+
"region_dir": (
|
| 198 |
+
os.path.join(region_dir, "train") if region_dir else None
|
| 199 |
+
),
|
| 200 |
+
"grid_dir": os.path.join(grid_dir, "train") if grid_dir else None,
|
| 201 |
+
},
|
| 202 |
+
),
|
| 203 |
+
datasets.SplitGenerator(
|
| 204 |
+
name=datasets.Split.VALIDATION,
|
| 205 |
+
gen_kwargs={
|
| 206 |
+
"filepath": os.path.join(annotation_dir, "val.json"),
|
| 207 |
+
"image_dir": (
|
| 208 |
+
os.path.join(image_dir, "val") if image_dir else None
|
| 209 |
+
),
|
| 210 |
+
"region_dir": (
|
| 211 |
+
os.path.join(region_dir, "val") if region_dir else None
|
| 212 |
+
),
|
| 213 |
+
"grid_dir": os.path.join(grid_dir, "val") if grid_dir else None,
|
| 214 |
+
},
|
| 215 |
+
),
|
| 216 |
+
datasets.SplitGenerator(
|
| 217 |
+
name=datasets.Split("test_dev"),
|
| 218 |
+
gen_kwargs={
|
| 219 |
+
"filepath": os.path.join(annotation_dir, "test_dev.json"),
|
| 220 |
+
"image_dir": (
|
| 221 |
+
os.path.join(image_dir, "test_dev") if image_dir else None
|
| 222 |
+
),
|
| 223 |
+
"region_dir": (
|
| 224 |
+
os.path.join(region_dir, "test_dev") if region_dir else None
|
| 225 |
+
),
|
| 226 |
+
"grid_dir": (
|
| 227 |
+
os.path.join(grid_dir, "test_dev") if grid_dir else None
|
| 228 |
+
),
|
| 229 |
+
"labeled": False,
|
| 230 |
+
},
|
| 231 |
+
),
|
| 232 |
+
datasets.SplitGenerator(
|
| 233 |
+
name=datasets.Split.TEST,
|
| 234 |
+
gen_kwargs={
|
| 235 |
+
"filepath": os.path.join(annotation_dir, "test.json"),
|
| 236 |
+
"image_dir": (
|
| 237 |
+
os.path.join(image_dir, "test") if image_dir else None
|
| 238 |
+
),
|
| 239 |
+
"region_dir": (
|
| 240 |
+
os.path.join(region_dir, "test") if region_dir else None
|
| 241 |
+
),
|
| 242 |
+
"grid_dir": os.path.join(grid_dir, "test") if grid_dir else None,
|
| 243 |
+
"labeled": False,
|
| 244 |
+
},
|
| 245 |
+
),
|
| 246 |
+
]
|
| 247 |
+
|
| 248 |
+
def _generate_examples(
|
| 249 |
+
self, filepath, image_dir=None, region_dir=None, grid_dir=None, labeled=True
|
| 250 |
+
):
|
| 251 |
+
lang, image_type = self.config.lang, self.config.image_type
|
| 252 |
+
use_cws = "cws" if self.config.use_cws else "raw"
|
| 253 |
+
"""Yields examples as (key, example) tuples."""
|
| 254 |
+
with open(filepath, encoding="utf-8") as f:
|
| 255 |
+
vtqa = json.load(f)
|
| 256 |
+
for id_, d in enumerate(vtqa):
|
| 257 |
+
text_dict = {
|
| 258 |
+
"question": (
|
| 259 |
+
d["question"][use_cws]
|
| 260 |
+
if lang == "all"
|
| 261 |
+
else d["question"][use_cws][lang]
|
| 262 |
+
),
|
| 263 |
+
"context": (
|
| 264 |
+
d["context"][use_cws]
|
| 265 |
+
if lang == "all"
|
| 266 |
+
else d["context"][use_cws][lang]
|
| 267 |
+
),
|
| 268 |
+
}
|
| 269 |
+
image_dict = {}
|
| 270 |
+
if image_dir is not None:
|
| 271 |
+
image_dict["image"] = os.path.join(
|
| 272 |
+
image_dir, d["image_name"]["image"]
|
| 273 |
+
)
|
| 274 |
+
if region_dir is not None:
|
| 275 |
+
image_dict["region"] = os.path.join(
|
| 276 |
+
region_dir, d["image_name"]["region"]
|
| 277 |
+
)
|
| 278 |
+
if grid_dir is not None:
|
| 279 |
+
image_dict["grid"] = os.path.join(grid_dir, d["image_name"]["grid"])
|
| 280 |
+
|
| 281 |
+
if labeled:
|
| 282 |
+
|
| 283 |
+
yield id_, {
|
| 284 |
+
"question_id": d["question_id"],
|
| 285 |
+
"image_id": d["image_id"],
|
| 286 |
+
"answers": [
|
| 287 |
+
{
|
| 288 |
+
"answer_type": a["answer_type"],
|
| 289 |
+
"answer": (
|
| 290 |
+
a["answer"] if lang == "all" else a["answer"][lang]
|
| 291 |
+
),
|
| 292 |
+
}
|
| 293 |
+
for a in d["answers"]
|
| 294 |
+
],
|
| 295 |
+
**text_dict,
|
| 296 |
+
"image_path": (
|
| 297 |
+
image_dict
|
| 298 |
+
if image_type == "all"
|
| 299 |
+
else image_dict[image_type]
|
| 300 |
+
),
|
| 301 |
+
}
|
| 302 |
+
else:
|
| 303 |
+
yield id_, {
|
| 304 |
+
"question_id": d["question_id"],
|
| 305 |
+
"image_id": d["image_id"],
|
| 306 |
+
"answers": None,
|
| 307 |
+
**text_dict,
|
| 308 |
+
"image_path": (
|
| 309 |
+
image_dict
|
| 310 |
+
if image_type == "all"
|
| 311 |
+
else image_dict[image_type]
|
| 312 |
+
),
|
| 313 |
+
}
|