File size: 5,522 Bytes
8821bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dee13c
8821bf7
 
 
 
 
 
 
 
 
 
2dee13c
8821bf7
 
2dee13c
 
 
8821bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
085424d
8821bf7
085424d
 
 
8821bf7
 
 
 
 
 
 
 
 
 
 
085424d
8821bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
import csv
import json
import os

import datasets
from PIL import Image
import tarfile
from io import BytesIO

import logging
import datasets
from datasets.utils.logging import get_logger, tqdm

logger = get_logger(__name__)          # <- same pattern used in HF scripts


_DESCRIPTION = "WikiChurches is a dataset for architectural style classification"
_HOMEPAGE = "https://huggingface.co/datasets/LZSI-2/WikiChurches"
_LICENSE = "cc-by-nc-4.0"
_CITATION = """@article{barz2021wikichurches,
    title={WikiChurches: A Fine-Grained Dataset of Architectural Styles with Real-World Challenges},
    author={Barz, Björn and Denzler, Joachim},
    journal={arXiv preprint arXiv:2108.06959},
    year={2021}
}"""

TAR_PARTS = 40

_TAR_URLS =[f"https://huggingface.co/datasets/LZSI-2/WikiChurches/resolve/main/images/images_part{part_id}.tar" for part_id in range(1, TAR_PARTS + 1)]

_URLS = {
    "churches_json": "https://huggingface.co/datasets/LZSI-2/WikiChurches/resolve/main/churches.json",
    "style_levels": "https://huggingface.co/datasets/LZSI-2/WikiChurches/resolve/main/style_levels.json",
    "style_names": "https://huggingface.co/datasets/LZSI-2/WikiChurches/resolve/main/style_names.txt",
}

class SacredBuildingsDataset(datasets.GeneratorBasedBuilder):
    """Sacred Buildings Dataset with support for different hierarchy levels."""

    VERSION = datasets.Version("0.0.1")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="wc4", 
            version=VERSION, 
            description="Wiki Churches 4 main classes"
        ),
        datasets.BuilderConfig(
            name="wc6", 
            version=VERSION, 
            description="Wiki Churches 6 largest classes"
        ),
        datasets.BuilderConfig(
            name="wc14", 
            version=VERSION, 
            description="Wiki Churches 14 first-level classes"
        ),
    ]

    DEFAULT_CONFIG_NAME = "wc4"

    def _info(self):
        features = datasets.Features({
            "image": datasets.Image(),
            "style": datasets.Value("string"),
            "church_id": datasets.Value("string"),

        })

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


    def _get_style_names(self, style_levels):
        return style_levels.get(self.config.name, {})

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""

        downloaded_files = dl_manager.download(_TAR_URLS)
        data_dir = dl_manager.download_and_extract(_URLS)


        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "file_paths": downloaded_files,
                    "data_dir": data_dir,
                },
            ),
        ]


    def _trace_to_top_parent(self, style_id, hierarchy):
        while style_id in hierarchy and hierarchy[style_id]["parent"]:
            style_id = hierarchy[style_id]["parent"]
        return style_id

    def _generate_examples(self, file_paths, data_dir):
        with open(data_dir["churches_json"], "r") as f:
            churches_data = json.load(f)

        with open(data_dir["style_levels"], "r") as f:
            style_levels = json.load(f)

        
        style_levels = json.load(open(data_dir["style_levels"], "r"))
        if self.config.name == "wc4":
            domain_styles = list(style_levels["statistics"]["styles_by_size"].keys())[:5]
        elif self.config.name == "wc6":
            domain_styles = list(style_levels["statistics"]["styles_by_size"].keys())[:7]
        elif self.config.name == "wc14":
            domain_styles = list(style_levels["statistics"]["styles_by_size"].keys())[:15]

  

        """Yields examples."""
        # Load churches data
        for filepath in file_paths:
            logger.info(f"Processing {filepath.split('/')[-1]}")
            with tarfile.open(filepath, "r") as tar:
                for member in tqdm(tar.getmembers(),
                                desc=f"{os.path.basename(filepath)}"):
                    if member.isfile():
                        logger.debug(f"Processing {member.name}")

                        image_file = tar.extractfile(member)


                        church_id = member.name.split("_")[0]
                        image_name = member.name
                        if church_id not in churches_data:
                            continue

                        church_styles = churches_data[church_id]["styles"]

                        top_level_set = set()
                        for s_id in church_styles:
                            top_parent = self._trace_to_top_parent(s_id, style_levels["hierarchy"])
                            if top_parent in domain_styles:
                                top_level_set.add(top_parent)

                        if len(top_level_set) == 1:
                            label_name = top_level_set.pop()

                            with Image.open(image_file) as image:
                                image = image.convert("RGB")
                                yield image_name, {
                                    "image": image,
                                    "style": label_name,
                                    "church_id": church_id,
                                }
                        else:
                            continue