| import os | |
| import datasets | |
| from datasets.tasks import ImageClassification | |
| from .classes_rod import ROD_CLASSES | |
| _CITATION = """\ | |
| @misc{lee2023hardwiring, | |
| title={Hardwiring ViT Patch Selectivity into CNNs using Patch Mixing}, | |
| author={Ariel N. Lee and Sarah Adel Bargal and Janavi Kasera and Stan Sclaroff and Kate Saenko and Nataniel Ruiz}, | |
| year={2023}, | |
| eprint={2306.17848}, | |
| archivePrefix={arXiv}, | |
| primaryClass={cs.CV} | |
| } | |
| """ | |
| _HOMEPAGE = "https://arielnlee.github.io/PatchMixing/" | |
| _DESCRIPTION = """\ | |
| ROD is meant to serve as a metric for evaluating models' robustness to occlusion. It is the product of a meticulous object collection protocol aimed at collecting and capturing 40+ distinct, real-world objects from 16 classes. | |
| """ | |
| _DATA_URL = { | |
| "rod": [ | |
| f"https://huggingface.co/datasets/ariellee/Realistic-Occlusion-Dataset/resolve/main/rod_{i}.tar.gz" | |
| for i in range(2) | |
| ] | |
| } | |
| class ROD(datasets.GeneratorBasedBuilder): | |
| VERSION = datasets.Version("1.0.0") | |
| DEFAULT_WRITER_BATCH_SIZE = 16 | |
| def _info(self): | |
| assert len(ROD_CLASSES) == 16 | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=datasets.Features( | |
| { | |
| "image": datasets.Image(), | |
| "label": datasets.ClassLabel(names=list(ROD_CLASSES.values())), | |
| } | |
| ), | |
| homepage=_HOMEPAGE, | |
| citation=_CITATION, | |
| task_templates=[ImageClassification(image_column="image", label_column="label")], | |
| ) | |
| def _split_generators(self, dl_manager): | |
| """Returns SplitGenerators.""" | |
| archives = dl_manager.download(_DATA_URL) | |
| return [ | |
| datasets.SplitGenerator( | |
| name="ROD", | |
| gen_kwargs={ | |
| "archives": [dl_manager.iter_archive(archive) for archive in archives["rod"]], | |
| }, | |
| ), | |
| ] | |
| def _generate_examples(self, archives): | |
| """Yields examples.""" | |
| idx = 0 | |
| for archive in archives: | |
| for path, file in archive: | |
| if path.endswith(".jpg"): | |
| synset_id = os.path.basename(os.path.dirname(path)) | |
| ex = {"image": {"path": path, "bytes": file.read()}, "label": synset_id} | |
| yield idx, ex | |
| idx += 1 | |