iamirulofficial commited on
Commit
e954b0c
·
verified ·
1 Parent(s): 6bb2d2f

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. TestNew.py +55 -44
TestNew.py CHANGED
@@ -1,4 +1,4 @@
1
- import os
2
  import json
3
  import random
4
  from datasets import (
@@ -15,8 +15,12 @@ from datasets import (
15
  )
16
  from huggingface_hub import hf_hub_url
17
 
 
 
 
 
18
  class ImageSubsetConfig(BuilderConfig):
19
- """BuilderConfig for small vs. full dataset."""
20
  def __init__(self, name, sample_size=None, **kwargs):
21
  super().__init__(
22
  name=name,
@@ -25,76 +29,83 @@ class ImageSubsetConfig(BuilderConfig):
25
  )
26
  self.sample_size = sample_size
27
 
 
28
  class MyImageDataset(GeneratorBasedBuilder):
29
- """A dataset of images with actuated_angle from metadata.json."""
30
  BUILDER_CONFIGS = [
31
  ImageSubsetConfig(
32
  name="full",
33
- sample_size=None,
34
- description="All images (≈100 GB). Use with streaming=True."
35
  ),
36
  ImageSubsetConfig(
37
  name="small",
38
- sample_size=2,
39
- description="Random subset of 10 000 images for quick iteration."
40
  ),
41
  ]
42
  DEFAULT_CONFIG_NAME = "small"
43
 
44
- def _info(self):
45
  return DatasetInfo(
46
- description="Images with a 2-D actuated_angle from metadata.json",
47
- features=Features({
48
- "image": Image(), # the image file
49
- "actuated_angle": Sequence(Value("int32")), # [angle0, angle1]
50
- }),
 
 
51
  supervised_keys=None,
52
  )
53
 
 
 
 
54
  def _split_generators(self, dl_manager: DownloadManager):
55
- data_dir = dl_manager.manual_dir or os.path.abspath(os.path.dirname(__file__))
56
- images_dir = os.path.join(data_dir, "images")
57
- repo_id = "iamirulofficial/TestNew"
58
  meta_path = dl_manager.download(
59
- hf_hub_url(repo_id, filename="metadata.json", repo_type="dataset")
60
  )
61
 
 
 
 
62
 
63
- # load your filename → {"0":…, "1":…} mapping
64
- with open(meta_path, "r", encoding="utf-8") as f:
65
- metadata = json.load(f)
66
-
67
- # sample if in "small" config
68
- if self.config.sample_size:
69
- all_fnames = list(metadata.keys())
70
  random.seed(42)
71
- selected = set(random.sample(all_fnames, self.config.sample_size))
72
  else:
73
- selected = set(metadata.keys())
 
 
 
 
 
 
 
 
 
74
 
75
  return [
76
  SplitGenerator(
77
  name=Split.TRAIN,
78
  gen_kwargs={
79
- "images_dir": images_dir,
80
  "metadata": metadata,
81
- "selected": selected,
82
  },
83
- ),
84
  ]
85
 
86
- def _generate_examples(self, images_dir, metadata, selected):
87
- """Yields examples of the form { "image": path, "actuated_angle": [int, int] }"""
88
- for idx, fname in enumerate(selected):
89
- if fname not in metadata:
90
- continue
91
- image_path = os.path.join(images_dir, fname)
92
- if not os.path.exists(image_path):
93
- continue
94
- meta = metadata[fname]
95
- # extract the two angle values (keys "0" and "1")
96
- angles = [ int(meta.get("0", 0)), int(meta.get("1", 0)) ]
97
- yield idx, {
98
- "image": image_path,
99
- "actuated_angle": angles,
100
- }
 
1
+ # TestNew.py
2
  import json
3
  import random
4
  from datasets import (
 
15
  )
16
  from huggingface_hub import hf_hub_url
17
 
18
+
19
+ _REPO_ID = "iamirulofficial/TestNew" # change if you ever fork the repo
20
+
21
+
22
  class ImageSubsetConfig(BuilderConfig):
23
+ """BuilderConfig for full dataset vs. small random sample."""
24
  def __init__(self, name, sample_size=None, **kwargs):
25
  super().__init__(
26
  name=name,
 
29
  )
30
  self.sample_size = sample_size
31
 
32
+
33
  class MyImageDataset(GeneratorBasedBuilder):
34
+ """Images + 2‑D actuated_angle labels stored in metadata.json."""
35
  BUILDER_CONFIGS = [
36
  ImageSubsetConfig(
37
  name="full",
38
+ sample_size=None, # all images
39
+ description="Entire dataset (≈100GB)"
40
  ),
41
  ImageSubsetConfig(
42
  name="small",
43
+ sample_size=2, # tiny sample for quick tests
44
+ description="Two random images"
45
  ),
46
  ]
47
  DEFAULT_CONFIG_NAME = "small"
48
 
49
+ def _info(self) -> DatasetInfo:
50
  return DatasetInfo(
51
+ description="Images with a 2D actuated_angle from metadata.json",
52
+ features=Features(
53
+ {
54
+ "image": Image(), # PIL.Image will be returned
55
+ "actuated_angle": Sequence(Value("int32")), # [angle0, angle1]
56
+ }
57
+ ),
58
  supervised_keys=None,
59
  )
60
 
61
+ # --------------------------------------------------------------------- #
62
+ # Download phase #
63
+ # --------------------------------------------------------------------- #
64
  def _split_generators(self, dl_manager: DownloadManager):
65
+ # 1️⃣ Download metadata.json (tiny text file)
 
 
66
  meta_path = dl_manager.download(
67
+ hf_hub_url(_REPO_ID, "metadata.json", repo_type="dataset")
68
  )
69
 
70
+ # 2️⃣ Decide which filenames we need
71
+ with open(meta_path, encoding="utf-8") as f:
72
+ metadata = json.load(f) # {"frame_000.png": {"0":…, …}, …}
73
 
74
+ all_fnames = list(metadata)
75
+ if self.config.sample_size: # small‑config branch
 
 
 
 
 
76
  random.seed(42)
77
+ selected = sorted(random.sample(all_fnames, self.config.sample_size))
78
  else:
79
+ selected = sorted(all_fnames) # full dataset
80
+
81
+ # 3️⃣ Build URLs → dl_manager.download() → local paths
82
+ url_dict = {
83
+ fname: hf_hub_url(
84
+ _REPO_ID, f"images/{fname}", repo_type="dataset"
85
+ )
86
+ for fname in selected
87
+ }
88
+ img_paths = dl_manager.download(url_dict) # same keys, but local files
89
 
90
  return [
91
  SplitGenerator(
92
  name=Split.TRAIN,
93
  gen_kwargs={
94
+ "img_paths": img_paths,
95
  "metadata": metadata,
 
96
  },
97
+ )
98
  ]
99
 
100
+ # --------------------------------------------------------------------- #
101
+ # Generate examples #
102
+ # --------------------------------------------------------------------- #
103
+ def _generate_examples(self, img_paths: dict, metadata: dict):
104
+ """
105
+ Yields (key, example) where example =
106
+ { "image": <local‑file‑path>, "actuated_angle": [int, int] }
107
+ """
108
+ for idx, (fname, local_path) in enumerate(img_paths.items()):
109
+ meta = metadata.get(fname, {})
110
+ angles = [int(meta.get("0", 0)), int(meta.get("1", 0))]
111
+ yield idx, {"image": local_path, "actuated_angle": angles}