Datasets:

Languages:
English
ArXiv:
License:
yasumasaonoe commited on
Commit
dfebd71
·
verified ·
1 Parent(s): 6e2720a

Create docci.py

Browse files
Files changed (1) hide show
  1. docci.py +156 -0
docci.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 the HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import datasets
17
+ import glob
18
+ import json
19
+ import os
20
+
21
+ from huggingface_hub import hf_hub_url
22
+
23
+
24
+ _DESCRIPTION = """
25
+ 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.
26
+ """
27
+
28
+ _HOMEPAGE = "https://google.github.io/docci/"
29
+
30
+ _LICENSE = "CC BY 4.0"
31
+
32
+ _URL = "https://storage.googleapis.com/docci/data/"
33
+
34
+ _URLS = {
35
+ "descriptions": _URL + "docci_descriptions.jsonlines",
36
+ "images": _URL + "docci_images.tar.gz",
37
+ }
38
+
39
+ _URL_AAR = _URL + "docci_images_aar.tar.gz"
40
+
41
+ _FEATURES_DOCCI = datasets.Features(
42
+ {
43
+ "image": datasets.Image(),
44
+ "example_id": datasets.Value('string'),
45
+ "description": [datasets.Value('string')],
46
+ }
47
+ )
48
+
49
+ _FEATURES_DOCCI_AAR = datasets.Features(
50
+ {
51
+ "image": datasets.Image(),
52
+ "example_id": datasets.Value('string'),
53
+ }
54
+ )
55
+
56
+ class DOCCI(datasets.GeneratorBasedBuilder):
57
+ """DOCCI"""
58
+
59
+ VERSION = datasets.Version("1.0.0")
60
+
61
+ BUILDER_CONFIGS = [
62
+ datasets.BuilderConfig(name="docci", version=VERSION, description="DOCCI images and descriptions"),
63
+ # datasets.BuilderConfig(name="docci_aar", version=VERSION, description="DOCCI-AAR images"),
64
+ ]
65
+
66
+ DEFAULT_CONFIG_NAME = "docci"
67
+
68
+ def _info(self):
69
+ return datasets.DatasetInfo(
70
+ features=_FEATURES_DOCCI self.config.name == 'docci' else _FEATURES_DOCCI_AAR
71
+ homepage=_HOMEPAGE,
72
+ description=_DESCRIPTION,
73
+ license=_LICENSE,
74
+ )
75
+
76
+ def _split_generators(self, dl_manager):
77
+ """Returns SplitGenerators."""
78
+ hf_auth_token = dl_manager.download_config.use_auth_token
79
+ if hf_auth_token is None:
80
+ raise ConnectionError(
81
+ "Please set use_auth_token=True or use_auth_token='<TOKEN>' to download this dataset"
82
+ )
83
+
84
+ if self.config.name == 'docci':
85
+ data = dl_manager.download_and_extract(_URLS)
86
+ return [
87
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'data': data, 'split': 'train'}),
88
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'data': data, 'split': 'test'}),
89
+ datasets.SplitGenerator(name=datasets.Split.QUAL_DEV, gen_kwargs={'data': data, 'split': 'qual_dev'}),
90
+ datasets.SplitGenerator(name=datasets.Split.QUAL_TEST, gen_kwargs={'data': data, 'split': 'qual_test'}),
91
+ ]
92
+ elif self.config.name == 'docci_aar':
93
+ data = dl_manager.download_and_extract(_URLS_AAR)
94
+ return [
95
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'data': data, 'split': 'train'}),
96
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'data': data, 'split': 'test'}),
97
+ ]
98
+
99
+ def _generate_examples(self, data, split):
100
+ if self.config.name == "docci":
101
+ return self._generate_examples_docci(data, split)
102
+ elif self.config.name == "docci_aar":
103
+ return self._generate_examples_docci_aar(data, split)
104
+
105
+ def _generate_examples_docci(self, data, split):
106
+ with open(data["descriptions"], "r") as f:
107
+ examples = [json.loads(l.stript()) for l in f]
108
+
109
+ for ex in annotations["images"]:
110
+ if split == "train":
111
+ if not (ex["split"] == "train" and ex['example_id'].startswith("train")):
112
+ continue
113
+ elif split == "test":
114
+ if not (ex["split"] == "test" and ex['example_id'].startswith("test")):
115
+ continue
116
+ elif split == "qual_dev":
117
+ if not (ex["split"] == "qual_dev" and ex['example_id'].startswith("qual_dev")):
118
+ continue
119
+ elif split == "qual_test":
120
+ if not (ex["split"] == "qual_test" and ex['example_id'].startswith("qual_test")):
121
+ continue
122
+
123
+ image_path = os.path.join(data["images"], ex["image_file"])
124
+
125
+ _ex = {
126
+ "image": str(image_path.absolute()),
127
+ "example_id": ex["example_id"],
128
+ "split": ex["split"],
129
+ "image_file": ex["image_file"],
130
+ "description": ex["description"],
131
+ }
132
+
133
+ yield _ex["example_id"], _ex
134
+
135
+ def _generate_examples_docci_aar(self, data, split):
136
+ image_files = glob.glob(data[split])
137
+
138
+ for image_path in image_files:
139
+
140
+ example_id = os.path.splitext(os.path.basename(image_path))[0]
141
+
142
+ if split == "train":
143
+ if not example_id.startswith("train"):
144
+ continue
145
+ elif split == "test":
146
+ if not example_id.startswith("test"):
147
+ continue
148
+
149
+ _ex = {
150
+ "image": str(image_path.absolute()),
151
+ "example_id": example_id,
152
+ "split": split,
153
+ "image_file": ex["image_file"],
154
+ }
155
+
156
+ yield _ex["example_id"], _ex