mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-26 11:16:00 +00:00
feat(dataset): add slice support to LeRobotDataset.__getitem__ (#4129)
* feat(dataset): add efficient slice support * fix(dataset): handle empty dataset slices * refactor(dataset): reuse scalar path for slices --------- Co-authored-by: Francesco Capuano <fc.francescocapuano@gmail.com>
This commit is contained in:
@@ -478,18 +478,19 @@ class LeRobotDataset(torch.utils.data.Dataset):
|
||||
"""Return the number of frames in the selected episodes."""
|
||||
return self.num_frames
|
||||
|
||||
def __getitem__(self, idx) -> dict:
|
||||
"""Return a single frame by index, with all transforms applied.
|
||||
def __getitem__(self, idx: int | slice) -> dict | list[dict]:
|
||||
"""Return one frame or a slice of frames, with all transforms applied.
|
||||
|
||||
Loads the frame from the underlying HF dataset, expands delta-timestamp
|
||||
windows, decodes video frames, and applies image transforms. Delegates
|
||||
the core logic to :meth:`DatasetReader.get_item`.
|
||||
the core logic to :class:`DatasetReader`.
|
||||
|
||||
Args:
|
||||
idx: Index into the (possibly episode-filtered) dataset.
|
||||
idx: Integer index or slice into the possibly episode-filtered dataset.
|
||||
|
||||
Returns:
|
||||
Dict mapping feature names to their tensor values for this frame.
|
||||
A frame dictionary for an integer index, or a list of frame
|
||||
dictionaries for a slice.
|
||||
|
||||
Raises:
|
||||
RuntimeError: If the dataset is currently being recorded and
|
||||
@@ -499,6 +500,9 @@ class LeRobotDataset(torch.utils.data.Dataset):
|
||||
raise RuntimeError(
|
||||
"Cannot read from a dataset that is being recorded. Call finalize() first, then access items."
|
||||
)
|
||||
if isinstance(idx, slice):
|
||||
return [self[item_idx] for item_idx in range(*idx.indices(len(self)))]
|
||||
|
||||
reader = self._ensure_reader()
|
||||
if reader.hf_dataset is None:
|
||||
# One-shot load after finalize()
|
||||
|
||||
Reference in New Issue
Block a user