File size: 14,970 Bytes
ec0a9aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
Classify GR1 humanoid episodes as 'markovian' or 'non_markovian' using
Qwen3-VL-30B-A3B-Instruct-FP8 served via vLLM.

Reads prompts from the two batch_input.json files (DreamDojo-HV_Eval and
EgoDex_Eval), classifies all ~133 episodes, and outputs:
  - scripts/gr1_task_labels.csv          -- all episodes with label + reason
  - scripts/gr1_episode_sampled.csv      -- balanced sample (equal per label
                                            AND equal across the two sources)

Usage:
    # 1. Start the vLLM server first:
    #    bash scripts/launch_qwen3vl_server.sh

    # 2. Run (text-only):
    python scripts/classify_markovian_gr1.py

    # Run with vision (sends input PNG image to model):
    python scripts/classify_markovian_gr1.py --use-vision --batch-size 5

    # Optional flags:
    python scripts/classify_markovian_gr1.py \
        --dreamdojo-json  sampling_dataset/humanoid/singleview/input/PhysicalAI-Robotics-GR00T-Teleop-GR1/DreamDojo-HV_Eval/batch_input.json \
        --egodex-json     sampling_dataset/humanoid/singleview/input/PhysicalAI-Robotics-GR00T-Teleop-GR1/EgoDex_Eval/batch_input.json \
        --cache-file      scripts/gr1_task_labels_cache.jsonl \
        --output-csv      scripts/gr1_task_labels.csv \
        --sampled-csv     scripts/gr1_episode_sampled.csv \
        --sample-per-label-per-source  33 \
        --base-url        http://localhost:8000/v1 \
        --model           Qwen/Qwen3-VL-30B-A3B-Instruct-FP8 \
        --batch-size      5 \
        --max-image-px    560 \
        --max-retries     2 \
        --seed            42
"""

import argparse
import base64
import io
import json
import sys
import time
from pathlib import Path

import requests
import pandas as pd
from PIL import Image, ImageFile
from tqdm import tqdm

ImageFile.LOAD_TRUNCATED_IMAGES = True

# ---------------------------------------------------------------------------
# Prompt
# ---------------------------------------------------------------------------

SYSTEM_PROMPT = """You are a robot manipulation task classifier.

You will be given one or more episodes. Each episode has:
- An image showing the robot's initial state (if provided)
- A task description prompt

Classify each task as exactly one of:
- "markovian": a SINGLE atomic manipulation action. The robot only needs to observe the current state to act. No memory of previous steps is required.
  Typical examples: "picks up a banana", "places a cup on the table", "pushes a ball", "removes a block from the stack", "opens a drawer"

- "non_markovian": involves MULTIPLE sequential steps, OR requires the robot to remember what it has already done, OR involves ongoing/continuous actions that track progress.
  Typical examples: "picks up X then places it into Y while holding Z", "stirs … repeatedly", "topples a line of tiles", "uses both arms to lift and rotate", "sprays water onto plants"

Rules:
- Connectors like "then", "while", "after", "followed by" → non_markovian
- Ongoing / repetitive actions (stir, spray, wipe, fold, rotate) → non_markovian
- Both arms doing different simultaneous things → non_markovian
- Simple single pick, place, push, remove, open/close → markovian
- A pick-and-place is markovian (one action) even with two locations
- Use the image to resolve ambiguity (e.g. verify object count, arm configuration)

Reply with ONLY a valid JSON array (no markdown fences, no extra text):
[{"id": "<id>", "label": "markovian"|"non_markovian", "reason": "<≤12 words>"}]"""

USER_TEMPLATE = "Classify these tasks:\n{tasks_json}"

# ---------------------------------------------------------------------------
# Vision helpers
# ---------------------------------------------------------------------------


def encode_image_b64(image_path: Path, max_px: int = 560) -> str:
    """Load image, resize so longest side <= max_px, return base64 PNG string."""
    img = Image.open(image_path).convert("RGB")
    w, h = img.size
    if max(w, h) > max_px:
        scale = max_px / max(w, h)
        img = img.resize((int(w * scale), int(h * scale)), Image.Resampling.LANCZOS)
    buf = io.BytesIO()
    img.save(buf, format="PNG")
    return base64.b64encode(buf.getvalue()).decode("utf-8")


def build_vision_user_content(batch: list[dict], max_px: int) -> list[dict]:
    """Build multimodal content list: interleave images with per-episode text."""
    content = []
    for i, row in enumerate(batch):
        img_path = Path(row["input_image"])
        img_ok = False
        if img_path.exists():
            try:
                b64 = encode_image_b64(img_path, max_px)
                content.append({
                    "type": "image_url",
                    "image_url": {"url": f"data:image/png;base64,{b64}"},
                })
                img_ok = True
            except Exception as e:
                print(f"  [warn] Could not load image {img_path.name}: {e}", file=sys.stderr)
        suffix = "" if img_ok else " [image unavailable]"
        content.append({
            "type": "text",
            "text": f'Episode {i+1} (id="{row["id"]}"): {row["prompt"]}{suffix}',
        })
    content.append({
        "type": "text",
        "text": "\nNow classify all episodes above. Reply with only the JSON array.",
    })
    return content

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def load_batch_json(path: Path, source: str) -> list[dict]:
    data = json.loads(path.read_text())
    rows = []
    for item in data:
        out_video = item.get("output_video", "")
        episode_name = Path(out_video).stem  # e.g. episode_000000
        rows.append(
            {
                "id": f"{source}/{episode_name}",
                "source": source,
                "episode": episode_name,
                "input_image": item.get("input_video", ""),   # it's a .png despite key name
                "output_video": out_video,
                "prompt": item.get("prompt", "").strip(),
            }
        )
    return rows


def load_cache(cache_file: Path) -> dict[str, dict]:
    cache: dict[str, dict] = {}
    if not cache_file.exists():
        return cache
    with open(cache_file) as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            obj = json.loads(line)
            cache[obj["id"]] = obj
    return cache


def append_to_cache(cache_file: Path, results: list[dict]) -> None:
    with open(cache_file, "a") as f:
        for r in results:
            f.write(json.dumps(r, ensure_ascii=False) + "\n")


def call_llm(
    base_url: str,
    model: str,
    batch: list[dict],
    max_retries: int,
    use_vision: bool = False,
    max_image_px: int = 560,
) -> list[dict]:
    if use_vision:
        user_content = build_vision_user_content(batch, max_image_px)
    else:
        payload_tasks = [{"id": r["id"], "task": r["prompt"]} for r in batch]
        user_content = USER_TEMPLATE.format(tasks_json=json.dumps(payload_tasks, ensure_ascii=False))

    payload = {
        "model": model,
        "messages": [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_content},
        ],
        "temperature": 0.0,
        "chat_template_kwargs": {"enable_thinking": False},
    }

    for attempt in range(1, max_retries + 2):
        try:
            resp = requests.post(
                f"{base_url}/chat/completions",
                json=payload,
                timeout=120,
            )
            resp.raise_for_status()
            raw = resp.json()["choices"][0]["message"]["content"].strip()
            if raw.startswith("```"):
                raw = raw.split("```")[1]
                if raw.startswith("json"):
                    raw = raw[4:]
                raw = raw.strip()

            parsed: list[dict] = json.loads(raw)
            id_map = {r["id"]: r for r in batch}
            results = []
            for item in parsed:
                rid = item.get("id", "")
                label = item.get("label", "").strip().lower()
                if label not in ("markovian", "non_markovian"):
                    label = "parse_error"
                src_row = id_map.get(rid, {})
                results.append(
                    {
                        "id": rid,
                        "source": src_row.get("source", ""),
                        "episode": src_row.get("episode", ""),
                        "input_image": src_row.get("input_image", ""),
                        "output_video": src_row.get("output_video", ""),
                        "prompt": src_row.get("prompt", ""),
                        "label": label,
                        "reason": item.get("reason", ""),
                    }
                )
            return results

        except (json.JSONDecodeError, KeyError, TypeError) as e:
            if attempt <= max_retries:
                time.sleep(2)
            else:
                print(f"  [warn] Parse failed: {e}", file=sys.stderr)
                return [{**r, "label": "parse_error", "reason": str(e)} for r in batch]
        except requests.RequestException as e:
            if attempt <= max_retries:
                time.sleep(5)
            else:
                print(f"  [error] HTTP error: {e}", file=sys.stderr)
                return [{**r, "label": "api_error", "reason": str(e)} for r in batch]

    return []


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------


def main() -> None:
    parser = argparse.ArgumentParser(description="Classify GR1 humanoid tasks")
    parser.add_argument(
        "--dreamdojo-json",
        type=Path,
        default=Path(
            "sampling_dataset/humanoid/singleview/input"
            "/PhysicalAI-Robotics-GR00T-Teleop-GR1/DreamDojo-HV_Eval/batch_input.json"
        ),
    )
    parser.add_argument(
        "--egodex-json",
        type=Path,
        default=Path(
            "sampling_dataset/humanoid/singleview/input"
            "/PhysicalAI-Robotics-GR00T-Teleop-GR1/EgoDex_Eval/batch_input.json"
        ),
    )
    parser.add_argument(
        "--cache-file",
        type=Path,
        default=Path("scripts/gr1_task_labels_cache.jsonl"),
    )
    parser.add_argument(
        "--output-csv",
        type=Path,
        default=Path("scripts/gr1_task_labels.csv"),
    )
    parser.add_argument(
        "--sampled-csv",
        type=Path,
        default=Path("scripts/gr1_episode_sampled.csv"),
    )
    parser.add_argument(
        "--sample-per-label-per-source",
        type=int,
        default=33,
        help=(
            "Episodes per (label × source) cell. "
            "e.g. 33 → up to 33 markovian from DreamDojo + 33 from EgoDex, "
            "same for non_markovian → 132 total max. 0 = keep all."
        ),
    )
    parser.add_argument("--base-url", type=str, default="http://localhost:8000/v1")
    parser.add_argument(
        "--model", type=str, default="Qwen/Qwen3-VL-30B-A3B-Instruct-FP8"
    )
    parser.add_argument("--batch-size", type=int, default=50)
    parser.add_argument("--max-retries", type=int, default=2)
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument(
        "--use-vision",
        action="store_true",
        default=False,
        help="Send input PNG image to the model alongside the prompt (recommended batch-size 5)",
    )
    parser.add_argument(
        "--max-image-px",
        type=int,
        default=560,
        help="Resize images so longest side <= this value before encoding (saves tokens)",
    )
    args = parser.parse_args()

    workspace = Path(__file__).parent.parent

    def resolve(p: Path) -> Path:
        return workspace / p if not p.is_absolute() else p

    dreamdojo_json = resolve(args.dreamdojo_json)
    egodex_json = resolve(args.egodex_json)
    cache_file = resolve(args.cache_file)
    output_csv = resolve(args.output_csv)
    sampled_csv = resolve(args.sampled_csv)
    cache_file.parent.mkdir(parents=True, exist_ok=True)
    output_csv.parent.mkdir(parents=True, exist_ok=True)

    # Load both sources
    all_rows: list[dict] = []
    all_rows += load_batch_json(dreamdojo_json, "DreamDojo-HV_Eval")
    all_rows += load_batch_json(egodex_json, "EgoDex_Eval")
    print(f"[init] Loaded {len(all_rows)} episodes total")

    # Resume from cache
    cache = load_cache(cache_file)
    print(f"[init] Already classified: {len(cache)}")

    pending = [r for r in all_rows if r["id"] not in cache]
    print(f"[init] Remaining to classify: {len(pending)}")

    if pending:
        batches = [
            pending[i : i + args.batch_size]
            for i in range(0, len(pending), args.batch_size)
        ]
        vision_info = f", vision=ON (max_px={args.max_image_px})" if args.use_vision else ", vision=OFF"
        print(f"[run]  {len(batches)} batch(es), model={args.model}, endpoint={args.base_url}{vision_info}")

        for batch in tqdm(batches, desc="Classifying", unit="batch"):
            results = call_llm(
                args.base_url, args.model, batch, args.max_retries,
                use_vision=args.use_vision, max_image_px=args.max_image_px,
            )
            append_to_cache(cache_file, results)
            for r in results:
                cache[r["id"]] = r

    # Build full CSV
    rows = list(cache.values())
    df = pd.DataFrame(rows)
    # Preserve original ordering (DreamDojo first, then EgoDex, sorted by episode)
    df["_order"] = df["id"].map({r["id"]: i for i, r in enumerate(all_rows)})
    df = df.sort_values("_order").drop(columns=["_order"]).reset_index(drop=True)
    df.to_csv(output_csv, index=False)
    print(f"\n[saved] Full labels → {output_csv}  ({len(df)} rows)")
    print(df.groupby(["source", "label"]).size().to_string())

    # Sample equally per (label × source)
    if args.sample_per_label_per_source > 0:
        parts = []
        for (source, label), group in df.groupby(["source", "label"]):
            if label in ("parse_error", "api_error", "unknown"):
                continue
            n = min(args.sample_per_label_per_source, len(group))
            parts.append(group.sample(n=n, random_state=args.seed))
            print(f"[sample] {source} / {label}: {n}/{len(group)}")

        sampled = (
            pd.concat(parts)
            .sort_values(["source", "episode"])
            .reset_index(drop=True)
        )
        sampled.to_csv(sampled_csv, index=False)
        print(f"\n[saved] Sampled → {sampled_csv}  ({len(sampled)} rows)")
        print(sampled.groupby(["source", "label"]).size().to_string())
    else:
        df.to_csv(sampled_csv, index=False)
        print(f"\n[saved] Sampled (all) → {sampled_csv}")


if __name__ == "__main__":
    main()