File size: 4,004 Bytes
b304077
 
 
 
 
618ab80
b304077
 
 
 
5a62704
b304077
 
 
 
 
 
 
 
 
 
b37892f
b304077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331339f
b304077
 
 
 
 
 
 
331339f
b304077
 
 
 
 
 
 
331339f
b304077
 
 
 
 
331339f
a070432
b304077
bcebddc
10e29a6
331339f
 
 
 
 
 
10e29a6
331339f
 
 
 
 
 
 
10e29a6
331339f
78db6be
331339f
 
 
 
 
10e29a6
78db6be
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
import json
import os
from pathlib import Path
import datasets
from datasets import DownloadManager
from PIL import Image

_CITATION = ""
_HOMEPAGE = ""
_DESCRIPTION = ""
_LICENSE = ""
_DATA_URL = {"train" : "https://vizwiz.cs.colorado.edu/VizWiz_final/images/train.zip",
             "test"  : "https://vizwiz.cs.colorado.edu/VizWiz_final/images/test.zip",
             "val"   : "https://vizwiz.cs.colorado.edu/VizWiz_final/images/val.zip" }
_ANNOTATION_URL = "https://vizwiz.cs.colorado.edu/VizWiz_final/vqa_data/Annotations.zip"

_FEATURES = datasets.Features(
    {
        "id"   : datasets.Value("int32"),
        "image": datasets.Image(),
        "filename": datasets.Value("string"),
        "question": datasets.Value("string"),
        "answers": datasets.Sequence(datasets.Value("string")),
        "answers_original": [
            {
                "answer": datasets.Value("string"),
                "answer_confidence": datasets.Value("string"),
            }
        ],
        "answer_type": datasets.Value("string"),
        "answerable": datasets.Value("int32")
    }
)


class VizWiz(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    def _info(self):
        return datasets.DatasetInfo(
            description = _DESCRIPTION,
            features    = _FEATURES,
            homepage    = _HOMEPAGE,
            license     = _LICENSE,
            citation    = _CITATION,
        )
    
    def _split_generators(self, dl_manager):
        ann_file_train = os.path.join(dl_manager.download_and_extract(_ANNOTATION_URL), "train.json")
        ann_file_val   = os.path.join(dl_manager.download_and_extract(_ANNOTATION_URL), "val.json")
        ann_file_test  = os.path.join(dl_manager.download_and_extract(_ANNOTATION_URL), "test.json")
        image_folders = {k: Path(v) for k, v in dl_manager.download_and_extract(_DATA_URL).items()}

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "annotation_file": ann_file_train,
                    "image_folders": image_folders,
                    "split_key": 'train'
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "annotation_file": ann_file_val,
                    "image_folders": image_folders,
                    "split_key": "val"
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "annotation_file": ann_file_test,
                    "image_folders": image_folders,
                    "split_key": "test"
                },
            ),
        ]
    
    def _generate_examples(self, annotation_file,image_folders,split_key):
        counter = 0 
        info = {}
        annotations = json.load(open(annotation_file))
        for ann in annotations:
            if split_key in ['train','val']:
                answers = [answer["answer"] for answer in ann["answers"]]
                answers_original = ann['answers']
                answer_type = ann["answer_type"]
                answerable  = ann["answerable"]
                
            else:
                
                answers = None
                answers_original = None
                answer_type = None
                answerable  = None
                
            yield counter, {
                        "id"   : counter,
                        "image": str(image_folders[split_key]/split_key/ann['image']),
                        "filename" : ann['image'],
                        "question" : ann["question"],
                        "answers"  : answers,
                        "answers_original"  : answers_original,
                        "answer_type"       : answer_type,
                        "answerable"        : answerable
                    }
            counter += 1