Datasets:
Download src/robotics/sim/tabletop.py from Nabidnur/RoboRACE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 8.42 kB
-
https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/resolve/main/src/robotics/sim/tabletop.py
- Command line
-
hf download hf://datasets/Nabidnur/RoboRACE-Bench/src/robotics/sim/tabletop.py
-
curl -L -o tabletop.py https://huggingface.co/datasets/Nabidnur/RoboRACE-Bench/resolve/main/src/robotics/sim/tabletop.py
8.42 kB
| """Procedural tabletop scenes for testing success verifiers (CPU, no physics needed). | |
| Each scenario places objects so that a task is either *completed* or a *near-miss* | |
| failure (object next to / on the rim of the target), renders an RGB image, and | |
| records the ground-truth label. This is a cheap stand-in for real rollout frames | |
| until GR00T/SO-101 rollouts exist. | |
| Rendering needs MUJOCO_GL=egl (set automatically below if unset). | |
| """ | |
| from __future__ import annotations | |
| import math | |
| import os | |
| import random | |
| from dataclasses import dataclass | |
| os.environ.setdefault("MUJOCO_GL", "egl") | |
| import mujoco # noqa: E402 | |
| import numpy as np # noqa: E402 | |
| COLORS = { | |
| "red": "0.85 0.1 0.1 1", "green": "0.1 0.7 0.2 1", "blue": "0.15 0.3 0.9 1", | |
| "yellow": "0.95 0.85 0.1 1", "purple": "0.55 0.2 0.7 1", "orange": "1 0.5 0.05 1", | |
| } | |
| CUBE = 0.025 # half-size (5 cm cube) | |
| BOWL_R, BOWL_H = 0.075, 0.04 | |
| def _bowl_xml(name: str, rgba: str) -> str: | |
| """Smooth round bowl: base disc + 48 overlapping wall segments + rim.""" | |
| walls = [] | |
| n = 48 | |
| half_w = BOWL_R * math.pi / n * 1.6 # >1 so neighbouring segments overlap (no gaps) | |
| for i in range(n): | |
| a = 2 * math.pi * i / n | |
| walls.append( | |
| f'<geom type="box" size="0.005 {half_w:.4f} {BOWL_H / 2}" ' | |
| f'pos="{BOWL_R * math.cos(a):.4f} {BOWL_R * math.sin(a):.4f} {BOWL_H / 2}" ' | |
| f'euler="0 0 {a:.4f}" rgba="{rgba}"/>') | |
| return (f'<body name="{name}" mocap="true"><geom type="cylinder" size="{BOWL_R} 0.004" ' | |
| f'pos="0 0 0.004" rgba="{rgba}"/>{"".join(walls)}</body>') | |
| def _scene_xml(cubes: list[str], bowl: str | None) -> str: | |
| bodies = [f'<body name="{c}_cube" mocap="true"><geom type="box" size="{CUBE} {CUBE} {CUBE}" ' | |
| f'rgba="{COLORS[c]}"/></body>' for c in cubes] | |
| if bowl: | |
| bodies.append(_bowl_xml(f"{bowl}_bowl", COLORS[bowl])) | |
| return f"""<mujoco><visual><global offwidth="640" offheight="640"/> | |
| <rgba haze="0.9 0.9 0.92 1"/><headlight ambient="0.45 0.45 0.45" diffuse="0.5 0.5 0.5"/></visual> | |
| <asset><texture name="wood" type="2d" builtin="checker" rgb1="0.72 0.6 0.45" rgb2="0.68 0.56 0.42" width="64" height="64"/> | |
| <material name="table" texture="wood" texrepeat="12 12"/></asset> | |
| <worldbody> | |
| <light pos="0.3 -0.3 1.2" dir="-0.3 0.3 -1" diffuse="0.6 0.6 0.6" castshadow="true"/> | |
| <geom type="plane" size="2 2 0.01" material="table"/> | |
| <camera name="front" pos="0 -0.55 0.55" xyaxes="1 0 0 0 0.71 0.71"/> | |
| <body name="anchor" pos="0 0.03 0.02"/> | |
| <camera name="left" mode="targetbody" target="anchor" pos="-0.62 0.03 0.42"/> | |
| <camera name="right" mode="targetbody" target="anchor" pos="0.62 0.03 0.42"/> | |
| <camera name="back" mode="targetbody" target="anchor" pos="0.05 0.66 0.48"/> | |
| <camera name="top" mode="targetbody" target="anchor" pos="0 -0.12 0.80"/> | |
| {''.join(bodies)} | |
| </worldbody></mujoco>""" | |
| class Scenario: | |
| task: str # natural-language instruction | |
| success: bool # ground truth | |
| kind: str # e.g. "in_bowl", "near_miss_rim" | |
| image: np.ndarray # HxWx3 uint8 (front camera) | |
| views: dict[str, np.ndarray] | None = None # camera name -> image, when multi-view | |
| CAMERAS = ("front", "left", "right", "back", "top") | |
| _CAMS: tuple[str, ...] = ("front",) # set by make_suite | |
| _RENDER = True # set by make_suite; False = layout only (fast, no GL context) | |
| _LOG: list | None = None # when a list, every scene's (xml, poses) is appended (used for videos) | |
| def _render(xml: str, poses: dict[str, tuple[float, float, float]], size: int): | |
| if _LOG is not None: | |
| _LOG.append((xml, dict(poses))) | |
| if not _RENDER: | |
| return {cam: np.zeros((size, size, 3), np.uint8) for cam in _CAMS} | |
| m = mujoco.MjModel.from_xml_string(xml) | |
| d = mujoco.MjData(m) | |
| for body, pos in poses.items(): | |
| d.mocap_pos[m.body_mocapid[m.body(body).id]] = pos | |
| mujoco.mj_forward(m, d) | |
| r = mujoco.Renderer(m, size, size) | |
| views = {} | |
| for cam in _CAMS: | |
| r.update_scene(d, camera=cam) | |
| views[cam] = r.render().copy() | |
| r.close() | |
| return views | |
| def _rand_xy(rng: random.Random, avoid: list[tuple[float, float]], min_d: float = 0.14): | |
| """Random table position at least min_d from `avoid`; relaxes min_d if the table is crowded.""" | |
| d = min_d | |
| while d > 0.09: | |
| for _ in range(200): | |
| p = (rng.uniform(-0.22, 0.22), rng.uniform(-0.12, 0.18)) | |
| if all(math.dist(p, a) > d for a in avoid): | |
| return p | |
| d *= 0.9 | |
| raise RuntimeError("could not place object") | |
| def put_in_bowl(rng: random.Random, success: bool, size: int = 448) -> Scenario: | |
| cube, bowl, distract = rng.sample(["red", "green", "yellow", "purple", "orange"], 2) + [None] | |
| bowl = rng.choice(["blue", "green", "purple"]) if bowl in ("red",) else bowl | |
| if bowl == cube: | |
| bowl = "blue" | |
| other = rng.choice([c for c in COLORS if c not in (cube, bowl)]) | |
| bxy = _rand_xy(rng, []) | |
| oxy = _rand_xy(rng, [bxy]) | |
| if success: | |
| r, a = rng.uniform(0, 0.03), rng.uniform(0, 2 * math.pi) | |
| cxy, kind = (bxy[0] + r * math.cos(a), bxy[1] + r * math.sin(a)), "in_bowl" | |
| cz = 0.008 + CUBE | |
| else: | |
| kind = rng.choice(["beside_bowl", "far_away", "other_object_in_bowl"]) | |
| a = rng.uniform(0, 2 * math.pi) | |
| if kind == "beside_bowl": | |
| cxy = (bxy[0] + 0.12 * math.cos(a), bxy[1] + 0.12 * math.sin(a)) | |
| elif kind == "far_away": | |
| cxy = _rand_xy(rng, [bxy, oxy], 0.2) | |
| else: # the distractor is in the bowl, the target cube is not | |
| cxy = _rand_xy(rng, [bxy, oxy], 0.16) | |
| oxy = bxy | |
| cz = CUBE | |
| oz = 0.008 + CUBE if oxy == bxy else CUBE | |
| xml = _scene_xml([cube, other], bowl) | |
| views = _render(xml, {f"{cube}_cube": (*cxy, cz), f"{other}_cube": (*oxy, oz), | |
| f"{bowl}_bowl": (*bxy, 0)}, size) | |
| return Scenario(f"put the {cube} cube in the {bowl} bowl", success, kind, views["front"], views) | |
| def stack(rng: random.Random, success: bool, size: int = 448) -> Scenario: | |
| top, base, other = rng.sample(["red", "green", "blue", "yellow", "orange"], 3) | |
| bxy = _rand_xy(rng, []) | |
| oxy = _rand_xy(rng, [bxy]) | |
| if success: | |
| txy, tz, kind = (bxy[0] + rng.uniform(-0.008, 0.008), bxy[1] + rng.uniform(-0.008, 0.008)), 3 * CUBE, "stacked" | |
| poses = {f"{top}_cube": (*txy, tz), f"{base}_cube": (*bxy, CUBE), f"{other}_cube": (*oxy, CUBE)} | |
| else: | |
| kind = rng.choice(["side_by_side", "wrong_base", "reversed"]) | |
| if kind == "side_by_side": | |
| a = rng.uniform(0, 2 * math.pi) | |
| poses = {f"{top}_cube": (bxy[0] + 0.06 * math.cos(a), bxy[1] + 0.06 * math.sin(a), CUBE), | |
| f"{base}_cube": (*bxy, CUBE), f"{other}_cube": (*oxy, CUBE)} | |
| elif kind == "wrong_base": # top cube is stacked, but on the wrong cube | |
| poses = {f"{top}_cube": (*oxy, 3 * CUBE), f"{other}_cube": (*oxy, CUBE), f"{base}_cube": (*bxy, CUBE)} | |
| else: # base is on top of top | |
| poses = {f"{base}_cube": (*bxy, 3 * CUBE), f"{top}_cube": (*bxy, CUBE), f"{other}_cube": (*oxy, CUBE)} | |
| views = _render(_scene_xml([top, base, other], None), poses, size) | |
| return Scenario(f"stack the {top} cube on the {base} cube", success, kind, views["front"], views) | |
| GENERATORS = {"put_in_bowl": put_in_bowl, "stack": stack} | |
| def make_suite(n_per_task: int = 10, seed: int = 0, size: int = 448, | |
| cameras: tuple[str, ...] = ("front",), render: bool = True) -> list[Scenario]: | |
| """Balanced suite: n_per_task scenarios per task family, half successes. | |
| Scene layouts depend only on `seed`, not on which cameras are rendered.""" | |
| global _CAMS, _RENDER | |
| _CAMS, _RENDER = tuple(cameras), render | |
| rng = random.Random(seed) | |
| out = [] | |
| for gen in GENERATORS.values(): | |
| for i in range(n_per_task): | |
| out.append(gen(rng, success=(i % 2 == 0), size=size)) | |
| return out | |
| def render_poses(xml: str, poses: dict, cameras=CAMERAS, size: int = 192) -> dict: | |
| """Render an arbitrary pose dict (e.g. one animation frame) from the given cameras.""" | |
| global _CAMS, _RENDER, _LOG | |
| saved = (_CAMS, _RENDER, _LOG) | |
| _CAMS, _RENDER, _LOG = tuple(cameras), True, None | |
| try: | |
| return _render(xml, poses, size) | |
| finally: | |
| _CAMS, _RENDER, _LOG = saved | |