Datasets:

Modalities:
Image
Text
Formats:
parquet
ArXiv:
Libraries:
Datasets
Dask
License:
wit_base / scripts /wit.py
mariosasko
Add card and scripts
8fe29d7
raw
history blame
7.13 kB
# coding=utf-8
# Copyright 2020 The 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.
""""WIT (Wikipedia-based Image Text Dataset) dataset (Wikimedia version)."""
import base64
import gzip
import json
import datasets
from .corrected_examples import CORRECTED_EXAMPLES
_CITATION = """\
@article{srinivasan2021wit,
title={WIT: Wikipedia-based Image Text Dataset for Multimodal Multilingual Machine Learning},
author={Srinivasan, Krishna and Raman, Karthik and Chen, Jiecao and Bendersky, Michael and Najork, Marc},
journal={arXiv preprint arXiv:2103.01913},
year={2021}
}
"""
_DESCRIPTION = """\
Wikipedia-based Image Text (WIT) Dataset is a large multimodal multilingual dataset.
It contains more than six million images from Wikipedia articles in 100+ languages, which correspond to almost all captioned images in Google's version of the WIT dataset.
Images are provided at a 300-px resolution, a size that is suitable for most of the learning frameworks used to classify and analyze images.
This version of the WIT dataset was released by Wikimedia Research team.
"""
_LICENSE = "CC BY-SA 4.0 international license"
_HOMEPAGE = "https://techblog.wikimedia.org/2021/09/09/the-wikipedia-image-caption-matching-challenge-and-a-huge-release-of-image-data-for-research/"
_BASE_URL = "https://storage.googleapis.com/huggingface-nlp/datasets/wit/"
_URLS = [_BASE_URL + f"part-{'%05d' % i}-48a6f07e-bb86-4735-aac7-883349f41a28-c000.json.gz" for i in range(400)]
class Wit(datasets.GeneratorBasedBuilder):
"""Builder for WIT dataset (Wikimedia version)."""
DEFAULT_WRITER_BATCH_SIZE = 1000
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"image": datasets.Image(),
"image_url": datasets.Value("string"),
"embedding": datasets.Sequence(datasets.Value("float64"), length=2048),
"metadata_url": datasets.Value("string"),
"original_height": datasets.Value("int32"),
"original_width": datasets.Value("int32"),
"mime_type": datasets.Value("string"),
"caption_attribution_description": datasets.Value("string"),
"wit_features": datasets.Sequence(
{
"language": datasets.Value("string"),
"page_url": datasets.Value("string"),
"attribution_passes_lang_id": datasets.Value("bool"),
"caption_alt_text_description": datasets.Value("string"),
"caption_reference_description": datasets.Value("string"),
"caption_title_and_reference_description": datasets.Value("string"),
"context_page_description": datasets.Value("string"),
"context_section_description": datasets.Value("string"),
"hierarchical_section_title": datasets.Value("string"),
"is_main_image": datasets.Value("bool"),
"page_changed_recently": datasets.Value("bool"),
"page_title": datasets.Value("string"),
"section_title": datasets.Value("string"),
}
),
}
),
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
downloaded_files = dl_manager.download(_URLS)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"data_files": downloaded_files}),
]
def _generate_examples(self, data_files):
"""Yields examples."""
wit_feature_names = self.info.features["wit_features"].feature.keys()
idx = 0
for data_file_idx, data_file in enumerate(data_files):
with gzip.open(open(data_file, "rb"), mode="rt", encoding="utf-8") as f:
for row_idx, row in enumerate(f):
example = json.loads(row)
ex_wit_features_non_empty = []
for feature in example["wit_features"]:
# If a feature is missing from feature dict, add it as None
for wit_feature_name in wit_feature_names:
if wit_feature_name not in feature:
feature[wit_feature_name] = None
# Here we take redundant values from wit_features and add them to example to avoid unnecessary duplication
extra_wit_feature_keys = [k for k in feature.keys() if k not in wit_feature_names]
for extra_wit_feature_key in extra_wit_feature_keys:
extra_wit_feature_value = feature.pop(extra_wit_feature_key)
if isinstance(extra_wit_feature_value, list):
extra_wit_feature_value = extra_wit_feature_value[0]
example[extra_wit_feature_key] = extra_wit_feature_value
# Remove empty wit features
if any(v is not None for v in feature.values()):
ex_wit_features_non_empty.append(feature)
example["wit_features"] = ex_wit_features_non_empty
# Check example now for missing keys, adding None to avoid failures
missing_keys = [k for k in self.info.features.keys() if k not in example]
for missing_key in missing_keys:
example[missing_key] = None
# Decode base64 encoded image bytes
b64_image_bytes = example.pop("b64_bytes")
example["image"] = (
{"path": None, "bytes": base64.b64decode(b64_image_bytes)}
if b64_image_bytes is not None
else None
)
corrections = CORRECTED_EXAMPLES.get((data_file_idx, row_idx))
if corrections is not None:
assert example["metadata_url"] == corrections["metadata_url"]
example.update(corrections)
yield idx, example
idx += 1