system HF staff commited on
Commit
966f88f
1 Parent(s): aae2beb

Update files from the datasets library (from 1.18.0)

Browse files

Release notes: https://github.com/huggingface/datasets/releases/tag/1.18.0

Files changed (1) hide show
  1. cats_vs_dogs.py +16 -15
cats_vs_dogs.py CHANGED
@@ -14,7 +14,7 @@
14
  # limitations under the License.
15
  """The Microsoft Cats vs. Dogs dataset"""
16
 
17
- from pathlib import Path
18
  from typing import List
19
 
20
  import datasets
@@ -55,25 +55,26 @@ class CatsVsDogs(datasets.GeneratorBasedBuilder):
55
  }
56
  ),
57
  supervised_keys=("image", "labels"),
58
- task_templates=[ImageClassification(image_column="image", label_column="labels", labels=["cat", "dog"])],
59
  homepage=_HOMEPAGE,
60
  citation=_CITATION,
61
  )
62
 
63
  def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
64
- images_path = Path(dl_manager.download_and_extract(_URL)) / "PetImages"
65
  return [
66
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"images_path": images_path}),
 
 
67
  ]
68
 
69
- def _generate_examples(self, images_path):
70
- logger.info("generating examples from = %s", images_path)
71
- for i, filepath in enumerate(images_path.glob("**/*.jpg")):
72
- with filepath.open("rb") as f:
73
- if b"JFIF" in f.peek(10):
74
- yield str(i), {
75
- "image_file_path": str(filepath),
76
- "image": str(filepath),
77
- "labels": filepath.parent.name.lower(),
78
- }
79
- continue
 
14
  # limitations under the License.
15
  """The Microsoft Cats vs. Dogs dataset"""
16
 
17
+ import os
18
  from typing import List
19
 
20
  import datasets
 
55
  }
56
  ),
57
  supervised_keys=("image", "labels"),
58
+ task_templates=[ImageClassification(image_column="image", label_column="labels")],
59
  homepage=_HOMEPAGE,
60
  citation=_CITATION,
61
  )
62
 
63
  def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
64
+ images_path = os.path.join(dl_manager.download_and_extract(_URL), "PetImages")
65
  return [
66
+ datasets.SplitGenerator(
67
+ name=datasets.Split.TRAIN, gen_kwargs={"files": dl_manager.iter_files([images_path])}
68
+ ),
69
  ]
70
 
71
+ def _generate_examples(self, files):
72
+ for i, file in enumerate(files):
73
+ if os.path.basename(file).endswith(".jpg"):
74
+ with open(file, "rb") as f:
75
+ if b"JFIF" in f.peek(10):
76
+ yield str(i), {
77
+ "image_file_path": file,
78
+ "image": file,
79
+ "labels": os.path.basename(os.path.dirname(file)).lower(),
80
+ }