mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-09 02:51:56 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c9d8e9de1 |
@@ -15,10 +15,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
import shutil
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
@@ -109,7 +107,6 @@ def update_meta_data(
|
||||
dst_meta,
|
||||
meta_idx,
|
||||
data_idx,
|
||||
data_file_map,
|
||||
videos_idx,
|
||||
):
|
||||
"""Updates metadata DataFrame with new chunk, file, and timestamp indices.
|
||||
@@ -130,25 +127,8 @@ def update_meta_data(
|
||||
|
||||
df["meta/episodes/chunk_index"] = df["meta/episodes/chunk_index"] + meta_idx["chunk"]
|
||||
df["meta/episodes/file_index"] = df["meta/episodes/file_index"] + meta_idx["file"]
|
||||
# Remap data chunk/file indices per-source-file using the actual destination
|
||||
# file chosen during data aggregation. A flat offset is incorrect when
|
||||
# multiple source files are concatenated into a single destination file.
|
||||
if data_file_map:
|
||||
new_data_chunk = []
|
||||
new_data_file = []
|
||||
for idx in df.index:
|
||||
src_chunk = int(df.at[idx, "data/chunk_index"]) # original source file location
|
||||
src_file = int(df.at[idx, "data/file_index"]) # original source file location
|
||||
dst_chunk, dst_file = data_file_map.get(
|
||||
(src_chunk, src_file), (src_chunk + data_idx["chunk"], src_file + data_idx["file"])
|
||||
)
|
||||
new_data_chunk.append(dst_chunk)
|
||||
new_data_file.append(dst_file)
|
||||
df["data/chunk_index"] = new_data_chunk
|
||||
df["data/file_index"] = new_data_file
|
||||
else:
|
||||
df["data/chunk_index"] = df["data/chunk_index"] + data_idx["chunk"]
|
||||
df["data/file_index"] = df["data/file_index"] + data_idx["file"]
|
||||
df["data/chunk_index"] = df["data/chunk_index"] + data_idx["chunk"]
|
||||
df["data/file_index"] = df["data/file_index"] + data_idx["file"]
|
||||
for key, video_idx in videos_idx.items():
|
||||
# Store original video file indices before updating
|
||||
orig_chunk_col = f"videos/{key}/chunk_index"
|
||||
@@ -186,7 +166,7 @@ def update_meta_data(
|
||||
return df
|
||||
|
||||
|
||||
def _aggregate_datasets(
|
||||
def aggregate_datasets(
|
||||
repo_ids: list[str],
|
||||
aggr_repo_id: str,
|
||||
roots: list[Path] | None = None,
|
||||
@@ -195,24 +175,39 @@ def _aggregate_datasets(
|
||||
video_files_size_in_mb: float | None = None,
|
||||
chunk_size: int | None = None,
|
||||
):
|
||||
"""Serial aggregation kernel: combines datasets into a destination dataset.
|
||||
"""Aggregates multiple LeRobot datasets into a single unified dataset.
|
||||
|
||||
This function performs a single-process aggregation. It assumes it is the
|
||||
sole writer for its destination `aggr_root`.
|
||||
This is the main function that orchestrates the aggregation process by:
|
||||
1. Loading and validating all source dataset metadata
|
||||
2. Creating a new destination dataset with unified tasks
|
||||
3. Aggregating videos, data, and metadata from all source datasets
|
||||
4. Finalizing the aggregated dataset with proper statistics
|
||||
|
||||
Args:
|
||||
repo_ids: List of repository IDs for the datasets to aggregate.
|
||||
aggr_repo_id: Repository ID for the aggregated output dataset.
|
||||
roots: Optional list of root paths for the source datasets.
|
||||
aggr_root: Optional root path for the aggregated dataset.
|
||||
data_files_size_in_mb: Maximum size for data files in MB (defaults to DEFAULT_DATA_FILE_SIZE_IN_MB)
|
||||
video_files_size_in_mb: Maximum size for video files in MB (defaults to DEFAULT_VIDEO_FILE_SIZE_IN_MB)
|
||||
chunk_size: Maximum number of files per chunk (defaults to DEFAULT_CHUNK_SIZE)
|
||||
"""
|
||||
# Build metadata objects, supporting a per-dataset "root" that may be None.
|
||||
# When root is provided we load from the local filesystem, otherwise from Hub cache.
|
||||
if roots is None:
|
||||
all_metadata = [LeRobotDatasetMetadata(repo_id) for repo_id in repo_ids]
|
||||
else:
|
||||
all_metadata = [
|
||||
(
|
||||
LeRobotDatasetMetadata(repo_id, root=root)
|
||||
if root is not None
|
||||
else LeRobotDatasetMetadata(repo_id)
|
||||
)
|
||||
for repo_id, root in zip(repo_ids, roots, strict=False)
|
||||
logging.info("Start aggregate_datasets")
|
||||
|
||||
if data_files_size_in_mb is None:
|
||||
data_files_size_in_mb = DEFAULT_DATA_FILE_SIZE_IN_MB
|
||||
if video_files_size_in_mb is None:
|
||||
video_files_size_in_mb = DEFAULT_VIDEO_FILE_SIZE_IN_MB
|
||||
if chunk_size is None:
|
||||
chunk_size = DEFAULT_CHUNK_SIZE
|
||||
|
||||
all_metadata = (
|
||||
[LeRobotDatasetMetadata(repo_id) for repo_id in repo_ids]
|
||||
if roots is None
|
||||
else [
|
||||
LeRobotDatasetMetadata(repo_id, root=root) for repo_id, root in zip(repo_ids, roots, strict=False)
|
||||
]
|
||||
)
|
||||
fps, robot_type, features = validate_all_metadata(all_metadata)
|
||||
video_keys = [key for key in features if features[key]["dtype"] == "video"]
|
||||
|
||||
@@ -242,11 +237,9 @@ def _aggregate_datasets(
|
||||
|
||||
for src_meta in tqdm.tqdm(all_metadata, desc="Copy data and videos"):
|
||||
videos_idx = aggregate_videos(src_meta, dst_meta, videos_idx, video_files_size_in_mb, chunk_size)
|
||||
data_idx, data_file_map = aggregate_data(
|
||||
src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_size
|
||||
)
|
||||
data_idx = aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_size)
|
||||
|
||||
meta_idx = aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, data_file_map, videos_idx)
|
||||
meta_idx = aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, videos_idx)
|
||||
|
||||
dst_meta.info["total_episodes"] += src_meta.total_episodes
|
||||
dst_meta.info["total_frames"] += src_meta.total_frames
|
||||
@@ -255,168 +248,6 @@ def _aggregate_datasets(
|
||||
logging.info("Aggregation complete.")
|
||||
|
||||
|
||||
def aggregate_datasets(
|
||||
repo_ids: list[str],
|
||||
aggr_repo_id: str,
|
||||
roots: list[Path] | None = None,
|
||||
aggr_root: Path | None = None,
|
||||
data_files_size_in_mb: float | None = None,
|
||||
video_files_size_in_mb: float | None = None,
|
||||
chunk_size: int | None = None,
|
||||
num_workers: int | None = None,
|
||||
tmp_root: Path | None = None,
|
||||
):
|
||||
"""Aggregates multiple LeRobot datasets into a single unified dataset.
|
||||
|
||||
This is the main function that orchestrates the aggregation process by:
|
||||
1. Loading and validating all source dataset metadata
|
||||
2. Creating a new destination dataset with unified tasks
|
||||
3. Aggregating videos, data, and metadata from all source datasets
|
||||
4. Finalizing the aggregated dataset with proper statistics
|
||||
|
||||
Args:
|
||||
repo_ids: List of repository IDs for the datasets to aggregate.
|
||||
aggr_repo_id: Repository ID for the aggregated output dataset.
|
||||
roots: Optional list of root paths for the source datasets.
|
||||
aggr_root: Optional root path for the aggregated dataset.
|
||||
data_files_size_in_mb: Maximum size for data files in MB (defaults to DEFAULT_DATA_FILE_SIZE_IN_MB)
|
||||
video_files_size_in_mb: Maximum size for video files in MB (defaults to DEFAULT_VIDEO_FILE_SIZE_IN_MB)
|
||||
chunk_size: Maximum number of files per chunk (defaults to DEFAULT_CHUNK_SIZE)
|
||||
num_workers: When > 1, performs a tree-based parallel reduction using a thread pool
|
||||
tmp_root: Optional base directory to store intermediate reduction outputs
|
||||
"""
|
||||
logging.info("Start aggregate_datasets")
|
||||
|
||||
if data_files_size_in_mb is None:
|
||||
data_files_size_in_mb = DEFAULT_DATA_FILE_SIZE_IN_MB
|
||||
if video_files_size_in_mb is None:
|
||||
video_files_size_in_mb = DEFAULT_VIDEO_FILE_SIZE_IN_MB
|
||||
if chunk_size is None:
|
||||
chunk_size = DEFAULT_CHUNK_SIZE
|
||||
|
||||
if num_workers is None or num_workers <= 1:
|
||||
# Run aggregation sequentially
|
||||
_aggregate_datasets(
|
||||
repo_ids=repo_ids,
|
||||
aggr_repo_id=aggr_repo_id,
|
||||
aggr_root=aggr_root,
|
||||
roots=roots,
|
||||
data_files_size_in_mb=data_files_size_in_mb,
|
||||
video_files_size_in_mb=video_files_size_in_mb,
|
||||
chunk_size=chunk_size,
|
||||
)
|
||||
|
||||
# Uses a parallel fan-out/fan-in strategy when num_workers is provided
|
||||
elif num_workers > 1:
|
||||
# Validate across all metadata early to fail fast
|
||||
all_metadata_for_validation = (
|
||||
[LeRobotDatasetMetadata(repo_id) for repo_id in repo_ids]
|
||||
if roots is None
|
||||
else [
|
||||
LeRobotDatasetMetadata(repo_id, root=root)
|
||||
for repo_id, root in zip(repo_ids, roots, strict=False)
|
||||
]
|
||||
)
|
||||
validate_all_metadata(all_metadata_for_validation)
|
||||
|
||||
# Clamp workers to a sensible upper bound (pairs per round)
|
||||
num_workers = min(num_workers, max(1, len(repo_ids) // 2))
|
||||
|
||||
# Choose a base temporary root for intermediate merge results
|
||||
if tmp_root is not None:
|
||||
base_tmp_root = tmp_root
|
||||
elif aggr_root is not None:
|
||||
base_tmp_root = aggr_root.parent / f".{aggr_repo_id}__tmp"
|
||||
else:
|
||||
base_tmp_root = Path.cwd() / f".{aggr_repo_id}__tmp"
|
||||
base_tmp_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
current_repo_ids: list[str] = list(repo_ids)
|
||||
# Always maintain a roots list aligned with repo_ids. Use None for Hub-backed inputs.
|
||||
current_roots: list[Path | None] = list(roots) if roots is not None else [None] * len(repo_ids)
|
||||
|
||||
try:
|
||||
level = 0
|
||||
while len(current_repo_ids) > 1:
|
||||
next_repo_ids: list[str] = []
|
||||
next_roots: list[Path | None] = []
|
||||
futures = []
|
||||
|
||||
with ThreadPoolExecutor(max_workers=num_workers) as executor:
|
||||
group_index = 0
|
||||
i = 0
|
||||
while i < len(current_repo_ids):
|
||||
group_repo_ids = current_repo_ids[i : i + 2]
|
||||
group_roots = current_roots[i : i + 2]
|
||||
|
||||
if len(group_repo_ids) == 1:
|
||||
# Carry over singleton to next level
|
||||
next_repo_ids.append(group_repo_ids[0])
|
||||
next_roots.append(group_roots[0])
|
||||
i += 1
|
||||
continue
|
||||
|
||||
out_repo_id = f"{aggr_repo_id}__reduce_l{level}_g{group_index}"
|
||||
out_root = base_tmp_root / f"reduce_l{level}_g{group_index}"
|
||||
|
||||
futures.append(
|
||||
executor.submit(
|
||||
_aggregate_datasets,
|
||||
group_repo_ids,
|
||||
out_repo_id,
|
||||
group_roots,
|
||||
out_root,
|
||||
data_files_size_in_mb,
|
||||
video_files_size_in_mb,
|
||||
chunk_size,
|
||||
)
|
||||
)
|
||||
|
||||
next_repo_ids.append(out_repo_id)
|
||||
next_roots.append(out_root)
|
||||
|
||||
i += 2
|
||||
group_index += 1
|
||||
|
||||
for f in as_completed(futures):
|
||||
# Bubble up any exception raised inside tasks
|
||||
f.result()
|
||||
|
||||
# Cleanup previous level temporary outputs that won't be used again
|
||||
base_resolved = base_tmp_root.resolve()
|
||||
keep_set = {nr.resolve() for nr in next_roots if nr is not None}
|
||||
for prev_root in current_roots:
|
||||
if prev_root is None:
|
||||
continue
|
||||
# Suppress per-iteration to keep cleaning other roots even if one fails
|
||||
with contextlib.suppress(Exception):
|
||||
pr = prev_root.resolve()
|
||||
if pr not in keep_set and base_resolved in pr.parents:
|
||||
shutil.rmtree(prev_root, ignore_errors=True)
|
||||
|
||||
current_repo_ids = next_repo_ids
|
||||
current_roots = next_roots # aligned list of Path|None after first level
|
||||
level += 1
|
||||
|
||||
# Final copy/aggregation into the desired output
|
||||
_aggregate_datasets(
|
||||
repo_ids=current_repo_ids,
|
||||
aggr_repo_id=aggr_repo_id,
|
||||
roots=current_roots,
|
||||
aggr_root=aggr_root,
|
||||
data_files_size_in_mb=data_files_size_in_mb,
|
||||
video_files_size_in_mb=video_files_size_in_mb,
|
||||
chunk_size=chunk_size,
|
||||
)
|
||||
finally:
|
||||
# Remove all temporary reduction artifacts
|
||||
with contextlib.suppress(Exception):
|
||||
shutil.rmtree(base_tmp_root, ignore_errors=True)
|
||||
|
||||
logging.info("Aggregation complete.")
|
||||
return
|
||||
|
||||
|
||||
def aggregate_videos(src_meta, dst_meta, videos_idx, video_files_size_in_mb, chunk_size):
|
||||
"""Aggregates video chunks from a source dataset into the destination dataset.
|
||||
|
||||
@@ -535,9 +366,6 @@ def aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_si
|
||||
|
||||
unique_chunk_file_ids = sorted(unique_chunk_file_ids)
|
||||
|
||||
# Map source (chunk,file) -> destination (chunk,file) actually used during write
|
||||
src_to_dst_file: dict[tuple[int, int], tuple[int, int]] = {}
|
||||
|
||||
for src_chunk_idx, src_file_idx in unique_chunk_file_ids:
|
||||
src_path = src_meta.root / DEFAULT_DATA_PATH.format(
|
||||
chunk_index=src_chunk_idx, file_index=src_file_idx
|
||||
@@ -545,7 +373,7 @@ def aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_si
|
||||
df = pd.read_parquet(src_path)
|
||||
df = update_data_df(df, src_meta, dst_meta)
|
||||
|
||||
data_idx, used_chunk, used_file = append_or_create_parquet_file(
|
||||
data_idx = append_or_create_parquet_file(
|
||||
df,
|
||||
src_path,
|
||||
data_idx,
|
||||
@@ -555,12 +383,11 @@ def aggregate_data(src_meta, dst_meta, data_idx, data_files_size_in_mb, chunk_si
|
||||
contains_images=len(dst_meta.image_keys) > 0,
|
||||
aggr_root=dst_meta.root,
|
||||
)
|
||||
src_to_dst_file[(src_chunk_idx, src_file_idx)] = (used_chunk, used_file)
|
||||
|
||||
return data_idx, src_to_dst_file
|
||||
return data_idx
|
||||
|
||||
|
||||
def aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, data_file_map, videos_idx):
|
||||
def aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, videos_idx):
|
||||
"""Aggregates metadata from a source dataset into the destination dataset.
|
||||
|
||||
Reads source metadata files, updates all indices and timestamps,
|
||||
@@ -594,11 +421,10 @@ def aggregate_metadata(src_meta, dst_meta, meta_idx, data_idx, data_file_map, vi
|
||||
dst_meta,
|
||||
meta_idx,
|
||||
data_idx,
|
||||
data_file_map,
|
||||
videos_idx,
|
||||
)
|
||||
|
||||
meta_idx, _m_used_chunk, _m_used_file = append_or_create_parquet_file(
|
||||
meta_idx = append_or_create_parquet_file(
|
||||
df,
|
||||
src_path,
|
||||
meta_idx,
|
||||
@@ -652,7 +478,7 @@ def append_or_create_parquet_file(
|
||||
to_parquet_with_hf_images(df, dst_path)
|
||||
else:
|
||||
df.to_parquet(dst_path)
|
||||
return idx, idx["chunk"], idx["file"]
|
||||
return idx
|
||||
|
||||
src_size = get_parquet_file_size_in_mb(src_path)
|
||||
dst_size = get_parquet_file_size_in_mb(dst_path)
|
||||
@@ -663,19 +489,17 @@ def append_or_create_parquet_file(
|
||||
new_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
final_df = df
|
||||
target_path = new_path
|
||||
used_chunk, used_file = idx["chunk"], idx["file"]
|
||||
else:
|
||||
existing_df = pd.read_parquet(dst_path)
|
||||
final_df = pd.concat([existing_df, df], ignore_index=True)
|
||||
target_path = dst_path
|
||||
used_chunk, used_file = idx["chunk"], idx["file"]
|
||||
|
||||
if contains_images:
|
||||
to_parquet_with_hf_images(final_df, target_path)
|
||||
else:
|
||||
final_df.to_parquet(target_path)
|
||||
|
||||
return idx, used_chunk, used_file
|
||||
return idx
|
||||
|
||||
|
||||
def finalize_aggregation(aggr_meta, all_metadata):
|
||||
|
||||
@@ -39,7 +39,6 @@ from lerobot.datasets.aggregate import aggregate_datasets
|
||||
from lerobot.datasets.compute_stats import aggregate_stats
|
||||
from lerobot.datasets.lerobot_dataset import LeRobotDataset, LeRobotDatasetMetadata
|
||||
from lerobot.datasets.utils import (
|
||||
DATA_DIR,
|
||||
DEFAULT_CHUNK_SIZE,
|
||||
DEFAULT_DATA_FILE_SIZE_IN_MB,
|
||||
DEFAULT_DATA_PATH,
|
||||
@@ -234,7 +233,6 @@ def merge_datasets(
|
||||
datasets: list[LeRobotDataset],
|
||||
output_repo_id: str,
|
||||
output_dir: str | Path | None = None,
|
||||
num_workers: int | None = None,
|
||||
) -> LeRobotDataset:
|
||||
"""Merge multiple LeRobotDatasets into a single dataset.
|
||||
|
||||
@@ -258,7 +256,6 @@ def merge_datasets(
|
||||
aggr_repo_id=output_repo_id,
|
||||
roots=roots,
|
||||
aggr_root=output_dir,
|
||||
num_workers=num_workers,
|
||||
)
|
||||
|
||||
merged_dataset = LeRobotDataset(
|
||||
@@ -331,7 +328,7 @@ def modify_features(
|
||||
|
||||
if repo_id is None:
|
||||
repo_id = f"{dataset.repo_id}_modified"
|
||||
output_dir = Path(output_dir, exists_ok=True) if output_dir is not None else HF_LEROBOT_HOME / repo_id
|
||||
output_dir = Path(output_dir) if output_dir is not None else HF_LEROBOT_HOME / repo_id
|
||||
|
||||
new_features = dataset.meta.features.copy()
|
||||
|
||||
@@ -965,23 +962,28 @@ def _copy_data_with_feature_changes(
|
||||
remove_features: list[str] | None = None,
|
||||
) -> None:
|
||||
"""Copy data while adding or removing features."""
|
||||
data_dir = dataset.root / DATA_DIR
|
||||
parquet_files = sorted(data_dir.glob("*/*.parquet"))
|
||||
if dataset.meta.episodes is None:
|
||||
dataset.meta.episodes = load_episodes(dataset.meta.root)
|
||||
|
||||
if not parquet_files:
|
||||
raise ValueError(f"No parquet files found in {data_dir}")
|
||||
# Map file paths to episode indices to extract chunk/file indices
|
||||
file_to_episodes: dict[Path, set[int]] = {}
|
||||
for ep_idx in range(dataset.meta.total_episodes):
|
||||
file_path = dataset.meta.get_data_file_path(ep_idx)
|
||||
if file_path not in file_to_episodes:
|
||||
file_to_episodes[file_path] = set()
|
||||
file_to_episodes[file_path].add(ep_idx)
|
||||
|
||||
frame_idx = 0
|
||||
|
||||
for src_path in tqdm(parquet_files, desc="Processing data files"):
|
||||
df = pd.read_parquet(src_path).reset_index(drop=True)
|
||||
for src_path in tqdm(sorted(file_to_episodes.keys()), desc="Processing data files"):
|
||||
df = pd.read_parquet(dataset.root / src_path).reset_index(drop=True)
|
||||
|
||||
relative_path = src_path.relative_to(dataset.root)
|
||||
chunk_dir = relative_path.parts[1]
|
||||
file_name = relative_path.parts[2]
|
||||
|
||||
chunk_idx = int(chunk_dir.split("-")[1])
|
||||
file_idx = int(file_name.split("-")[1].split(".")[0])
|
||||
# Get chunk_idx and file_idx from the source file's first episode
|
||||
episodes_in_file = file_to_episodes[src_path]
|
||||
first_ep_idx = min(episodes_in_file)
|
||||
src_ep = dataset.meta.episodes[first_ep_idx]
|
||||
chunk_idx = src_ep["data/chunk_index"]
|
||||
file_idx = src_ep["data/file_index"]
|
||||
|
||||
if remove_features:
|
||||
df = df.drop(columns=remove_features, errors="ignore")
|
||||
@@ -1007,7 +1009,7 @@ def _copy_data_with_feature_changes(
|
||||
df[feature_name] = feature_slice
|
||||
frame_idx = end_idx
|
||||
|
||||
# Write using the same chunk/file structure as source
|
||||
# Write using the preserved chunk_idx and file_idx from source
|
||||
dst_path = new_meta.root / DEFAULT_DATA_PATH.format(chunk_index=chunk_idx, file_index=file_idx)
|
||||
dst_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
@@ -940,26 +940,11 @@ class LeRobotDataset(torch.utils.data.Dataset):
|
||||
return query_timestamps
|
||||
|
||||
def _query_hf_dataset(self, query_indices: dict[str, list[int]]) -> dict:
|
||||
"""
|
||||
Query dataset for indices across keys, skipping video keys.
|
||||
|
||||
Tries column-first [key][indices] for speed, falls back to row-first.
|
||||
|
||||
Args:
|
||||
query_indices: Dict mapping keys to index lists to retrieve
|
||||
|
||||
Returns:
|
||||
Dict with stacked tensors of queried data (video keys excluded)
|
||||
"""
|
||||
result: dict = {}
|
||||
for key, q_idx in query_indices.items():
|
||||
if key in self.meta.video_keys:
|
||||
continue
|
||||
try:
|
||||
result[key] = torch.stack(self.hf_dataset[key][q_idx])
|
||||
except (KeyError, TypeError, IndexError):
|
||||
result[key] = torch.stack(self.hf_dataset[q_idx][key])
|
||||
return result
|
||||
return {
|
||||
key: torch.stack(self.hf_dataset[q_idx][key])
|
||||
for key, q_idx in query_indices.items()
|
||||
if key not in self.meta.video_keys
|
||||
}
|
||||
|
||||
def _query_videos(self, query_timestamps: dict[str, list[float]], ep_idx: int) -> dict[str, torch.Tensor]:
|
||||
"""Note: When using data workers (e.g. DataLoader with num_workers>0), do not call this function
|
||||
|
||||
@@ -103,7 +103,6 @@ class SplitConfig:
|
||||
class MergeConfig:
|
||||
type: str = "merge"
|
||||
repo_ids: list[str] | None = None
|
||||
num_workers: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -216,7 +215,6 @@ def handle_merge(cfg: EditDatasetConfig) -> None:
|
||||
datasets,
|
||||
output_repo_id=cfg.repo_id,
|
||||
output_dir=output_dir,
|
||||
num_workers=cfg.operation.num_workers,
|
||||
)
|
||||
|
||||
logging.info(f"Merged dataset saved to {output_dir}")
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from .config_custom import CustomConfig
|
||||
from .custom import Custom
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from ..config import TeleoperatorConfig
|
||||
|
||||
|
||||
@TeleoperatorConfig.register_subclass("custom")
|
||||
@dataclass
|
||||
class CustomConfig(TeleoperatorConfig):
|
||||
"""Custom teleoperator config that dynamically wraps a base teleoperator class.
|
||||
|
||||
The base class and its configuration are loaded from a JSON config file at runtime.
|
||||
Port and baud_rate are taken from the first device in the config file.
|
||||
"""
|
||||
config_path: str | None = None # REQUIRED: Path to custom config JSON file
|
||||
port: str = "/dev/ttyACM0" # Default port
|
||||
baud_rate: int = 115200 # Default baud rate
|
||||
@@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from lerobot.motors.motors_bus import MotorNormMode
|
||||
|
||||
from ..teleoperator import Teleoperator
|
||||
from .config_custom import CustomConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Custom(Teleoperator):
|
||||
"""
|
||||
Custom teleoperator that dynamically wraps a base teleoperator class and applies configurable joint mapping.
|
||||
The base class is specified in custom_config.json, allowing flexible teleoperator configurations.
|
||||
"""
|
||||
|
||||
config_class = CustomConfig
|
||||
name = "custom"
|
||||
|
||||
def __init__(self, config: CustomConfig):
|
||||
# Load custom configuration from JSON file
|
||||
if config.config_path is None:
|
||||
raise ValueError(
|
||||
"config_path must be provided for custom teleoperator. "
|
||||
"Example: --teleop.config_path=/path/to/custom_config.json"
|
||||
)
|
||||
|
||||
config_path = Path(config.config_path)
|
||||
|
||||
with open(config_path) as f:
|
||||
custom_config = json.load(f)
|
||||
|
||||
logger.info(f"Loaded custom config from {config_path}")
|
||||
logger.info(f"Found {len(custom_config)} teleoperator(s): {list(custom_config.keys())}")
|
||||
|
||||
# Initialize the base Teleoperator class
|
||||
super().__init__(config)
|
||||
|
||||
# Store multiple base teleoperators and their action mappings
|
||||
self.base_teleops = {}
|
||||
self.robot_actions_configs = {}
|
||||
|
||||
# Instantiate each base teleoperator from the config
|
||||
for device_name, device_config in custom_config.items():
|
||||
base_class_name = device_config["base_class"]
|
||||
|
||||
# Create a config copy for this teleoperator
|
||||
from dataclasses import replace
|
||||
teleop_config = replace(
|
||||
config,
|
||||
port=device_config.get("port", config.port),
|
||||
id=device_config.get("id", f"{config.id}_{device_name}"),
|
||||
baud_rate=device_config.get("baud_rate", config.baud_rate)
|
||||
)
|
||||
|
||||
logger.info(f" {device_name}: class={base_class_name}, port={teleop_config.port}, id={teleop_config.id}")
|
||||
|
||||
# Dynamically import and instantiate the base teleoperator class
|
||||
module_path, class_name_full = base_class_name.rsplit(".", 1)
|
||||
module = importlib.import_module(module_path)
|
||||
base_class = getattr(module, class_name_full)
|
||||
|
||||
# Store the teleoperator and its action mapping
|
||||
self.base_teleops[device_name] = base_class(teleop_config)
|
||||
self.robot_actions_configs[device_name] = device_config["robot_actions"]
|
||||
|
||||
@property
|
||||
def action_features(self) -> dict:
|
||||
# Aggregate action features from all teleoperators' action mappings
|
||||
all_actions = {}
|
||||
for device_config in self.robot_actions_configs.values():
|
||||
for robot_action in device_config.keys():
|
||||
all_actions[robot_action] = float
|
||||
return all_actions
|
||||
|
||||
@property
|
||||
def feedback_features(self) -> dict:
|
||||
# Aggregate feedback features from all base teleoperators
|
||||
all_feedback = {}
|
||||
for teleop in self.base_teleops.values():
|
||||
all_feedback.update(teleop.feedback_features)
|
||||
return all_feedback
|
||||
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
# All teleoperators must be connected
|
||||
return all(teleop.is_connected for teleop in self.base_teleops.values())
|
||||
|
||||
@property
|
||||
def is_calibrated(self) -> bool:
|
||||
# All teleoperators must be calibrated
|
||||
return all(teleop.is_calibrated for teleop in self.base_teleops.values())
|
||||
|
||||
def connect(self, calibrate: bool = True) -> None:
|
||||
# Connect all base teleoperators
|
||||
for device_name, teleop in self.base_teleops.items():
|
||||
logger.info(f"Connecting {device_name}...")
|
||||
teleop.connect(calibrate=calibrate)
|
||||
|
||||
def calibrate(self) -> None:
|
||||
# Calibrate all base teleoperators
|
||||
for device_name, teleop in self.base_teleops.items():
|
||||
logger.info(f"Calibrating {device_name}...")
|
||||
teleop.calibrate()
|
||||
|
||||
def configure(self) -> None:
|
||||
# Configure all base teleoperators
|
||||
for teleop in self.base_teleops.values():
|
||||
teleop.configure()
|
||||
|
||||
def send_feedback(self, feedback: dict[str, float]) -> None:
|
||||
# Send feedback to all base teleoperators
|
||||
for teleop in self.base_teleops.values():
|
||||
teleop.send_feedback(feedback)
|
||||
|
||||
def disconnect(self) -> None:
|
||||
# Disconnect all base teleoperators
|
||||
for device_name, teleop in self.base_teleops.items():
|
||||
logger.info(f"Disconnecting {device_name}...")
|
||||
teleop.disconnect()
|
||||
|
||||
def _normalize_to_unit_range(self, teleop, joint_name: str, value: float) -> float:
|
||||
"""Convert a joint value from base teleoperator's normalization mode to [0, 1] range.
|
||||
|
||||
Args:
|
||||
teleop: The base teleoperator instance
|
||||
joint_name: Name of the joint (e.g., "shoulder_pitch")
|
||||
value: Value in the base teleoperator's normalization mode
|
||||
|
||||
Returns:
|
||||
Value normalized to [0, 1] range
|
||||
"""
|
||||
norm_mode = teleop.joints[joint_name]
|
||||
|
||||
if norm_mode == MotorNormMode.RANGE_M100_100:
|
||||
# Convert from [-100, 100] to [0, 1]
|
||||
return (value + 100.0) / 200.0
|
||||
elif norm_mode == MotorNormMode.RANGE_0_100:
|
||||
# Convert from [0, 100] to [0, 1]
|
||||
return value / 100.0
|
||||
elif norm_mode == MotorNormMode.DEGREES:
|
||||
# For degrees, we need calibration to know the range
|
||||
# Use calibration min/max to normalize
|
||||
if teleop.calibration and joint_name in teleop.calibration:
|
||||
min_deg = teleop.calibration[joint_name].range_min
|
||||
max_deg = teleop.calibration[joint_name].range_max
|
||||
if max_deg != min_deg:
|
||||
return (value - min_deg) / (max_deg - min_deg)
|
||||
# Fallback: assume common range like [-180, 180]
|
||||
return (value + 180.0) / 360.0
|
||||
else:
|
||||
raise ValueError(f"Unknown normalization mode: {norm_mode}")
|
||||
|
||||
def get_action(self) -> dict[str, float]:
|
||||
# Build action dict by reading from all base teleoperators
|
||||
action = {}
|
||||
|
||||
# Loop through each teleoperator
|
||||
for device_name, teleop in self.base_teleops.items():
|
||||
# Read joint positions from this teleoperator
|
||||
# These are in the teleoperator's normalization mode (e.g., -100 to 100)
|
||||
joint_positions = teleop._read()
|
||||
|
||||
# Get the robot actions config for this teleoperator
|
||||
robot_actions_config = self.robot_actions_configs[device_name]
|
||||
|
||||
# Process each robot action for this teleoperator
|
||||
for robot_action, config in robot_actions_config.items():
|
||||
if config["source"] == "neutral":
|
||||
# Use fixed neutral value (already in [0, 1] range)
|
||||
value = config["value"]
|
||||
elif config["source"] == "teleop":
|
||||
# Get value from teleop joint
|
||||
teleop_joint = config["joint"]
|
||||
value = joint_positions[teleop_joint]
|
||||
|
||||
# Convert from base teleoperator's normalization mode to [0, 1] range
|
||||
value = self._normalize_to_unit_range(teleop, teleop_joint, value)
|
||||
|
||||
# Apply inversion if specified
|
||||
if config.get("invert", False):
|
||||
value = 1.0 - value
|
||||
else:
|
||||
raise ValueError(f"Unknown source '{config['source']}' for robot action '{robot_action}'")
|
||||
|
||||
action[robot_action] = value
|
||||
return action
|
||||
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"right_arm": {
|
||||
"base_class": "lerobot.teleoperators.homunculus.homunculus_arm.HomunculusArm",
|
||||
"port": "/dev/ttyACM0",
|
||||
"id": "unitree_right",
|
||||
"baud_rate": 115200,
|
||||
"robot_actions": {
|
||||
"kRightShoulderPitch.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kRightShoulderRoll.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kRightShoulderYaw.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kRightElbow.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kRightWristRoll.pos": {
|
||||
"source": "teleop",
|
||||
"joint": "wrist_roll",
|
||||
"invert": true
|
||||
},
|
||||
"kRightWristPitch.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kRightWristYaw.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
}
|
||||
}
|
||||
},
|
||||
"left_arm": {
|
||||
"base_class": "lerobot.teleoperators.homunculus.homunculus_arm.HomunculusArm",
|
||||
"port": "/dev/ttyACM1",
|
||||
"id": "unitree_left",
|
||||
"baud_rate": 115200,
|
||||
"robot_actions": {
|
||||
"kLeftShoulderPitch.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kLeftShoulderRoll.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kLeftShoulderYaw.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kLeftElbow.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kLeftWristRoll.pos": {
|
||||
"source": "teleop",
|
||||
"joint": "wrist_roll",
|
||||
"invert": true
|
||||
},
|
||||
"kLeftWristPitch.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
},
|
||||
"kLeftWristyaw.pos": {
|
||||
"source": "neutral",
|
||||
"value": 0.5
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user