Datasets:
Federico Galatolo
commited on
Commit
•
f451243
1
Parent(s):
68cf0bf
Initial dataset file
Browse files- TeTIm-Eval.py +120 -0
TeTIm-Eval.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
logger = datasets.logging.get_logger(__name__)
|
6 |
+
|
7 |
+
_CITATION = """\
|
8 |
+
TODO
|
9 |
+
"""
|
10 |
+
|
11 |
+
_HOMEPAGE = ""
|
12 |
+
|
13 |
+
_DESCRIPTION = """\
|
14 |
+
Text To Image Evaluation (TeTIm-Eval)
|
15 |
+
"""
|
16 |
+
|
17 |
+
_URLS = {
|
18 |
+
"mini": "https://huggingface.co/datasets/galatolo/TeTIm-Eval/resolve/main/data/TeTIm-Eval-Mini.zip"
|
19 |
+
}
|
20 |
+
|
21 |
+
_CATEGORIES = [
|
22 |
+
"digital_art",
|
23 |
+
"sketch_art",
|
24 |
+
"traditional_art",
|
25 |
+
"baroque_painting",
|
26 |
+
"high_renaissance_painting",
|
27 |
+
"neoclassical_painting",
|
28 |
+
"animal_photo",
|
29 |
+
"food_photo",
|
30 |
+
"landscape_photo",
|
31 |
+
"person_photo"
|
32 |
+
]
|
33 |
+
|
34 |
+
|
35 |
+
_FOLDERS = {
|
36 |
+
"mini": {
|
37 |
+
_CATEGORIES[0]: "TeTIm-Eval-Mini/sampled_art_digital",
|
38 |
+
_CATEGORIES[1]: "TeTIm-Eval-Mini/sampled_art_sketch",
|
39 |
+
_CATEGORIES[2]: "TeTIm-Eval-Mini/sampled_art_traditional",
|
40 |
+
_CATEGORIES[3]: "TeTIm-Eval-Mini/sampled_painting_baroque",
|
41 |
+
_CATEGORIES[4]: "TeTIm-Eval-Mini/sampled_painting_high-renaissance",
|
42 |
+
_CATEGORIES[5]: "TeTIm-Eval-Mini/sampled_painting_neoclassicism",
|
43 |
+
_CATEGORIES[6]: "TeTIm-Eval-Mini/sampled_photo_animal",
|
44 |
+
_CATEGORIES[7]: "TeTIm-Eval-Mini/sampled_photo_food",
|
45 |
+
_CATEGORIES[8]: "TeTIm-Eval-Mini/sampled_photo_landscape",
|
46 |
+
_CATEGORIES[9]: "TeTIm-Eval-Mini/sampled_photo_person",
|
47 |
+
}
|
48 |
+
}
|
49 |
+
|
50 |
+
class TeTImConfig(datasets.BuilderConfig):
|
51 |
+
def __init__(self, **kwargs):
|
52 |
+
super(TeTImConfig, self).__init__(**kwargs)
|
53 |
+
|
54 |
+
|
55 |
+
class TeTIm(datasets.GeneratorBasedBuilder):
|
56 |
+
BUILDER_CONFIGS = [
|
57 |
+
TeTImConfig(
|
58 |
+
name="mini",
|
59 |
+
version=datasets.Version("1.0.0", ""),
|
60 |
+
description="A random sampling of 300 images (30 for category) from the TeTIm dataset, manually annotated by the same person",
|
61 |
+
),
|
62 |
+
]
|
63 |
+
|
64 |
+
def _info(self):
|
65 |
+
return datasets.DatasetInfo(
|
66 |
+
description=_DESCRIPTION,
|
67 |
+
features=datasets.Features(
|
68 |
+
{
|
69 |
+
"id": datasets.Value("int32"),
|
70 |
+
"image": datasets.Value("string"),
|
71 |
+
"caption": datasets.Value("string"),
|
72 |
+
"category": datasets.Value("string"),
|
73 |
+
}
|
74 |
+
),
|
75 |
+
supervised_keys=None,
|
76 |
+
homepage=_HOMEPAGE,
|
77 |
+
citation=_CITATION,
|
78 |
+
)
|
79 |
+
|
80 |
+
def _split_generators(self, dl_manager):
|
81 |
+
target = os.environ.get(f"TETIMEVAL_{self.config.name}", _URLS[self.config.name])
|
82 |
+
downloaded_files = dl_manager.download_and_extract(target)
|
83 |
+
|
84 |
+
return [
|
85 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"path": downloaded_files}),
|
86 |
+
]
|
87 |
+
|
88 |
+
|
89 |
+
def _generate_examples(self, path):
|
90 |
+
id = 0
|
91 |
+
for category, folder in _FOLDERS[self.config.name].items():
|
92 |
+
images_folder = os.path.join(path, folder, "images")
|
93 |
+
annotations_folder = os.path.join(path, folder, "annotations")
|
94 |
+
|
95 |
+
for image in os.listdir(images_folder):
|
96 |
+
image_id = int(image.split(".")[0])
|
97 |
+
annotation_file = os.path.join(annotations_folder, f"{image_id}.json")
|
98 |
+
with open(annotation_file) as f:
|
99 |
+
annotation = json.load(f)
|
100 |
+
|
101 |
+
yield id, {
|
102 |
+
"id": id,
|
103 |
+
"image": os.path.join(images_folder, image),
|
104 |
+
"caption": annotation["caption"],
|
105 |
+
"category": category
|
106 |
+
}
|
107 |
+
id += 1
|
108 |
+
|
109 |
+
|
110 |
+
if __name__ == "__main__":
|
111 |
+
from datasets import load_dataset
|
112 |
+
dataset_config = {
|
113 |
+
"LOADING_SCRIPT_FILES": os.path.join(os.getcwd(), "TeTIm-Eval.py"),
|
114 |
+
"CONFIG_NAME": "mini",
|
115 |
+
}
|
116 |
+
ds = load_dataset(
|
117 |
+
dataset_config["LOADING_SCRIPT_FILES"],
|
118 |
+
dataset_config["CONFIG_NAME"],
|
119 |
+
)
|
120 |
+
print(ds)
|