Datasets:

toniwuest commited on
Commit
71f6c3a
·
verified ·
1 Parent(s): 018e07d

Upload sudoku_dataset_builder.py

Browse files
Files changed (1) hide show
  1. sudoku_dataset_builder.py +150 -0
sudoku_dataset_builder.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import datasets
5
+
6
+ _DESCRIPTION = """\
7
+ CLEVR-Sudoku is a dataset for the task of Sudoku puzzle solving. It is a synthetic dataset generated using the CLEVR engine. The dataset consists of 3x3 Sudoku puzzles with varying levels of difficulty. The dataset is divided into three categories based on the number of known cells in the puzzle: Easy (K10), Medium (K30), and Hard (K50). Each puzzle is accompanied by a set of 10 possible solutions. The dataset is generated using the CLEVR engine and is available in the form of images and JSON files. The images are 256x256 pixels in size and are stored in the PNG format. The JSON files contain the puzzle, the solution, and the possible solutions in the form of a dictionary. The dataset is available for download in the form of a zip file. The dataset is intended for use in the development of machine learning models for the task of Sudoku puzzle solving.
8
+ """
9
+
10
+ _CITATION = """\
11
+ @article{stammer2024neural,
12
+ title={Neural Concept Binder},
13
+ author={Stammer, Wolfgang and W{\"u}st, Antonia and Steinmann, David and Kersting, Kristian},
14
+ journal={Advances in Neural Information Processing Systems},
15
+ year={2024}
16
+ }"""
17
+
18
+ _HOME_PAGE = "https://ml-research.github.io/NeuralConceptBinder/"
19
+ _IMAGES_URL = "https://huggingface.co/datasets/AIML-TUDA/CLEVR-Sudoku/blob/main"
20
+ _LICENSE = "cc-by-4.0"
21
+ _DIR = _IMAGES_URL
22
+
23
+ _URL_DATA = {
24
+ "CLEVR-Easy-K10": [f"{_DIR}/CLEVR-Easy-Sudoku-K10.zip", f"{_DIR}/CLEVR-Easy-1.zip"],
25
+ "CLEVR-Easy-K30": [f"{_DIR}/CLEVR-Easy-Sudoku-K30.zip", f"{_DIR}/CLEVR-Easy-1.zip"],
26
+ "CLEVR-Easy-K50": [f"{_DIR}/CLEVR-Easy-Sudoku-K50.zip", f"{_DIR}/CLEVR-Easy-1.zip"],
27
+ "CLEVR-4-K10": [f"{_DIR}/CLEVR-4-Sudoku-K10.zip", f"{_DIR}/sudoku.zip"],
28
+ "CLEVR-4-K30": [f"{_DIR}/CLEVR-4-Sudoku-K30.zip", f"{_DIR}/sudoku.zip"],
29
+ "CLEVR-4-K50": [f"{_DIR}/CLEVR-4-Sudoku-K50.zip", f"{_DIR}/sudoku.zip"],
30
+ }
31
+
32
+
33
+ class CLEVRSudokuConfig(datasets.BuilderConfig):
34
+ """Builder Config for CLEVR-Sudoku."""
35
+
36
+ def __init__(self, data_url, **kwargs):
37
+ """Builder Config for CLEVR-Sudoku.
38
+ Args:
39
+ metadata_urls: dictionary with keys 'train' and 'validation' containing the archive metadata URLs
40
+ **kwargs: keyword arguments forwarded to super.
41
+ """
42
+ super(CLEVRSudokuConfig, self).__init__(
43
+ version=datasets.Version("1.0.0"), **kwargs
44
+ )
45
+ # if isinstance(data_url, dict):
46
+ # self.metadata_urls = data_url
47
+ # else:
48
+ self.data_url = data_url
49
+ self.metadata_urls = {"train": data_url, "test": None}
50
+
51
+
52
+ class CLEVRSudoku(datasets.GeneratorBasedBuilder):
53
+
54
+ BUILDER_CONFIGS = [
55
+ CLEVRSudokuConfig(
56
+ name=name, description=name, data_url=urls[0], image_url=urls[1]
57
+ )
58
+ for name, urls in _URL_DATA.items()
59
+ ]
60
+
61
+ def _info(self):
62
+ return datasets.DatasetInfo(
63
+ description=_DESCRIPTION,
64
+ features=datasets.Features(
65
+ {
66
+ # TODO: Add features
67
+ "sudoku": datasets.Array2D(shape=(9, 9), dtype=datasets.Image()),
68
+ "options": datasets.Array2D(shape=(9, 10), dtype=datasets.Image()),
69
+ # attributes as dict of features
70
+ "attributes": datasets.Value("dict"),
71
+ "label": datasets.Array2D(shape=(9, 9), dtype="int32"),
72
+ }
73
+ ),
74
+ supervised_keys=("image", "label"),
75
+ homepage=_HOME_PAGE,
76
+ citation=_CITATION,
77
+ license=_LICENSE,
78
+ )
79
+
80
+ def get_data(self, dl_manager, url, image_url):
81
+ archive_path = os.path.join(
82
+ dl_manager.download_and_extract(url), url.split("/")[-1].split(".")[0]
83
+ )
84
+ image_path = os.path.join(
85
+ dl_manager.download_and_extract(image_url),
86
+ image_url.split("/")[-1].split(".")[0],
87
+ )
88
+
89
+ json_dir = os.path.join(archive_path, "json")
90
+ # get all files of the json directory
91
+ json_files = os.listdir(json_dir)
92
+
93
+ # sort the files
94
+ json_files.sort()
95
+
96
+ sudokus, options, labels, attributes = [], [], [], []
97
+
98
+ # load data
99
+ for json_file in json_files:
100
+ with open(os.path.join(json_dir, json_file), "r") as f:
101
+ data = json.load(f)
102
+ sudokus.append(data["images"])
103
+ options.append(data["options"])
104
+ labels.append(data["solution"])
105
+ attributes.append(data["map_number_to_attributes"])
106
+
107
+ return archive_path, sudokus, options, labels, attributes
108
+
109
+ def _split_generators(self, dl_manager):
110
+
111
+ # TODO: define image directory
112
+ meta_data_path = dl_manager.download_and_extract(self.config.data_url)
113
+
114
+ archive_path, sudokus, options, labels, attributes = self.get_data(
115
+ dl_manager, self.config.data_url
116
+ )
117
+
118
+ return [
119
+ datasets.SplitGenerator(
120
+ name=datasets.Split.TRAIN,
121
+ gen_kwargs={
122
+ "image_dir": meta_data_path,
123
+ "sudokus": sudokus,
124
+ "options": options,
125
+ "labels": labels,
126
+ "attributes": attributes,
127
+ },
128
+ ),
129
+ datasets.SplitGenerator(
130
+ name=datasets.Split.TEST,
131
+ gen_kwargs={
132
+ "image_dir": meta_data_path,
133
+ "sudokus": sudokus,
134
+ "options": options,
135
+ "labels": labels,
136
+ "attributes": attributes,
137
+ },
138
+ ),
139
+ ]
140
+
141
+ def _generate_examples(self, image_dir, sudokus, options, labels, attributes):
142
+ for i, (sudoku, opt, label, attr) in enumerate(
143
+ zip(sudokus, options, labels, attributes)
144
+ ):
145
+ yield i, {
146
+ "sudoku": sudoku,
147
+ "options": opt,
148
+ "label": label,
149
+ "attributes": attr,
150
+ }