mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-30 13:09:40 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 45243dcf7c | |||
| 034693f724 | |||
| bb3ef3537f |
@@ -519,6 +519,13 @@ def compute_episode_stats(
|
|||||||
if features[key]["dtype"] in {"string", "language"}:
|
if features[key]["dtype"] in {"string", "language"}:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Features with a zero-width dimension contain no statistics-bearing
|
||||||
|
# values. Skip them like strings instead of letting
|
||||||
|
# get_feature_stats -> RunningQuantileStats.update reshape a size-0 array,
|
||||||
|
# which raises "ValueError: cannot reshape array of size 0".
|
||||||
|
if any(dim == 0 for dim in features[key].get("shape", ())):
|
||||||
|
continue
|
||||||
|
|
||||||
if features[key]["dtype"] in ["image", "video"]:
|
if features[key]["dtype"] in ["image", "video"]:
|
||||||
ep_ft_array = sample_images(data)
|
ep_ft_array = sample_images(data)
|
||||||
axes_to_reduce = (0, 2, 3)
|
axes_to_reduce = (0, 2, 3)
|
||||||
|
|||||||
@@ -64,12 +64,20 @@ def get_hf_features_from_features(features: dict) -> datasets.Features:
|
|||||||
continue
|
continue
|
||||||
elif ft["dtype"] == "image":
|
elif ft["dtype"] == "image":
|
||||||
hf_features[key] = datasets.Image()
|
hf_features[key] = datasets.Image()
|
||||||
|
elif len(ft["shape"]) > 1 and any(dim == 0 for dim in ft["shape"]):
|
||||||
|
raise ValueError(
|
||||||
|
f"Multidimensional features with a zero-width dimension are not supported: "
|
||||||
|
f"'{key}' has shape {ft['shape']}. Only the one-dimensional shape (0,) is supported."
|
||||||
|
)
|
||||||
elif ft["shape"] == (1,):
|
elif ft["shape"] == (1,):
|
||||||
hf_features[key] = datasets.Value(dtype=ft["dtype"])
|
hf_features[key] = datasets.Value(dtype=ft["dtype"])
|
||||||
elif len(ft["shape"]) == 1:
|
elif len(ft["shape"]) == 1:
|
||||||
hf_features[key] = datasets.Sequence(
|
# A zero-width feature (shape=(0,)) has no fixed-size Arrow representation:
|
||||||
length=ft["shape"][0], feature=datasets.Value(dtype=ft["dtype"])
|
# pyarrow rejects a fixed-size list of length 0 ("list_size needs to be a
|
||||||
)
|
# strict positive integer"). Store it as a variable-length sequence
|
||||||
|
# (length=-1) so each per-frame value is simply an empty list.
|
||||||
|
seq_length = ft["shape"][0] if ft["shape"][0] > 0 else -1
|
||||||
|
hf_features[key] = datasets.Sequence(length=seq_length, feature=datasets.Value(dtype=ft["dtype"]))
|
||||||
elif len(ft["shape"]) == 2:
|
elif len(ft["shape"]) == 2:
|
||||||
hf_features[key] = datasets.Array2D(shape=ft["shape"], dtype=ft["dtype"])
|
hf_features[key] = datasets.Array2D(shape=ft["shape"], dtype=ft["dtype"])
|
||||||
elif len(ft["shape"]) == 3:
|
elif len(ft["shape"]) == 3:
|
||||||
|
|||||||
@@ -687,6 +687,26 @@ def test_compute_episode_stats_string_features_skipped():
|
|||||||
assert "q01" in stats["action"]
|
assert "q01" in stats["action"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("shape", [(0,), (0, 2), (2, 0), (1, 0, 2)])
|
||||||
|
def test_compute_episode_stats_zero_width_feature_skipped(shape):
|
||||||
|
"""Features with any zero-width dimension carry no values and are skipped."""
|
||||||
|
episode_data = {
|
||||||
|
"action": np.random.normal(0, 1, (100, 5)).astype(np.float32),
|
||||||
|
"target": np.zeros((100, *shape), dtype=np.float32),
|
||||||
|
}
|
||||||
|
features = {
|
||||||
|
"action": {"dtype": "float32", "shape": (5,)},
|
||||||
|
"target": {"dtype": "float32", "shape": shape},
|
||||||
|
}
|
||||||
|
|
||||||
|
stats = compute_episode_stats(episode_data, features)
|
||||||
|
|
||||||
|
# Zero-width features are skipped, just like strings; non-empty features are unaffected.
|
||||||
|
assert "target" not in stats
|
||||||
|
assert "action" in stats
|
||||||
|
assert "q01" in stats["action"]
|
||||||
|
|
||||||
|
|
||||||
def test_aggregate_feature_stats_with_quantiles():
|
def test_aggregate_feature_stats_with_quantiles():
|
||||||
"""Test aggregating feature stats that include quantiles."""
|
"""Test aggregating feature stats that include quantiles."""
|
||||||
stats_ft_list = [
|
stats_ft_list = [
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ pytest.importorskip("datasets", reason="datasets is required (install lerobot[da
|
|||||||
|
|
||||||
from lerobot.configs import VideoEncoderConfig
|
from lerobot.configs import VideoEncoderConfig
|
||||||
from lerobot.datasets.dataset_writer import _encode_video_worker
|
from lerobot.datasets.dataset_writer import _encode_video_worker
|
||||||
|
from lerobot.datasets.feature_utils import get_hf_features_from_features
|
||||||
from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
||||||
from lerobot.datasets.utils import DEFAULT_IMAGE_PATH
|
from lerobot.datasets.utils import DEFAULT_IMAGE_PATH
|
||||||
from tests.fixtures.constants import DEFAULT_FPS, DUMMY_REPO_ID
|
from tests.fixtures.constants import DEFAULT_FPS, DUMMY_REPO_ID
|
||||||
@@ -189,6 +190,36 @@ def test_save_multiple_episodes(tmp_path):
|
|||||||
assert dataset.meta.total_frames == total_frames
|
assert dataset.meta.total_frames == total_frames
|
||||||
|
|
||||||
|
|
||||||
|
def test_save_episode_with_zero_width_feature(tmp_path):
|
||||||
|
"""A one-dimensional empty numeric feature round-trips and has no statistics."""
|
||||||
|
features = {
|
||||||
|
**SIMPLE_FEATURES,
|
||||||
|
"target": {"dtype": "float32", "shape": (0,), "names": None},
|
||||||
|
}
|
||||||
|
root = tmp_path / "ds"
|
||||||
|
dataset = LeRobotDataset.create(repo_id=DUMMY_REPO_ID, fps=DEFAULT_FPS, features=features, root=root)
|
||||||
|
for _ in range(4):
|
||||||
|
dataset.add_frame(_make_frame(features))
|
||||||
|
dataset.save_episode()
|
||||||
|
dataset.finalize()
|
||||||
|
|
||||||
|
assert dataset.meta.total_episodes == 1
|
||||||
|
assert dataset.meta.total_frames == 4
|
||||||
|
|
||||||
|
reloaded = LeRobotDataset(repo_id=DUMMY_REPO_ID, root=root)
|
||||||
|
target = np.asarray(reloaded[0]["target"])
|
||||||
|
assert target.shape == (0,)
|
||||||
|
assert "target" not in (reloaded.meta.stats or {})
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("shape", [(0, 2), (2, 0), (1, 0, 2)])
|
||||||
|
def test_multidimensional_zero_width_feature_rejected(shape):
|
||||||
|
features = {"target": {"dtype": "float32", "shape": shape, "names": None}}
|
||||||
|
|
||||||
|
with pytest.raises(ValueError, match="Multidimensional features with a zero-width dimension"):
|
||||||
|
get_hf_features_from_features(features)
|
||||||
|
|
||||||
|
|
||||||
# ── clear / lifecycle ────────────────────────────────────────────────
|
# ── clear / lifecycle ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user