frgfm commited on
Commit
52861f0
1 Parent(s): 6e52dd9

feat: Added dataset builder

Browse files
Files changed (1) hide show
  1. imagenette.py +136 -0
imagenette.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (C) 2022, François-Guillaume Fernandez.
2
+
3
+ # This program is licensed under the Apache License 2.0.
4
+ # See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
5
+
6
+ """Imagenette dataset."""
7
+
8
+ import os
9
+ import json
10
+
11
+ import datasets
12
+
13
+
14
+ _HOMEPAGE = "https://github.com/fastai/imagenette"
15
+
16
+ _LICENSE = "Apache License 2.0"
17
+
18
+ _CITATION = """\
19
+ @software{Howard_Imagenette_2019,
20
+ title={Imagenette: A smaller subset of 10 easily classified classes from Imagenet},
21
+ author={Jeremy Howard},
22
+ year={2019},
23
+ month={March},
24
+ publisher = {GitHub},
25
+ url = {https://github.com/fastai/imagenette}
26
+ }
27
+ """
28
+
29
+ _DESCRIPTION = """\
30
+ Imagenette is a subset of 10 easily classified classes from Imagenet
31
+ (tench, English springer, cassette player, chain saw, church, French
32
+ horn, garbage truck, gas pump, golf ball, parachute).
33
+ """
34
+
35
+ _LABEL_MAP = [
36
+ 'n01440764',
37
+ 'n02102040',
38
+ 'n02979186',
39
+ 'n03000684',
40
+ 'n03028079',
41
+ 'n03394916',
42
+ 'n03417042',
43
+ 'n03425413',
44
+ 'n03445777',
45
+ 'n03888257',
46
+ ]
47
+
48
+
49
+
50
+ class OpenFireConfig(datasets.BuilderConfig):
51
+ """BuilderConfig for OpenFire."""
52
+
53
+ def __init__(self, data_url, **kwargs):
54
+ """BuilderConfig for OpenFire.
55
+ Args:
56
+ data_url: `string`, url to download the zip file from.
57
+ **kwargs: keyword arguments forwarded to super.
58
+ """
59
+ super(OpenFireConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
60
+ self.data_url = data_url
61
+
62
+
63
+ class OpenFire(datasets.GeneratorBasedBuilder):
64
+ """OpenFire dataset."""
65
+
66
+ BUILDER_CONFIGS = [
67
+ OpenFireConfig(
68
+ name="full_size",
69
+ description="All images are in their original size.",
70
+ data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz",
71
+ ),
72
+ OpenFireConfig(
73
+ name="320px",
74
+ description="All images were resized on their shortest side to 320 pixels.",
75
+ data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgz",
76
+ ),
77
+ OpenFireConfig(
78
+ name="160px",
79
+ description="All images were resized on their shortest side to 160 pixels.",
80
+ data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgz",
81
+ ),
82
+ ]
83
+
84
+ def _info(self):
85
+ return datasets.DatasetInfo(
86
+ description=_DESCRIPTION + self.config.description,
87
+ features=datasets.Features(
88
+ {
89
+ "image": datasets.Image(),
90
+ "label": datasets.ClassLabel(
91
+ names=[
92
+ "tench",
93
+ "English springer",
94
+ "cassette player",
95
+ "chain saw",
96
+ "church",
97
+ "French horn",
98
+ "garbage truck",
99
+ "gas pump",
100
+ "golf ball",
101
+ "parachute",
102
+ ]
103
+ ),
104
+ }
105
+ ),
106
+ supervised_keys=None,
107
+ homepage=_HOMEPAGE,
108
+ license=_LICENSE,
109
+ citation=_CITATION,
110
+ )
111
+
112
+ def _split_generators(self, dl_manager):
113
+ data_dir = dl_manager.download_and_extract(self.config.data_url)
114
+ return [
115
+ datasets.SplitGenerator(
116
+ name=datasets.Split.TRAIN,
117
+ gen_kwargs={
118
+ "image_folder": os.path.join(data_dir, "train"),
119
+ "split": "train",
120
+ },
121
+ ),
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.VALIDATION,
124
+ gen_kwargs={
125
+ "image_folder": os.path.join(data_dir, "val"),
126
+ "split": "validation",
127
+ },
128
+ ),
129
+ ]
130
+
131
+ def _generate_examples(self, image_folder, split):
132
+ idx = 0
133
+ for class_idx, class_folder in _LABEL_MAP:
134
+ for filepath in os.listdir(class_folder):
135
+ yield idx, {"image": os.path.join(image_folder, class_folder, filepath), "label": class_idx}
136
+ idx += 1