jerpint commited on
Commit
3b370d2
1 Parent(s): 5d79cd0

Create imagenette.py

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