imageinwords / imageinwords.py
roopalgarg's picture
initial IIW setup
aadeaaf verified
raw
history blame
7.31 kB
import datasets
import json
import pandas as pd
import string
_DESCRIPTION = """
"""
_HOMEPAGE = "https://google.github.io/imageinwords/"
_LICENSE = "CC BY 4.0"
_DATASET_GITHUB_URLS = {
"IIW-400": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/IIW-400/data.jsonl?token=GHSAT0AAAAAACRUNGOO3YHB2HLA3RGXDT3SZRUODLA",
"DCI_Test": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/DCI_Test/data.jsonl?token=GHSAT0AAAAAACRUNGOOB7E5WQASMIECN4JGZRUOEJQ",
"DOCCI_Test": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/DOCCI_Test/data.jsonl?token=GHSAT0AAAAAACRUNGOPOYXGW4TFIX4X2LGKZRUOEVA",
"CM_3600": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/CM_3600/data.jsonl?token=GHSAT0AAAAAACRUNGOO3U44BE7ICZ3LG5D2ZRUOEAA",
"LocNar_Eval": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/LocNar_Eval/data.jsonl?token=GHSAT0AAAAAACRUNGOOYF254H574CK24TV2ZRUOC3Q",
}
_DATASET_FEATURES = {
"IIW-400": datasets.Features({
"image/key": datasets.Value('string'),
"image/thumbnail_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'),
"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_THUMBNAIL_URL_PATTERN = string.Template("https://storage.googleapis.com/docci/thumbnails/$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='<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(filepath):
with open(filepath) as fp:
for json_line in fp:
json_obj = json.loads(json_line.strip())
json_obj["image/thumbnail_url"] = _DOCCI_AAR_THUMBNAIL_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image/key"])
yield json_obj["image/key"], json_obj
def _generate_examples_dci_test(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(filepath):
with open(filepath) as fp:
for json_line in fp:
json_obj = json.loads(json_line.strip())
json_obj["image/thumbnail_url"] = _DOCCI_AAR_THUMBNAIL_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image"])
yield json_obj["image"], json_obj
def _generate_examples_cm_3600(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(filepath):
with open(filepath) as fp:
for json_line in fp:
json_obj = json.loads(json_line.strip())
del json_obj["image/source"]
yield json_obj["image/key"], json_obj