jerpint commited on
Commit
1c45ad4
·
1 Parent(s): 54e3945

Update imagenette.py

Browse files
Files changed (1) hide show
  1. 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
- 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",
94
- label_column="labels",
 
 
95
  )
96
- ],
97
- )
98
-
99
- def _split_generators(self, dl_manager):
100
- """Returns SplitGenerators."""
101
- print(self.__dict__.keys())
102
- print(self.config)
103
- dirname = self.config.dirname
104
- url = _URL_PREFIX + "{}.tgz".format(dirname)
105
- path = dl_manager.download_and_extract(url)
106
- train_path = os.path.join(path, dirname, "train")
107
- val_path = os.path.join(path, dirname, "val")
108
- assert os.path.exists(train_path)
109
- return [
110
- datasets.SplitGenerator(
111
- name=datasets.Split.TRAIN,
112
- gen_kwargs={
113
- "datapath": train_path,
114
- },
115
- ),
116
- datasets.SplitGenerator(
117
- name=datasets.Split.VALIDATION,
118
- gen_kwargs={
119
- "datapath": val_path,
120
- },
121
- ),
122
- ]
123
-
124
- def _generate_examples(self, datapath):
125
- """Yields examples."""
126
- for path in Path(datapath).glob("**/*.JPEG"):
127
- record = {
128
- "image_file_path": str(path),
129
- "labels": path.parent.name
130
- }
131
- yield path.name, record
 
 
 
 
 
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