roopalgarg commited on
Commit
aadeaaf
1 Parent(s): ae3ee8c

initial IIW setup

Browse files
Files changed (1) hide show
  1. imageinwords.py +166 -0
imageinwords.py CHANGED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import json
3
+ import pandas as pd
4
+ import string
5
+
6
+ _DESCRIPTION = """
7
+ """
8
+
9
+ _HOMEPAGE = "https://google.github.io/imageinwords/"
10
+
11
+ _LICENSE = "CC BY 4.0"
12
+
13
+ _DATASET_GITHUB_URLS = {
14
+ "IIW-400": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/IIW-400/data.jsonl?token=GHSAT0AAAAAACRUNGOO3YHB2HLA3RGXDT3SZRUODLA",
15
+ "DCI_Test": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/DCI_Test/data.jsonl?token=GHSAT0AAAAAACRUNGOOB7E5WQASMIECN4JGZRUOEJQ",
16
+ "DOCCI_Test": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/DOCCI_Test/data.jsonl?token=GHSAT0AAAAAACRUNGOPOYXGW4TFIX4X2LGKZRUOEVA",
17
+ "CM_3600": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/CM_3600/data.jsonl?token=GHSAT0AAAAAACRUNGOO3U44BE7ICZ3LG5D2ZRUOEAA",
18
+ "LocNar_Eval": "https://raw.githubusercontent.com/google/imageinwords/main/datasets/LocNar_Eval/data.jsonl?token=GHSAT0AAAAAACRUNGOOYF254H574CK24TV2ZRUOC3Q",
19
+ }
20
+
21
+ _DATASET_FEATURES = {
22
+ "IIW-400": datasets.Features({
23
+ "image/key": datasets.Value('string'),
24
+ "image/thumbnail_url": datasets.Value('string'),
25
+ "IIW": datasets.Value('string'),
26
+ "IIW-P5B": datasets.Value('string'),
27
+ "iiw-human-sxs-gpt4v": {
28
+ "metrics/Comprehensiveness": datasets.Value('string'),
29
+ "metrics/Specificity": datasets.Value('string'),
30
+ "metrics/Hallucination": datasets.Value('string'),
31
+ "metrics/First few line(s) as tldr": datasets.Value('string'),
32
+ "metrics/Human Like": datasets.Value('string'),
33
+ },
34
+ "iiw-human-sxs-iiw-p5b": {
35
+ "metrics/Comprehensiveness": datasets.Value('string'),
36
+ "metrics/Specificity": datasets.Value('string'),
37
+ "metrics/Hallucination": datasets.Value('string'),
38
+ "metrics/First few line(s) as tldr": datasets.Value('string'),
39
+ "metrics/Human Like": datasets.Value('string'),
40
+ },
41
+ }),
42
+ "DCI_Test": datasets.Features({
43
+ "image": datasets.Value('string'),
44
+ "ex_id": datasets.Value('string'),
45
+ "IIW": datasets.Value('string'),
46
+ "metrics/Comprehensiveness": datasets.Value('string'),
47
+ "metrics/Specificity": datasets.Value('string'),
48
+ "metrics/Hallucination": datasets.Value('string'),
49
+ "metrics/First few line(s) as tldr": datasets.Value('string'),
50
+ "metrics/Human Like": datasets.Value('string'),
51
+ }),
52
+ "DOCCI_Test": datasets.Features({
53
+ "image": datasets.Value('string'),
54
+ "image/thumbnail_url": datasets.Value('string'),
55
+ "IIW": datasets.Value('string'),
56
+ "DOCCI": datasets.Value('string'),
57
+ "metrics/Comprehensiveness": datasets.Value('string'),
58
+ "metrics/Specificity": datasets.Value('string'),
59
+ "metrics/Hallucination": datasets.Value('string'),
60
+ "metrics/First few line(s) as tldr": datasets.Value('string'),
61
+ "metrics/Human Like": datasets.Value('string'),
62
+ }),
63
+ "CM_3600": datasets.Features({
64
+ "image/key": datasets.Value('string'),
65
+ "image/url": datasets.Value('string'),
66
+ "IIW-P5B": datasets.Value('string'),
67
+ }),
68
+ "LocNar_Eval": datasets.Features({
69
+ "image/key": datasets.Value('string'),
70
+ "IIW-P5B": datasets.Value('string'),
71
+ }),
72
+ }
73
+
74
+
75
+ _CM_3600_URL_PATTERN = string.Template("https://open-images-dataset.s3.amazonaws.com/crossmodal-3600/$IMAGE_KEY.jpg")
76
+ _DOCCI_AAR_THUMBNAIL_URL_PATTERN = string.Template("https://storage.googleapis.com/docci/thumbnails/$IMAGE_KEY.jpg")
77
+
78
+
79
+ class ImageInWords(datasets.GeneratorBasedBuilder):
80
+ """ImageInWords dataset"""
81
+
82
+ VERSION = datasets.Version("1.0.0")
83
+
84
+ BUILDER_CONFIGS = [
85
+ datasets.BuilderConfig(name="IIW-400", version=VERSION, description="IIW-400"),
86
+ datasets.BuilderConfig(name="DCI_Test", version=VERSION, description="DCI_Test"),
87
+ datasets.BuilderConfig(name="DOCCI_Test", version=VERSION, description="DOCCI_Test"),
88
+ datasets.BuilderConfig(name="CM_3600", version=VERSION, description="CM_3600"),
89
+ datasets.BuilderConfig(name="LocNar_Eval", version=VERSION, description="LocNar_Eval"),
90
+ ]
91
+
92
+ DEFAULT_CONFIG_NAME = "IIW-400"
93
+
94
+ def _info(self):
95
+ return datasets.DatasetInfo(
96
+ features=_DATASET_FEATURES[self.config.name],
97
+ homepage=_HOMEPAGE,
98
+ description=_DESCRIPTION,
99
+ license=_LICENSE,
100
+ )
101
+
102
+ def _split_generators(self, dl_manager):
103
+ """Returns SplitGenerators."""
104
+ hf_auth_token = dl_manager.download_config.use_auth_token
105
+ if hf_auth_token is None:
106
+ raise ConnectionError(
107
+ "Please set use_auth_token=True or use_auth_token='<TOKEN>' to download this dataset"
108
+ )
109
+
110
+ downloaded_file = dl_manager.download_and_extract(_DATASET_GITHUB_URLS[self.config.name])
111
+ if self.config.name == "LocNar_Eval":
112
+ split_type = datasets.Split.VALIDATION
113
+ else:
114
+ split_type = datasets.Split.TEST
115
+ return [
116
+ datasets.SplitGenerator(name=split_type, gen_kwargs={"filepath": downloaded_file}),
117
+ ]
118
+
119
+ def _generate_examples(self, filepath):
120
+ match self.config.name:
121
+ case "IIW-400":
122
+ return self._generate_examples_iiw_400(filepath)
123
+ case "DCI_Test":
124
+ return self._generate_examples_dci_test(filepath)
125
+ case "DOCCI_Test":
126
+ return self._generate_examples_docci_test(filepath)
127
+ case "CM_3600":
128
+ return self._generate_examples_cm_3600(filepath)
129
+ case "LocNar_Eval":
130
+ return self._generate_examples_locnar_eval(filepath)
131
+
132
+ def _generate_examples_iiw_400(filepath):
133
+ with open(filepath) as fp:
134
+ for json_line in fp:
135
+ json_obj = json.loads(json_line.strip())
136
+ json_obj["image/thumbnail_url"] = _DOCCI_AAR_THUMBNAIL_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image/key"])
137
+ yield json_obj["image/key"], json_obj
138
+
139
+ def _generate_examples_dci_test(filepath):
140
+ with open(filepath) as fp:
141
+ for json_line in fp:
142
+ json_obj = json.loads(json_line.strip())
143
+ yield json_obj["image"], json_obj
144
+
145
+ def _generate_examples_docci_test(filepath):
146
+ with open(filepath) as fp:
147
+ for json_line in fp:
148
+ json_obj = json.loads(json_line.strip())
149
+ json_obj["image/thumbnail_url"] = _DOCCI_AAR_THUMBNAIL_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image"])
150
+ yield json_obj["image"], json_obj
151
+
152
+ def _generate_examples_cm_3600(filepath):
153
+ with open(filepath) as fp:
154
+ for json_line in fp:
155
+ json_obj = json.loads(json_line.strip())
156
+ json_obj["image/url"] = _CM_3600_URL_PATTERN.substitute(IMAGE_KEY=json_obj["image/key"])
157
+ del json_obj["image/source"]
158
+ yield json_obj["image/key"], json_obj
159
+
160
+
161
+ def _generate_examples_locnar_eval(filepath):
162
+ with open(filepath) as fp:
163
+ for json_line in fp:
164
+ json_obj = json.loads(json_line.strip())
165
+ del json_obj["image/source"]
166
+ yield json_obj["image/key"], json_obj