Create imagenette.py
Browse files- imagenette.py +129 -0
imagenette.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
from datasets.tasks import ImageClassification
|
6 |
+
|
7 |
+
|
8 |
+
_CITATION = """
|
9 |
+
@misc{imagenette,
|
10 |
+
author = "Jeremy Howard",
|
11 |
+
title = "imagenette",
|
12 |
+
url = "https://github.com/fastai/imagenette/"
|
13 |
+
}
|
14 |
+
"""
|
15 |
+
|
16 |
+
_DESCRIPTION = """\
|
17 |
+
Imagenette is a subset of 10 easily classified classes from the Imagenet
|
18 |
+
dataset. It was originally prepared by Jeremy Howard of FastAI. The objective
|
19 |
+
behind putting together a small version of the Imagenet dataset was mainly
|
20 |
+
because running new ideas/algorithms/experiments on the whole Imagenet take a
|
21 |
+
lot of time.
|
22 |
+
This version of the dataset allows researchers/practitioners to quickly try out
|
23 |
+
ideas and share with others. The dataset comes in three variants:
|
24 |
+
* Full size
|
25 |
+
* 320 px
|
26 |
+
* 160 px
|
27 |
+
Note: The v2 config correspond to the new 70/30 train/valid split (released
|
28 |
+
in Dec 6 2019).
|
29 |
+
"""
|
30 |
+
|
31 |
+
_LABELS_FNAME = "image_classification/imagenette_labels.txt"
|
32 |
+
_URL_PREFIX = "https://s3.amazonaws.com/fast-ai-imageclas/"
|
33 |
+
|
34 |
+
LABELS = [
|
35 |
+
"n01440764",
|
36 |
+
"n02102040",
|
37 |
+
"n02979186",
|
38 |
+
"n03000684",
|
39 |
+
"n03028079",
|
40 |
+
"n03394916",
|
41 |
+
"n03417042",
|
42 |
+
"n03425413",
|
43 |
+
"n03445777",
|
44 |
+
"n03888257"
|
45 |
+
]
|
46 |
+
|
47 |
+
class ImagenetteConfig(datasets.BuilderConfig):
|
48 |
+
"""BuilderConfig for Imagenette."""
|
49 |
+
|
50 |
+
def __init__(self, size, base, **kwargs):
|
51 |
+
super(ImagenetteConfig, self).__init__(
|
52 |
+
# `320px-v2`,...
|
53 |
+
name=size + ("-v2" if base == "imagenette2" else ""),
|
54 |
+
description="{} variant.".format(size),
|
55 |
+
**kwargs)
|
56 |
+
# e.g. `imagenette2-320.tgz`
|
57 |
+
self.dirname = base + {
|
58 |
+
"full-size": "",
|
59 |
+
"320px": "-320",
|
60 |
+
"160px": "-160",
|
61 |
+
}[size]
|
62 |
+
|
63 |
+
|
64 |
+
def _make_builder_configs():
|
65 |
+
configs = []
|
66 |
+
for base in ["imagenette2", "imagenette"]:
|
67 |
+
for size in ["full-size", "320px", "160px"]:
|
68 |
+
configs.append(ImagenetteConfig(base=base, size=size))
|
69 |
+
return configs
|
70 |
+
|
71 |
+
|
72 |
+
class Imagenette(datasets.GeneratorBasedBuilder):
|
73 |
+
"""A smaller subset of 10 easily classified classes from Imagenet."""
|
74 |
+
|
75 |
+
VERSION = datasets.Version("1.0.0")
|
76 |
+
|
77 |
+
BUILDER_CONFIGS = _make_builder_configs()
|
78 |
+
|
79 |
+
def _info(self):
|
80 |
+
return datasets.DatasetInfo(
|
81 |
+
# builder=self,
|
82 |
+
description=_DESCRIPTION,
|
83 |
+
features=datasets.Features({
|
84 |
+
"image_file_path": datasets.Value("string"),
|
85 |
+
"labels": datasets.ClassLabel(names=LABELS)
|
86 |
+
}),
|
87 |
+
supervised_keys=("image_file_path", "labels"),
|
88 |
+
homepage="https://github.com/fastai/imagenette",
|
89 |
+
citation=_CITATION,
|
90 |
+
task_templates=[
|
91 |
+
ImageClassification(
|
92 |
+
image_file_path_column="image_file_path", label_column="labels", labels=LABELS
|
93 |
+
)
|
94 |
+
],
|
95 |
+
)
|
96 |
+
|
97 |
+
def _split_generators(self, dl_manager):
|
98 |
+
"""Returns SplitGenerators."""
|
99 |
+
print(self.__dict__.keys())
|
100 |
+
print(self.config)
|
101 |
+
dirname = self.config.dirname
|
102 |
+
url = _URL_PREFIX + "{}.tgz".format(dirname)
|
103 |
+
path = dl_manager.download_and_extract(url)
|
104 |
+
train_path = os.path.join(path, dirname, "train")
|
105 |
+
val_path = os.path.join(path, dirname, "val")
|
106 |
+
assert os.path.exists(train_path)
|
107 |
+
return [
|
108 |
+
datasets.SplitGenerator(
|
109 |
+
name=datasets.Split.TRAIN,
|
110 |
+
gen_kwargs={
|
111 |
+
"datapath": train_path,
|
112 |
+
},
|
113 |
+
),
|
114 |
+
datasets.SplitGenerator(
|
115 |
+
name=datasets.Split.VALIDATION,
|
116 |
+
gen_kwargs={
|
117 |
+
"datapath": val_path,
|
118 |
+
},
|
119 |
+
),
|
120 |
+
]
|
121 |
+
|
122 |
+
def _generate_examples(self, datapath):
|
123 |
+
"""Yields examples."""
|
124 |
+
for path in Path(datapath).glob("**/*.JPEG"):
|
125 |
+
record = {
|
126 |
+
"image_file_path": str(path),
|
127 |
+
"labels": path.parent.name
|
128 |
+
}
|
129 |
+
yield path.name, record
|