File size: 5,047 Bytes
c905b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0117593
 
 
 
 
c905b66
 
 
0117593
 
 
 
 
c905b66
 
 
 
 
 
 
 
 
 
4088711
 
 
 
c905b66
4088711
 
 
 
 
 
 
 
 
c905b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0117593
 
 
c905b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f3e681
 
4088711
 
 
 
 
 
c905b66
 
 
 
 
 
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
# Copyright 2020 The HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ScienceQA loading script."""


import json
from pathlib import Path
import os

import datasets


_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}
}
"""

_DESCRIPTION = """\
This is the ScienceQA dataset.
"""

_HOMEPAGE = "https://scienceqa.github.io/"

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

_URLS = {
    "pid_splits": "https://drive.google.com/uc?id=1OXlNBuW74dsrwYZIpQMshFqxkjcMPPgV&export=download",
    "problems": "https://drive.google.com/uc?id=1nJ86OLnF2C6eDoi5UOAdTAS5Duc0wuTl&export=download",
    "train": "https://drive.google.com/uc?id=1swX4Eei1ZqrXRvM-JAZxN6QVwcBLPHV8&export=download",
    "val": "https://drive.google.com/uc?id=1ijThWZc1tsoqGrOCWhYYj1HUJ48Hl8Zz&export=download",
    "test": "https://drive.google.com/uc?id=1eyjFaHxbvEJZzdZILn3vnTihBNDmKcIj&export=download",
}

_SUB_FOLDER_OR_FILE_NAME = {
    "pid_splits": "pid_splits.json",
    "problems": "problems.json",
    "train": "train",
    "val": "val",
    "test": "test",
}


class ScienceQADataset(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "question": datasets.Value("string"),
                "choices": datasets.Sequence(datasets.Value("string")),
                "answer": datasets.Value("int32"),
                "hint": datasets.Value("string"),
                "image": datasets.Image(),
                "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"),
                "split": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract(_URLS)
        gen_kwargs = {}
        for split_name in ["train", "val", "test"]:
            gen_kwargs_per_split = {}
            gen_kwargs_per_split["pid_splits_path"] = Path(data_dir["pid_splits"]) / _SUB_FOLDER_OR_FILE_NAME["pid_splits"]
            gen_kwargs_per_split["problems_path"] = Path(data_dir["problems"]) / _SUB_FOLDER_OR_FILE_NAME["problems"]
            gen_kwargs_per_split["images_path"] = Path(data_dir[split_name]) / _SUB_FOLDER_OR_FILE_NAME[split_name]
            gen_kwargs_per_split["split_name"] = split_name
            gen_kwargs[split_name] = gen_kwargs_per_split
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs=gen_kwargs["train"],
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs=gen_kwargs["val"],
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs=gen_kwargs["test"],
            ),
        ]

    def _generate_examples(self, pid_splits_path, problems_path, images_path, split_name):
        #pid_splits = json.load(open(pid_splits_path, "r"))
        #problems = json.load(open(problems_path, "r"))

        #pid_splits = {"train": ["1"], "val": ["7"], "test": ["5"]}
        #problems = {"1": {"image": "image.png"}, "5": {"image": "image.png"}, "7": {"image": "image.png"}}

        pid_splits = json.load(open("/gpfswork/rech/cnw/urd43gx/pid_splits.json", "r"))
        problems = json.load(open("/gpfswork/rech/cnw/urd43gx/problems.json", "r"))

        for idx, key in enumerate(pid_splits[split_name]):
            example = problems[key]
            if example["image"]:
                example["image"] = os.path.join(images_path, key, example["image"])
            yield idx, example