derek-thomas HF staff commited on
Commit
29afe55
1 Parent(s): 179a2d6

init commit

Browse files
Files changed (4) hide show
  1. .gitignore +4 -0
  2. ScienceQA.py +122 -0
  3. create_dataset.ipynb +0 -0
  4. download.sh +36 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .ipynb_checkpoints
2
+ .idea
3
+ images/
4
+ text/
ScienceQA.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from pathlib import Path
3
+
4
+ import datasets
5
+
6
+ _DESCRIPTION = """Science Question Answering (ScienceQA), a new benchmark that consists of 21,208 multimodal
7
+ multiple choice questions with a diverse set of science topics and annotations of their answers
8
+ with corresponding lectures and explanations.
9
+ The lecture and explanation provide general external knowledge and specific reasons,
10
+ respectively, for arriving at the correct answer."""
11
+
12
+ # Lets use the project page instead of the github repo
13
+ _HOMEPAGE = "https://scienceqa.github.io"
14
+
15
+ _CITATION = """\
16
+ @inproceedings{lu2022learn,
17
+ title={Learn to Explain: Multimodal Reasoning via Thought Chains for Science Question Answering},
18
+ author={Lu, Pan and Mishra, Swaroop and Xia, Tony and Qiu, Liang and Chang, Kai-Wei and Zhu, Song-Chun and Tafjord, Oyvind and Clark, Peter and Ashwin Kalyan},
19
+ booktitle={The 36th Conference on Neural Information Processing Systems (NeurIPS)},
20
+ year={2022}
21
+ }
22
+ """
23
+
24
+ _LICENSE = "Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)"
25
+
26
+
27
+ class ScienceQA(datasets.GeneratorBasedBuilder):
28
+ """Science Question Answering (ScienceQA), a new benchmark that consists of 21,208 multimodal
29
+ multiple choice questions with a diverse set of science topics and annotations of their answers
30
+ with corresponding lectures and explanations.
31
+ The lecture and explanation provide general external knowledge and specific reasons,
32
+ respectively, for arriving at the correct answer."""
33
+
34
+ VERSION = datasets.Version("1.0.0")
35
+
36
+ def _info(self):
37
+ return datasets.DatasetInfo(
38
+ description=_DESCRIPTION,
39
+ features=datasets.Features(
40
+ {
41
+ "image": datasets.Image(),
42
+ "question": datasets.Value("string"),
43
+ "choices": datasets.features.Sequence(datasets.Value("string")),
44
+ "answer": datasets.Value("int8"),
45
+ "hint": datasets.Value("string"),
46
+ "task": datasets.Value("string"),
47
+ "grade": datasets.Value("string"),
48
+ "subject": datasets.Value("string"),
49
+ "topic": datasets.Value("string"),
50
+ "category": datasets.Value("string"),
51
+ "skill": datasets.Value("string"),
52
+ "lecture": datasets.Value("string"),
53
+ "solution": datasets.Value("string")
54
+ }
55
+ ),
56
+ homepage=_HOMEPAGE,
57
+ citation=_CITATION,
58
+ license=_LICENSE,
59
+ )
60
+
61
+ def _split_generators(self, dl_manager):
62
+ text_path = Path.cwd() / 'text' / 'problems.json'
63
+ image_dir = Path.cwd() / 'images'
64
+ return [
65
+ datasets.SplitGenerator(
66
+ name=datasets.Split.TRAIN,
67
+ # These kwargs will be passed to _generate_examples
68
+ gen_kwargs={
69
+ "text_path": text_path,
70
+ "image_dir": image_dir,
71
+ "split": "train",
72
+ },
73
+ ),
74
+ datasets.SplitGenerator(
75
+ name=datasets.Split.VALIDATION,
76
+ # These kwargs will be passed to _generate_examples
77
+ gen_kwargs={
78
+ "text_path": text_path,
79
+ "image_dir": image_dir,
80
+ "split": "val",
81
+ },
82
+ ),
83
+ datasets.SplitGenerator(
84
+ name=datasets.Split.TEST,
85
+ # These kwargs will be passed to _generate_examples
86
+ gen_kwargs={
87
+ "text_path": text_path,
88
+ "image_dir": image_dir,
89
+ "split": "test"
90
+ },
91
+ ),
92
+ ]
93
+
94
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
95
+ def _generate_examples(self, text_path, image_dir, split):
96
+ with open(text_path, encoding="utf-8") as f:
97
+ # Load all the text. Note that if this was HUGE, we would need to find a better way to load the json
98
+ data = json.load(f)
99
+ ignore_keys = ['image', 'split']
100
+
101
+ # Get image_id from its annoying location
102
+ for image_id, row in data.items():
103
+ # Only look for the rows in our split
104
+ if row['split'] == split:
105
+
106
+ # Note, not all rows have images.
107
+ # Get all the image data we need
108
+ if row['image']:
109
+ image_path = image_dir / split / image_id / 'image.png'
110
+ image_bytes = image_path.read_bytes()
111
+ image_dict = {'path': str(image_path), 'bytes': image_bytes}
112
+ else:
113
+ image_dict = None
114
+
115
+ # Keep only the keys we need
116
+ relevant_row = {k: v for k, v in row.items() if k not in ignore_keys}
117
+
118
+ return_dict = {
119
+ 'image': image_dict,
120
+ **relevant_row
121
+ }
122
+ yield image_id, return_dict
create_dataset.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
download.sh ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # Modified from the original here: https://github.com/lupantech/ScienceQA/blob/main/tools/download.sh
3
+
4
+ cd images
5
+
6
+ if [ -d "train" ];
7
+ then
8
+ echo "Already downloaded train"
9
+ else
10
+ ls -alF
11
+ wget https://scienceqa.s3.us-west-1.amazonaws.com/images/train.zip
12
+ unzip -q train.zip
13
+ rm train.zip
14
+ fi
15
+
16
+ if [ -d "val" ];
17
+ then
18
+ echo "Already downloaded val"
19
+ else
20
+ ls -alF
21
+ wget https://scienceqa.s3.us-west-1.amazonaws.com/images/val.zip
22
+ unzip -q val.zip
23
+ rm val.zip
24
+ fi
25
+
26
+ if [ -d "test" ];
27
+ then
28
+ echo "Already downloaded test"
29
+ else
30
+ ls -alF
31
+ wget https://scienceqa.s3.us-west-1.amazonaws.com/images/test.zip
32
+ unzip -q test.zip
33
+ rm test.zip
34
+ fi
35
+
36
+ echo "Completed downloads!"