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path
|
5 |
+
from typing import Dict, List, Tuple
|
6 |
+
|
7 |
+
import datasets
|
8 |
+
import pandas as pd
|
9 |
+
|
10 |
+
from seacrowd.utils import schemas
|
11 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
12 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
13 |
+
|
14 |
+
_CITATION = """\
|
15 |
+
@inproceedings{tran2021vivqa,
|
16 |
+
title={ViVQA: Vietnamese visual question answering},
|
17 |
+
author={Tran, Khanh Quoc and Nguyen, An Trong and Le, An Tran-Hoai and Van Nguyen, Kiet},
|
18 |
+
booktitle={Proceedings of the 35th Pacific Asia Conference on Language, Information and Computation},
|
19 |
+
pages={683--691},
|
20 |
+
year={2021}
|
21 |
+
}
|
22 |
+
"""
|
23 |
+
_DATASETNAME = "vivqa"
|
24 |
+
_DESCRIPTION = """\
|
25 |
+
Vietnamese Visual Question Answering (ViVQA) consist of 10328 images and 15000 question-answer
|
26 |
+
pairs in Vietnamese for evaluating Vietnamese VQA models. This dataset is built based on 10328 randomly
|
27 |
+
selected images from MS COCO dataset. The question-answer pairs were based on the COCO-QA dataset that
|
28 |
+
was automatically translated from English to Vietnamese.
|
29 |
+
"""
|
30 |
+
_HOMEPAGE = "https://github.com/kh4nh12/ViVQA"
|
31 |
+
_LANGUAGES = ["vie"]
|
32 |
+
_LICENSE = Licenses.UNKNOWN.value
|
33 |
+
_LOCAL = False
|
34 |
+
_URLS = {
|
35 |
+
"viviq": {"train": "https://raw.githubusercontent.com/kh4nh12/ViVQA/main/train.csv",
|
36 |
+
"test": "https://raw.githubusercontent.com/kh4nh12/ViVQA/main/test.csv"},
|
37 |
+
"cocodata": {
|
38 |
+
"coco2014_train_val_annots": "http://images.cocodataset.org/annotations/annotations_trainval2014.zip",
|
39 |
+
"coco2014_train_images": "http://images.cocodataset.org/zips/train2014.zip",
|
40 |
+
"coco2014_val_images": "http://images.cocodataset.org/zips/val2014.zip",
|
41 |
+
},
|
42 |
+
}
|
43 |
+
_SUPPORTED_TASKS = [Tasks.VISUAL_QUESTION_ANSWERING]
|
44 |
+
_SOURCE_VERSION = "1.0.0"
|
45 |
+
_SEACROWD_VERSION = "2024.06.20"
|
46 |
+
|
47 |
+
|
48 |
+
class VivQADataset(datasets.GeneratorBasedBuilder):
|
49 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
50 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
51 |
+
|
52 |
+
BUILDER_CONFIGS = [
|
53 |
+
SEACrowdConfig(
|
54 |
+
name=f"{_DATASETNAME}_source",
|
55 |
+
version=SOURCE_VERSION,
|
56 |
+
description=f"{_DATASETNAME} source schema",
|
57 |
+
schema="source",
|
58 |
+
subset_id=f"{_DATASETNAME}",
|
59 |
+
),
|
60 |
+
SEACrowdConfig(
|
61 |
+
name=f"{_DATASETNAME}_seacrowd_imqa",
|
62 |
+
version=SEACROWD_VERSION,
|
63 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
64 |
+
schema="seacrowd_imqa",
|
65 |
+
subset_id=f"{_DATASETNAME}",
|
66 |
+
),
|
67 |
+
]
|
68 |
+
|
69 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
70 |
+
|
71 |
+
def _info(self) -> datasets.DatasetInfo:
|
72 |
+
|
73 |
+
if self.config.schema == "source":
|
74 |
+
features = datasets.Features(
|
75 |
+
{
|
76 |
+
"img_id": datasets.Value("string"),
|
77 |
+
"question": datasets.Value("string"),
|
78 |
+
"answer": datasets.Value("string"),
|
79 |
+
"type": datasets.Value("string"),
|
80 |
+
"coco_url": datasets.Value("string"),
|
81 |
+
"flickr_url": datasets.Value("string"),
|
82 |
+
"img_name": datasets.Value("string"),
|
83 |
+
"coco_license": datasets.Value("int32"),
|
84 |
+
"coco_width": datasets.Value("int32"),
|
85 |
+
"coco_height": datasets.Value("int32"),
|
86 |
+
"coco_date_captured": datasets.Value("string"),
|
87 |
+
"image_path": datasets.Value("string"),
|
88 |
+
}
|
89 |
+
)
|
90 |
+
elif self.config.schema == "seacrowd_imqa":
|
91 |
+
features = schemas.imqa_features
|
92 |
+
features["meta"] = {
|
93 |
+
"coco_img_id": datasets.Value("string"),
|
94 |
+
"type": datasets.Value("string"),
|
95 |
+
"flickr_url": datasets.Value("string"),
|
96 |
+
"coco_url": datasets.Value("string"),
|
97 |
+
"img_name": datasets.Value("string"),
|
98 |
+
"coco_license": datasets.Value("int32"),
|
99 |
+
"coco_width": datasets.Value("int32"),
|
100 |
+
"coco_height": datasets.Value("int32"),
|
101 |
+
"coco_date_captured": datasets.Value("string"),
|
102 |
+
"image_path": datasets.Value("string"),
|
103 |
+
}
|
104 |
+
else:
|
105 |
+
raise ValueError(f"No schema matched for {self.config.schema}")
|
106 |
+
|
107 |
+
return datasets.DatasetInfo(
|
108 |
+
description=_DESCRIPTION,
|
109 |
+
features=features,
|
110 |
+
homepage=_HOMEPAGE,
|
111 |
+
license=_LICENSE,
|
112 |
+
citation=_CITATION,
|
113 |
+
)
|
114 |
+
|
115 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
116 |
+
"""Returns SplitGenerators."""
|
117 |
+
urls = _URLS["viviq"]
|
118 |
+
data_dir = dl_manager.download_and_extract(urls)
|
119 |
+
cocodata = dl_manager.download_and_extract(_URLS["cocodata"])
|
120 |
+
Coco_Dict = self._get_image_detail(cocodata)
|
121 |
+
|
122 |
+
return [
|
123 |
+
datasets.SplitGenerator(
|
124 |
+
name=datasets.Split.TRAIN,
|
125 |
+
gen_kwargs={
|
126 |
+
"filepath": data_dir["train"],
|
127 |
+
"split": "train",
|
128 |
+
"coco_dict": Coco_Dict,
|
129 |
+
},
|
130 |
+
),
|
131 |
+
datasets.SplitGenerator(
|
132 |
+
name=datasets.Split.TEST,
|
133 |
+
gen_kwargs={
|
134 |
+
"filepath": data_dir["test"],
|
135 |
+
"split": "test",
|
136 |
+
"coco_dict": Coco_Dict,
|
137 |
+
},
|
138 |
+
),
|
139 |
+
]
|
140 |
+
|
141 |
+
def _get_image_detail(self, coco_dir) -> Dict:
|
142 |
+
coco2014_train_val_annots = os.path.join(coco_dir["coco2014_train_val_annots"], "annotations")
|
143 |
+
train_ann_2014_path = os.path.join(coco2014_train_val_annots, "captions_train2014.json")
|
144 |
+
val_ann_2014_path = os.path.join(coco2014_train_val_annots, "captions_val2014.json")
|
145 |
+
coco_dict_val = {itm["id"]: itm for itm in json.load(open(val_ann_2014_path, "r"))["images"]}
|
146 |
+
coco_dict_train = {itm["id"]: itm for itm in json.load(open(train_ann_2014_path, "r"))["images"]}
|
147 |
+
coco_train_path = os.path.join(coco_dir["coco2014_train_images"], "train2014")
|
148 |
+
coco_val_path = os.path.join(coco_dir["coco2014_val_images"], "val2014")
|
149 |
+
coco_dict = {"train": coco_dict_train, "val": coco_dict_val, "coco_train_path": coco_train_path, "coco_val_path": coco_val_path}
|
150 |
+
|
151 |
+
return coco_dict
|
152 |
+
|
153 |
+
def _generate_examples(self, filepath: Path, split: str, coco_dict: Dict = None) -> Tuple[int, Dict]:
|
154 |
+
"""Yields examples as (key, example) tuples."""
|
155 |
+
|
156 |
+
raw_examples = pd.read_csv(filepath)
|
157 |
+
coco_train_ref = coco_dict["train"]
|
158 |
+
coco_val_ref = coco_dict["val"]
|
159 |
+
coco_ref = {**coco_train_ref, **coco_val_ref}
|
160 |
+
coco_train_path = coco_dict["coco_train_path"]
|
161 |
+
coco_val_path = coco_dict["coco_val_path"]
|
162 |
+
|
163 |
+
for eid, exam in raw_examples.iterrows():
|
164 |
+
assert len(exam) == 5
|
165 |
+
exam_id, exam_quest, exam_answer, exam_img_id, exam_type = exam
|
166 |
+
coco_info = coco_ref[exam_img_id]
|
167 |
+
flickr_url = coco_info["flickr_url"]
|
168 |
+
img_name = coco_info["file_name"]
|
169 |
+
coco_url = coco_info["coco_url"]
|
170 |
+
coco_license = coco_info["license"]
|
171 |
+
coco_width = coco_info["width"]
|
172 |
+
coco_height = coco_info["height"]
|
173 |
+
coco_date_captured = coco_info["date_captured"]
|
174 |
+
coco_path = coco_train_path if exam_img_id in coco_train_ref else coco_val_path
|
175 |
+
image_path = os.path.join(coco_path, img_name)
|
176 |
+
|
177 |
+
if self.config.schema == "source":
|
178 |
+
yield eid, {
|
179 |
+
"img_id": str(exam_img_id),
|
180 |
+
"question": exam_quest,
|
181 |
+
"answer": exam_answer,
|
182 |
+
"type": exam_type,
|
183 |
+
"coco_url": coco_url,
|
184 |
+
"flickr_url": flickr_url,
|
185 |
+
"img_name": img_name,
|
186 |
+
"coco_license": coco_license,
|
187 |
+
"coco_width": coco_width,
|
188 |
+
"coco_height": coco_height,
|
189 |
+
"coco_date_captured": coco_date_captured,
|
190 |
+
"image_path": image_path,
|
191 |
+
}
|
192 |
+
|
193 |
+
elif self.config.schema == "seacrowd_imqa":
|
194 |
+
example = {
|
195 |
+
"id": str(eid),
|
196 |
+
"question_id": str(exam_id),
|
197 |
+
"document_id": str(eid),
|
198 |
+
"questions": [exam_quest],
|
199 |
+
"type": None,
|
200 |
+
"choices": None,
|
201 |
+
"context": None,
|
202 |
+
"answer": [exam_answer],
|
203 |
+
"image_paths": [image_path],
|
204 |
+
"meta": {
|
205 |
+
"coco_img_id": str(exam_img_id),
|
206 |
+
"type": exam_type,
|
207 |
+
"flickr_url": flickr_url,
|
208 |
+
"coco_url": coco_url,
|
209 |
+
"img_name": img_name,
|
210 |
+
"coco_license": coco_license,
|
211 |
+
"coco_width": coco_width,
|
212 |
+
"coco_height": coco_height,
|
213 |
+
"coco_date_captured": coco_date_captured,
|
214 |
+
"image_path": image_path,
|
215 |
+
},
|
216 |
+
}
|
217 |
+
|
218 |
+
yield eid, example
|