import datasets import json import pandas as pd import string _DESCRIPTION = """ ImageInWords (IIW), a carefully designed human-in-the-loop annotation framework for curating hyper-detailed image descriptions and a new dataset resulting from this process. We validate the framework through evaluations focused on the quality of the dataset and its utility for fine-tuning with considerations for readability, comprehensiveness, specificity, hallucinations, and human-likeness. """ _HOMEPAGE = "https://google.github.io/imageinwords/" _LICENSE = "CC BY 4.0" _DATASET_GITHUB_PREFIX = "https://github.com/google/imageinwords/raw/main/datasets" _DATASET_GITHUB_URLS = { "IIW-400": f"{_DATASET_GITHUB_PREFIX}/IIW-400/data.jsonl", "DCI_Test": f"{_DATASET_GITHUB_PREFIX}/DCI_Test/data.jsonl", "DOCCI_Test": f"{_DATASET_GITHUB_PREFIX}/DOCCI_Test/data.jsonl", "CM_3600": f"{_DATASET_GITHUB_PREFIX}/CM_3600/data.jsonl", "LocNar_Eval": f"{_DATASET_GITHUB_PREFIX}/LocNar_Eval/data.jsonl", } _DATASET_FEATURES = { "IIW-400": datasets.Features({ "image/key": datasets.Value('string'), "image/url": datasets.Value('string'), "IIW": datasets.Value('string'), "IIW-P5B": datasets.Value('string'), "iiw-human-sxs-gpt4v": { "metrics/Comprehensiveness": datasets.Value('string'), "metrics/Specificity": datasets.Value('string'), "metrics/Hallucination": datasets.Value('string'), "metrics/First few line(s) as tldr": datasets.Value('string'), "metrics/Human Like": datasets.Value('string'), }, "iiw-human-sxs-iiw-p5b": { "metrics/Comprehensiveness": datasets.Value('string'), "metrics/Specificity": datasets.Value('string'), "metrics/Hallucination": datasets.Value('string'), "metrics/First few line(s) as tldr": datasets.Value('string'), "metrics/Human Like": datasets.Value('string'), }, }), "DCI_Test": datasets.Features({ "image": datasets.Value('string'), "ex_id": datasets.Value('string'), "IIW": datasets.Value('string'), "metrics/Comprehensiveness": datasets.Value('string'), "metrics/Specificity": datasets.Value('string'), "metrics/Hallucination": datasets.Value('string'), "metrics/First few line(s) as tldr": datasets.Value('string'), "metrics/Human Like": datasets.Value('string'), }), "DOCCI_Test": datasets.Features({ "image": datasets.Value('string'), "image/thumbnail_url": datasets.Value('string'), "IIW": datasets.Value('string'), "DOCCI": datasets.Value('string'), "metrics/Comprehensiveness": datasets.Value('string'), "metrics/Specificity": datasets.Value('string'), "metrics/Hallucination": datasets.Value('string'), "metrics/First few line(s) as tldr": datasets.Value('string'), "metrics/Human Like": datasets.Value('string'), }), "CM_3600": datasets.Features({ "image/key": datasets.Value('string'), "image/url": datasets.Value('string'), "IIW-P5B": datasets.Value('string'), }), "LocNar_Eval": datasets.Features({ "image/key": datasets.Value('string'), "image/url": datasets.Value('string'), "IIW-P5B": datasets.Value('string'), }), } _CM_3600_URL_PATTERN = string.Template("https://open-images-dataset.s3.amazonaws.com/crossmodal-3600/$IMAGE_KEY.jpg") _DOCCI_AAR_URL_PATTERN = string.Template("https://storage.googleapis.com/docci/data/images_aar/$IMAGE_KEY.jpg") _DOCCI_THUMBNAIL_URL_PATTERN = string.Template("https://storage.googleapis.com/docci/thumbnails/$IMAGE_KEY.jpg") _LOCNAR_VALIDATION_URL_PATTERN = string.Template("https://open-images-dataset.s3.amazonaws.com/validation/$IMAGE_KEY.jpg") class ImageInWords(datasets.GeneratorBasedBuilder): """ImageInWords dataset""" VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ datasets.BuilderConfig(name="IIW-400", version=VERSION, description="IIW-400"), datasets.BuilderConfig(name="DCI_Test", version=VERSION, description="DCI_Test"), datasets.BuilderConfig(name="DOCCI_Test", version=VERSION, description="DOCCI_Test"), datasets.BuilderConfig(name="CM_3600", version=VERSION, description="CM_3600"), datasets.BuilderConfig(name="LocNar_Eval", version=VERSION, description="LocNar_Eval"), ] DEFAULT_CONFIG_NAME = "IIW-400" def _info(self): return datasets.DatasetInfo( features=_DATASET_FEATURES[self.config.name], 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='' to download this dataset" ) downloaded_file = dl_manager.download_and_extract(_DATASET_GITHUB_URLS[self.config.name]) if self.config.name == "LocNar_Eval": split_type = datasets.Split.VALIDATION else: split_type = datasets.Split.TEST return [ datasets.SplitGenerator(name=split_type, gen_kwargs={"filepath": downloaded_file}), ] def _generate_examples(self, filepath): match self.config.name: case "IIW-400": return self._generate_examples_iiw_400(filepath) case "DCI_Test": return self._generate_examples_dci_test(filepath) case "DOCCI_Test": return self._generate_examples_docci_test(filepath) case "CM_3600": return self._generate_examples_cm_3600(filepath) case "LocNar_Eval": return self._generate_examples_locnar_eval(filepath) def _generate_examples_iiw_400(self, filepath): with open(filepath) as fp: for json_line in fp: json_obj = json.loads(json_line.strip()) json_obj["image/url"] = _DOCCI_AAR_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image/key"]) yield json_obj["image/key"], json_obj def _generate_examples_dci_test(self, filepath): with open(filepath) as fp: for json_line in fp: json_obj = json.loads(json_line.strip()) yield json_obj["image"], json_obj def _generate_examples_docci_test(self, filepath): with open(filepath) as fp: for json_line in fp: json_obj = json.loads(json_line.strip()) json_obj["image/thumbnail_url"] = _DOCCI_THUMBNAIL_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image"]) yield json_obj["image"], json_obj def _generate_examples_cm_3600(self, filepath): with open(filepath) as fp: for json_line in fp: json_obj = json.loads(json_line.strip()) json_obj["image/url"] = _CM_3600_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image/key"]) del json_obj["image/source"] yield json_obj["image/key"], json_obj def _generate_examples_locnar_eval(self, filepath): with open(filepath) as fp: for json_line in fp: json_obj = json.loads(json_line.strip()) json_obj["image/url"] = _LOCNAR_VALIDATION_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image/key"]) del json_obj["image/source"] yield json_obj["image/key"], json_obj