File size: 7,724 Bytes
aadeaaf
 
 
 
 
 
c9ae554
 
aadeaaf
 
 
 
 
 
9bf1625
 
1b822a9
aadeaaf
1b822a9
 
 
 
 
aadeaaf
 
 
 
 
f63a4cb
aadeaaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3a0a94
aadeaaf
 
 
 
 
 
b4f7386
 
b3a0a94
aadeaaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49d6246
aadeaaf
 
 
b4f7386
aadeaaf
 
49d6246
aadeaaf
 
 
 
 
49d6246
aadeaaf
 
 
b4f7386
aadeaaf
 
49d6246
aadeaaf
 
 
 
 
 
 
 
49d6246
aadeaaf
 
 
b3a0a94
aadeaaf
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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='<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