Datasets:

Languages:
English
ArXiv:
License:
File size: 5,923 Bytes
dfebd71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
899626b
 
 
dfebd71
 
8c7ccc0
 
 
 
 
 
dfebd71
 
8c7ccc0
 
 
 
 
dfebd71
e1d29f7
dfebd71
 
 
 
 
 
 
dcf5a02
dfebd71
 
 
 
 
 
bd1e3d6
dfebd71
 
 
 
 
 
 
 
 
 
 
 
e1d29f7
 
dfebd71
 
dc45641
dfebd71
 
 
 
 
 
 
 
 
 
 
 
 
78b62e0
dfebd71
866639d
dfebd71
 
 
 
 
 
 
 
 
 
 
 
 
f558679
dfebd71
 
781ae43
dfebd71
 
 
 
 
 
 
 
 
aed18e3
dfebd71
 
 
 
 
 
3b312f9
dfebd71
 
3b312f9
dfebd71
 
 
781ae43
dfebd71
 
9b016de
dfebd71
 
 
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
# coding=utf-8
# Copyright 2022 the HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datasets
import glob
import json
import os

from huggingface_hub import hf_hub_url


_DESCRIPTION = """
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.
"""

_HOMEPAGE = "https://google.github.io/docci/"

_LICENSE = "CC BY 4.0"

_URL = "https://storage.googleapis.com/docci/data/"

_URLS = {
    "descriptions": _URL + "docci_descriptions.jsonlines",
    "images": _URL + "docci_images.tar.gz",
}

_URL_AAR = {
    "images": _URL + "docci_images_aar.tar.gz"
}

_FEATURES_DOCCI = datasets.Features(
    {
        "image": datasets.Image(),
        "example_id": datasets.Value('string'),
        "description": datasets.Value('string'),
    }
)

_FEATURES_DOCCI_AAR = datasets.Features(
    {
        "image": datasets.Image(),
        "example_id": datasets.Value('string'),
    }
)


class DOCCI(datasets.GeneratorBasedBuilder):
    """DOCCI"""
    
    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="docci", version=VERSION, description="DOCCI images and descriptions"),
        datasets.BuilderConfig(name="docci_aar", version=VERSION, description="DOCCI-AAR images"),
    ]

    DEFAULT_CONFIG_NAME = "docci"

    def _info(self):
        return datasets.DatasetInfo(
            features=_FEATURES_DOCCI if self.config.name == 'docci' else _FEATURES_DOCCI_AAR,
            homepage=_HOMEPAGE,
            description=_DESCRIPTION,
            license=_LICENSE,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        if self.config.name == 'docci':
            data = dl_manager.download_and_extract(_URLS)
            return [
                datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'data': data, 'split': 'train'}),
                datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'data': data, 'split': 'test'}),
                datasets.SplitGenerator(name=datasets.Split("qual_dev"), gen_kwargs={'data': data, 'split': 'qual_dev'}),
                datasets.SplitGenerator(name=datasets.Split("qual_test"), gen_kwargs={'data': data, 'split': 'qual_test'}),
            ]
        elif self.config.name == 'docci_aar':
            data = dl_manager.download_and_extract(_URL_AAR)
            return [
                datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'data': data, 'split': 'train'}),
                datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'data': data, 'split': 'test'}),
            ]

    def _generate_examples(self, data, split):
        if self.config.name == "docci":
            return self._generate_examples_docci(data, split)
        elif self.config.name == "docci_aar":
            return self._generate_examples_docci_aar(data, split)

    def _generate_examples_docci(self, data, split):
        with open(data["descriptions"], "r") as f:
            examples = [json.loads(l.strip()) for l in f]

            for ex in examples:
                if split == "train":
                    if not (ex["split"] == "train" and ex['example_id'].startswith("train")):
                        continue
                elif split == "test":
                    if not (ex["split"] == "test" and ex['example_id'].startswith("test")):
                        continue
                elif split == "qual_dev":
                    if not (ex["split"] == "qual_dev" and ex['example_id'].startswith("qual_dev")):
                        continue
                elif split == "qual_test":
                    if not (ex["split"] == "qual_test" and ex['example_id'].startswith("qual_test")):
                        continue

                image_path = os.path.join(data["images"], "images", ex["image_file"])

                _ex = {
                    "image": image_path,
                    "example_id": ex["example_id"],
                    "split": ex["split"],
                    "image_file": ex["image_file"],
                    "description": ex["description"],
                }

                yield _ex["example_id"], _ex

    def _generate_examples_docci_aar(self, data, split):
        image_files = glob.glob(os.path.join(data["images"], "images_aar", "*.jpg"))
        
        for image_path in image_files:

            example_id = os.path.splitext(os.path.basename(image_path))[0]
            
            if split == "train":
                if not example_id.startswith("aar_train"):
                    continue
            elif split == "test":
                if not example_id.startswith("aar_test"):
                    continue

            _ex = {
                "image": image_path,
                "example_id": example_id,
                "split": split,
                "image_file": os.path.basename(image_path),
            }

            yield _ex["example_id"], _ex