|
|
|
|
|
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" |
|
|
|
|
|
|
|
|
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, |
|
|
description="Entire dataset (≈100 GB)" |
|
|
), |
|
|
ImageSubsetConfig( |
|
|
name="small", |
|
|
sample_size=2, |
|
|
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(), |
|
|
"actuated_angle": Sequence(Value("int32")), |
|
|
} |
|
|
), |
|
|
supervised_keys=None, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _split_generators(self, dl_manager: DownloadManager): |
|
|
|
|
|
meta_path = dl_manager.download( |
|
|
hf_hub_url(_REPO_ID, "metadata.json", repo_type="dataset") |
|
|
) |
|
|
|
|
|
|
|
|
with open(meta_path, encoding="utf-8") as f: |
|
|
metadata = json.load(f) |
|
|
|
|
|
all_fnames = list(metadata) |
|
|
if self.config.sample_size: |
|
|
random.seed(42) |
|
|
selected = sorted(random.sample(all_fnames, self.config.sample_size)) |
|
|
else: |
|
|
selected = sorted(all_fnames) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
return [ |
|
|
SplitGenerator( |
|
|
name=Split.TRAIN, |
|
|
gen_kwargs={ |
|
|
"img_paths": img_paths, |
|
|
"metadata": metadata, |
|
|
}, |
|
|
) |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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} |
|
|
|