File size: 5,609 Bytes
933ec1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e0bad4
933ec1f
7da3ca9
933ec1f
 
 
 
 
 
5156d02
933ec1f
 
 
 
 
 
 
5156d02
933ec1f
 
 
 
 
 
 
 
 
 
 
 
 
5156d02
933ec1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94ade36
a990c51
3797893
eae6ec5
3797893
 
 
94ade36
933ec1f
3797893
6e0bad4
933ec1f
 
 
 
 
6e0bad4
 
933ec1f
 
 
 
6e0bad4
933ec1f
 
 
 
6e0bad4
933ec1f
 
 
 
 
 
 
 
 
 
6e0bad4
933ec1f
 
 
 
 
 
 
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
"""
This is the huggingface data loader for TOPVIEWRS Benchmark.
"""

import json
import os
import shutil

import datasets


_CITATION = """
@misc{li2024topviewrs,
        title={TopViewRS: Vision-Language Models as Top-View Spatial Reasoners},
        author={Chengzu Li and Caiqi Zhang and Han Zhou and Nigel Collier and Anna Korhonen and Ivan Vulić},
        year={2024},
        eprint={2406.02537},
        archivePrefix={arXiv},
        primaryClass={cs.CL}
}
"""

_DESCRIPTION = """
TopViewRS dataset, comprising 11,384 multiple-choice questions with either photo-realistic 
or semantic top-view maps of real-world scenarios through a pipeline of automatic collection followed by human alignment.
"""

_HOMEPAGE = "https://topviewrs.github.io/"

_LICENSE = "MIT"

TASK_SPLIT = ['top_view_recognition', 'top_view_localization', 'static_spatial_reasoning', 'dynamic_spatial_reasoning']

_URLS = {
    "realistic_json": f"released_realistic_datasets.json",
    "semantic_json": f"released_semantic_datasets.json",
    "images": f"data.zip"
}


class TOPVIEWRSConfig(datasets.BuilderConfig):
    """BuilderConfig for TOPVIEWRS."""

    def __init__(self, task_split, map_type, image_save_dir, **kwargs):
        """BuilderConfig for TOPVIEWRS.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(TOPVIEWRSConfig, self).__init__(**kwargs)
        self.task_split = task_split
        self.map_type = map_type
        self.image_save_dir = image_save_dir


class TOPVIEWRS(datasets.GeneratorBasedBuilder):
    """TOPVIEWRS Dataset"""

    BUILDER_CONFIG_CLASS = TOPVIEWRSConfig
    BUILDER_CONFIGS = [
        TOPVIEWRSConfig(
            name="topviewrs",
            version=datasets.Version("0.0.0"),
            description=_DESCRIPTION,
            task_split=None,
            map_type=None,
            image_save_dir="."
        )
    ]

    DEFAULT_CONFIG_NAME = "topviewrs"

    def _info(self):
        features = datasets.Features(
            {
                "index": datasets.Value("int32"),
                "scene_id": datasets.Value("string"),
                "question": datasets.Value("string"),
                "choices": datasets.Sequence(datasets.Value("string")),
                "labels": datasets.Sequence(datasets.Value("string")),
                "choice_type": datasets.Value("string"),
                "map_path": datasets.Value("string"),
                "question_ability": datasets.Value("string"),
            }
        )
        if self.config.task_split == "dynamic_spatial_reasoning":
            features = datasets.Features(
                {
                    "index": datasets.Value("int32"),
                    "scene_id": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "choices": datasets.Sequence(datasets.Value("string")),
                    "labels": datasets.Sequence(datasets.Value("string")),
                    "choice_type": datasets.Value("string"),
                    "map_path": datasets.Value("string"),
                    "question_ability": datasets.Value("string"),
                    "reference_path": datasets.Sequence(datasets.Sequence(datasets.Value("int32")))
                }
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        zip_file = dl_manager.download({"images": _URLS['images']})
        os.rename(zip_file['images'], os.path.join(os.path.dirname(zip_file['images']), _URLS['images']))
        try:
            shutil.unpack_archive(os.path.join(os.path.dirname(zip_file['images']), _URLS['images']), self.config.image_save_dir)
        except:
            raise FileNotFoundError(f"Unpacking the image data.zip failed. Make sure that you have the zip file at {zip_file}. ")
        
        downloaded_files = dl_manager.download_and_extract({k: v for k, v in _URLS.items() if k != "images"})
        
        image_base_file_dir = self.config.image_save_dir
        json_file_path = downloaded_files[f"{self.config.map_type}_json"]

        return [
            datasets.SplitGenerator(
                name=datasets.Split('val'),
                gen_kwargs={
                    "json_file_path": json_file_path,
                    "image_base_dir": image_base_file_dir
                },
            )
        ]

    def _generate_examples(self, json_file_path: str, image_base_dir: str):
        task = self.config.task_split
        map_type = self.config.map_type

        map_key = "rgb" if map_type.lower() == "realistic" else map_type
        with open(json_file_path) as f:
            data_list = json.load(f)[task]

        for idx, data_item in enumerate(data_list):
            return_item = {
                "index": idx,
                "scene_id": data_item['scene_id'],
                "question": data_item['question'],
                "choices": data_item['choices'],
                "labels": data_item['labels'],
                "choice_type": str(data_item["question_meta_data"]["choices"]),
                "map_path": os.path.join(image_base_dir, data_item[f"{map_key}_map"]),
                "question_ability": data_item['ability'],
            }
            if "reference_path" in data_item.keys():
                return_item["reference_path"] = data_item["reference_path"]

            yield idx, return_item
            idx += 1