xingyaoww commited on
Commit
cb92051
·
1 Parent(s): aceea1f

Upload omnilabel.py

Browse files
Files changed (1) hide show
  1. omnilabel.py +172 -0
omnilabel.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Omnilabel dataset."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+ _CITATION = """\
25
+ @misc{schulter2023omnilabel,
26
+ title={OmniLabel: A Challenging Benchmark for Language-Based Object Detection},
27
+ author={Samuel Schulter and Vijay Kumar B G and Yumin Suh and Konstantinos M. Dafnis and Zhixing Zhang and Shiyu Zhao and Dimitris Metaxas},
28
+ year={2023},
29
+ eprint={2304.11463},
30
+ archivePrefix={arXiv},
31
+ primaryClass={cs.CV}
32
+ }
33
+ """
34
+
35
+ # TODO: Add description of the dataset here
36
+ # You can copy an official description
37
+ _DESCRIPTION = """OmniLabel Benchmark."""
38
+
39
+ # TODO: Add a link to an official homepage for the dataset here
40
+ _HOMEPAGE = "https://www.omnilabel.org"
41
+
42
+ # TODO: Add the licence for the dataset here if you can find it
43
+ _LICENSE = "https://www.omnilabel.org/dataset/download#h.frhys3no2ecq"
44
+
45
+ # TODO: Add link to the official dataset URLs here
46
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
47
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
48
+ # _URLS = {
49
+ # # "first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
50
+ # # "second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
51
+ # }
52
+
53
+
54
+ _URLS = {
55
+ "val": "https://huggingface.co/datasets/xingyaoww/omnilabel/resolve/main/dataset_all_val_v0.1.3.json",
56
+ }
57
+
58
+
59
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
60
+ class OmniLabel(datasets.GeneratorBasedBuilder):
61
+ """TODO: Short description of my dataset."""
62
+
63
+ VERSION = datasets.Version("1.0.0")
64
+
65
+ # This is an example of a dataset with multiple configurations.
66
+ # If you don't want/need to define several sub-sets in your dataset,
67
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
68
+
69
+ # If you need to make complex sub-parts in the datasets with configurable options
70
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
71
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
72
+
73
+ # You will be able to load one or the other configurations in the following list with
74
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
75
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
76
+ # BUILDER_CONFIGS = [
77
+ # datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
78
+ # datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
79
+ # ]
80
+
81
+ # DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
82
+
83
+ def _info(self):
84
+ features = datasets.Features(
85
+ {
86
+ "id": datasets.Value("int64"), # int (Unique instance ID)
87
+ "image_id": datasets.Value(
88
+ "int64"
89
+ ), # the image id this annotation belongs to
90
+ "image_filename": datasets.Value(
91
+ "string"
92
+ ), # the image filename this annotation belongs to
93
+ "bbox": datasets.features.Sequence(
94
+ datasets.Value("float64")
95
+ ), # [x, y, width, height] (Bounding box co-ordinates for the instance)
96
+ "descriptions": datasets.features.Sequence(
97
+ datasets.Value("string")
98
+ ), # list of the object description text
99
+ }
100
+ )
101
+
102
+ return datasets.DatasetInfo(
103
+ # This is the description that will appear on the datasets page.
104
+ description=_DESCRIPTION,
105
+ # This defines the different columns of the dataset and their types
106
+ features=features, # Here we define them above because they are different between the two configurations
107
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
108
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
109
+ # supervised_keys=("sentence", "label"),
110
+ # Homepage of the dataset for documentation
111
+ homepage=_HOMEPAGE,
112
+ # License for the dataset if available
113
+ license=_LICENSE,
114
+ # Citation for the dataset
115
+ citation=_CITATION,
116
+ )
117
+
118
+ def _split_generators(self, dl_manager):
119
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
120
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
121
+
122
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
123
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
124
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
125
+
126
+ # urls = _URLS[self.config.name]
127
+ # data_dir = dl_manager.download_and_extract(urls)
128
+
129
+ downloaded_files = dl_manager.download_and_extract(_URLS)
130
+ print("downloaded_files: ", downloaded_files)
131
+ return [
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.VALIDATION,
134
+ # These kwargs will be passed to _generate_examples
135
+ gen_kwargs={
136
+ "filepath": downloaded_files["val"],
137
+ "split": "val",
138
+ },
139
+ ),
140
+ ]
141
+
142
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
143
+ def _generate_examples(self, filepath, split):
144
+ if split == "val":
145
+ with open(filepath) as f:
146
+ data = json.load(f)
147
+
148
+ IMAGE_ID_TO_FILENAME = {}
149
+ for image_info in data["images"]:
150
+ IMAGE_ID_TO_FILENAME[image_info["id"]] = image_info["file_name"]
151
+
152
+ DESCRIPTION_ID_TO_DESC = {}
153
+ for desc in data["descriptions"]:
154
+ DESCRIPTION_ID_TO_DESC[desc["id"]] = desc
155
+
156
+ for key, anno in enumerate(data["annotations"]):
157
+ image_id = anno["image_id"]
158
+ image_filename = IMAGE_ID_TO_FILENAME[image_id]
159
+ description_ids = anno["description_ids"]
160
+ descriptions = [
161
+ DESCRIPTION_ID_TO_DESC[description_id]["text"]
162
+ for description_id in description_ids
163
+ ]
164
+
165
+ yield key, {
166
+ "id": anno["id"],
167
+ "image_id": image_id,
168
+ # TODO: make this a PIL.Image
169
+ "image_filename": image_filename,
170
+ "bbox": anno["bbox"],
171
+ "descriptions": descriptions,
172
+ }