File size: 7,733 Bytes
7debf8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35d646
 
 
7debf8d
 
 
 
 
 
 
 
 
 
 
d35d646
 
 
 
 
 
7debf8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35d646
7debf8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35d646
7debf8d
d35d646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7debf8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35d646
 
7debf8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a9445b
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and 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.

# Lint as: python3
"""The ELEVATER benchmark"""

import json
import os

import datasets

from zipfile import ZipFile
from io import BytesIO
from PIL import Image

_VERSION = "1.0.0"
_BASE_URL = "https://cvinthewildeus.blob.core.windows.net/datasets/"

_ELEVATER_CITATION = """\
@article{li2022elevater,
    title={ELEVATER: A Benchmark and Toolkit for Evaluating Language-Augmented Visual Models},
    author={Li, Chunyuan and Liu, Haotian and Li, Liunian Harold and Zhang, Pengchuan and Aneja, Jyoti and Yang, Jianwei and Jin, Ping and Lee, Yong Jae and Hu, Houdong and Liu, Zicheng and Gao, Jianfeng},
    journal={Neural Information Processing Systems},
    year={2022}
}
Note that each ELEVATER dataset has its own citation. Please see the source to
get the correct citation for each contained dataset.
"""

_VOC_2007_CLASSIFICATION_CITATION="""\
@misc{pascal-voc-2007,
	author = "Everingham, M. and Van~Gool, L. and Williams, C. K. I. and Winn, J. and Zisserman, A.",
	title = "The {PASCAL} {V}isual {O}bject {C}lasses {C}hallenge 2007 {(VOC2007)} {R}esults",
	howpublished = "http://www.pascal-network.org/challenges/VOC/voc2007/workshop/index.html"
}"""

_CIFAR_10_CITATION="""\
@article{krizhevsky2009learning,
  title={Learning multiple layers of features from tiny images},
  author={Krizhevsky, Alex and Hinton, Geoffrey and others},
  year={2009},
  publisher={Toronto, ON, Canada}
}"""

class ELEVATERConfig(datasets.BuilderConfig):

    """BuilderConfig for ELEVATER."""
    def __init__(self, name, description, contact, version, type_, root_folder, labelmap, num_classes, train, test, citation, url, **kwargs):
        """BuilderConfig for ELEVATER.
        Args:
          features: `list[string]`, list of the features that will appear in the
            feature dict. Should not include "label".
          data_url: `string`, url to download the zip file from.
          citation: `string`, citation for the data set.
          url: `string`, url for information about the data set.
          label_classes: `list[string]`, the list of classes for the label if the
            label is present as a string. Non-string labels will be cast to either
            'False' or 'True'.
          **kwargs: keyword arguments forwarded to super.
        """
        super(ELEVATERConfig, self).__init__(**kwargs)
        self.name = name
        self.description = description
        self.contact = contact
        self.version = version
        self.type = type_
        self.root_folder = root_folder
        self.labelmap = labelmap
        self.num_classes = num_classes
        self.train = train
        self.test = test
        self.citation = citation
        self.url = url


class ELEVATER(datasets.GeneratorBasedBuilder):

    BUILDER_CONFIGS = [
        ELEVATERConfig(
            name="cifar-10",
            description="The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.",
            contact="pinjin",
            version=_VERSION,
            type_="classification_multiclass",
            root_folder="classification/cifar_10_20211007",
            labelmap="labels.txt",
            num_classes=10,
            train={
                "index_path": "train.txt",
                "files_for_local_usage": ["train.zip"],
                "num_images": 50000
            },
            test={
                "index_path": "test.txt",
                "files_for_local_usage": ["val.zip"],
                "num_images": 10000
            },
            citation=_CIFAR_10_CITATION,
            url=_BASE_URL,
        ),
        ELEVATERConfig(
            "name"="voc-2007-classification",
            "description"="Voc2007 classification dataset.",
            "contact"="pinjin",
            "version"=_VERSION,
            "type"="classification_multilabel",
            "root_folder"="classification/voc2007_20211007",
            "train": {
                "index_path": "train_ic.txt",
                "files_for_local_usage": ["train.zip"],
                "num_images": 2501
            },
            "val": {
                "index_path": "val_ic.txt",
                "files_for_local_usage": ["train.zip"],
                "num_images": 2510
            },
            "test": {
                "index_path": "test_ic.txt",
                "files_for_local_usage": ["test.zip"],
                "num_images": 4952
            },
            "labelmap": "labels.txt",
            "num_classes": 20,
            citation=_VOC_2007_CLASSIFICATION_CITATION,
            url=_BASE_URL,
        )
    ]

    def _info(self):
        features = datasets.Features(
            {
                "image_file_path": datasets.Value("string"),
                "image": datasets.Image(),
                "labels": datasets.Value("int32")
            }
        )
        return datasets.DatasetInfo(
            description=self.config.description,
            features=features,
            citation=self.config.citation + '\n' + _ELEVATER_CITATION,
        )

    def _split_generators(self, dl_manager):
        _URL = self.config.url + self.config.root_folder
        print(self.config.val)
        print(good)
        urls_to_download = {
            "labelmap": os.path.join(_URL, self.config.labelmap),
            "train": {
                "images": os.path.join(_URL, self.config.train['files_for_local_usage'][0]),
                "index": os.path.join(_URL, self.config.train['index_path']),
            },
            "test": {
                "images": os.path.join(_URL, self.config.test['files_for_local_usage'][0]),
                "index": os.path.join(_URL, self.config.test['index_path']),
            }
        } 
        downloaded_files = dl_manager.download_and_extract(urls_to_download)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": downloaded_files["train"]["images"],
                    "index": downloaded_files["train"]["index"],
                    "split": datasets.Split.TRAIN,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "images": downloaded_files["test"]["images"],
                    "index": downloaded_files["test"]["index"],
                    "split": datasets.Split.TEST,
                },
            )
        ]

    def _generate_examples(self, images, index, split):
        image_path_label_list = []
        with open(index, "r") as f:
            lines = f.readlines()
            for i, line in enumerate(lines):
                line_split = line[:-1].split(" ")
                label = int(line_split[1])
                image_path = line_split[0].split('@')[1]
                path = images + '/' + image_path
                yield i, {
                    "image_file_path": path,
                    "image": path,
                    "labels": label,
                }