holylovenia
commited on
Commit
•
42c88c2
1
Parent(s):
3070b76
Upload xm3600.py with huggingface_hub
Browse files
xm3600.py
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
import jsonlines as jl
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
from seacrowd.utils import schemas
|
9 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
10 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
11 |
+
|
12 |
+
_CITATION = """\
|
13 |
+
@inproceedings{thapliyal-etal-2022-crossmodal,
|
14 |
+
title = "Crossmodal-3600: A Massively Multilingual Multimodal Evaluation Dataset",
|
15 |
+
author = "Thapliyal, Ashish V. and
|
16 |
+
Pont Tuset, Jordi and
|
17 |
+
Chen, Xi and
|
18 |
+
Soricut, Radu",
|
19 |
+
editor = "Goldberg, Yoav and
|
20 |
+
Kozareva, Zornitsa and
|
21 |
+
Zhang, Yue",
|
22 |
+
booktitle = "Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing",
|
23 |
+
month = dec,
|
24 |
+
year = "2022",
|
25 |
+
address = "Abu Dhabi, United Arab Emirates",
|
26 |
+
publisher = "Association for Computational Linguistics",
|
27 |
+
url = "https://aclanthology.org/2022.emnlp-main.45",
|
28 |
+
doi = "10.18653/v1/2022.emnlp-main.45",
|
29 |
+
pages = "715--729",
|
30 |
+
}
|
31 |
+
"""
|
32 |
+
|
33 |
+
_DATASETNAME = "xm3600"
|
34 |
+
|
35 |
+
_DESCRIPTION = """\
|
36 |
+
Crossmodal-3600 dataset (XM3600 in short), a geographically-diverse set of 3600 images annotated with
|
37 |
+
human-generated reference captions in 36 languages. The images were selected from across the world,
|
38 |
+
covering regions where the languages are spoken, and annotated with captions that achieve consistency in
|
39 |
+
terms of style across all languages, while avoiding annotation artifacts due to direct translation.
|
40 |
+
The languages covered in the dataset include Filipino, Indonesian, Thai, and Vietnamnese
|
41 |
+
"""
|
42 |
+
|
43 |
+
_HOMEPAGE = "https://google.github.io/crossmodal-3600/"
|
44 |
+
|
45 |
+
_LICENSE = Licenses.CC_BY_4_0.value
|
46 |
+
|
47 |
+
_URLS = {
|
48 |
+
"captions": "https://google.github.io/crossmodal-3600/web-data/captions.zip",
|
49 |
+
"images": "https://open-images-dataset.s3.amazonaws.com/crossmodal-3600/images.tgz",
|
50 |
+
"image_attributions": "https://google.github.io/crossmodal-3600/web-data/image_attributions.csv",
|
51 |
+
}
|
52 |
+
|
53 |
+
_SUPPORTED_TASKS = [Tasks.IMAGE_CAPTIONING]
|
54 |
+
|
55 |
+
_SOURCE_VERSION = "1.0.0"
|
56 |
+
|
57 |
+
_SEACROWD_VERSION = "2024.06.20"
|
58 |
+
|
59 |
+
_LANGUAGES = ["fil", "id", "th", "vi"]
|
60 |
+
|
61 |
+
_LOCAL = False
|
62 |
+
|
63 |
+
|
64 |
+
class XM3600Dataset(datasets.GeneratorBasedBuilder):
|
65 |
+
"""
|
66 |
+
Crossmodal-3600 dataset (XM3600 in short), a geographically-diverse set of 3600 images annotated with
|
67 |
+
human-generated reference captions in 36 languages. The images were selected from across the world,
|
68 |
+
covering regions where the languages are spoken, and annotated with captions that achieve consistency in
|
69 |
+
terms of style across all languages, while avoiding annotation artifacts due to direct translation.
|
70 |
+
The languages covered in the dataset include Filipino, Indonesian, Thai, and Vietnamnese
|
71 |
+
"""
|
72 |
+
|
73 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
74 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
75 |
+
|
76 |
+
BUILDER_CONFIGS = [
|
77 |
+
SEACrowdConfig(
|
78 |
+
name=f"{_DATASETNAME}_{lang}_source",
|
79 |
+
version=datasets.Version(_SOURCE_VERSION),
|
80 |
+
description=f"{_DATASETNAME}_{lang} source schema",
|
81 |
+
schema="source",
|
82 |
+
subset_id=f"{_DATASETNAME}_{lang}",
|
83 |
+
)
|
84 |
+
for lang in _LANGUAGES
|
85 |
+
] + [
|
86 |
+
SEACrowdConfig(
|
87 |
+
name=f"{_DATASETNAME}_{lang}_seacrowd_imtext",
|
88 |
+
version=datasets.Version(_SEACROWD_VERSION),
|
89 |
+
description=f"{_DATASETNAME}_{lang} SEACrowd schema",
|
90 |
+
schema="seacrowd_imtext",
|
91 |
+
subset_id=f"{_DATASETNAME}_{lang}",
|
92 |
+
)
|
93 |
+
for lang in _LANGUAGES
|
94 |
+
]
|
95 |
+
|
96 |
+
DEFAULT_CONFIG_NAME = f"xm3600_{sorted(_LANGUAGES)[0]}_source"
|
97 |
+
|
98 |
+
def _info(self) -> datasets.DatasetInfo:
|
99 |
+
if self.config.schema == "source":
|
100 |
+
features = datasets.Features(
|
101 |
+
{
|
102 |
+
"id": datasets.Value("string"),
|
103 |
+
"image_paths": datasets.Value("string"),
|
104 |
+
"texts": {
|
105 |
+
"caption": datasets.Value("string"),
|
106 |
+
"caption/tokenized": datasets.Value("string"),
|
107 |
+
"caption/tokenized/lowercase": datasets.Value("string"),
|
108 |
+
},
|
109 |
+
}
|
110 |
+
)
|
111 |
+
elif self.config.schema == "seacrowd_imtext":
|
112 |
+
features = schemas.image_text_features()
|
113 |
+
|
114 |
+
return datasets.DatasetInfo(
|
115 |
+
description=_DESCRIPTION,
|
116 |
+
features=features,
|
117 |
+
homepage=_HOMEPAGE,
|
118 |
+
license=_LICENSE,
|
119 |
+
citation=_CITATION,
|
120 |
+
)
|
121 |
+
|
122 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
123 |
+
"""Returns SplitGenerators."""
|
124 |
+
captions_path = dl_manager.download_and_extract(_URLS["captions"])
|
125 |
+
images_path = dl_manager.download_and_extract(_URLS["images"])
|
126 |
+
attr_path = dl_manager.download(_URLS["image_attributions"])
|
127 |
+
|
128 |
+
train_caps = {}
|
129 |
+
test_caps = {}
|
130 |
+
val_caps = {}
|
131 |
+
|
132 |
+
current_lang = self.config.subset_id.split("_")[1]
|
133 |
+
|
134 |
+
img_df = pd.read_csv(attr_path)
|
135 |
+
|
136 |
+
img_df_train = img_df.loc[img_df["Subset"] == "train"][["ImageID", "Subset"]]
|
137 |
+
img_df_test = img_df.loc[img_df["Subset"] == "test"][["ImageID", "Subset"]]
|
138 |
+
img_df_val = img_df.loc[img_df["Subset"] == "validation"][["ImageID", "Subset"]]
|
139 |
+
|
140 |
+
with jl.open(os.path.join(captions_path, "captions.jsonl"), mode="r") as jsonl_file:
|
141 |
+
for line in jsonl_file:
|
142 |
+
if line["image/key"] in img_df_train.ImageID.values:
|
143 |
+
train_caps[line["image/key"]] = line[current_lang]
|
144 |
+
elif line["image/key"] in img_df_test.ImageID.values:
|
145 |
+
test_caps[line["image/key"]] = line[current_lang]
|
146 |
+
elif line["image/key"] in img_df_val.ImageID.values:
|
147 |
+
val_caps[line["image/key"]] = line[current_lang]
|
148 |
+
|
149 |
+
return [
|
150 |
+
datasets.SplitGenerator(
|
151 |
+
name=datasets.Split.TRAIN,
|
152 |
+
gen_kwargs={
|
153 |
+
"filepath": {"img_ids": img_df_train.ImageID.values, "images": {img_id: os.path.join(images_path, img_id + ".jpg") for img_id in img_df_train.ImageID.values}, "captions": train_caps},
|
154 |
+
},
|
155 |
+
),
|
156 |
+
datasets.SplitGenerator(
|
157 |
+
name=datasets.Split.TEST,
|
158 |
+
gen_kwargs={
|
159 |
+
"filepath": {"img_ids": img_df_test.ImageID.values, "images": {img_id: os.path.join(images_path, img_id + ".jpg") for img_id in img_df_test.ImageID.values}, "captions": test_caps},
|
160 |
+
},
|
161 |
+
),
|
162 |
+
datasets.SplitGenerator(
|
163 |
+
name=datasets.Split.VALIDATION,
|
164 |
+
gen_kwargs={
|
165 |
+
"filepath": {"img_ids": img_df_val.ImageID.values, "images": {img_id: os.path.join(images_path, img_id + ".jpg") for img_id in img_df_val.ImageID.values}, "captions": val_caps},
|
166 |
+
},
|
167 |
+
),
|
168 |
+
]
|
169 |
+
|
170 |
+
def _generate_examples(self, filepath: dict) -> Tuple[int, Dict]:
|
171 |
+
"""Yields examples as (key, example) tuples."""
|
172 |
+
counter = 0
|
173 |
+
for img_id in filepath["img_ids"]:
|
174 |
+
cap = filepath["captions"][img_id]
|
175 |
+
for line in cap["caption"]:
|
176 |
+
cap_index = cap["caption"].index(line)
|
177 |
+
if self.config.schema == "source":
|
178 |
+
yield counter, {
|
179 |
+
"id": img_id + "_" + str(counter),
|
180 |
+
"image_paths": filepath["images"][img_id],
|
181 |
+
"texts": {
|
182 |
+
"caption": line,
|
183 |
+
"caption/tokenized": cap["caption/tokenized"][cap_index],
|
184 |
+
"caption/tokenized/lowercase": cap["caption/tokenized/lowercase"][cap_index],
|
185 |
+
},
|
186 |
+
}
|
187 |
+
|
188 |
+
elif self.config.schema == "seacrowd_imtext":
|
189 |
+
yield counter, {
|
190 |
+
"id": img_id + "_" + str(counter),
|
191 |
+
"image_paths": [filepath["images"][img_id]],
|
192 |
+
"texts": line,
|
193 |
+
"metadata": {
|
194 |
+
"context": None,
|
195 |
+
"labels": None,
|
196 |
+
},
|
197 |
+
}
|
198 |
+
|
199 |
+
else:
|
200 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|
201 |
+
|
202 |
+
counter += 1
|