File size: 5,401 Bytes
29afe55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from pathlib import Path

import datasets

_DESCRIPTION = """Science Question Answering (ScienceQA), a new benchmark that consists of 21,208 multimodal 
multiple choice questions with a diverse set of science topics and annotations of their answers 
with corresponding lectures and explanations. 
The lecture and explanation provide general external knowledge and specific reasons,
respectively, for arriving at the correct answer."""

# Lets use the project page instead of the github repo
_HOMEPAGE = "https://scienceqa.github.io"

_CITATION = """\
@inproceedings{lu2022learn,
    title={Learn to Explain: Multimodal Reasoning via Thought Chains for Science Question Answering},
    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},
    booktitle={The 36th Conference on Neural Information Processing Systems (NeurIPS)},
    year={2022}
}
"""

_LICENSE = "Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)"


class ScienceQA(datasets.GeneratorBasedBuilder):
    """Science Question Answering (ScienceQA), a new benchmark that consists of 21,208 multimodal
multiple choice questions with a diverse set of science topics and annotations of their answers
with corresponding lectures and explanations.
The lecture and explanation provide general external knowledge and specific reasons,
respectively, for arriving at the correct answer."""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
                description=_DESCRIPTION,
                features=datasets.Features(
                        {
                            "image": datasets.Image(),
                            "question": datasets.Value("string"),
                            "choices": datasets.features.Sequence(datasets.Value("string")),
                            "answer": datasets.Value("int8"),
                            "hint": datasets.Value("string"),
                            "task": datasets.Value("string"),
                            "grade": datasets.Value("string"),
                            "subject": datasets.Value("string"),
                            "topic": datasets.Value("string"),
                            "category": datasets.Value("string"),
                            "skill": datasets.Value("string"),
                            "lecture": datasets.Value("string"),
                            "solution": datasets.Value("string")
                            }
                        ),
                homepage=_HOMEPAGE,
                citation=_CITATION,
                license=_LICENSE,
                )

    def _split_generators(self, dl_manager):
        text_path = Path.cwd() / 'text' / 'problems.json'
        image_dir = Path.cwd() / 'images'
        return [
            datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "text_path": text_path,
                        "image_dir": image_dir,
                        "split": "train",
                        },
                    ),
            datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "text_path": text_path,
                        "image_dir": image_dir,
                        "split": "val",
                        },
                    ),
            datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "text_path": text_path,
                        "image_dir": image_dir,
                        "split": "test"
                        },
                    ),
            ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, text_path, image_dir, split):
        with open(text_path, encoding="utf-8") as f:
            # Load all the text. Note that if this was HUGE, we would need to find a better way to load the json
            data = json.load(f)
            ignore_keys = ['image', 'split']

            # Get image_id from its annoying location
            for image_id, row in data.items():
                # Only look for the rows in our split
                if row['split'] == split:

                    # Note, not all rows have images.
                    # Get all the image data we need
                    if row['image']:
                        image_path = image_dir / split / image_id / 'image.png'
                        image_bytes = image_path.read_bytes()
                        image_dict = {'path': str(image_path), 'bytes': image_bytes}
                    else:
                        image_dict = None

                    # Keep only the keys we need
                    relevant_row = {k: v for k, v in row.items() if k not in ignore_keys}

                    return_dict = {
                        'image': image_dict,
                        **relevant_row
                        }
                    yield image_id, return_dict