gnn_wm2 / Ctrl-World-Graph /scripts /viz_batch_grid.py
EndeavourDD's picture
Upload folder using huggingface_hub (part 17)
f15a766 verified
Raw
History Blame Contribute Delete
2.53 kB
"""Batch 1s/3s/6s/10s prefix grid (GT top / PRED bottom) for every window.
Matches the protocol's visualization format (scripts/viz_pred_vs_gt.py grid):
for each saved pred window, render a 2-row x 4-col grid of GT (top) vs PRED
(bottom) at the cumulative-prefix endpoints 1s/3s/6s/10s (frames 5/15/30/50),
labeled with the second and the 5fps frame id. Mirrors preds/<episode>/start<NNNN>.
"""
import argparse
from pathlib import Path
import numpy as np
from PIL import Image, ImageDraw
DATA = Path("/workspace/gnn_data/hanoi_0420_balanced_5fps")
W, H = 320, 192
PREFIX_IDX = {"1s": 4, "3s": 14, "6s": 29, "10s": 49} # last frame of each prefix (0-based)
PAD = 6
def load_gt(episode, frame_ids):
return np.stack([
np.asarray(Image.open(DATA / episode / "side" / "rgb" / f"frame_{int(f):06d}.png").convert("RGB").resize((W, H), Image.BILINEAR), dtype=np.uint8)
for f in frame_ids
])
def make_grid(gt, pred, frame_ids, pred_label):
cols = list(PREFIX_IDX.items())
grid = np.full((2 * H + 3 * PAD + 14, len(cols) * W + (len(cols) + 1) * PAD, 3), 255, np.uint8)
for c, (name, idx) in enumerate(cols):
x = PAD + c * (W + PAD)
grid[14 + PAD:14 + PAD + H, x:x + W] = gt[idx]
grid[14 + 2 * PAD + H:14 + 2 * PAD + 2 * H, x:x + W] = pred[idx]
im = Image.fromarray(grid)
dr = ImageDraw.Draw(im)
for c, (name, idx) in enumerate(cols):
x = PAD + c * (W + PAD)
dr.text((x + 4, 2), f"{name} (frame {int(frame_ids[idx])})", fill=(0, 0, 0))
dr.text((2, 14 + PAD + H // 2), "GT", fill=(255, 0, 0))
dr.text((2, 14 + 2 * PAD + H + H // 2), pred_label, fill=(255, 0, 0))
return im
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--preds-dir", type=Path, required=True)
ap.add_argument("--out-dir", type=Path, required=True)
ap.add_argument("--pred-label", default="PRED")
cli = ap.parse_args()
npzs = sorted(cli.preds_dir.glob("*/start*.npz"))
print(f"{len(npzs)} windows -> {cli.out_dir}")
for i, npz in enumerate(npzs, 1):
episode = npz.parent.name
d = np.load(npz)
pred, fids = d["pred"], d["frame_ids"]
gt = load_gt(episode, fids)
od = cli.out_dir / episode
od.mkdir(parents=True, exist_ok=True)
make_grid(gt, pred, fids, cli.pred_label).save(od / f"{npz.stem}.png")
if i % 20 == 0 or i == len(npzs):
print(f" {i}/{len(npzs)}", flush=True)
print("done:", cli.out_dir)
if __name__ == "__main__":
main()