File size: 2,649 Bytes
cc5cbc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Dataset builder for ImageNet-100.

References:
    https://huggingface.co/datasets/imagenet-1k/blob/main/imagenet-1k.py
"""

import os
from pathlib import Path
from typing import List

import datasets
from datasets.tasks import ImageClassification

from .classes import IMAGENET100_CLASSES

_CITATION = """\
@inproceedings{tian2020contrastive,
  title={Contrastive multiview coding},
  author={Tian, Yonglong and Krishnan, Dilip and Isola, Phillip},
  booktitle={Computer Vision--ECCV 2020: 16th European Conference, Glasgow, UK, August 23--28, 2020, Proceedings, Part XI 16},
  pages={776--794},
  year={2020},
  organization={Springer}
}
"""

_HOMEPAGE = "https://github.com/HobbitLong/CMC"

_DESCRIPTION = f"""\
ImageNet-100 is a subset of ImageNet with 100 classes randomly selected from the original ImageNet-1k dataset.
"""

_IMAGENET_ROOT = os.environ.get("IMAGENET_ROOT", "/data/imagenet")

_DATA_URL = {
    "train": [f"{_IMAGENET_ROOT}/train/{label}" for label in IMAGENET100_CLASSES],
    "val": [f"{_IMAGENET_ROOT}/val/{label}" for label in IMAGENET100_CLASSES],
}


class Imagenet100(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    DEFAULT_WRITER_BATCH_SIZE = 1000

    def _info(self):
        assert len(IMAGENET100_CLASSES) == 100
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.ClassLabel(
                        names=list(IMAGENET100_CLASSES.values())
                    ),
                }
            ),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            task_templates=[
                ImageClassification(image_column="image", label_column="label")
            ],
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"folders": _DATA_URL["train"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"folders": _DATA_URL["val"]},
            ),
        ]

    def _generate_examples(self, folders: List[str]):
        """Yields examples."""
        idx = 0
        for folder in folders:
            synset_id = Path(folder).name
            label = IMAGENET100_CLASSES[synset_id]

            for path in Path(folder).glob("*.JPEG"):
                ex = {"image": str(path), "label": label}
                yield idx, ex
                idx += 1