File size: 3,956 Bytes
13c3710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# TestNew.py
import json
import random
from datasets import (
    BuilderConfig,
    DatasetInfo,
    DownloadManager,
    GeneratorBasedBuilder,
    SplitGenerator,
    Split,
    Features,
    Image,
    Sequence,
    Value,
)
from huggingface_hub import hf_hub_url


_REPO_ID = "iamirulofficial/TestNew"          # change if you ever fork the repo


class ImageSubsetConfig(BuilderConfig):
    """BuilderConfig for full dataset vs. small random sample."""
    def __init__(self, name, sample_size=None, **kwargs):
        super().__init__(
            name=name,
            version="1.0.1",
            description=kwargs.get("description", "")
        )
        self.sample_size = sample_size


class MyImageDataset(GeneratorBasedBuilder):
    """Images + 2‑D actuated_angle labels stored in metadata.json."""
    BUILDER_CONFIGS = [
        ImageSubsetConfig(
            name="full",
            sample_size=None,                     # all images
            description="Entire dataset (≈100 GB)"
        ),
        ImageSubsetConfig(
            name="small",
            sample_size=2,                        # tiny sample for quick tests
            description="Two random images"
        ),
    ]
    DEFAULT_CONFIG_NAME = "small"

    def _info(self) -> DatasetInfo:
        return DatasetInfo(
            description="Images with a 2‑D actuated_angle from metadata.json",
            features=Features(
                {
                    "image": Image(),                    # PIL.Image will be returned
                    "actuated_angle": Sequence(Value("int32")),  # [angle0, angle1]
                }
            ),
            supervised_keys=None,
        )

    # --------------------------------------------------------------------- #
    # Download phase                                                         #
    # --------------------------------------------------------------------- #
    def _split_generators(self, dl_manager: DownloadManager):
        # 1️⃣  Download metadata.json (tiny text file)
        meta_path = dl_manager.download(
            hf_hub_url(_REPO_ID, "metadata.json", repo_type="dataset")
        )

        # 2️⃣  Decide which filenames we need
        with open(meta_path, encoding="utf-8") as f:
            metadata = json.load(f)                      # {"frame_000.png": {"0":…, …}, …}

        all_fnames = list(metadata)
        if self.config.sample_size:                      # small‑config branch
            random.seed(42)
            selected = sorted(random.sample(all_fnames, self.config.sample_size))
        else:
            selected = sorted(all_fnames)                # full dataset

        # 3️⃣  Build URLs → dl_manager.download() → local paths
        url_dict = {
            fname: hf_hub_url(
                _REPO_ID, f"images/{fname}", repo_type="dataset"
            )
            for fname in selected
        }
        img_paths = dl_manager.download(url_dict)        # same keys, but local files

        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={
                    "img_paths": img_paths,
                    "metadata": metadata,
                },
            )
        ]

    # --------------------------------------------------------------------- #
    # Generate examples                                                     #
    # --------------------------------------------------------------------- #
    def _generate_examples(self, img_paths: dict, metadata: dict):
        """
        Yields (key, example) where example =
        { "image": <local‑file‑path>, "actuated_angle": [int, int] }
        """
        for idx, (fname, local_path) in enumerate(img_paths.items()):
            meta = metadata.get(fname, {})
            angles = [int(meta.get("0", 0)), int(meta.get("1", 0))]
            yield idx, {"image": local_path, "actuated_angle": angles}