Add
Browse files- image_dataset.py +95 -0
- images/frame_0a0ab072-9729-4e16-88c5-a3ea9ed959d0_0036.png +3 -0
- images/frame_0a0bf5c3-9ee4-41aa-9298-ccfebdb1b5ea_0029.png +3 -0
- images/frame_0a1d30e1-d358-4533-a192-75c5c86a2d56_0049.png +3 -0
- images/frame_0a1d5ccc-e1c7-4c66-8fbc-f87080c3f408_0046.png +3 -0
- metadata.json +18 -0
image_dataset.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
from datasets import (
|
| 5 |
+
BuilderConfig,
|
| 6 |
+
DatasetInfo,
|
| 7 |
+
DownloadManager,
|
| 8 |
+
GeneratorBasedBuilder,
|
| 9 |
+
SplitGenerator,
|
| 10 |
+
Split,
|
| 11 |
+
Features,
|
| 12 |
+
Image,
|
| 13 |
+
Sequence,
|
| 14 |
+
Value,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
class ImageSubsetConfig(BuilderConfig):
|
| 18 |
+
"""BuilderConfig for small vs. full dataset."""
|
| 19 |
+
def __init__(self, name, sample_size=None, **kwargs):
|
| 20 |
+
super().__init__(
|
| 21 |
+
name=name,
|
| 22 |
+
version="1.0.0",
|
| 23 |
+
description=kwargs.get("description", "")
|
| 24 |
+
)
|
| 25 |
+
self.sample_size = sample_size
|
| 26 |
+
|
| 27 |
+
class MyImageDataset(GeneratorBasedBuilder):
|
| 28 |
+
"""A dataset of images with actuated_angle from metadata.json."""
|
| 29 |
+
BUILDER_CONFIGS = [
|
| 30 |
+
ImageSubsetConfig(
|
| 31 |
+
name="full",
|
| 32 |
+
sample_size=None,
|
| 33 |
+
description="All images (≈100 GB). Use with streaming=True."
|
| 34 |
+
),
|
| 35 |
+
ImageSubsetConfig(
|
| 36 |
+
name="small",
|
| 37 |
+
sample_size=2,
|
| 38 |
+
description="Random subset of 10 000 images for quick iteration."
|
| 39 |
+
),
|
| 40 |
+
]
|
| 41 |
+
DEFAULT_CONFIG_NAME = "smgit all"
|
| 42 |
+
|
| 43 |
+
def _info(self):
|
| 44 |
+
return DatasetInfo(
|
| 45 |
+
description="Images with a 2-D actuated_angle from metadata.json",
|
| 46 |
+
features=Features({
|
| 47 |
+
"image": Image(), # the image file
|
| 48 |
+
"actuated_angle": Sequence(Value("int32")), # [angle0, angle1]
|
| 49 |
+
}),
|
| 50 |
+
supervised_keys=None,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
def _split_generators(self, dl_manager: DownloadManager):
|
| 54 |
+
data_dir = dl_manager.manual_dir or os.path.abspath(os.path.dirname(__file__))
|
| 55 |
+
images_dir = os.path.join(data_dir, "images")
|
| 56 |
+
meta_path = os.path.join(data_dir, "metadata.json")
|
| 57 |
+
|
| 58 |
+
# load your filename → {"0":…, "1":…} mapping
|
| 59 |
+
with open(meta_path, "r", encoding="utf-8") as f:
|
| 60 |
+
metadata = json.load(f)
|
| 61 |
+
|
| 62 |
+
# sample if in "small" config
|
| 63 |
+
if self.config.sample_size:
|
| 64 |
+
all_fnames = list(metadata.keys())
|
| 65 |
+
random.seed(42)
|
| 66 |
+
selected = set(random.sample(all_fnames, self.config.sample_size))
|
| 67 |
+
else:
|
| 68 |
+
selected = set(metadata.keys())
|
| 69 |
+
|
| 70 |
+
return [
|
| 71 |
+
SplitGenerator(
|
| 72 |
+
name=Split.TRAIN,
|
| 73 |
+
gen_kwargs={
|
| 74 |
+
"images_dir": images_dir,
|
| 75 |
+
"metadata": metadata,
|
| 76 |
+
"selected": selected,
|
| 77 |
+
},
|
| 78 |
+
),
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
def _generate_examples(self, images_dir, metadata, selected):
|
| 82 |
+
"""Yields examples of the form { "image": path, "actuated_angle": [int, int] }"""
|
| 83 |
+
for idx, fname in enumerate(selected):
|
| 84 |
+
if fname not in metadata:
|
| 85 |
+
continue
|
| 86 |
+
image_path = os.path.join(images_dir, fname)
|
| 87 |
+
if not os.path.exists(image_path):
|
| 88 |
+
continue
|
| 89 |
+
meta = metadata[fname]
|
| 90 |
+
# extract the two angle values (keys "0" and "1")
|
| 91 |
+
angles = [ int(meta.get("0", 0)), int(meta.get("1", 0)) ]
|
| 92 |
+
yield idx, {
|
| 93 |
+
"image": image_path,
|
| 94 |
+
"actuated_angle": angles,
|
| 95 |
+
}
|
images/frame_0a0ab072-9729-4e16-88c5-a3ea9ed959d0_0036.png
ADDED
|
Git LFS Details
|
images/frame_0a0bf5c3-9ee4-41aa-9298-ccfebdb1b5ea_0029.png
ADDED
|
Git LFS Details
|
images/frame_0a1d30e1-d358-4533-a192-75c5c86a2d56_0049.png
ADDED
|
Git LFS Details
|
images/frame_0a1d5ccc-e1c7-4c66-8fbc-f87080c3f408_0046.png
ADDED
|
Git LFS Details
|
metadata.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"frame_0a0ab072-9729-4e16-88c5-a3ea9ed959d0_0036.png": {
|
| 3 |
+
"0": 90,
|
| 4 |
+
"1": 20
|
| 5 |
+
},
|
| 6 |
+
"frame_0a1d5ccc-e1c7-4c66-8fbc-f87080c3f408_0046.png": {
|
| 7 |
+
"0": 90,
|
| 8 |
+
"1": 20
|
| 9 |
+
},
|
| 10 |
+
"frame_0a1d30e1-d358-4533-a192-75c5c86a2d56_0049.png": {
|
| 11 |
+
"0": 90,
|
| 12 |
+
"1": 20
|
| 13 |
+
},
|
| 14 |
+
"frame_0a0bf5c3-9ee4-41aa-9298-ccfebdb1b5ea_0029.png": {
|
| 15 |
+
"0": 90,
|
| 16 |
+
"1": 20
|
| 17 |
+
}
|
| 18 |
+
}
|