lerobot/tests/test_envs.py

66 lines
1.9 KiB
Python
Raw Normal View History

import importlib
import pytest
import torch
from lerobot.common.datasets.factory import make_dataset
2024-04-05 21:35:20 +08:00
import gymnasium as gym
from gymnasium.utils.env_checker import check_env
2024-02-20 22:22:16 +08:00
from lerobot.common.envs.factory import make_env
2024-03-28 02:33:48 +08:00
from lerobot.common.utils import init_hydra_config
from lerobot.common.envs.utils import preprocess_observation
2024-03-28 02:33:48 +08:00
from .utils import DEVICE, DEFAULT_CONFIG_PATH
2024-03-26 23:23:34 +08:00
@pytest.mark.parametrize(
2024-04-08 22:54:52 +08:00
"env_name, task, obs_type",
2024-03-26 23:23:34 +08:00
[
# ("AlohaInsertion-v0", "state"),
("aloha", "AlohaInsertion-v0", "pixels"),
("aloha", "AlohaInsertion-v0", "pixels_agent_pos"),
("aloha", "AlohaTransferCube-v0", "pixels"),
("aloha", "AlohaTransferCube-v0", "pixels_agent_pos"),
("xarm", "XarmLift-v0", "state"),
("xarm", "XarmLift-v0", "pixels"),
("xarm", "XarmLift-v0", "pixels_agent_pos"),
("pusht", "PushT-v0", "state"),
("pusht", "PushT-v0", "pixels"),
("pusht", "PushT-v0", "pixels_agent_pos"),
2024-03-26 23:23:34 +08:00
],
)
2024-04-08 22:54:52 +08:00
def test_env(env_name, task, obs_type):
package_name = f"gym_{env_name}"
importlib.import_module(package_name)
2024-04-08 22:54:52 +08:00
env = gym.make(f"{package_name}/{task}", obs_type=obs_type)
2024-04-06 07:27:12 +08:00
check_env(env.unwrapped)
2024-04-09 16:41:20 +08:00
env.close()
2024-02-20 22:22:16 +08:00
@pytest.mark.parametrize(
"env_name",
2024-02-20 22:22:16 +08:00
[
"pusht",
"xarm",
2024-04-06 07:27:12 +08:00
"aloha",
2024-02-20 22:22:16 +08:00
],
)
def test_factory(env_name):
2024-03-28 02:33:48 +08:00
cfg = init_hydra_config(
DEFAULT_CONFIG_PATH,
overrides=[f"env={env_name}", f"device={DEVICE}"],
)
dataset = make_dataset(cfg)
2024-04-06 07:27:12 +08:00
env = make_env(cfg, num_parallel_envs=1)
obs, _ = env.reset()
obs = preprocess_observation(obs, transform=dataset.transform)
for key in dataset.image_keys:
img = obs[key]
assert img.dtype == torch.float32
# TODO(rcadene): we assume for now that image normalization takes place in the model
assert img.max() <= 1.0
assert img.min() >= 0.0
2024-04-09 16:41:20 +08:00
env.close()