File size: 2,411 Bytes
32c59c6
 
 
 
 
 
 
 
 
3d709e7
 
 
 
 
 
 
32c59c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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