Thinh Huynh Nguyen Truong
commited on
Commit
·
6896071
1
Parent(s):
cf18101
Update
Browse files- requirements.txt +2 -0
- test.py +47 -35
requirements.txt
CHANGED
@@ -1 +1,3 @@
|
|
|
|
1 |
datasets
|
|
|
|
1 |
+
# Python 3.10.6
|
2 |
datasets
|
3 |
+
Pillow
|
test.py
CHANGED
@@ -18,6 +18,8 @@
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
|
|
|
|
21 |
|
22 |
import datasets
|
23 |
from datasets import DownloadManager
|
@@ -56,6 +58,19 @@ _URLS = {
|
|
56 |
_BASE_URL = ""
|
57 |
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
60 |
class Test(datasets.GeneratorBasedBuilder):
|
61 |
"""TODO: Short description of my dataset."""
|
@@ -98,6 +113,7 @@ class Test(datasets.GeneratorBasedBuilder):
|
|
98 |
"depth": datasets.Image(),
|
99 |
"rgb": datasets.Image(),
|
100 |
"gt": datasets.Image(),
|
|
|
101 |
# These are the features of your dataset like images, labels ...
|
102 |
}
|
103 |
), # Here we define them above because they are different between the two configurations
|
@@ -121,52 +137,48 @@ class Test(datasets.GeneratorBasedBuilder):
|
|
121 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
122 |
urls = _URLS[self.config.name]
|
123 |
data_dir = dl_manager.download_and_extract(urls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
return [
|
125 |
datasets.SplitGenerator(
|
126 |
name=datasets.Split.TRAIN,
|
127 |
# These kwargs will be passed to _generate_examples
|
128 |
-
gen_kwargs={
|
129 |
-
"filepath": os.path.join(data_dir, "train.jsonl"),
|
130 |
-
"split": "train",
|
131 |
-
},
|
132 |
),
|
133 |
datasets.SplitGenerator(
|
134 |
name=datasets.Split.VALIDATION,
|
135 |
# These kwargs will be passed to _generate_examples
|
136 |
-
gen_kwargs={
|
137 |
-
"filepath": os.path.join(data_dir, "dev.jsonl"),
|
138 |
-
"split": "dev",
|
139 |
-
},
|
140 |
-
),
|
141 |
-
datasets.SplitGenerator(
|
142 |
-
name=datasets.Split.TEST,
|
143 |
-
# These kwargs will be passed to _generate_examples
|
144 |
-
gen_kwargs={
|
145 |
-
"filepath": os.path.join(data_dir, "test.jsonl"),
|
146 |
-
"split": "test",
|
147 |
-
},
|
148 |
),
|
|
|
|
|
|
|
|
|
|
|
149 |
]
|
150 |
|
151 |
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
152 |
-
def _generate_examples(self,
|
153 |
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
154 |
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
155 |
-
with open(
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
"second_domain_answer": ""
|
170 |
-
if split == "test"
|
171 |
-
else data["second_domain_answer"],
|
172 |
-
}
|
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
21 |
+
from typing import Dict, List
|
22 |
+
from PIL import Image
|
23 |
|
24 |
import datasets
|
25 |
from datasets import DownloadManager
|
|
|
58 |
_BASE_URL = ""
|
59 |
|
60 |
|
61 |
+
def get_download_url(config_name: str, partition: str) -> str:
|
62 |
+
"""Get download URL based on config name and parition (train/dev/test)
|
63 |
+
|
64 |
+
Args:
|
65 |
+
config_name (str): can be "v1", "v2",...
|
66 |
+
partition (str): can be "train", "dev" or "test"
|
67 |
+
|
68 |
+
Returns:
|
69 |
+
str: URL to download file
|
70 |
+
"""
|
71 |
+
return f"https://huggingface.co/datasets/RGBD-SOD/test/tree/main/data{config_name}/{partition}.zip"
|
72 |
+
|
73 |
+
|
74 |
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
75 |
class Test(datasets.GeneratorBasedBuilder):
|
76 |
"""TODO: Short description of my dataset."""
|
|
|
113 |
"depth": datasets.Image(),
|
114 |
"rgb": datasets.Image(),
|
115 |
"gt": datasets.Image(),
|
116 |
+
"name": datasets.Value("string"),
|
117 |
# These are the features of your dataset like images, labels ...
|
118 |
}
|
119 |
), # Here we define them above because they are different between the two configurations
|
|
|
137 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
138 |
urls = _URLS[self.config.name]
|
139 |
data_dir = dl_manager.download_and_extract(urls)
|
140 |
+
train_dir = dl_manager.download_and_extract(
|
141 |
+
get_download_url(self.config.name, "train")
|
142 |
+
)
|
143 |
+
dev_dir = dl_manager.download_and_extract(
|
144 |
+
get_download_url(self.config.name, "dev")
|
145 |
+
)
|
146 |
+
# test_dir = dl_manager.download_and_extract(
|
147 |
+
# get_download_url(self.config.name, "test")
|
148 |
+
# )
|
149 |
return [
|
150 |
datasets.SplitGenerator(
|
151 |
name=datasets.Split.TRAIN,
|
152 |
# These kwargs will be passed to _generate_examples
|
153 |
+
gen_kwargs={"dir_path": train_dir},
|
|
|
|
|
|
|
154 |
),
|
155 |
datasets.SplitGenerator(
|
156 |
name=datasets.Split.VALIDATION,
|
157 |
# These kwargs will be passed to _generate_examples
|
158 |
+
gen_kwargs={"dir_path": dev_dir},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
),
|
160 |
+
# datasets.SplitGenerator(
|
161 |
+
# name=datasets.Split.TEST,
|
162 |
+
# # These kwargs will be passed to _generate_examples
|
163 |
+
# gen_kwargs={"dir_path": test_dir},
|
164 |
+
# ),
|
165 |
]
|
166 |
|
167 |
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
168 |
+
def _generate_examples(self, dir_path: str):
|
169 |
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
170 |
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
171 |
+
with open(os.path.join(dir_path, "metadata.json"), "r") as f:
|
172 |
+
json_object = json.load(f)
|
173 |
+
|
174 |
+
metadata: List[Dict[str, str]] = json_object["metadata"]
|
175 |
+
|
176 |
+
for key, row in enumerate(metadata):
|
177 |
+
yield key, {
|
178 |
+
"name": row["name"],
|
179 |
+
"rgb": Image.open(os.path.join(dir_path, metadata["rgb"]), mode="RGB"),
|
180 |
+
"gt": Image.open(os.path.join(dir_path, metadata["gt"]), mode="L"),
|
181 |
+
"depth": Image.open(
|
182 |
+
os.path.join(dir_path, metadata["depth"]), mode="L"
|
183 |
+
),
|
184 |
+
}
|
|
|
|
|
|
|
|