File size: 12,298 Bytes
8aa2acf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""
pi0 PAIRWISE-GRID eval — mirrors yqi19/Maniskill_gen_new data collection
exactly, with the MP solver replaced by a pi0 openpi-websocket policy.

Implemented: color_size  (collect_pairwise_attribute.py :: experiment=color_size)
  • sweep color(6) × size(6) full grid (small,large,smaller,larger,smallest,largest)
  • FIXED verb=lift, shape=cube  (per collection definition)
  • size→scene preset (verbatim from collect_pairwise_attribute._size_controls /
    _sample_small_large_distractors):
      small  0.72 / no distractor       large  1.34 / no distractor
      smaller 0.82 d[1.08] n1           larger 1.18 d[0.92] n1
      smallest 0.78 d[1.00,1.24] n2     largest 1.26 d[1.00,0.80] n2
  • env built via the repo's own get_env_id_and_color(); size scales →
    make_kw, num_distractors → reset options (exactly as the MP runner).
  • instruction: "Lift the {size} {color} cube."  (color AND size in language)
  • success = env "success"; task_difficulty configurable (harder).
"""
from __future__ import annotations

import argparse
import os
import pathlib
import random
import sys

import numpy as np

MGEN_ROOT = os.environ.get("MGEN_ROOT", "/workspace/Maniskill_gen_new")
SIM_ROOT = os.environ.get("SIM_ROOT", "/workspace/eval_simulation/simulation")
for _p in (SIM_ROOT, MGEN_ROOT):
    if _p not in sys.path:
        sys.path.insert(0, _p)

import gymnasium as gym  # noqa: E402
import mani_skill.envs  # noqa: E402,F401  (registers VerbObjectColor-v1)
from openpi_client import image_tools  # noqa: E402
from openpi_client import websocket_client_policy as _wcp  # noqa: E402
# Repo's own canonical env-id/color resolver (handles legacy routing exactly).
from scripts.run_verb_color_shape_motion_planning import get_env_id_and_color  # noqa: E402

COLORS = ("red", "yellow", "blue", "orange", "green", "black")
SIZES = ("small", "large", "smaller", "larger", "smallest", "largest")
SPATIALS = ("left", "right", "middle", "front", "behind")
VERB_POOL = ("lift", "grasp", "push")  # training_vocab THIRD_VERBS (first 3)
VERB_CAP = {"lift": "Lift", "grasp": "Grasp", "push": "Push",
            "pull": "Pull", "rotate": "Rotate", "slide": "Slide"}
SPATIAL_PHRASE = {"left": "on the left", "right": "on the right",
                  "middle": "in the middle", "front": "in front",
                  "behind": "at the back"}
SPATIAL_ANCHOR = {"left": (-0.10, 0.0), "right": (0.10, 0.0),
                  "middle": (0.0, 0.0), "front": (0.0, 0.10),
                  "behind": (0.0, -0.10)}


def _spatial_xy(spatial, rng):
    ax, ay = SPATIAL_ANCHOR[spatial]
    return [ax + rng.uniform(-0.012, 0.012), ay + rng.uniform(-0.012, 0.012)]


# ── size presets: VERBATIM from collect_pairwise_attribute.py ───────────────
def _sample_small_large(size_label):
    if size_label == "small":
        return 0.72, None, 0
    if size_label == "large":
        return 1.34, None, 0
    raise ValueError(size_label)


def _size_controls(size_label):
    if size_label == "smaller":
        return 0.82, [1.08], 1
    if size_label == "larger":
        return 1.18, [0.92], 1
    if size_label == "smallest":
        return 0.78, [1.00, 1.24], 2
    if size_label == "largest":
        return 1.26, [1.00, 0.80], 2
    raise ValueError(size_label)


def _to_hwc_uint8(x):
    import torch
    if torch.is_tensor(x):
        x = x.detach().float().cpu().numpy()
    x = np.asarray(x)
    if x.ndim == 4:
        x = x[0]
    if x.ndim == 3 and x.shape[0] in (1, 3) and x.shape[-1] != 3:
        x = np.transpose(x, (1, 2, 0))
    if np.issubdtype(x.dtype, np.floating) and x.max() <= 1.0 + 1e-6:
        x = (np.clip(x, 0, 1) * 255).astype(np.uint8)
    else:
        x = x.astype(np.uint8)
    return np.ascontiguousarray(x)


def _state8(env):
    import torch
    q = env.unwrapped.agent.robot.get_qpos()
    if torch.is_tensor(q):
        q = q[0].detach().cpu().numpy()
    q = np.asarray(q, dtype=np.float32).ravel()
    out = np.zeros(8, dtype=np.float32)
    out[: min(8, q.size)] = q[: min(8, q.size)]
    return out


def _success(info):
    import torch
    s = info.get("success", False)
    if torch.is_tensor(s):
        return bool(s.squeeze().item())
    return bool(np.asarray(s).squeeze())


# HARD color_size: EVERY cell has same-color same-shape(cube) distractor(s)
# differing ONLY in size → model MUST use the size word to disambiguate.
# (target_scale, [distractor_scales], n_d).  small/large now also forced a
# contrasting same-color cube distractor (no more single-object gift cells).
HARD_SIZE = {
    "small":    (0.72, [1.20], 1),
    "large":    (1.34, [0.80], 1),
    "smaller":  (0.82, [1.08], 1),
    "larger":   (1.18, [0.92], 1),
    "smallest": (0.78, [1.00, 1.24], 2),
    "largest":  (1.26, [1.00, 0.80], 2),
}


def build_color_size_cell(color, size_label, *, distractor_max_arg, task_difficulty):
    """HARD: ≥1 same-color cube distractor, size-only difference."""
    verb, shape = "lift", "cube"
    t_scale, d_scales, n_d = HARD_SIZE[size_label]
    env_id, color_id, extra = get_env_id_and_color(
        verb, color, shape, distractor_max=max(int(distractor_max_arg), n_d),
        task_difficulty=task_difficulty)
    make_kw = dict(obs_mode="rgb", control_mode="pd_joint_pos",
                   render_mode="rgb_array")
    if env_id == "VerbObjectColor-v1":
        make_kw.update(extra)
        make_kw["object_size_jiggle"] = 0.0
        # distractor_specs is an env CONSTRUCTOR arg: force cube + target color
        make_kw["distractor_specs"] = [("cube", int(color_id))] * n_d + \
            [None] * (3 - n_d)
    else:
        make_kw["object_color_id"] = color_id
    reset_opts = {"num_distractors": int(n_d),
                  "target_size_scale": float(t_scale),
                  "distractor_size_scales": [float(x) for x in d_scales]}
    instruction = f"Lift the {size_label} {color} cube."
    return env_id, make_kw, reset_opts, instruction


def build_color_spatial_cell(color, spatial, *, rng, distractor_max_arg, task_difficulty):
    """HARD: same-color same-shape(cube) distractor at a DIFFERENT spatial
    anchor → model MUST use the spatial phrase to disambiguate."""
    verb = rng.choice(VERB_POOL)
    shape = "cube"
    others = [s for s in SPATIALS if s != spatial]
    d_spatial = rng.choice(others)
    env_id, color_id, extra = get_env_id_and_color(
        verb, color, shape, distractor_max=1, task_difficulty=task_difficulty)
    make_kw = dict(obs_mode="rgb", control_mode="pd_joint_pos",
                   render_mode="rgb_array")
    if env_id == "VerbObjectColor-v1":
        make_kw.update(extra)
        make_kw["object_size_jiggle"] = 0.0
        make_kw["distractor_specs"] = [("cube", int(color_id)), None, None]
    else:
        make_kw["object_color_id"] = color_id
    reset_opts = {"num_distractors": 1, "target_size_scale": 1.0,
                  "obj_xy": _spatial_xy(spatial, rng),
                  "distractor_xy": [_spatial_xy(d_spatial, rng)],
                  "distractor_size_scales": [1.0]}
    instruction = f"{VERB_CAP[verb]} the {color} cube {SPATIAL_PHRASE[spatial]}."
    return env_id, make_kw, reset_opts, instruction


def run_cell(client, env_id, make_kw, reset_opts, instruction, *,
             seed, sim_backend, max_steps, replan, resize=224):
    mk = dict(make_kw)
    mk["sim_backend"] = sim_backend
    mk["render_backend"] = sim_backend
    env = gym.make(env_id, **mk)
    obs, _ = env.reset(seed=seed, options=reset_opts)
    plan = []
    done = ok = False
    try:
        while not done:
            b = _to_hwc_uint8(obs["sensor_data"]["base_camera"]["rgb"])
            h = _to_hwc_uint8(obs["sensor_data"]["hand_camera"]["rgb"])
            if not plan:
                img = image_tools.convert_to_uint8(image_tools.resize_with_pad(b, resize, resize))
                wri = image_tools.convert_to_uint8(image_tools.resize_with_pad(h, resize, resize))
                chunk = client.infer({
                    "observation/image": img,
                    "observation/wrist_image": wri,
                    "observation/state": _state8(env).astype(np.float32),
                    "prompt": instruction,
                })["actions"]
                chunk = np.asarray(chunk, dtype=np.float32)
                n = min(replan, len(chunk))
                if n < 1:
                    break
                plan = list(chunk[:n])
            act = np.asarray(plan.pop(0), dtype=np.float32).ravel()[:8]
            obs, _r, term, trunc, info = env.step(act)
            if _success(info):
                ok = True
            done = bool(term or trunc) or ok
    finally:
        env.close()
    return ok


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--experiment", required=True,
                    choices=["color_size", "color_spatial"])
    ap.add_argument("--host", default="127.0.0.1")
    ap.add_argument("--port", type=int, default=8000)
    ap.add_argument("--seed", type=int, default=42)
    ap.add_argument("--results-txt", required=True)
    ap.add_argument("--sim-backend", default="cpu")
    ap.add_argument("--task-difficulty", type=float, default=1.3)
    ap.add_argument("--max-episode-steps", type=int, default=150)
    ap.add_argument("--replan-steps", type=int, default=10)
    ap.add_argument("--distractor-max", type=int, default=2)
    ap.add_argument("--max-cells", type=int, default=0)
    ap.add_argument("--target-episodes", type=int, default=0,
                    help=">0: run ceil(target/ncells) reps per cell (~target total)")
    a = ap.parse_args()

    if a.experiment == "color_size":
        f2vals, f2name = SIZES, "size"
        meta = "fixed verb=lift shape=cube"
    else:  # color_spatial
        f2vals, f2name = SPATIALS, "spatial"
        meta = "fixed shape=cube; verb~{lift,grasp,push}; no distractor"
    cells = [(c, x) for c in COLORS for x in f2vals]  # full grid
    if a.max_cells > 0:
        cells = cells[: a.max_cells]

    client = _wcp.WebsocketClientPolicy(a.host, a.port)
    rt = pathlib.Path(a.results_txt)
    rt.parent.mkdir(parents=True, exist_ok=True)
    with rt.open("w") as f:
        f.write(f"# pi0 {a.experiment} grid  {meta}  "
                f"seed={a.seed} task_difficulty={a.task_difficulty} "
                f"sim={a.sim_backend} cells={len(cells)}\n")
        f.write(f"idx color {f2name} success prompt\n")

    import math as _m
    ncells = len(cells)
    reps = max(1, _m.ceil(a.target_episodes / ncells)) if a.target_episodes > 0 else 1
    print(f"cells={ncells} reps/cell={reps}{ncells*reps} episodes/seed", flush=True)

    succ = tot = 0
    for idx, (color, f2) in enumerate(cells, 1):
        for r in range(reps):
            rng = random.Random(a.seed * 100003 + idx * 131 + r)
            try:
                if a.experiment == "color_size":
                    env_id, mk, ro, instr = build_color_size_cell(
                        color, f2, distractor_max_arg=a.distractor_max,
                        task_difficulty=a.task_difficulty)
                else:
                    env_id, mk, ro, instr = build_color_spatial_cell(
                        color, f2, rng=rng, distractor_max_arg=a.distractor_max,
                        task_difficulty=a.task_difficulty)
                ok = run_cell(client, env_id, mk, ro, instr,
                              seed=a.seed + idx * 1000 + r,
                              sim_backend=a.sim_backend,
                              max_steps=a.max_episode_steps, replan=a.replan_steps)
            except Exception as e:  # noqa: BLE001
                print(f"[{idx}/{ncells} r{r}] FAIL {color},{f2}: {e}", flush=True)
                ok, instr = False, f"ERROR:{e}"
            succ += int(ok)
            tot += 1
            with rt.open("a") as f:
                f.write(f'{idx} {color} {f2} rep{r} {int(ok)} "{instr}"\n')
        print(f"[{idx}/{ncells}] {color} {f2} {reps}reps → cum {succ}/{tot}",
              flush=True)

    rate = 100.0 * succ / tot if tot else 0.0
    with rt.open("a") as f:
        f.write(f"\noverall_success={succ}/{tot} ({rate:.1f}%)\n")
    print(f"\nDone {a.experiment}: overall_success={succ}/{tot} ({rate:.1f}%)")


if __name__ == "__main__":
    main()