Datasets:

Languages:
English
ArXiv:
License:
docci / docci.py
yasumasaonoe's picture
Update docci.py
7f10e06 verified
raw
history blame
6.18 kB
# coding=utf-8
# Copyright 2022 the HuggingFace Datasets Authors.
#
# 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 datasets
import glob
import json
import os
from huggingface_hub import hf_hub_url
_DESCRIPTION = """
DOCCI (Descriptions of Connected and Contrasting Images) is a collection of images paired with detailed descriptions. The descriptions explain the key elements of the images, as well as secondary information such as background, lighting, and settings. The images are specifically taken to help assess the precise visual properties of images. DOCCI also includes many related images that vary in having key differences from the others. All descriptions are manually annotated to ensure they adequately distinguish each image from its counterparts.
"""
_HOMEPAGE = "https://google.github.io/docci/"
_LICENSE = "CC BY 4.0"
_URL = "https://storage.googleapis.com/docci/data/"
_URLS = {
"descriptions": _URL + "docci_descriptions.jsonlines",
"images": _URL + "docci_images.tar.gz",
}
_URL_AAR = {
"images": _URL + "docci_images_aar.tar.gz"
}
_FEATURES_DOCCI = datasets.Features(
{
"image": datasets.Image(),
"example_id": datasets.Value('string'),
"description": datasets.Value('string'),
}
)
_FEATURES_DOCCI_AAR = datasets.Features(
{
"image": datasets.Image(),
"example_id": datasets.Value('string'),
}
)
class DOCCI(datasets.GeneratorBasedBuilder):
"""DOCCI"""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="docci", version=VERSION, description="DOCCI images and descriptions"),
datasets.BuilderConfig(name="docci_aar", version=VERSION, description="DOCCI-AAR images"),
]
DEFAULT_CONFIG_NAME = "docci"
def _info(self):
return datasets.DatasetInfo(
features=_FEATURES_DOCCI if self.config.name == 'docci' else _FEATURES_DOCCI_AAR,
homepage=_HOMEPAGE,
description=_DESCRIPTION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
hf_auth_token = dl_manager.download_config.use_auth_token
if hf_auth_token is None:
raise ConnectionError(
"Please set use_auth_token=True or use_auth_token='<TOKEN>' to download this dataset"
)
if self.config.name == 'docci':
data = dl_manager.download_and_extract(_URLS)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'data': data, 'split': 'train'}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'data': data, 'split': 'test'}),
datasets.SplitGenerator(name=datasets.Split("qual_dev"), gen_kwargs={'data': data, 'split': 'qual_dev'}),
datasets.SplitGenerator(name=datasets.Split("qual_test"), gen_kwargs={'data': data, 'split': 'qual_test'}),
]
elif self.config.name == 'docci_aar':
data = dl_manager.download_and_extract(_URL_AAR)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'data': data, 'split': 'train'}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'data': data, 'split': 'test'}),
]
def _generate_examples(self, data, split):
if self.config.name == "docci":
return self._generate_examples_docci(data, split)
elif self.config.name == "docci_aar":
return self._generate_examples_docci_aar(data, split)
def _generate_examples_docci(self, data, split):
with open(data["descriptions"], "r") as f:
examples = [json.loads(l.strip()) for l in f]
for ex in examples:
if split == "train":
if not (ex["split"] == "train" and ex['example_id'].startswith("train")):
continue
elif split == "test":
if not (ex["split"] == "test" and ex['example_id'].startswith("test")):
continue
elif split == "qual_dev":
if not (ex["split"] == "qual_dev" and ex['example_id'].startswith("qual_dev")):
continue
elif split == "qual_test":
if not (ex["split"] == "qual_test" and ex['example_id'].startswith("qual_test")):
continue
image_path = os.path.join(data["images"], "images", ex["image_file"])
_ex = {
"image": image_path,
"example_id": ex["example_id"],
"split": ex["split"],
"image_file": ex["image_file"],
"description": ex["description"],
}
yield _ex["example_id"], _ex
def _generate_examples_docci_aar(self, data, split):
image_files = glob.glob(os.path.join(data["images"], "images_aar", "*.jpg"))
for image_path in image_files:
example_id = os.path.splitext(os.path.basename(image_path))[0]
if split == "train":
if not example_id.startswith("aar_train"):
continue
elif split == "test":
if not example_id.startswith("aar_test"):
continue
_ex = {
"image": image_path,
"example_id": example_id,
"split": split,
"image_file": os.path.basename(image_path),
}
yield _ex["example_id"], _ex