Update imagenette.py
Browse files- imagenette.py +141 -96
imagenette.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
import datasets
|
|
@@ -15,117 +16,161 @@ _CITATION = """
|
|
| 15 |
"""
|
| 16 |
|
| 17 |
_DESCRIPTION = """\
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
This version of the dataset allows researchers/practitioners to quickly try out
|
| 24 |
ideas and share with others. The dataset comes in three variants:
|
| 25 |
* Full size
|
| 26 |
* 320 px
|
| 27 |
* 160 px
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
-
_LABELS_FNAME = "image_classification/imagenette_labels.txt"
|
| 33 |
_URL_PREFIX = "https://s3.amazonaws.com/fast-ai-imageclas/"
|
| 34 |
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
class ImagenetteConfig(datasets.BuilderConfig):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
|
| 65 |
def _make_builder_configs():
|
| 66 |
-
|
| 67 |
-
for base in ["imagenette2", "imagenette"]:
|
| 68 |
-
for size in ["full-size", "320px", "160px"]:
|
| 69 |
-
configs.append(ImagenetteConfig(base=base, size=size))
|
| 70 |
-
return configs
|
| 71 |
|
| 72 |
|
| 73 |
class Imagenette(datasets.GeneratorBasedBuilder):
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
import datasets
|
|
|
|
| 16 |
"""
|
| 17 |
|
| 18 |
_DESCRIPTION = """\
|
| 19 |
+
# ImageNette
|
| 20 |
+
|
| 21 |
+
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).
|
| 22 |
+
|
| 23 |
+
'Imagenette' is pronounced just like 'Imagenet', except with a corny inauthentic French accent.
|
| 24 |
+
If you've seen Peter Sellars in The Pink Panther, then think something like that.
|
| 25 |
+
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".
|
| 26 |
+
(Note to native French speakers: to avoid confusion, be sure to use a corny inauthentic American accent when saying "Imagenet".
|
| 27 |
+
Think something like the philosophy restaurant skit from Monty Python's The Meaning of Life.)
|
| 28 |
+
|
| 29 |
This version of the dataset allows researchers/practitioners to quickly try out
|
| 30 |
ideas and share with others. The dataset comes in three variants:
|
| 31 |
* Full size
|
| 32 |
* 320 px
|
| 33 |
* 160 px
|
| 34 |
+
|
| 35 |
+
The '320 px' and '160 px' versions have their shortest side resized to that size, with their aspect ratio maintained.
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
Too easy for you? In that case, you might want to try Imagewoof.
|
| 39 |
+
|
| 40 |
+
# Imagewoof
|
| 41 |
+
Imagewoof is a subset of 10 classes from Imagenet that aren't so easy to classify, since they're all dog breeds.
|
| 42 |
+
The breeds are: Australian terrier, Border terrier, Samoyed, Beagle, Shih-Tzu, English foxhound, Rhodesian ridgeback, Dingo, Golden retriever, Old English sheepdog.
|
| 43 |
+
(No we will not enter in to any discussion in to whether a dingo is in fact a dog.
|
| 44 |
+
Any suggestions to the contrary are un-Australian. Thank you for your cooperation.)
|
| 45 |
+
|
| 46 |
+
Full size download;
|
| 47 |
+
320 px download;
|
| 48 |
+
160 px download.
|
| 49 |
"""
|
| 50 |
|
|
|
|
| 51 |
_URL_PREFIX = "https://s3.amazonaws.com/fast-ai-imageclas/"
|
| 52 |
|
| 53 |
+
_LABELS = {
|
| 54 |
+
"imagenette": [
|
| 55 |
+
"cassette_player",
|
| 56 |
+
"chain_saw",
|
| 57 |
+
"church",
|
| 58 |
+
"English_springer",
|
| 59 |
+
"French_horn",
|
| 60 |
+
"garbage_truck",
|
| 61 |
+
"gas_pump",
|
| 62 |
+
"golf_ball",
|
| 63 |
+
"parachute",
|
| 64 |
+
"tench",
|
| 65 |
+
],
|
| 66 |
+
"imagewoof": [
|
| 67 |
+
"Australian_terrier",
|
| 68 |
+
"beagle",
|
| 69 |
+
"Border_terrier",
|
| 70 |
+
"dingo",
|
| 71 |
+
"English_foxhound",
|
| 72 |
+
"golden_retriever",
|
| 73 |
+
"Old_English_sheepdog",
|
| 74 |
+
"Rhodesian_ridgeback",
|
| 75 |
+
"Samoyed",
|
| 76 |
+
"Shih-Tzu",
|
| 77 |
+
],
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
_NAME_TO_DIR = {
|
| 82 |
+
"imagenette-full-res": "imagenette2",
|
| 83 |
+
"imagenette-320px": "imagenette2-320",
|
| 84 |
+
"imagenette-160px": "imagenette2-160",
|
| 85 |
+
"imagewoof-full-res": "imagewoof2",
|
| 86 |
+
"imagewoof-320px": "imagewoof2-320",
|
| 87 |
+
"imagewoof-160px": "imagewoof2-160",
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
|
| 91 |
class ImagenetteConfig(datasets.BuilderConfig):
|
| 92 |
+
"""BuilderConfig for Imagenette."""
|
| 93 |
+
|
| 94 |
+
def __init__(self, name, **kwargs):
|
| 95 |
+
super(ImagenetteConfig, self).__init__(
|
| 96 |
+
name=name, description="{} version.".format(name), **kwargs
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
self.dataset = name.split("-")[0]
|
| 100 |
+
self.labels = _LABELS[self.dataset]
|
| 101 |
+
self.name = name
|
| 102 |
+
|
| 103 |
+
with open("imagenet_refs.json", "r") as f:
|
| 104 |
+
self.imagenet_refs = json.load(f)
|
| 105 |
+
self.ref_to_labels = {}
|
| 106 |
|
| 107 |
|
| 108 |
def _make_builder_configs():
|
| 109 |
+
return [ImagenetteConfig(name) for name in _NAME_TO_DIR]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
class Imagenette(datasets.GeneratorBasedBuilder):
|
| 113 |
+
"""A smaller subset of 10 easily classified classes from Imagenet."""
|
| 114 |
+
|
| 115 |
+
VERSION = datasets.Version("1.0.0")
|
| 116 |
+
|
| 117 |
+
BUILDER_CONFIGS = _make_builder_configs()
|
| 118 |
+
|
| 119 |
+
def _info(self):
|
| 120 |
+
return datasets.DatasetInfo(
|
| 121 |
+
# builder=self,
|
| 122 |
+
description=_DESCRIPTION,
|
| 123 |
+
features=datasets.Features(
|
| 124 |
+
{
|
| 125 |
+
"image": datasets.Image(),
|
| 126 |
+
"labels": datasets.ClassLabel(names=self.config.labels),
|
| 127 |
+
}
|
| 128 |
+
),
|
| 129 |
+
supervised_keys=("path", "labels"),
|
| 130 |
+
homepage="https://github.com/fastai/imagenette",
|
| 131 |
+
citation=_CITATION,
|
| 132 |
+
task_templates=[
|
| 133 |
+
ImageClassification(
|
| 134 |
+
image_column="path",
|
| 135 |
+
label_column="labels",
|
| 136 |
)
|
| 137 |
+
],
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
def _split_generators(self, dl_manager):
|
| 141 |
+
"""Returns SplitGenerators."""
|
| 142 |
+
print(self.__dict__.keys())
|
| 143 |
+
print(self.config)
|
| 144 |
+
name = self.config.name
|
| 145 |
+
dirname = _NAME_TO_DIR[name]
|
| 146 |
+
url = _URL_PREFIX + "{}.tgz".format(dirname)
|
| 147 |
+
path = dl_manager.download_and_extract(url)
|
| 148 |
+
train_path = os.path.join(path, dirname, "train")
|
| 149 |
+
val_path = os.path.join(path, dirname, "val")
|
| 150 |
+
assert os.path.exists(train_path)
|
| 151 |
+
return [
|
| 152 |
+
datasets.SplitGenerator(
|
| 153 |
+
name=datasets.Split.TRAIN,
|
| 154 |
+
gen_kwargs={
|
| 155 |
+
"datapath": train_path,
|
| 156 |
+
},
|
| 157 |
+
),
|
| 158 |
+
datasets.SplitGenerator(
|
| 159 |
+
name=datasets.Split.VALIDATION,
|
| 160 |
+
gen_kwargs={
|
| 161 |
+
"datapath": val_path,
|
| 162 |
+
},
|
| 163 |
+
),
|
| 164 |
+
]
|
| 165 |
+
|
| 166 |
+
def _generate_examples(self, datapath):
|
| 167 |
+
"""Yields examples."""
|
| 168 |
+
imagenet_refs = self.config.imagenet_refs
|
| 169 |
+
for path in Path(datapath).glob("**/*.JPEG"):
|
| 170 |
+
record = {
|
| 171 |
+
# In Imagenette, the parent folder of the file is
|
| 172 |
+
# the imagenet reference to the label name.
|
| 173 |
+
"image": str(path),
|
| 174 |
+
"labels": imagenet_refs[path.parent.name],
|
| 175 |
+
}
|
| 176 |
+
yield path.name, record
|