TeTIm-Eval / TeTIm-Eval.py
Federico Galatolo
fixed column type
57fcc79
raw
history blame
3.72 kB
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.Image(),
"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)