File size: 5,382 Bytes
f451243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a9bde4
 
f451243
 
ff20c49
f451243
 
 
 
 
 
 
 
 
 
 
 
ff20c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f451243
 
5a9bde4
ff20c49
 
 
 
 
 
 
 
 
 
e2b666c
5a9bde4
ff20c49
 
 
 
 
 
 
 
 
 
f451243
 
 
 
 
 
 
 
 
 
 
5a9bde4
f451243
e2b666c
 
 
5a9bde4
e2b666c
 
f451243
 
5a9bde4
 
f451243
 
 
 
 
 
 
57fcc79
f451243
ff20c49
 
f451243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff20c49
f451243
 
 
 
 
 
 
 
 
 
 
 
 
ff20c49
 
f451243
 
 
 
 
 
 
 
5a9bde4
f451243
 
 
 
 
ff20c49
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import json
import datasets

logger = datasets.logging.get_logger(__name__)

_CITATION = """\
TODO
"""

_HOMEPAGE = ""

_DESCRIPTION = """\
Text To Image Evaluation (TeTIm-Eval)
"""

_URLS = {
    "captioned": "https://huggingface.co/datasets/galatolo/TeTIm-Eval/resolve/main/data/TeTIm-Eval-Mini.zip",
    "uncaptioned": "https://huggingface.co/datasets/galatolo/TeTIm-Eval/resolve/main/data/TeTIm-Eval.zip"
}

_CLASSES = [
    "digital_art",
    "sketch_art",
    "traditional_art",
    "baroque_painting",
    "high_renaissance_painting",
    "neoclassical_painting",
    "animal_photo",
    "food_photo",
    "landscape_photo",
    "person_photo"
]

_CATEGORIES = [
    "art",
    "painting",
    "photo"
]

_MAP_CATEGORY = {
    _CLASSES[0]: _CATEGORIES[0],
    _CLASSES[1]: _CATEGORIES[0],
    _CLASSES[2]: _CATEGORIES[0],

    _CLASSES[3]: _CATEGORIES[1],
    _CLASSES[4]: _CATEGORIES[1],
    _CLASSES[5]: _CATEGORIES[1],

    _CLASSES[6]: _CATEGORIES[2],
    _CLASSES[7]: _CATEGORIES[2],
    _CLASSES[8]: _CATEGORIES[2],
    _CLASSES[9]: _CATEGORIES[2],
}

_FOLDERS = {
    "captioned": {
        _CLASSES[0]: "TeTIm-Eval-Mini/sampled_art_digital",
        _CLASSES[1]: "TeTIm-Eval-Mini/sampled_art_sketch",
        _CLASSES[2]: "TeTIm-Eval-Mini/sampled_art_traditional",
        _CLASSES[3]: "TeTIm-Eval-Mini/sampled_painting_baroque",
        _CLASSES[4]: "TeTIm-Eval-Mini/sampled_painting_high-renaissance",
        _CLASSES[5]: "TeTIm-Eval-Mini/sampled_painting_neoclassicism",
        _CLASSES[6]: "TeTIm-Eval-Mini/sampled_photo_animal",
        _CLASSES[7]: "TeTIm-Eval-Mini/sampled_photo_food",
        _CLASSES[8]: "TeTIm-Eval-Mini/sampled_photo_landscape",
        _CLASSES[9]: "TeTIm-Eval-Mini/sampled_photo_person",
    },
    "uncaptioned": {
        _CLASSES[0]: "TeTIm-Eval/sampled_art_digital",
        _CLASSES[1]: "TeTIm-Eval/sampled_art_sketch",
        _CLASSES[2]: "TeTIm-Eval/sampled_art_traditional",
        _CLASSES[3]: "TeTIm-Eval/sampled_painting_baroque",
        _CLASSES[4]: "TeTIm-Eval/sampled_painting_high-renaissance",
        _CLASSES[5]: "TeTIm-Eval/sampled_painting_neoclassicism",
        _CLASSES[6]: "TeTIm-Eval/sampled_photo_animal",
        _CLASSES[7]: "TeTIm-Eval/sampled_photo_food",
        _CLASSES[8]: "TeTIm-Eval/sampled_photo_landscape",
        _CLASSES[9]: "TeTIm-Eval/sampled_photo_person",
    }
}

class TeTImConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(TeTImConfig, self).__init__(**kwargs)


class TeTIm(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        TeTImConfig(
            name="captioned",
            version=datasets.Version("1.0.0", ""),
            description="A random sampling of 300 text-images pairs (30 per category) from the TeTIm dataset, manually annotated by the same person",
        ),
        TeTImConfig(
            name="uncaptioned",
            version=datasets.Version("1.0.0", ""),
            description="2500 labelled images (250 per category) from the TeTIm dataset",
        ),
    ]

    DEFAULT_CONFIG_NAME="captioned"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "image": datasets.Image(),
                    "caption": datasets.Value("string"),
                    "category": datasets.ClassLabel(num_classes=len(_CATEGORIES), names=_CATEGORIES),
                    "class": datasets.ClassLabel(num_classes=len(_CLASSES), names=_CLASSES)
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        target = os.environ.get(f"TETIMEVAL_{self.config.name}", _URLS[self.config.name])
        downloaded_files = dl_manager.download_and_extract(target)

        return [
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"path": downloaded_files}),
        ]

    
    def _generate_examples(self, path):
        id = 0
        for _class, folder in _FOLDERS[self.config.name].items():
            images_folder = os.path.join(path, folder, "images")
            annotations_folder = os.path.join(path, folder, "annotations")

            for image in os.listdir(images_folder):
                image_id = int(image.split(".")[0])
                annotation_file = os.path.join(annotations_folder, f"{image_id}.json")
                with open(annotation_file) as f:
                    annotation = json.load(f)
                
                yield id, {
                    "id": id,
                    "image": os.path.join(images_folder, image),
                    "caption": annotation["caption"],
                    "category": _CATEGORIES.index(_MAP_CATEGORY[_class]),
                    "class": _CLASSES.index(_class)
                }
                id += 1


if __name__ == "__main__":
    from datasets import load_dataset
    dataset_config = {
        "LOADING_SCRIPT_FILES": os.path.join(os.getcwd(), "TeTIm-Eval.py"),
        "CONFIG_NAME": "uncaptioned",
    }
    ds = load_dataset(
        dataset_config["LOADING_SCRIPT_FILES"],
        dataset_config["CONFIG_NAME"],
    )
    print(ds)

    for i, e in zip(range(0, 10), ds["test"]):
        print(i, e)