frgfm commited on
Commit
713dd4b
1 Parent(s): 33bc6ca

fix: Tries to fix preview

Browse files
Files changed (1) hide show
  1. imagenette.py +30 -8
imagenette.py CHANGED
@@ -45,6 +45,7 @@ _LABEL_MAP = [
45
  'n03888257',
46
  ]
47
 
 
48
 
49
 
50
  class OpenFireConfig(datasets.BuilderConfig):
@@ -68,16 +69,28 @@ class OpenFire(datasets.GeneratorBasedBuilder):
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
 
@@ -110,26 +123,35 @@ class OpenFire(datasets.GeneratorBasedBuilder):
110
  )
111
 
112
  def _split_generators(self, dl_manager):
113
- _path = dl_manager.download_and_extract(self.config.data_url)
114
- local_extracted_archive = os.path.join(_path, self.config.data_url.rpartition("/")[-1].split('.')[0])
 
115
  return [
116
  datasets.SplitGenerator(
117
  name=datasets.Split.TRAIN,
118
  gen_kwargs={
119
- "split_folder": os.path.join(local_extracted_archive, "train"),
 
120
  },
121
  ),
122
  datasets.SplitGenerator(
123
  name=datasets.Split.VALIDATION,
124
  gen_kwargs={
125
- "split_folder": os.path.join(local_extracted_archive, "val"),
 
126
  },
127
  ),
128
  ]
129
 
130
- def _generate_examples(self, split_folder):
 
 
131
  idx = 0
132
- for class_idx, class_folder in enumerate(_LABEL_MAP):
133
- for path in os.listdir(os.path.join(split_folder, class_folder)):
134
- yield idx, {"image": os.path.join(split_folder, class_folder, path), "label": class_idx}
 
 
 
 
135
  idx += 1
 
45
  'n03888257',
46
  ]
47
 
48
+ _REPO = "https://huggingface.co/datasets/frgfm/imagenette/resolve/main/metadata"
49
 
50
 
51
  class OpenFireConfig(datasets.BuilderConfig):
 
69
  name="full_size",
70
  description="All images are in their original size.",
71
  data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz",
72
+ metadata_urls={
73
+ "train": f"{_REPO}/full_size/train.txt",
74
+ "validation": f"{_REPO}/imagenette2/val.txt",
75
+ },
76
  ),
77
  OpenFireConfig(
78
  name="320px",
79
  description="All images were resized on their shortest side to 320 pixels.",
80
  data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgz",
81
+ metadata_urls={
82
+ "train": f"{_REPO}/imagenette2-320/train.txt",
83
+ "validation": f"{_REPO}/imagenette2-320/val.txt",
84
+ },
85
  ),
86
  OpenFireConfig(
87
  name="160px",
88
  description="All images were resized on their shortest side to 160 pixels.",
89
  data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgz",
90
+ metadata_urls={
91
+ "train": f"{_REPO}/imagenette2-160/train.txt",
92
+ "validation": f"{_REPO}/imagenette2-160/val.txt",
93
+ },
94
  ),
95
  ]
96
 
 
123
  )
124
 
125
  def _split_generators(self, dl_manager):
126
+ archive_path = dl_manager.download(self.config.data_url)
127
+ metadata_paths = dl_manager.download(self.config.metadata_urls)
128
+ archive_iter = dl_manager.iter_archive(archive_path)
129
  return [
130
  datasets.SplitGenerator(
131
  name=datasets.Split.TRAIN,
132
  gen_kwargs={
133
+ "images": archive_iter,
134
+ "metadata_path": metadata_paths["train"],
135
  },
136
  ),
137
  datasets.SplitGenerator(
138
  name=datasets.Split.VALIDATION,
139
  gen_kwargs={
140
+ "images": archive_iter,
141
+ "metadata_path": metadata_paths["validation"],
142
  },
143
  ),
144
  ]
145
 
146
+ def _generate_examples(self, images, metadata_path):
147
+ with open(metadata_path, encoding="utf-8") as f:
148
+ files_to_keep = set(f.read().split("\n"))
149
  idx = 0
150
+ for file_path, file_obj in images:
151
+ if file_path in files_to_keep:
152
+ label = _LABEL_MAP.index(file_path.split("/")[-1])
153
+ yield idx, {
154
+ "image": {"path": file_path, "bytes": file_obj.read()},
155
+ "label": label,
156
+ }
157
  idx += 1