mikewang commited on
Commit
586db0e
·
1 Parent(s): 79a7b49

add padv2 part 1

Browse files
Files changed (2) hide show
  1. README.md +43 -0
  2. padv2.py +254 -0
README.md ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pretty_name: 'Padv2 Dataset - Part1'
3
+ language:
4
+ - en
5
+ ---
6
+ # Dataset Card for Padv2 Part1
7
+
8
+ ## Dataset Description
9
+
10
+ **Official Repo:** https://github.com/lhc1224/OSAD_Net#-dataset-;
11
+
12
+ **IMPORTANT Notes**:
13
+ - This Huggingface dataset loads the Part1 of the Padv2 dataset, i.e., the PADv2_part1.zip; The file can also be downloaded from: https://uofi.box.com/s/1atjh3d2p82qyxm3gp11514006va0llq
14
+ - Each instance in the loaded HF dataset contains the following fields:
15
+ - `image_uid`: unique id to a dataset instanec
16
+ - `image_path`: path to the raw rgb image
17
+ - `depth_path`: path to the depth annotation of the image
18
+ - `mask_path`: path to the object mask of the image
19
+ - `affordance_type`: affordance type of the object in the image
20
+ - `original_divisions`: there are three versions of divisions on the affordance types in the original dataset, this field stores the split ("train" or "test") of this instance in the three different divisions ("divide_1", "divide_2", "divide_3")
21
+
22
+
23
+ **Paper Citation:**
24
+ ```
25
+ @inproceedings{Oneluo,
26
+ title={One-Shot Affordance Detection},
27
+ author={Hongchen Luo and Wei Zhai and Jing Zhang and Yang Cao and Dacheng Tao},
28
+ booktitle={IJCAI},
29
+ year={2021}
30
+ }
31
+ ```
32
+ ```
33
+ @article{luo2021one,
34
+ title={One-Shot Object Affordance Detection in the Wild},
35
+ author={Zhai, Wei and Luo, Hongchen and Zhang, Jing and Cao, Yang and Tao, Dacheng},
36
+ journal={arXiv preprint arXiv:2108.03658},
37
+ year={2021}
38
+ }
39
+ ```
40
+ ## Dataset Summary
41
+ With complex scenes and rich annotations, the PADv2 dataset can be used as a test bed to benchmark affordance detection methods and may also facilitate downstream vision tasks, such as scene understanding, action recognition, and robot manipulation.
42
+
43
+ It contains 30k diverse images covering 39 affordance categories as well as 103 object categories from different scenes.
padv2.py ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Padv2 dataset Part1"""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+ import requests
24
+ from tqdm import tqdm
25
+ from zipfile import ZipFile
26
+ from collections import defaultdict
27
+
28
+ _CITATION = """\
29
+ @article{luo2021one,
30
+ title={One-Shot Object Affordance Detection in the Wild},
31
+ author={Zhai, Wei and Luo, Hongchen and Zhang, Jing and Cao, Yang and Tao, Dacheng},
32
+ journal={arXiv preprint arXiv:2108.03658},
33
+ year={2021}
34
+ }
35
+ """
36
+
37
+ # TODO: Add description of the dataset here
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ Padv2 dataset:https://github.com/lhc1224/OSAD_Net#-dataset-;
41
+ - This dataset loads the PADv2_part1.zip
42
+ - Each instance contains the following fields:
43
+ - `image_uid`: unique id to a dataset instanec
44
+ - `image_path`: path to the raw rgb image
45
+ - `depth_path`: path to the depth annotation of the image
46
+ - `mask_path`: path to the object mask of the image
47
+ - `affordance_type`: affordance type of the object in the image
48
+ - `original_divisions`: there are three versions of divisions on the affordance types in the original dataset, this field stores the split ("train" or "test") of this instance in the three different divisions ("divide_1", "divide_2", "divide_3")
49
+ """
50
+
51
+ # TODO: Add a link to an official homepage for the dataset here
52
+ _HOMEPAGE = "https://github.com/lhc1224/OSAD_Net#-dataset-"
53
+
54
+ # TODO: Add the licence for the dataset here if you can find it
55
+ _LICENSE = ""
56
+
57
+ # TODO: Add link to the official dataset URLs here
58
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
59
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
60
+ # _URLS = {
61
+ # # "first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
62
+ # # "second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
63
+ # }
64
+
65
+
66
+ _URLS = {
67
+ "val": "https://uofi.box.com/shared/static/1atjh3d2p82qyxm3gp11514006va0llq.zip" # original division and split info are stored in the "original_divisions" field for each instance
68
+ }
69
+
70
+
71
+ def _load_padv2_part1_instances(Padv2_part1_unzipped_root):
72
+ data = defaultdict(lambda: {
73
+ "image_path": None,
74
+ "depth_path": None,
75
+ "mask_path": None,
76
+ "affordance_type": None,
77
+ "original_divisions": {
78
+ "divide_1": None,
79
+ "divide_2": None,
80
+ "divide_3": None
81
+ }
82
+ })
83
+
84
+ for division in ["divide_1", "divide_2", "divide_3"]:
85
+ for split in ["train", "test"]:
86
+ split_dir = os.path.join(Padv2_part1_unzipped_root, division, split)
87
+ image_dir = os.path.join(split_dir, "images")
88
+ mask_dir = os.path.join(split_dir, "masks")
89
+ depth_dir = os.path.join(split_dir, "depth")
90
+ for affordance_type in os.listdir(image_dir):
91
+ affordance_type_dir = os.path.join(image_dir, affordance_type)
92
+ for object_type in os.listdir(affordance_type_dir):
93
+ object_type_dir = os.path.join(affordance_type_dir, object_type)
94
+ for image_name_with_extension in os.listdir(object_type_dir):
95
+ image_name = image_name_with_extension.split(".")[0]
96
+ image_uid = f"{affordance_type}__{object_type}__{image_name}"
97
+ assert len(image_uid.split("__")) == 3
98
+ if data[image_uid]["image_path"] is None:
99
+ data[image_uid]["image_path"] = os.path.join(object_type_dir, image_name_with_extension)
100
+ data[image_uid]["depth_path"] = os.path.join(depth_dir, affordance_type, object_type, image_name + ".png")
101
+ data[image_uid]["mask_path"] = os.path.join(mask_dir, affordance_type, object_type, image_name + ".png")
102
+ data[image_uid]["affordance_type"] = affordance_type
103
+ data[image_uid]["original_divisions"][division] = split
104
+ return data
105
+
106
+ def _download_box_file(url, destination):
107
+ response = requests.get(url, stream=True)
108
+
109
+ total_size = int(response.headers.get('content-length', 0))
110
+ block_size = 1024 # Adjust the block size as needed
111
+ progress_bar = tqdm(total=total_size, unit='B', unit_scale=True)
112
+
113
+ with open(destination, 'wb') as file:
114
+ for data in response.iter_content(block_size):
115
+ progress_bar.update(len(data))
116
+ file.write(data)
117
+
118
+ progress_bar.close()
119
+ print("Download completed!")
120
+
121
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
122
+ class PadV2(datasets.GeneratorBasedBuilder):
123
+ """TODO: Short description of my dataset."""
124
+
125
+ VERSION = datasets.Version("1.0.0")
126
+
127
+ # This is an example of a dataset with multiple configurations.
128
+ # If you don't want/need to define several sub-sets in your dataset,
129
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
130
+
131
+ # If you need to make complex sub-parts in the datasets with configurable options
132
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
133
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
134
+
135
+ # You will be able to load one or the other configurations in the following list with
136
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
137
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
138
+ # BUILDER_CONFIGS = [
139
+ # datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
140
+ # datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
141
+ # ]
142
+
143
+ # DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
144
+
145
+ def _info(self):
146
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
147
+ # if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
148
+ # features = datasets.Features(
149
+ # {
150
+ # "sentence": datasets.Value("string"),
151
+ # "option1": datasets.Value("string"),
152
+ # "answer": datasets.Value("string")
153
+ # # These are the features of your dataset like images, labels ...
154
+ # }
155
+ # )
156
+ # else: # This is an example to show how to have different features for "first_domain" and "second_domain"
157
+ # features = datasets.Features(
158
+ # {
159
+ # "sentence": datasets.Value("string"),
160
+ # "option2": datasets.Value("string"),
161
+ # "second_domain_answer": datasets.Value("string")
162
+ # # These are the features of your dataset like images, labels ...
163
+ # }
164
+ # )
165
+
166
+ features = datasets.Features(
167
+ {
168
+ "image_uid": datasets.Value("string"),
169
+ "image_path": datasets.Value("string"),
170
+ "depth_path": datasets.Value("string"),
171
+ "mask_path": datasets.Value("string"),
172
+ "affordance_type": datasets.Value("string"),
173
+ "original_divisions": {
174
+ "divide_1": datasets.Value("string"),
175
+ "divide_2": datasets.Value("string"),
176
+ "divide_3": datasets.Value("string")
177
+ }
178
+ }
179
+ )
180
+
181
+ return datasets.DatasetInfo(
182
+ # This is the description that will appear on the datasets page.
183
+ description=_DESCRIPTION,
184
+ # This defines the different columns of the dataset and their types
185
+ features=features, # Here we define them above because they are different between the two configurations
186
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
187
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
188
+ # supervised_keys=("sentence", "label"),
189
+ # Homepage of the dataset for documentation
190
+ homepage=_HOMEPAGE,
191
+ # License for the dataset if available
192
+ license=_LICENSE,
193
+ # Citation for the dataset
194
+ citation=_CITATION,
195
+ )
196
+
197
+ def _split_generators(self, dl_manager):
198
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
199
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
200
+
201
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
202
+ # 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.
203
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
204
+
205
+ # urls = _URLS[self.config.name]
206
+ # data_dir = dl_manager.download_and_extract(urls)
207
+
208
+ cache_dir = dl_manager.download_config.cache_dir # use the huggingface cache dir
209
+ zip_file_path = os.path.join(cache_dir, "PADv2_part1.zip")
210
+
211
+ # download the zip file from Box if not exist
212
+ if not os.path.exists(zip_file_path):
213
+ print("downloading to:", zip_file_path)
214
+ _download_box_file(_URLS["val"], zip_file_path)
215
+
216
+ # unzip folder if not exist
217
+ Padv2_part1_unzipped_root = os.path.join(os.path.dirname(zip_file_path), "extracted", "PADv2_part1")
218
+ if not os.path.exists(Padv2_part1_unzipped_root):
219
+ print("unzipping to:", Padv2_part1_unzipped_root)
220
+ with ZipFile(zip_file_path, 'r') as zObject:
221
+ zObject.extractall(path=os.path.dirname(Padv2_part1_unzipped_root))
222
+
223
+ downloaded_files = {'val': os.path.join(Padv2_part1_unzipped_root)}
224
+ # downloaded_files = dl_manager.download_and_extract(_URLS)
225
+ print("downloaded_files:", downloaded_files)
226
+
227
+ return [
228
+ datasets.SplitGenerator(
229
+ name=datasets.Split.VALIDATION,
230
+ # These kwargs will be passed to _generate_examples
231
+ gen_kwargs={
232
+ "filepath": downloaded_files["val"],
233
+ "split": "val",
234
+ },
235
+ )
236
+ ]
237
+
238
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
239
+ def _generate_examples(self, filepath, split):
240
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
241
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
242
+
243
+ # the filepath points to the unzipped Padv2_part1 folder
244
+ if split == "val":
245
+ data = _load_padv2_part1_instances(filepath)
246
+ for key, row in data.items():
247
+ yield key, {
248
+ "image_uid": key,
249
+ "image_path": row['image_path'],
250
+ "depth_path": row['depth_path'],
251
+ "mask_path": row['mask_path'],
252
+ "affordance_type": row['affordance_type'],
253
+ "original_divisions": row['original_divisions']
254
+ }