File size: 5,885 Bytes
3b370d2
1c45ad4
3b370d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c45ad4
 
 
 
 
 
 
 
 
 
3b370d2
 
 
 
 
1c45ad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b370d2
 
 
9f12edc
3b370d2
1c45ad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b370d2
 
1c45ad4
 
 
 
 
 
 
 
 
 
 
3b370d2
 
 
1c45ad4
3b370d2
 
 
1c45ad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54e3945
1c45ad4
 
 
 
 
 
 
f97fc90
 
 
9f12edc
 
 
 
 
 
 
1c45ad4
f97fc90
1c45ad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f12edc
1c45ad4
 
 
 
 
9f12edc
1c45ad4
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import json
from pathlib import Path

import datasets
from datasets.tasks import ImageClassification

# Adapted from https://huggingface.co/datasets/nateraw/imagenette/blob/main/imagenette.py

_CITATION = """
@misc{imagenette,
  author    = "Jeremy Howard",
  title     = "imagenette",
  url       = "https://github.com/fastai/imagenette/"
}
"""

_DESCRIPTION = """\
# ImageNette

Imagenette is a subset of 10 easily classified classes from Imagenet (tench, English springer, cassette player, chain saw, church, French horn, garbage truck, gas pump, golf ball, parachute).

'Imagenette' is pronounced just like 'Imagenet', except with a corny inauthentic French accent.
If you've seen Peter Sellars in The Pink Panther, then think something like that.
It's important to ham up the accent as much as possible, otherwise people might not be sure whether you're refering to "Imagenette" or "Imagenet".
(Note to native French speakers: to avoid confusion, be sure to use a corny inauthentic American accent when saying "Imagenet".
Think something like the philosophy restaurant skit from Monty Python's The Meaning of Life.)

This version of the dataset allows researchers/practitioners to quickly try out
ideas and share with others. The dataset comes in three variants:
  * Full size
  * 320 px
  * 160 px

The '320 px' and '160 px' versions have their shortest side resized to that size, with their aspect ratio maintained.


Too easy for you? In that case, you might want to try Imagewoof.

# Imagewoof
Imagewoof is a subset of 10 classes from Imagenet that aren't so easy to classify, since they're all dog breeds.
The breeds are: Australian terrier, Border terrier, Samoyed, Beagle, Shih-Tzu, English foxhound, Rhodesian ridgeback, Dingo, Golden retriever, Old English sheepdog.
(No we will not enter in to any discussion in to whether a dingo is in fact a dog.
Any suggestions to the contrary are un-Australian. Thank you for your cooperation.)

Full size download;
320 px download;
160 px download.
"""

_URL_PREFIX = "https://s3.amazonaws.com/fast-ai-imageclas/"
_URL_IMAGENET_REFS = 'https://huggingface.co/datasets/jerpint/imagenette/raw/main/imagenet_refs.json'

_LABELS = {
    "imagenette": [
        "cassette_player",
        "chain_saw",
        "church",
        "English_springer",
        "French_horn",
        "garbage_truck",
        "gas_pump",
        "golf_ball",
        "parachute",
        "tench",
    ],
    "imagewoof": [
        "Australian_terrier",
        "beagle",
        "Border_terrier",
        "dingo",
        "English_foxhound",
        "golden_retriever",
        "Old_English_sheepdog",
        "Rhodesian_ridgeback",
        "Samoyed",
        "Shih-Tzu",
    ],
}


_NAME_TO_DIR = {
    "imagenette-full-res": "imagenette2",
    "imagenette-320px": "imagenette2-320",
    "imagenette-160px": "imagenette2-160",
    "imagewoof-full-res": "imagewoof2",
    "imagewoof-320px": "imagewoof2-320",
    "imagewoof-160px": "imagewoof2-160",
}


class ImagenetteConfig(datasets.BuilderConfig):
    """BuilderConfig for Imagenette."""

    def __init__(self, name, **kwargs):
        super(ImagenetteConfig, self).__init__(
            name=name, description="{} version.".format(name), **kwargs
        )

        self.dataset = name.split("-")[0]
        self.labels = _LABELS[self.dataset]
        self.name = name



def _make_builder_configs():
    return [ImagenetteConfig(name) for name in _NAME_TO_DIR]


class Imagenette(datasets.GeneratorBasedBuilder):
    """A smaller subset of 10 easily classified classes from Imagenet."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = _make_builder_configs()

    def _info(self):
        return datasets.DatasetInfo(
            # builder=self,
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "labels": datasets.ClassLabel(names=self.config.labels),
                }
            ),
            supervised_keys=("path", "labels"),
            homepage="https://github.com/fastai/imagenette",
            citation=_CITATION,
            task_templates=[
                ImageClassification(
                    image_column="path",
                    label_column="labels",
                )
            ],
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        print(self.__dict__.keys())
        print(self.config)
        name = self.config.name
        dirname = _NAME_TO_DIR[name]

                
        # Download the ref:label map for imagenet
        refs_path = dl_manager.download(_URL_IMAGENET_REFS)
        with open(refs_path) as f:
            self.ref_to_label = json.load(f)
            
           
        url = _URL_PREFIX + "{}.tgz".format(dirname)
        path = dl_manager.download_and_extract(url)
        train_path = os.path.join(path, dirname, "train")
        val_path = os.path.join(path, dirname, "val")
        assert os.path.exists(train_path)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "datapath": train_path,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "datapath": val_path,
                },
            ),
        ]

    def _generate_examples(self, datapath):
        """Yields examples."""

        for path in Path(datapath).glob("**/*.JPEG"):
            record = {
                # In Imagenette, the parent folder of the file is
                # the imagenet reference to the label name.
                "image": str(path),
                "labels": self.ref_to_label[path.parent.name],
            }
            yield path.name, record