File size: 27,938 Bytes
5f08626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
"""
Feed-forward camera pose benchmark on CO3D (multi-backend) — reference implementation
======================================================================================

This script standardizes **relative pose error** evaluation on the **Common Objects
in 3D (CO3D)** test annotations, following the protocol in ``eval_pose_vggt.py``.

Supported backends (``--backends``)
-----------------------------------
- ``anysplat_baseline``: ``AnySplat.from_pretrained`` (feed-forward encoder + pose head path used in ``src.eval_pose.process_sequence``).
- ``vggt``: ``VGGT.from_pretrained`` with ``pose_enc`` decoded via ``pose_encoding_to_extri_intri``.
- ``anysplat_finetune``: optional; requires ``--finetune_ckpt``.

Sampling & reproducibility
--------------------------
- By default, each category builds a **fixed sampling plan**: ``num_frames`` indices per
  sequence, gated by ``min_num_images`` and max image size ``>= 448`` (same gates as the
  legacy script). The plan is written to ``<output>/co3d_sampling_plan.json``.
- Pass ``--sampling_plan_path`` to reuse a saved plan (recommended for paper numbers).

Metrics
-------
- Per-frame relative rotation / translation errors vs. GT extrinsics, then **AUC**
  curves at thresholds **5°, 10°, 20°, 30°** (``calculate_auc_np``), aggregated per
  category and mean over categories.

Pose alignment (``--pose_postprocess``)
---------------------------------------
- ``legacy``: align GT to the first camera only (AnySplat path flag ``gt_only``; VGGT ``gt_only``).
- ``align_both``: align **both** predictions and GT to the first camera before error.

Outputs
-------
- ``co3d_pose_metrics.json``, ``co3d_pose_summary.txt`` under ``--output_dir/<run_tag>/``.

Dependencies when vendoring
---------------------------
Requires CO3D images + ``*_test.jgz`` annotations, VGGT / AnySplat code paths under
``src.model``, ``src.utils.pose``, and ``src.eval_pose.process_sequence``.

``BENCHMARK_VERSION`` documents the protocol; bump when sampling, AUC definition, or
alignment semantics change.
"""

from __future__ import annotations

import os
import sys
import json
import gzip
import argparse
import datetime
import gc
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple

import numpy as np
from PIL import Image

import torch

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.model.encoder.vggt.models.vggt import VGGT
from src.model.encoder.vggt.utils.pose_enc import pose_encoding_to_extri_intri
from src.model.encoder.vggt.utils.load_fn import load_and_preprocess_images
from src.model.model.anysplat import AnySplat
from src.utils.pose import (
    align_to_first_camera,
    calculate_auc_np,
    convert_pt3d_RT_to_opencv,
    se3_to_relative_pose_error,
)
from src.eval_pose import process_sequence as process_sequence_anysplat

BENCHMARK_VERSION = "1.0.0"
BENCHMARK_NAME = "feed_forward_pose_co3d_multi_backend"


def setup_args():
    parser = argparse.ArgumentParser(
        description=(
            f"{BENCHMARK_NAME} v{BENCHMARK_VERSION}: CO3D pose eval for feed-forward models "
            "(AnySplat baseline, VGGT, optional finetune; fixed frame ids via sampling plan; "
            "pose postprocess options)."
        )
    )
    parser.add_argument("--debug", action="store_true", help="Enable debug mode (only test on specific category)")
    parser.add_argument("--use_ba", action="store_true", default=False, help="Bundle adjustment (AnySplat backends only)")
    parser.add_argument("--fast_eval", action="store_true", default=False, help="Only evaluate 10 sequences per category")
    parser.add_argument("--min_num_images", type=int, default=50, help="Minimum number of images for a sequence")
    parser.add_argument("--num_frames", type=int, default=10, help="Number of frames to use for testing")
    parser.add_argument("--co3d_dir", type=str, required=True, help="Path to CO3D dataset")
    parser.add_argument("--co3d_anno_dir", type=str, required=True, help="Path to CO3D annotations")
    parser.add_argument(
        "--categories",
        type=str,
        default="auto",
        help='Comma-separated categories, or "auto" to detect from *_test.jgz',
    )
    parser.add_argument("--seed", type=int, default=0, help="Random seed for sampling (scan phase only)")
    parser.add_argument(
        "--vggt_repo_id",
        type=str,
        default="facebook/VGGT-1B",
        help='HuggingFace repo id for VGGT weights (default: "facebook/VGGT-1B")',
    )
    parser.add_argument(
        "--anysplat_pretrained_id",
        type=str,
        default="lhjiang/anysplat",
        help="HuggingFace id for baseline AnySplat",
    )
    parser.add_argument(
        "--finetune_ckpt",
        type=str,
        default=None,
        help="Finetuned weights: .ckpt, run dir with checkpoints/, or HF-style AnySplat folder.",
    )
    parser.add_argument(
        "--output_dir",
        type=str,
        default="output/exp_output2_bench_finetune_singlegpu_gt",
        help="Root directory for this evaluation run.",
    )
    parser.add_argument(
        "--run_tag",
        type=str,
        default=None,
        help='Subfolder under output_dir (default: timestamp "%%Y-%%m-%%d_%%H-%%M-%%S").',
    )
    parser.add_argument(
        "--sampling_plan_path",
        type=str,
        default=None,
        help="If set, load co3d_sampling_plan.json from this path and skip the scan phase (must match categories / annotations).",
    )
    parser.add_argument(
        "--pose_postprocess",
        type=str,
        choices=["legacy", "align_both"],
        default="legacy",
        help="legacy: align GT to first camera only (previous behavior). align_both: align pred and GT to first camera on all backends.",
    )
    parser.add_argument(
        "--backends",
        type=str,
        default="all",
        help='Which models to run, comma-separated: anysplat_baseline, vggt, anysplat_finetune. '
        'Use "all" to run baseline + VGGT and also anysplat_finetune when --finetune_ckpt is set.',
    )
    return parser.parse_args()


def parse_backends_selection(backends: str, finetune_ckpt: Optional[str]) -> List[str]:
    raw = backends.strip().lower()
    allowed = frozenset({"anysplat_baseline", "vggt", "anysplat_finetune"})
    if raw == "all":
        out = ["anysplat_baseline", "vggt"]
        if finetune_ckpt:
            out.append("anysplat_finetune")
        else:
            print("[info] --backends all: omitting anysplat_finetune (pass --finetune_ckpt to include it)")
        return out
    names = [x.strip().lower() for x in backends.split(",") if x.strip()]
    if not names:
        raise SystemExit("--backends is empty; use e.g. anysplat_finetune or all")
    seen = set()
    deduped: List[str] = []
    for n in names:
        if n in seen:
            continue
        seen.add(n)
        deduped.append(n)
    names = deduped
    for n in names:
        if n not in allowed:
            raise SystemExit(f"Unknown backend {n!r}. Allowed: {sorted(allowed)}")
    if "anysplat_finetune" in names and not finetune_ckpt:
        raise SystemExit("Including anysplat_finetune requires --finetune_ckpt")
    return names


def build_backends_for_eval(
    selected: List[str],
    args: argparse.Namespace,
    device: torch.device,
) -> List[Tuple[str, Callable[[], torch.nn.Module], str]]:
    """Return list of (name, loader, kind) in the same order as ``selected``."""
    out: List[Tuple[str, Callable[[], torch.nn.Module], str]] = []
    for name in selected:
        if name == "anysplat_baseline":
            hf = args.anysplat_pretrained_id
            out.append((name, lambda hf=hf: AnySplat.from_pretrained(hf), "anysplat"))
        elif name == "vggt":
            rid = args.vggt_repo_id
            out.append((name, lambda rid=rid: VGGT.from_pretrained(rid), "vggt"))
        elif name == "anysplat_finetune":
            ck = args.finetune_ckpt
            bid = args.anysplat_pretrained_id
            out.append((name, lambda ck=ck, bid=bid: load_finetune_anysplat(ck, device, bid), "anysplat"))
        else:
            raise RuntimeError(f"Unhandled backend: {name}")
    return out


def _anysplat_pose_align_flag(pose_postprocess: str) -> str:
    return "both" if pose_postprocess == "align_both" else "gt_only"


def _vggt_align_mode(pose_postprocess: str) -> str:
    return "both" if pose_postprocess == "align_both" else "gt_only"


def prepare_co3d_views(
    seq_data: list,
    co3d_dir: str,
    min_num_images: int,
    num_frames: int,
    rng: np.random.Generator,
) -> Optional[Dict[str, Any]]:
    """
    Same gate + RNG semantics as the original per-sequence eval:
    rng.choice is executed only after a valid metadata list is built; if max_size < 448 after that, returns None but RNG was already consumed.
    """
    if len(seq_data) < min_num_images:
        return None

    metadata = []
    for data in seq_data:
        if data["T"][0] + data["T"][1] + data["T"][2] > 1e5:
            return None
        extri_opencv = convert_pt3d_RT_to_opencv(data["R"], data["T"])
        metadata.append({"filepath": data["filepath"], "extri": extri_opencv})

    ids = rng.choice(len(metadata), num_frames, replace=False)
    frame_filepaths = [metadata[int(i)]["filepath"] for i in ids]
    image_names = [os.path.join(co3d_dir, fp) for fp in frame_filepaths]
    gt_extri = np.stack([np.array(metadata[int(i)]["extri"]) for i in ids], axis=0)

    max_size = max(Image.open(image_names[0]).size)
    if max_size < 448:
        return None

    return {
        "ids": ids.astype(np.int64),
        "frame_filepaths": frame_filepaths,
        "gt_extri": gt_extri,
    }


def plan_entry_to_json(entry: Dict[str, Any]) -> dict:
    return {
        "seq_name": entry["seq_name"],
        "ids": [int(x) for x in entry["ids"]],
        "frame_filepaths": list(entry["frame_filepaths"]),
        "gt_extri": np.asarray(entry["gt_extri"], dtype=np.float64).tolist(),
    }


def plan_entry_from_json(obj: dict, co3d_dir: str) -> Dict[str, Any]:
    ids = np.asarray(obj["ids"], dtype=np.int64)
    fps = list(obj["frame_filepaths"])
    image_names = [os.path.join(co3d_dir, fp) for fp in fps]
    gt_extri = np.asarray(obj["gt_extri"], dtype=np.float64)
    return {
        "seq_name": obj["seq_name"],
        "ids": ids,
        "frame_filepaths": fps,
        "image_names": image_names,
        "gt_extri": gt_extri,
    }


def build_sampling_plan(
    args: argparse.Namespace,
    categories: List[str],
) -> Dict[str, List[dict]]:
    rng = np.random.default_rng(args.seed)
    plan: Dict[str, List[dict]] = {c: [] for c in categories}

    for category in categories:
        annotation_file = os.path.join(args.co3d_anno_dir, f"{category}_test.jgz")
        try:
            with gzip.open(annotation_file, "r") as fin:
                annotation = json.loads(fin.read())
        except FileNotFoundError:
            print(f"Annotation file not found for {category}, skipping plan")
            continue

        n_success = 0
        for seq_name, seq_data in annotation.items():
            if args.debug and not os.path.exists(os.path.join(args.co3d_dir, category, seq_name)):
                continue

            prep = prepare_co3d_views(
                seq_data,
                args.co3d_dir,
                args.min_num_images,
                args.num_frames,
                rng,
            )
            if prep is None:
                continue

            entry = {"seq_name": seq_name, **prep}
            plan[category].append(plan_entry_to_json(entry))
            n_success += 1

            if args.fast_eval and n_success >= 10:
                break

    return plan


def save_sampling_plan(path: Path, plan: Dict[str, List[dict]], meta: dict) -> None:
    payload = {"version": 1, "meta": meta, "plan": plan}
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w") as f:
        json.dump(payload, f, indent=2)
    print(f"Saved sampling plan to {path}")


def load_sampling_plan(path: Path, co3d_dir: str) -> Dict[str, List[Dict[str, Any]]]:
    with open(path, "r") as f:
        payload = json.load(f)
    raw_plan = payload.get("plan", payload)
    out: Dict[str, List[Dict[str, Any]]] = {}
    for cat, entries in raw_plan.items():
        out[cat] = [plan_entry_from_json(e, co3d_dir) for e in entries]
    print(f"Loaded sampling plan from {path} ({sum(len(v) for v in out.values())} entries)")
    return out


def _relative_pose_errors_from_extrinsics(
    pred_extrinsic: torch.Tensor,
    gt_extri_np: np.ndarray,
    num_frames: int,
    device: torch.device,
    align_mode: str,
) -> Tuple[np.ndarray, np.ndarray]:
    gt_extrinsic = torch.from_numpy(gt_extri_np).to(device)
    add_row = torch.tensor([0, 0, 0, 1], device=device).expand(pred_extrinsic.size(0), 1, 4)
    pred_se3 = torch.cat((pred_extrinsic, add_row), dim=1)
    gt_se3 = torch.cat((gt_extrinsic, add_row), dim=1)

    if align_mode == "gt_only":
        gt_se3 = align_to_first_camera(gt_se3)
    elif align_mode == "both":
        pred_se3 = align_to_first_camera(pred_se3)
        gt_se3 = align_to_first_camera(gt_se3)
    else:
        raise ValueError(f"Unknown align_mode: {align_mode}")

    rel_rangle_deg, rel_tangle_deg = se3_to_relative_pose_error(pred_se3, gt_se3, num_frames)
    return rel_rangle_deg.cpu().numpy(), rel_tangle_deg.cpu().numpy()


def infer_vggt_on_plan_entry(
    model,
    prepared: Dict[str, Any],
    category: str,
    seq_name: str,
    num_frames: int,
    device: torch.device,
    dtype: torch.dtype,
    align_mode: str,
) -> Tuple[np.ndarray, np.ndarray]:
    image_names = prepared["image_names"]
    gt_extri = prepared["gt_extri"]
    images = load_and_preprocess_images(image_names)[None].to(device)

    with torch.no_grad(), torch.cuda.amp.autocast(dtype=dtype):
        pred = model(images)
        pred_all_pose_enc = pred["pose_enc"]

    with torch.cuda.amp.autocast(dtype=torch.float32):
        pred_all_extrinsic, _ = pose_encoding_to_extri_intri(pred_all_pose_enc, images.shape[-2:])
        pred_extrinsic = pred_all_extrinsic[0]

    rel_r, rel_t = _relative_pose_errors_from_extrinsics(
        pred_extrinsic, gt_extri, num_frames, device, align_mode
    )
    print(f"{category} sequence {seq_name} Rot Error: {rel_r.mean():.4f}")
    print(f"{category} sequence {seq_name} Trans Error: {rel_t.mean():.4f}")
    return rel_r, rel_t


def _resolve_checkpoint_file(path: Path) -> Path:
    path = path.expanduser().resolve()
    if path.is_file():
        return path
    if path.is_dir():
        ckpt_dir = path / "checkpoints"
        if ckpt_dir.is_dir():
            ckpts = list(ckpt_dir.glob("*.ckpt"))
            if ckpts:
                ckpts.sort(key=lambda p: p.stat().st_mtime)
                return ckpts[-1]
        ckpts = sorted(path.glob("**/*.ckpt"), key=lambda p: p.stat().st_mtime)
        if ckpts:
            return ckpts[-1]
        if (path / "config.json").exists():
            return path
    raise FileNotFoundError(f"No checkpoint or AnySplat bundle found at: {path}")


def load_finetune_anysplat(ckpt: str, device: torch.device, base_hf_id: str = "lhjiang/anysplat") -> AnySplat:
    resolved = _resolve_checkpoint_file(Path(ckpt))
    if resolved.is_dir():
        model = AnySplat.from_pretrained(str(resolved))
        model.to(device)
        model.eval()
        for p in model.parameters():
            p.requires_grad = False
        return model

    model = AnySplat.from_pretrained(base_hf_id)
    try:
        blob = torch.load(resolved, map_location="cpu", weights_only=False)
    except TypeError:
        blob = torch.load(resolved, map_location="cpu")
    state = blob["state_dict"] if isinstance(blob, dict) and "state_dict" in blob else blob
    if not isinstance(state, dict):
        raise ValueError(f"Unexpected checkpoint format in {resolved}")

    stripped: Dict[str, Any] = {}
    for k, v in state.items():
        nk = k
        if nk.startswith("module."):
            nk = nk[len("module.") :]
        if nk.startswith("model."):
            nk = nk[len("model.") :]
        stripped[nk] = v

    model_keys = set(model.state_dict().keys())
    stripped_keys = set(stripped.keys())
    overlap = len(model_keys & stripped_keys)
    print(
        f"[finetune] ckpt keys={len(stripped)} overlap_with_AnySplat={overlap} / {len(model_keys)} "
        f"(resolved file: {resolved})"
    )

    missing, unexpected = model.load_state_dict(stripped, strict=False)
    print(f"[finetune] load_state_dict strict=False: missing={len(missing)}, unexpected={len(unexpected)}")
    if missing:
        print(f"[finetune] missing (first 8): {missing[:8]}")
    if unexpected:
        print(f"[finetune] unexpected (first 8): {unexpected[:8]}")
    if overlap < 50 or len(missing) > len(model_keys) * 0.25:
        print(
            "[finetune][warn] Few keys matched the Hub AnySplat — weights may be mostly baseline or load is wrong; "
            "pose metrics can be misleading."
        )
    model.to(device)
    model.eval()
    for p in model.parameters():
        p.requires_grad = False
    return model


def _finalize_category(r_error_list: List[float], t_error_list: List[float]) -> Optional[Dict[str, Any]]:
    if not r_error_list:
        return None
    r_error = np.array(r_error_list)
    t_error = np.array(t_error_list)
    thresholds = [5, 10, 20, 30]
    aucs = {}
    for th in thresholds:
        auc, _ = calculate_auc_np(r_error, t_error, max_threshold=th)
        aucs[th] = auc
    return {
        "rError": r_error,
        "tError": t_error,
        "Auc_5": aucs[5],
        "Auc_10": aucs[10],
        "Auc_20": aucs[20],
        "Auc_30": aucs[30],
    }


def _run_one_backend_on_plan(
    backend_name: str,
    model: torch.nn.Module,
    backend_kind: str,
    plan: Dict[str, List[Dict[str, Any]]],
    args: argparse.Namespace,
    categories: List[str],
    device: torch.device,
    dtype: torch.dtype,
    pose_align_anysplat: str,
    vggt_align_mode: str,
) -> Dict[str, Any]:
    per_category: Dict[str, Any] = {}
    _unused_rng = np.random.default_rng(0)

    for category in categories:
        entries = plan.get(category) or []
        if not entries:
            print(f"[{backend_name}] No cached entries for {category}, skipping")
            continue

        annotation_file = os.path.join(args.co3d_anno_dir, f"{category}_test.jgz")
        try:
            with gzip.open(annotation_file, "r") as fin:
                annotation = json.loads(fin.read())
        except FileNotFoundError:
            print(f"Annotation file not found for {category}, skipping")
            continue

        print(f"[{backend_name}] Evaluating {len(entries)} cached sequences for {category}")
        r_err: List[float] = []
        t_err: List[float] = []

        for prepared in entries:
            seq_name = prepared["seq_name"]
            print("-" * 50)
            print(f"[{backend_name}] {category} / {seq_name}")

            if args.debug and not os.path.exists(os.path.join(args.co3d_dir, category, seq_name)):
                print(f"Skipping {seq_name} (not found)")
                continue

            seq_data = annotation.get(seq_name)
            if seq_data is None:
                print(f"No annotation for {seq_name}, skipping")
                continue

            if backend_kind == "vggt":
                seq_r, seq_t = infer_vggt_on_plan_entry(
                    model,
                    prepared,
                    category,
                    seq_name,
                    args.num_frames,
                    device,
                    dtype,
                    vggt_align_mode,
                )
            elif backend_kind == "anysplat":
                seq_r, seq_t = process_sequence_anysplat(
                    model,
                    seq_name,
                    seq_data,
                    category,
                    args.co3d_dir,
                    args.min_num_images,
                    args.num_frames,
                    args.use_ba,
                    device,
                    dtype,
                    _unused_rng,
                    frame_ids=prepared["ids"],
                    pose_align=pose_align_anysplat,
                )
            else:
                raise ValueError(f"Unknown backend_kind: {backend_kind}")

            print("-" * 50)
            if seq_r is not None and seq_t is not None:
                r_err.extend(np.asarray(seq_r).reshape(-1).tolist())
                t_err.extend(np.asarray(seq_t).reshape(-1).tolist())

        fin = _finalize_category(r_err, t_err)
        if fin is None:
            print(f"No valid results for {category} ({backend_name}), skipping")
            continue

        print("=" * 80)
        print(f"[{backend_name}] AUC of {category} test set: {fin['Auc_30']:.4f}")
        print("=" * 80)
        per_category[category] = fin

    return per_category


def _print_and_collect_means(per_category: Dict[str, Any]) -> Dict[str, float]:
    means = {}
    if not per_category:
        return means
    for key in ["Auc_5", "Auc_10", "Auc_20", "Auc_30"]:
        means[key] = float(np.mean([per_category[c][key] for c in per_category]))
    print("\nSummary of AUC results:")
    print("-" * 50)
    for category in sorted(per_category.keys()):
        print(f"{category:<15} AUC_5: {per_category[category]['Auc_5']:.4f}")
        print(f"{category:<15} AUC_30: {per_category[category]['Auc_30']:.4f}")
        print(f"{category:<15} AUC_20: {per_category[category]['Auc_20']:.4f}")
        print(f"{category:<15} AUC_10: {per_category[category]['Auc_10']:.4f}")
    print("-" * 50)
    print(f"Mean AUC_5: {means['Auc_5']:.4f}")
    print(f"Mean AUC_30: {means['Auc_30']:.4f}")
    print(f"Mean AUC_20: {means['Auc_20']:.4f}")
    print(f"Mean AUC_10: {means['Auc_10']:.4f}")
    return means


def _save_results(
    out_root: Path,
    args: argparse.Namespace,
    all_backends: Dict[str, Dict[str, Any]],
    all_means: Dict[str, Dict[str, float]],
):
    out_root.mkdir(parents=True, exist_ok=True)

    serializable = {}
    for bname, per_cat in all_backends.items():
        serializable[bname] = {}
        for cat, d in per_cat.items():
            serializable[bname][cat] = {
                "Auc_5": float(d["Auc_5"]),
                "Auc_10": float(d["Auc_10"]),
                "Auc_20": float(d["Auc_20"]),
                "Auc_30": float(d["Auc_30"]),
            }

    payload = {
        "benchmark": BENCHMARK_NAME,
        "version": BENCHMARK_VERSION,
        "args": vars(args),
        "per_category_auc": serializable,
        "mean_auc": {k: v for k, v in all_means.items()},
    }
    with open(out_root / "co3d_pose_metrics.json", "w") as f:
        json.dump(payload, f, indent=2)

    lines = [
        f"CO3D pose evaluation — {BENCHMARK_NAME} v{BENCHMARK_VERSION} (AnySplat / VGGT / finetune)",
        "=" * 60,
        json.dumps(vars(args), indent=2),
        "",
    ]
    for bname in sorted(all_backends.keys()):
        lines.append(f"### {bname}")
        lines.append("-" * 40)
        pc = all_backends[bname]
        for cat in sorted(pc.keys()):
            lines.append(
                f"{cat:<15} AUC_5/10/20/30: {pc[cat]['Auc_5']:.4f} / {pc[cat]['Auc_10']:.4f} / "
                f"{pc[cat]['Auc_20']:.4f} / {pc[cat]['Auc_30']:.4f}"
            )
        if bname in all_means and all_means[bname]:
            m = all_means[bname]
            lines.append(
                f"MEAN            AUC_5/10/20/30: {m['Auc_5']:.4f} / {m['Auc_10']:.4f} / "
                f"{m['Auc_20']:.4f} / {m['Auc_30']:.4f}"
            )
        lines.append("")

    with open(out_root / "co3d_pose_summary.txt", "w") as f:
        f.write("\n".join(lines))

    print(f"Wrote {out_root / 'co3d_pose_metrics.json'} and {out_root / 'co3d_pose_summary.txt'}")


def run_feed_forward_co3d_pose_benchmark(args: argparse.Namespace) -> None:
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    dtype = torch.bfloat16

    if args.categories.strip().lower() == "auto":
        anno_dir = Path(args.co3d_anno_dir)
        categories = sorted(p.name[:-9] for p in anno_dir.glob("*_test.jgz") if p.name.endswith("_test.jgz"))
    else:
        categories = [c.strip() for c in args.categories.split(",") if c.strip()]

    if not categories:
        raise RuntimeError(f"No categories found to evaluate in {args.co3d_anno_dir}")

    if args.debug:
        categories = categories[:1]
        print(f"Debug mode on, only evaluating category: {categories[0]}")

    tag = args.run_tag or datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    out_root = Path(args.output_dir).expanduser().resolve() / tag
    out_root.mkdir(parents=True, exist_ok=True)
    print(f"Results directory: {out_root}")

    pose_align_anysplat = _anysplat_pose_align_flag(args.pose_postprocess)
    vggt_align = _vggt_align_mode(args.pose_postprocess)

    if args.sampling_plan_path:
        plan_path = Path(args.sampling_plan_path).expanduser().resolve()
        hydrated_full = load_sampling_plan(plan_path, args.co3d_dir)
        hydrated = {c: hydrated_full.get(c, []) for c in categories}
        unknown = [c for c in categories if c not in hydrated_full]
        if unknown:
            print(f"[warn] No entries in loaded plan for categories (empty lists): {unknown}")
    else:
        raw_plan = build_sampling_plan(args, categories)
        plan_serializable = {cat: list(raw_plan.get(cat, [])) for cat in categories}
        meta = {
            "benchmark": BENCHMARK_NAME,
            "version": BENCHMARK_VERSION,
            "seed": args.seed,
            "co3d_dir": os.path.abspath(args.co3d_dir),
            "co3d_anno_dir": os.path.abspath(args.co3d_anno_dir),
            "num_frames": args.num_frames,
            "min_num_images": args.min_num_images,
            "fast_eval": args.fast_eval,
            "categories": categories,
        }
        save_sampling_plan(out_root / "co3d_sampling_plan.json", plan_serializable, meta)
        hydrated = {cat: [plan_entry_from_json(e, args.co3d_dir) for e in plan_serializable[cat]] for cat in categories}

    total_entries = sum(len(hydrated.get(c, [])) for c in categories)
    if total_entries == 0:
        raise RuntimeError("Sampling plan is empty — no valid sequences. Check CO3D paths and filters.")

    selected = parse_backends_selection(args.backends, args.finetune_ckpt)
    print(f"Backends to evaluate (in order): {selected}")
    backends = build_backends_for_eval(selected, args, device)

    all_backends: Dict[str, Dict[str, Any]] = {}
    all_means: Dict[str, Dict[str, float]] = {}

    for backend_name, loader, kind in backends:
        print("\n" + "#" * 80)
        print(f"Loading backend: {backend_name}")
        print("#" * 80)
        model = loader()
        model.to(device)
        model.eval()
        for p in model.parameters():
            p.requires_grad = False

        per_cat = _run_one_backend_on_plan(
            backend_name,
            model,
            kind,
            hydrated,
            args,
            categories,
            device,
            dtype,
            pose_align_anysplat,
            vggt_align,
        )
        all_backends[backend_name] = per_cat
        all_means[backend_name] = _print_and_collect_means(per_cat)

        del model
        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    _save_results(out_root, args, all_backends, all_means)


def main() -> None:
    args = setup_args()
    run_feed_forward_co3d_pose_benchmark(args)


# Backward-compatible name for callers that imported ``evaluate`` from ``eval_pose_vggt``.
evaluate = run_feed_forward_co3d_pose_benchmark


if __name__ == "__main__":
    main()