File size: 3,728 Bytes
f451243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import datasets

logger = datasets.logging.get_logger(__name__)

_CITATION = """\
TODO
"""

_HOMEPAGE = ""

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

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

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


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

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


class TeTIm(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        TeTImConfig(
            name="mini",
            version=datasets.Version("1.0.0", ""),
            description="A random sampling of 300 images (30 for category) from the TeTIm dataset, manually annotated by the same person",
        ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "image": datasets.Value("string"),
                    "caption": datasets.Value("string"),
                    "category": datasets.Value("string"),
                }
            ),
            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 category, 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": category
                }
                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": "mini",
    }
    ds = load_dataset(
        dataset_config["LOADING_SCRIPT_FILES"],
        dataset_config["CONFIG_NAME"],
    )
    print(ds)