Datasets:
Chengzu Li
commited on
Upload topviewrs.py
Browse files- topviewrs.py +152 -0
topviewrs.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This is the huggingface data loader for TOPVIEWRS Benchmark.
|
3 |
+
"""
|
4 |
+
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
import shutil
|
8 |
+
|
9 |
+
import datasets
|
10 |
+
|
11 |
+
|
12 |
+
_CITATION = """
|
13 |
+
@misc{li2024topviewrs,
|
14 |
+
title={TopViewRS: Vision-Language Models as Top-View Spatial Reasoners},
|
15 |
+
author={Chengzu Li and Caiqi Zhang and Han Zhou and Nigel Collier and Anna Korhonen and Ivan Vulić},
|
16 |
+
year={2024},
|
17 |
+
eprint={2406.02537},
|
18 |
+
archivePrefix={arXiv},
|
19 |
+
primaryClass={cs.CL}
|
20 |
+
}
|
21 |
+
"""
|
22 |
+
|
23 |
+
_DESCRIPTION = """
|
24 |
+
TopViewRS dataset, comprising 11,384 multiple-choice questions with either photo-realistic
|
25 |
+
or semantic top-view maps of real-world scenarios through a pipeline of automatic collection followed by human alignment.
|
26 |
+
"""
|
27 |
+
|
28 |
+
_HOMEPAGE = "https://topviewrs.github.io/"
|
29 |
+
|
30 |
+
_LICENSE = "MIT"
|
31 |
+
|
32 |
+
TASK_SPLIT = ['top_view_recognition', 'top_view_localization', 'static_spatial_reasoning', 'dynamic_spatial_reasoning']
|
33 |
+
|
34 |
+
_URLS = {
|
35 |
+
"rgb_json": f"released_realistic_datasets.json",
|
36 |
+
"semantic_json": f"released_semantic_datasets.json",
|
37 |
+
"images": f"release_data.zip"
|
38 |
+
}
|
39 |
+
|
40 |
+
|
41 |
+
class TOPVIEWRSConfig(datasets.BuilderConfig):
|
42 |
+
"""BuilderConfig for TOPVIEWRS."""
|
43 |
+
|
44 |
+
def __init__(self, task_split, map_type, **kwargs):
|
45 |
+
"""BuilderConfig for TOPVIEWRS.
|
46 |
+
Args:
|
47 |
+
**kwargs: keyword arguments forwarded to super.
|
48 |
+
"""
|
49 |
+
super(TOPVIEWRSConfig, self).__init__(**kwargs)
|
50 |
+
self.task_split = task_split
|
51 |
+
self.map_type = map_type
|
52 |
+
|
53 |
+
|
54 |
+
class TOPVIEWRS(datasets.GeneratorBasedBuilder):
|
55 |
+
"""TOPVIEWRS Dataset"""
|
56 |
+
|
57 |
+
BUILDER_CONFIG_CLASS = TOPVIEWRSConfig
|
58 |
+
BUILDER_CONFIGS = [
|
59 |
+
TOPVIEWRSConfig(
|
60 |
+
name="topviewrs",
|
61 |
+
version=datasets.Version("0.0.0"),
|
62 |
+
description=_DESCRIPTION,
|
63 |
+
task_split=None,
|
64 |
+
map_type=None,
|
65 |
+
)
|
66 |
+
]
|
67 |
+
|
68 |
+
DEFAULT_CONFIG_NAME = "topviewrs"
|
69 |
+
|
70 |
+
def _info(self):
|
71 |
+
features = datasets.Features(
|
72 |
+
{
|
73 |
+
"index": datasets.Value("int32"),
|
74 |
+
"scene_id": datasets.Value("string"),
|
75 |
+
"question": datasets.Value("string"),
|
76 |
+
"choices": datasets.Sequence(datasets.Value("string")),
|
77 |
+
"labels": datasets.Sequence(datasets.Value("string")),
|
78 |
+
"choice_type": datasets.Value("string"),
|
79 |
+
"map_path": datasets.Value("string"),
|
80 |
+
"question_ability": datasets.Value("string"),
|
81 |
+
}
|
82 |
+
)
|
83 |
+
if self.config.task_split == "dynamic_spatial_reasoning":
|
84 |
+
features = datasets.Features(
|
85 |
+
{
|
86 |
+
"index": datasets.Value("int32"),
|
87 |
+
"scene_id": datasets.Value("string"),
|
88 |
+
"question": datasets.Value("string"),
|
89 |
+
"choices": datasets.Sequence(datasets.Value("string")),
|
90 |
+
"labels": datasets.Sequence(datasets.Value("string")),
|
91 |
+
"choice_type": datasets.Value("string"),
|
92 |
+
"map_path": datasets.Value("string"),
|
93 |
+
"question_ability": datasets.Value("string"),
|
94 |
+
"reference_path": datasets.Sequence(datasets.Sequence(datasets.Value("int32")))
|
95 |
+
}
|
96 |
+
)
|
97 |
+
|
98 |
+
return datasets.DatasetInfo(
|
99 |
+
description=_DESCRIPTION,
|
100 |
+
features=features,
|
101 |
+
supervised_keys=None,
|
102 |
+
homepage=_HOMEPAGE,
|
103 |
+
license=_LICENSE,
|
104 |
+
citation=_CITATION,
|
105 |
+
)
|
106 |
+
|
107 |
+
def _split_generators(self, dl_manager):
|
108 |
+
downloaded_files = _URLS
|
109 |
+
|
110 |
+
for k, v in downloaded_files.items():
|
111 |
+
if v.endswith("zip"):
|
112 |
+
try:
|
113 |
+
shutil.unpack_archive(v)
|
114 |
+
except:
|
115 |
+
raise FileNotFoundError(f"Unpacking the image data.zip failed. Make sure that you have enough space at {os.path.dirname(v)}. ")
|
116 |
+
|
117 |
+
base_file_dir = os.path.dirname(v)
|
118 |
+
|
119 |
+
return [
|
120 |
+
datasets.SplitGenerator(
|
121 |
+
name=datasets.Split('val'),
|
122 |
+
gen_kwargs={
|
123 |
+
"file_path": base_file_dir
|
124 |
+
},
|
125 |
+
)
|
126 |
+
]
|
127 |
+
|
128 |
+
def _generate_examples(self, file_path: str):
|
129 |
+
task = self.config.task_split
|
130 |
+
map_type = self.config.map_type
|
131 |
+
|
132 |
+
file_name = "RGB_datasets.json" if map_type.lower() == "realistic" else "semantic_datasets.json"
|
133 |
+
map_key = "rgb" if map_type.lower() == "realistic" else map_type
|
134 |
+
with open(os.path.join(file_path, file_name)) as f:
|
135 |
+
data_list = json.load(f)[task]
|
136 |
+
|
137 |
+
for idx, data_item in enumerate(data_list):
|
138 |
+
return_item = {
|
139 |
+
"index": idx,
|
140 |
+
"scene_id": data_item['scene_id'],
|
141 |
+
"question": data_item['question'],
|
142 |
+
"choices": data_item['choices'],
|
143 |
+
"labels": data_item['labels'],
|
144 |
+
"choice_type": str(data_item["question_meta_data"]["choices"]),
|
145 |
+
"map_path": os.path.join(file_path, data_item[f"{map_key}_map"]),
|
146 |
+
"question_ability": data_item['ability'],
|
147 |
+
}
|
148 |
+
if "reference_path" in data_item.keys():
|
149 |
+
return_item["reference_path"] = data_item["reference_path"]
|
150 |
+
|
151 |
+
yield idx, return_item
|
152 |
+
idx += 1
|