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", "full": "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 = { "mini": { _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", }, "full": { _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="mini", 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="full", version=datasets.Version("1.0.0", ""), description="2500 labelled images (250 per category) from the TeTIm dataset", ), ] DEFAULT_CONFIG_NAME="mini" 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": "mini", } 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)