2024-05-15 18:13:09 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright 2024 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.
|
2024-04-23 20:13:25 +08:00
|
|
|
import json
|
2024-04-18 20:47:42 +08:00
|
|
|
import logging
|
2024-04-23 20:13:25 +08:00
|
|
|
from copy import deepcopy
|
2024-05-31 00:46:25 +08:00
|
|
|
from itertools import chain
|
2024-04-11 01:10:46 +08:00
|
|
|
from pathlib import Path
|
2024-04-18 20:47:42 +08:00
|
|
|
|
2024-04-11 01:10:46 +08:00
|
|
|
import einops
|
2024-02-26 01:42:47 +08:00
|
|
|
import pytest
|
Add Aloha env and ACT policy
WIP Aloha env tests pass
Rendering works (fps look fast tho? TODO action bounding is too wide [-1,1])
Update README
Copy past from act repo
Remove download.py add a WIP for Simxarm
Remove download.py add a WIP for Simxarm
Add act yaml (TODO: try train.py)
Training can runs (TODO: eval)
Add tasks without end_effector that are compatible with dataset, Eval can run (TODO: training and pretrained model)
Add AbstractEnv, Refactor AlohaEnv, Add rendering_hook in env, Minor modifications, (TODO: Refactor Pusht and Simxarm)
poetry lock
fix bug in compute_stats for action normalization
fix more bugs in normalization
fix training
fix import
PushtEnv inheriates AbstractEnv, Improve factory Normalization
Add _make_env to EnvAbstract
Add call_rendering_hooks to pusht env
SimxarmEnv inherites from AbstractEnv (NOT TESTED)
Add aloha tests artifacts + update pusht stats
fix image normalization: before env was in [0,1] but dataset in [0,255], and now both in [0,255]
Small fix on simxarm
Add next to obs
Add top camera to Aloha env (TODO: make it compatible with set of cameras)
Add top camera to Aloha env (TODO: make it compatible with set of cameras)
2024-03-08 17:47:39 +08:00
|
|
|
import torch
|
2024-04-18 20:47:42 +08:00
|
|
|
from datasets import Dataset
|
2024-04-23 20:13:25 +08:00
|
|
|
from safetensors.torch import load_file
|
2024-02-26 01:42:47 +08:00
|
|
|
|
2024-04-18 20:47:42 +08:00
|
|
|
import lerobot
|
2024-05-30 23:12:21 +08:00
|
|
|
from lerobot.common.datasets.compute_stats import (
|
|
|
|
aggregate_stats,
|
2024-04-18 20:47:42 +08:00
|
|
|
compute_stats,
|
|
|
|
get_stats_einops_patterns,
|
2024-05-03 06:50:19 +08:00
|
|
|
)
|
2024-05-30 23:12:21 +08:00
|
|
|
from lerobot.common.datasets.factory import make_dataset
|
2024-05-31 00:46:25 +08:00
|
|
|
from lerobot.common.datasets.lerobot_dataset import LeRobotDataset, MultiLeRobotDataset
|
2024-05-03 06:50:19 +08:00
|
|
|
from lerobot.common.datasets.utils import (
|
|
|
|
flatten_dict,
|
2024-04-23 20:13:25 +08:00
|
|
|
hf_transform_to_torch,
|
2024-04-18 20:47:42 +08:00
|
|
|
load_previous_and_future_frames,
|
2024-04-23 20:13:25 +08:00
|
|
|
unflatten_dict,
|
2024-02-26 01:42:47 +08:00
|
|
|
)
|
2024-05-30 23:12:21 +08:00
|
|
|
from lerobot.common.utils.utils import init_hydra_config, seeded_context
|
2024-04-30 20:25:41 +08:00
|
|
|
from tests.utils import DEFAULT_CONFIG_PATH, DEVICE
|
2024-04-18 20:47:42 +08:00
|
|
|
|
|
|
|
|
2024-05-30 23:12:21 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"env_name, repo_id, policy_name",
|
|
|
|
lerobot.env_dataset_policy_triplets
|
|
|
|
+ [("aloha", ["lerobot/aloha_sim_insertion_human", "lerobot/aloha_sim_transfer_cube_human"], "act")],
|
|
|
|
)
|
2024-04-25 18:23:12 +08:00
|
|
|
def test_factory(env_name, repo_id, policy_name):
|
2024-05-30 23:12:21 +08:00
|
|
|
"""
|
|
|
|
Tests that:
|
|
|
|
- we can create a dataset with the factory.
|
|
|
|
- for a commonly used set of data keys, the data dimensions are correct.
|
|
|
|
"""
|
2024-03-28 02:33:48 +08:00
|
|
|
cfg = init_hydra_config(
|
|
|
|
DEFAULT_CONFIG_PATH,
|
2024-04-18 20:47:42 +08:00
|
|
|
overrides=[
|
|
|
|
f"env={env_name}",
|
2024-04-30 23:08:59 +08:00
|
|
|
f"dataset_repo_id={repo_id}",
|
2024-04-18 20:47:42 +08:00
|
|
|
f"policy={policy_name}",
|
|
|
|
f"device={DEVICE}",
|
|
|
|
],
|
2024-03-28 02:33:48 +08:00
|
|
|
)
|
2024-03-31 23:05:25 +08:00
|
|
|
dataset = make_dataset(cfg)
|
2024-04-08 22:02:03 +08:00
|
|
|
delta_timestamps = dataset.delta_timestamps
|
2024-05-06 09:03:14 +08:00
|
|
|
camera_keys = dataset.camera_keys
|
2024-03-31 23:05:25 +08:00
|
|
|
|
|
|
|
item = dataset[0]
|
|
|
|
|
2024-04-08 22:02:03 +08:00
|
|
|
keys_ndim_required = [
|
|
|
|
("action", 1, True),
|
2024-04-23 20:13:25 +08:00
|
|
|
("episode_index", 0, True),
|
|
|
|
("frame_index", 0, True),
|
2024-04-08 22:02:03 +08:00
|
|
|
("timestamp", 0, True),
|
|
|
|
# TODO(rcadene): should we rename it agent_pos?
|
|
|
|
("observation.state", 1, True),
|
|
|
|
("next.reward", 0, False),
|
|
|
|
("next.done", 0, False),
|
|
|
|
]
|
|
|
|
|
|
|
|
# test number of dimensions
|
|
|
|
for key, ndim, required in keys_ndim_required:
|
|
|
|
if key not in item:
|
|
|
|
if required:
|
|
|
|
assert key in item, f"{key}"
|
|
|
|
else:
|
|
|
|
logging.warning(f'Missing key in dataset: "{key}" not in {dataset}.')
|
|
|
|
continue
|
2024-04-18 20:47:42 +08:00
|
|
|
|
2024-04-08 22:02:03 +08:00
|
|
|
if delta_timestamps is not None and key in delta_timestamps:
|
|
|
|
assert item[key].ndim == ndim + 1, f"{key}"
|
|
|
|
assert item[key].shape[0] == len(delta_timestamps[key]), f"{key}"
|
|
|
|
else:
|
|
|
|
assert item[key].ndim == ndim, f"{key}"
|
2024-04-18 20:47:42 +08:00
|
|
|
|
2024-05-06 09:03:14 +08:00
|
|
|
if key in camera_keys:
|
2024-04-08 22:02:03 +08:00
|
|
|
assert item[key].dtype == torch.float32, f"{key}"
|
|
|
|
# TODO(rcadene): we assume for now that image normalization takes place in the model
|
|
|
|
assert item[key].max() <= 1.0, f"{key}"
|
|
|
|
assert item[key].min() >= 0.0, f"{key}"
|
|
|
|
|
|
|
|
if delta_timestamps is not None and key in delta_timestamps:
|
|
|
|
# test t,c,h,w
|
|
|
|
assert item[key].shape[1] == 3, f"{key}"
|
|
|
|
else:
|
2024-04-18 20:47:42 +08:00
|
|
|
# test c,h,w
|
2024-04-08 22:02:03 +08:00
|
|
|
assert item[key].shape[0] == 3, f"{key}"
|
|
|
|
|
|
|
|
if delta_timestamps is not None:
|
|
|
|
# test missing keys in delta_timestamps
|
|
|
|
for key in delta_timestamps:
|
|
|
|
assert key in item, f"{key}"
|
2024-03-31 23:05:25 +08:00
|
|
|
|
2024-04-02 23:40:33 +08:00
|
|
|
|
2024-05-31 16:03:28 +08:00
|
|
|
# TODO(alexander-soare): If you're hunting for savings on testing time, this takes about 5 seconds.
|
2024-05-31 00:46:25 +08:00
|
|
|
def test_multilerobotdataset_frames():
|
|
|
|
"""Check that all dataset frames are incorporated."""
|
|
|
|
# Note: use the image variants of the dataset to make the test approx 3x faster.
|
2024-05-31 16:03:28 +08:00
|
|
|
# Note: We really do need three repo_ids here as at some point this caught an issue with the chaining
|
|
|
|
# logic that wouldn't be caught with two repo IDs.
|
|
|
|
repo_ids = [
|
|
|
|
"lerobot/aloha_sim_insertion_human_image",
|
|
|
|
"lerobot/aloha_sim_transfer_cube_human_image",
|
|
|
|
"lerobot/aloha_sim_insertion_scripted_image",
|
|
|
|
]
|
2024-05-31 00:46:25 +08:00
|
|
|
sub_datasets = [LeRobotDataset(repo_id) for repo_id in repo_ids]
|
|
|
|
dataset = MultiLeRobotDataset(repo_ids)
|
|
|
|
assert len(dataset) == sum(len(d) for d in sub_datasets)
|
|
|
|
assert dataset.num_samples == sum(d.num_samples for d in sub_datasets)
|
|
|
|
assert dataset.num_episodes == sum(d.num_episodes for d in sub_datasets)
|
|
|
|
|
|
|
|
# Run through all items of the LeRobotDatasets in parallel with the items of the MultiLerobotDataset and
|
|
|
|
# check they match.
|
|
|
|
expected_dataset_indices = []
|
|
|
|
for i, sub_dataset in enumerate(sub_datasets):
|
|
|
|
expected_dataset_indices.extend([i] * len(sub_dataset))
|
|
|
|
|
|
|
|
for expected_dataset_index, sub_dataset_item, dataset_item in zip(
|
|
|
|
expected_dataset_indices, chain(*sub_datasets), dataset, strict=True
|
|
|
|
):
|
|
|
|
dataset_index = dataset_item.pop("dataset_index")
|
|
|
|
assert dataset_index == expected_dataset_index
|
|
|
|
assert sub_dataset_item.keys() == dataset_item.keys()
|
|
|
|
for k in sub_dataset_item:
|
|
|
|
assert torch.equal(sub_dataset_item[k], dataset_item[k])
|
|
|
|
|
|
|
|
|
2024-04-18 20:47:42 +08:00
|
|
|
def test_compute_stats_on_xarm():
|
2024-04-11 01:10:46 +08:00
|
|
|
"""Check that the statistics are computed correctly according to the stats_patterns property.
|
|
|
|
|
|
|
|
We compare with taking a straight min, mean, max, std of all the data in one pass (which we can do
|
|
|
|
because we are working with a small dataset).
|
|
|
|
"""
|
2024-05-04 22:07:14 +08:00
|
|
|
dataset = LeRobotDataset("lerobot/xarm_lift_medium")
|
2024-04-11 01:10:46 +08:00
|
|
|
|
2024-04-25 18:23:12 +08:00
|
|
|
# reduce size of dataset sample on which stats compute is tested to 10 frames
|
|
|
|
dataset.hf_dataset = dataset.hf_dataset.select(range(10))
|
|
|
|
|
2024-04-11 01:10:46 +08:00
|
|
|
# Note: we set the batch size to be smaller than the whole dataset to make sure we are testing batched
|
|
|
|
# computation of the statistics. While doing this, we also make sure it works when we don't divide the
|
2024-04-18 20:47:42 +08:00
|
|
|
# dataset into even batches.
|
2024-05-03 06:50:19 +08:00
|
|
|
computed_stats = compute_stats(dataset, batch_size=int(len(dataset) * 0.25), num_workers=0)
|
2024-04-11 01:10:46 +08:00
|
|
|
|
|
|
|
# get einops patterns to aggregate batches and compute statistics
|
2024-05-03 06:50:19 +08:00
|
|
|
stats_patterns = get_stats_einops_patterns(dataset)
|
2024-04-11 01:10:46 +08:00
|
|
|
|
|
|
|
# get all frames from the dataset in the same dtype and range as during compute_stats
|
2024-04-16 20:20:38 +08:00
|
|
|
dataloader = torch.utils.data.DataLoader(
|
|
|
|
dataset,
|
2024-05-03 06:50:19 +08:00
|
|
|
num_workers=0,
|
2024-04-16 20:20:38 +08:00
|
|
|
batch_size=len(dataset),
|
|
|
|
shuffle=False,
|
|
|
|
)
|
2024-04-23 20:13:25 +08:00
|
|
|
full_batch = next(iter(dataloader))
|
2024-04-11 01:10:46 +08:00
|
|
|
|
|
|
|
# compute stats based on all frames from the dataset without any batching
|
|
|
|
expected_stats = {}
|
|
|
|
for k, pattern in stats_patterns.items():
|
2024-04-23 20:13:25 +08:00
|
|
|
full_batch[k] = full_batch[k].float()
|
2024-04-11 01:10:46 +08:00
|
|
|
expected_stats[k] = {}
|
2024-04-23 20:13:25 +08:00
|
|
|
expected_stats[k]["mean"] = einops.reduce(full_batch[k], pattern, "mean")
|
2024-04-18 20:47:42 +08:00
|
|
|
expected_stats[k]["std"] = torch.sqrt(
|
2024-04-23 20:13:25 +08:00
|
|
|
einops.reduce((full_batch[k] - expected_stats[k]["mean"]) ** 2, pattern, "mean")
|
2024-04-18 20:47:42 +08:00
|
|
|
)
|
2024-04-23 20:13:25 +08:00
|
|
|
expected_stats[k]["min"] = einops.reduce(full_batch[k], pattern, "min")
|
|
|
|
expected_stats[k]["max"] = einops.reduce(full_batch[k], pattern, "max")
|
2024-04-11 01:10:46 +08:00
|
|
|
|
|
|
|
# test computed stats match expected stats
|
|
|
|
for k in stats_patterns:
|
|
|
|
assert torch.allclose(computed_stats[k]["mean"], expected_stats[k]["mean"])
|
|
|
|
assert torch.allclose(computed_stats[k]["std"], expected_stats[k]["std"])
|
|
|
|
assert torch.allclose(computed_stats[k]["min"], expected_stats[k]["min"])
|
|
|
|
assert torch.allclose(computed_stats[k]["max"], expected_stats[k]["max"])
|
|
|
|
|
2024-04-23 20:13:25 +08:00
|
|
|
# load stats used during training which are expected to match the ones returned by computed_stats
|
|
|
|
loaded_stats = dataset.stats # noqa: F841
|
2024-04-11 01:10:46 +08:00
|
|
|
|
2024-04-23 20:13:25 +08:00
|
|
|
# TODO(rcadene): we can't test this because expected_stats is computed on a subset
|
2024-04-11 01:10:46 +08:00
|
|
|
# # test loaded stats match expected stats
|
|
|
|
# for k in stats_patterns:
|
|
|
|
# assert torch.allclose(loaded_stats[k]["mean"], expected_stats[k]["mean"])
|
|
|
|
# assert torch.allclose(loaded_stats[k]["std"], expected_stats[k]["std"])
|
|
|
|
# assert torch.allclose(loaded_stats[k]["min"], expected_stats[k]["min"])
|
|
|
|
# assert torch.allclose(loaded_stats[k]["max"], expected_stats[k]["max"])
|
2024-04-11 20:59:09 +08:00
|
|
|
|
|
|
|
|
2024-04-15 18:08:10 +08:00
|
|
|
def test_load_previous_and_future_frames_within_tolerance():
|
2024-04-18 20:47:42 +08:00
|
|
|
hf_dataset = Dataset.from_dict(
|
|
|
|
{
|
|
|
|
"timestamp": [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
|
|
"index": [0, 1, 2, 3, 4],
|
2024-04-23 20:13:25 +08:00
|
|
|
"episode_index": [0, 0, 0, 0, 0],
|
2024-04-18 20:47:42 +08:00
|
|
|
}
|
|
|
|
)
|
2024-04-23 20:13:25 +08:00
|
|
|
hf_dataset.set_transform(hf_transform_to_torch)
|
|
|
|
episode_data_index = {
|
|
|
|
"from": torch.tensor([0]),
|
|
|
|
"to": torch.tensor([5]),
|
|
|
|
}
|
2024-04-11 21:16:47 +08:00
|
|
|
delta_timestamps = {"index": [-0.2, 0, 0.139]}
|
2024-04-11 20:59:09 +08:00
|
|
|
tol = 0.04
|
2024-04-23 20:13:25 +08:00
|
|
|
item = hf_dataset[2]
|
|
|
|
item = load_previous_and_future_frames(item, hf_dataset, episode_data_index, delta_timestamps, tol)
|
2024-04-15 18:08:10 +08:00
|
|
|
data, is_pad = item["index"], item["index_is_pad"]
|
2024-04-11 21:21:06 +08:00
|
|
|
assert torch.equal(data, torch.tensor([0, 2, 3])), "Data does not match expected values"
|
2024-04-15 18:08:10 +08:00
|
|
|
assert not is_pad.any(), "Unexpected padding detected"
|
2024-04-11 20:59:09 +08:00
|
|
|
|
2024-04-18 20:47:42 +08:00
|
|
|
|
2024-04-15 18:08:10 +08:00
|
|
|
def test_load_previous_and_future_frames_outside_tolerance_inside_episode_range():
|
2024-04-18 20:47:42 +08:00
|
|
|
hf_dataset = Dataset.from_dict(
|
|
|
|
{
|
|
|
|
"timestamp": [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
|
|
"index": [0, 1, 2, 3, 4],
|
2024-04-23 20:13:25 +08:00
|
|
|
"episode_index": [0, 0, 0, 0, 0],
|
2024-04-18 20:47:42 +08:00
|
|
|
}
|
|
|
|
)
|
2024-04-23 20:13:25 +08:00
|
|
|
hf_dataset.set_transform(hf_transform_to_torch)
|
|
|
|
episode_data_index = {
|
|
|
|
"from": torch.tensor([0]),
|
|
|
|
"to": torch.tensor([5]),
|
|
|
|
}
|
2024-04-11 21:16:47 +08:00
|
|
|
delta_timestamps = {"index": [-0.2, 0, 0.141]}
|
|
|
|
tol = 0.04
|
2024-04-23 20:13:25 +08:00
|
|
|
item = hf_dataset[2]
|
2024-06-04 21:43:10 +08:00
|
|
|
with pytest.raises(ValueError):
|
2024-04-23 20:13:25 +08:00
|
|
|
load_previous_and_future_frames(item, hf_dataset, episode_data_index, delta_timestamps, tol)
|
2024-04-15 18:08:10 +08:00
|
|
|
|
2024-04-18 20:47:42 +08:00
|
|
|
|
2024-04-15 18:08:10 +08:00
|
|
|
def test_load_previous_and_future_frames_outside_tolerance_outside_episode_range():
|
2024-04-18 20:47:42 +08:00
|
|
|
hf_dataset = Dataset.from_dict(
|
|
|
|
{
|
|
|
|
"timestamp": [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
|
|
"index": [0, 1, 2, 3, 4],
|
2024-04-23 20:13:25 +08:00
|
|
|
"episode_index": [0, 0, 0, 0, 0],
|
2024-04-18 20:47:42 +08:00
|
|
|
}
|
|
|
|
)
|
2024-04-23 20:13:25 +08:00
|
|
|
hf_dataset.set_transform(hf_transform_to_torch)
|
|
|
|
episode_data_index = {
|
|
|
|
"from": torch.tensor([0]),
|
|
|
|
"to": torch.tensor([5]),
|
|
|
|
}
|
2024-04-11 20:59:09 +08:00
|
|
|
delta_timestamps = {"index": [-0.3, -0.24, 0, 0.26, 0.3]}
|
|
|
|
tol = 0.04
|
2024-04-23 20:13:25 +08:00
|
|
|
item = hf_dataset[2]
|
|
|
|
item = load_previous_and_future_frames(item, hf_dataset, episode_data_index, delta_timestamps, tol)
|
2024-04-15 18:08:10 +08:00
|
|
|
data, is_pad = item["index"], item["index_is_pad"]
|
2024-04-11 20:59:09 +08:00
|
|
|
assert torch.equal(data, torch.tensor([0, 0, 2, 4, 4])), "Data does not match expected values"
|
2024-04-18 20:47:42 +08:00
|
|
|
assert torch.equal(
|
|
|
|
is_pad, torch.tensor([True, False, False, True, True])
|
|
|
|
), "Padding does not match expected values"
|
2024-04-23 20:13:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_flatten_unflatten_dict():
|
|
|
|
d = {
|
|
|
|
"obs": {
|
|
|
|
"min": 0,
|
|
|
|
"max": 1,
|
|
|
|
"mean": 2,
|
|
|
|
"std": 3,
|
|
|
|
},
|
|
|
|
"action": {
|
|
|
|
"min": 4,
|
|
|
|
"max": 5,
|
|
|
|
"mean": 6,
|
|
|
|
"std": 7,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
original_d = deepcopy(d)
|
|
|
|
d = unflatten_dict(flatten_dict(d))
|
|
|
|
|
|
|
|
# test equality between nested dicts
|
|
|
|
assert json.dumps(original_d, sort_keys=True) == json.dumps(d, sort_keys=True), f"{original_d} != {d}"
|
|
|
|
|
|
|
|
|
2024-04-30 20:25:41 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"repo_id",
|
|
|
|
[
|
2024-04-29 06:08:17 +08:00
|
|
|
"lerobot/pusht",
|
2024-04-30 20:25:41 +08:00
|
|
|
"lerobot/aloha_sim_insertion_human",
|
|
|
|
"lerobot/xarm_lift_medium",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_backward_compatibility(repo_id):
|
|
|
|
"""The artifacts for this test have been generated by `tests/scripts/save_dataset_to_safetensors.py`."""
|
2024-04-29 06:08:17 +08:00
|
|
|
|
2024-04-30 20:25:41 +08:00
|
|
|
dataset = LeRobotDataset(
|
|
|
|
repo_id,
|
|
|
|
)
|
2024-04-29 06:08:17 +08:00
|
|
|
|
2024-04-30 20:25:41 +08:00
|
|
|
test_dir = Path("tests/data/save_dataset_to_safetensors") / repo_id
|
|
|
|
|
|
|
|
def load_and_compare(i):
|
|
|
|
new_frame = dataset[i] # noqa: B023
|
|
|
|
old_frame = load_file(test_dir / f"frame_{i}.safetensors") # noqa: B023
|
|
|
|
|
|
|
|
new_keys = set(new_frame.keys())
|
|
|
|
old_keys = set(old_frame.keys())
|
|
|
|
assert new_keys == old_keys, f"{new_keys=} and {old_keys=} are not the same"
|
|
|
|
|
|
|
|
for key in new_frame:
|
|
|
|
assert torch.isclose(
|
2024-05-04 22:20:30 +08:00
|
|
|
new_frame[key], old_frame[key]
|
2024-04-30 20:25:41 +08:00
|
|
|
).all(), f"{key=} for index={i} does not contain the same value"
|
|
|
|
|
|
|
|
# test2 first frames of first episode
|
|
|
|
i = dataset.episode_data_index["from"][0].item()
|
|
|
|
load_and_compare(i)
|
|
|
|
load_and_compare(i + 1)
|
|
|
|
|
|
|
|
# test 2 frames at the middle of first episode
|
|
|
|
i = int((dataset.episode_data_index["to"][0].item() - dataset.episode_data_index["from"][0].item()) / 2)
|
|
|
|
load_and_compare(i)
|
|
|
|
load_and_compare(i + 1)
|
|
|
|
|
|
|
|
# test 2 last frames of first episode
|
|
|
|
i = dataset.episode_data_index["to"][0].item()
|
|
|
|
load_and_compare(i - 2)
|
|
|
|
load_and_compare(i - 1)
|
|
|
|
|
|
|
|
# TODO(rcadene): Enable testing on second and last episode
|
|
|
|
# We currently cant because our test dataset only contains the first episode
|
|
|
|
|
|
|
|
# # test 2 first frames of second episode
|
|
|
|
# i = dataset.episode_data_index["from"][1].item()
|
|
|
|
# load_and_compare(i)
|
|
|
|
# load_and_compare(i + 1)
|
|
|
|
|
|
|
|
# # test 2 last frames of second episode
|
|
|
|
# i = dataset.episode_data_index["to"][1].item()
|
|
|
|
# load_and_compare(i - 2)
|
|
|
|
# load_and_compare(i - 1)
|
|
|
|
|
|
|
|
# # test 2 last frames of last episode
|
|
|
|
# i = dataset.episode_data_index["to"][-1].item()
|
|
|
|
# load_and_compare(i - 2)
|
|
|
|
# load_and_compare(i - 1)
|
2024-05-30 23:12:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_aggregate_stats():
|
|
|
|
"""Makes 3 basic datasets and checks that aggregate stats are computed correctly."""
|
|
|
|
with seeded_context(0):
|
|
|
|
data_a = torch.rand(30, dtype=torch.float32)
|
|
|
|
data_b = torch.rand(20, dtype=torch.float32)
|
|
|
|
data_c = torch.rand(20, dtype=torch.float32)
|
|
|
|
|
|
|
|
hf_dataset_1 = Dataset.from_dict(
|
|
|
|
{"a": data_a[:10], "b": data_b[:10], "c": data_c[:10], "index": torch.arange(10)}
|
|
|
|
)
|
|
|
|
hf_dataset_1.set_transform(hf_transform_to_torch)
|
|
|
|
hf_dataset_2 = Dataset.from_dict({"a": data_a[10:20], "b": data_b[10:], "index": torch.arange(10)})
|
|
|
|
hf_dataset_2.set_transform(hf_transform_to_torch)
|
|
|
|
hf_dataset_3 = Dataset.from_dict({"a": data_a[20:], "c": data_c[10:], "index": torch.arange(10)})
|
|
|
|
hf_dataset_3.set_transform(hf_transform_to_torch)
|
|
|
|
dataset_1 = LeRobotDataset.from_preloaded("d1", hf_dataset=hf_dataset_1)
|
|
|
|
dataset_1.stats = compute_stats(dataset_1, batch_size=len(hf_dataset_1), num_workers=0)
|
|
|
|
dataset_2 = LeRobotDataset.from_preloaded("d2", hf_dataset=hf_dataset_2)
|
|
|
|
dataset_2.stats = compute_stats(dataset_2, batch_size=len(hf_dataset_2), num_workers=0)
|
|
|
|
dataset_3 = LeRobotDataset.from_preloaded("d3", hf_dataset=hf_dataset_3)
|
|
|
|
dataset_3.stats = compute_stats(dataset_3, batch_size=len(hf_dataset_3), num_workers=0)
|
|
|
|
stats = aggregate_stats([dataset_1, dataset_2, dataset_3])
|
|
|
|
for data_key, data in zip(["a", "b", "c"], [data_a, data_b, data_c], strict=True):
|
|
|
|
for agg_fn in ["mean", "min", "max"]:
|
|
|
|
assert torch.allclose(stats[data_key][agg_fn], einops.reduce(data, "n -> 1", agg_fn))
|
|
|
|
assert torch.allclose(stats[data_key]["std"], torch.std(data, correction=0))
|