dali-does commited on
Commit
2299b0b
1 Parent(s): 56dc17d

Added loading script and metadata

Browse files
Files changed (2) hide show
  1. clevr-math.py +156 -0
  2. dataset_infos.json +1 -0
clevr-math.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """CLEVR-math dataset of arithmetic operations for visual reasoning"""
15
+
16
+
17
+ import csv
18
+ import json
19
+ import os
20
+ import pandas
21
+ import datasets
22
+
23
+
24
+ # Find for instance the citation on arxiv or on the dataset repo/website
25
+ _CITATION = """\
26
+ @misc{https://doi.org/10.48550/arxiv.2208.05358,
27
+ doi = {10.48550/ARXIV.2208.05358},
28
+ url = {https://arxiv.org/abs/2208.05358},
29
+ author = {Lindström, Adam Dahlgren and Abraham, Savitha Sam},
30
+ keywords = {Machine Learning (cs.LG), Computation and Language (cs.CL), Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences, I.2.7; I.2.10; I.2.6; I.4.8; I.1.4},
31
+ title = {CLEVR-Math: A Dataset for Compositional Language, Visual, and Mathematical Reasoning},
32
+ publisher = {arXiv},
33
+ year = {2022},
34
+ copyright = {Creative Commons Attribution Share Alike 4.0 International}
35
+ }
36
+ """
37
+
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ CLEVR-Math is a dataset for compositional language, visual and mathematical reasoning. CLEVR-Math poses questions about mathematical operations on visual scenes using subtraction and addition, such as "Remove all large red cylinders. How many objects are left?". There are also adversarial (e.g. "Remove all blue cubes. How many cylinders are left?") and multihop questions (e.g. "Remove all blue cubes. Remove all small purple spheres. How many objects are left?").
41
+ """
42
+
43
+ _HOMEPAGE = "https://people.cs.umu.se/dali/clevr-math"
44
+
45
+ _LICENSE = "Creative Commons Attribution Share Alike 4.0 International"
46
+
47
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
48
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
49
+ _URLS = {
50
+ "general": "data/clevr-math.zip",
51
+ "multihop": "data/clevr-math-multihop.zip",
52
+ "images": "https://dl.fbaipublicfiles.com/clevr/CLEVR_v1.0.zip",
53
+ "test_images": "data/clevr-math-test-images.zip",
54
+ }
55
+
56
+
57
+ class ClevrMath(datasets.GeneratorBasedBuilder):
58
+ """Generator for CLEVR-Math used to test compositional language, visual, and mathematical reasoning. Contains two versions of the dataset, one general and one where multihop questions are only available in the test data to provide a zero-shot setting."""
59
+
60
+ VERSION = datasets.Version("1.1.0")
61
+
62
+ # You will be able to load one or the other configurations in the following list with
63
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
64
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
65
+ BUILDER_CONFIGS = [
66
+ datasets.BuilderConfig(name="general", version=VERSION, description="CLEVR-math general"),
67
+ datasets.BuilderConfig(name="multihop", version=VERSION, description="CLEVR-math with single hop in train/val and multihop in test"),
68
+ ]
69
+
70
+ DEFAULT_CONFIG_NAME = "general"
71
+
72
+ def _info(self):
73
+ features = datasets.Features(
74
+ {
75
+ "template": datasets.Value("string"),
76
+ "id": datasets.Value("string"),
77
+ "question": datasets.Value("string"),
78
+ "image": datasets.Image(),
79
+ "label": datasets.Value("int64")
80
+ }
81
+ )
82
+ return datasets.DatasetInfo(
83
+ # This is the description that will appear on the datasets page.
84
+ description=_DESCRIPTION,
85
+ # This defines the different columns of the dataset and their types
86
+ features=features, # Here we define them above because they are different between the two configurations
87
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
88
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
89
+ # supervised_keys=("sentence", "label"),
90
+ # Homepage of the dataset for documentation
91
+ homepage=_HOMEPAGE,
92
+ # License for the dataset if available
93
+ license=_LICENSE,
94
+ # Citation for the dataset
95
+ citation=_CITATION,
96
+ )
97
+
98
+ def _split_generators(self, dl_manager):
99
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
100
+
101
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
102
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
103
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
104
+ data_url = _URLS[self.config.name]
105
+ data_dir = dl_manager.download_and_extract(data_url)
106
+ image_dir = dl_manager.download_and_extract(_URLS['images'])
107
+ image_dir_test = dl_manager.download_and_extract(_URLS['test_images'])
108
+ return [
109
+ datasets.SplitGenerator(
110
+ name=datasets.Split.TRAIN,
111
+ # These kwargs will be passed to _generate_examples
112
+ gen_kwargs={
113
+ "filepath": os.path.join(data_dir, "clevr-math-train.json"),
114
+ "imgpath": image_dir,
115
+ "split": "train",
116
+ },
117
+ ),
118
+ datasets.SplitGenerator(
119
+ name=datasets.Split.TEST,
120
+ # These kwargs will be passed to _generate_examples
121
+ gen_kwargs={
122
+ "filepath": os.path.join(data_dir, "clevr-math-test.json"),
123
+ "imgpath": image_dir_test,
124
+ "split": "test"
125
+ },
126
+ ),
127
+ datasets.SplitGenerator(
128
+ name=datasets.Split.VALIDATION,
129
+ # These kwargs will be passed to _generate_examples
130
+ gen_kwargs={
131
+ "filepath": os.path.join(data_dir, "clevr-math-val.json"),
132
+ "imgpath": image_dir,
133
+ "split": "val",
134
+ },
135
+ ),
136
+ ]
137
+
138
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
139
+ def _generate_examples(self, filepath, imgpath, split):
140
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
141
+ with open(filepath, encoding="utf-8") as f:
142
+ json_file = json.load(f)
143
+ df = pandas.json_normalize(json_file['questions'])
144
+ for key, sample in df.iterrows():
145
+ img_path = os.path.join(imgpath,
146
+ "CLEVR_v1.0/images",
147
+ sample['split'],
148
+ sample['image_filename'])
149
+
150
+ yield key, {
151
+ "template": sample['template_filename'][:-5],
152
+ "id": sample['image_filename'],
153
+ "question": sample["question"],
154
+ "image": img_path,
155
+ "label": sample["answer"]
156
+ }
dataset_infos.json ADDED
@@ -0,0 +1 @@
 
1
+ {".": {"description": "", "citation": "", "homepage": "", "license": "", "features": {"image": {"decode": true, "id": null, "_type": "Image"}, "label": {"num_classes": 1, "names": ["test"], "id": null, "_type": "ClassLabel"}}, "post_processed": null, "supervised_keys": null, "task_templates": [{"task": "image-classification", "image_column": "image", "label_column": "label"}], "builder_name": "image_folder", "config_name": ".", "version": {"version_str": "0.0.0", "description": null, "major": 0, "minor": 0, "patch": 0}, "splits": {"test": {"name": "test", "num_bytes": 184125, "num_examples": 1000, "dataset_name": "image_folder"}}, "download_checksums": {"/home/dali/work/phd/projects/clevr-math-problems/clevr-dataset-gen/clevr-math-fixed/data/clevr-math-test-images.zip": {"num_bytes": 96381360, "checksum": "823ba63880b84fb753bd77ee8e58368a465e69ade259988c69d78eab61302f88"}}, "download_size": 96381360, "post_processing_size": null, "dataset_size": 184125, "size_in_bytes": 96565485}}