gnn_wm2 / Ctrl-World-Graph /scripts /viz_pred_vs_gt.py
EndeavourDD's picture
Upload folder using huggingface_hub (part 17)
f15a766 verified
Raw
History Blame Contribute Delete
3.69 kB
"""Visualize a saved 5fps-protocol prediction window against GT.
pred npz (eval_5fps_protocol/<variant>/<ckpt>/preds/<ep>/start<NNNN>.npz) holds
pred[50,192,320,3] + frame_ids[50]. GT for pred[k] is the 5fps dataset frame
side/rgb/frame_<frame_ids[k]>.png (resized to 320x192). Writes a side-by-side
mp4 (GT | pred) and a labeled grid png.
"""
import argparse
from pathlib import Path
import numpy as np
from PIL import Image
DATA = Path("/workspace/gnn_data/hanoi_0420_balanced_5fps")
EVAL = Path("/workspace/Ctrl-World-Graph/eval_5fps_protocol")
W, H = 320, 192
PREFIX_IDX = {"1s": 4, "3s": 14, "6s": 29, "10s": 49} # last frame of each prefix (0-based)
def load_gt(episode, frame_ids):
out = []
for fid in frame_ids:
im = Image.open(DATA / episode / "side" / "rgb" / f"frame_{int(fid):06d}.png").convert("RGB").resize((W, H), Image.BILINEAR)
out.append(np.asarray(im, dtype=np.uint8))
return np.stack(out)
def write_mp4(path, frames, fps=5):
try:
import mediapy as media
media.write_video(str(path), frames, fps=fps)
except Exception:
import imageio.v2 as imageio
imageio.mimwrite(str(path), frames, fps=fps, macro_block_size=None)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--pred-npz", type=Path, default=None)
ap.add_argument("--variant", default=None)
ap.add_argument("--step", type=int, default=None)
ap.add_argument("--episode", default=None)
ap.add_argument("--start", type=int, default=None)
ap.add_argument("--out-dir", type=Path, default=Path("/workspace/Ctrl-World-Graph/viz_pred_vs_gt"))
cli = ap.parse_args()
if cli.pred_npz is None:
cli.pred_npz = EVAL / cli.variant / f"checkpoint-{cli.step}" / "preds" / cli.episode / f"start{cli.start:04d}.npz"
d = np.load(cli.pred_npz)
pred, frame_ids = d["pred"], d["frame_ids"]
# infer episode from path: .../preds/<episode>/startNNNN.npz
episode = cli.episode or cli.pred_npz.parent.name
gt = load_gt(episode, frame_ids)
assert gt.shape == pred.shape, f"{gt.shape} vs {pred.shape}"
tag = f"{cli.pred_npz.parts[-4]}_{episode}_{cli.pred_npz.stem}" # variant_episode_startNNNN-ish
out = cli.out_dir / tag
out.mkdir(parents=True, exist_ok=True)
# 1) side-by-side mp4 (GT left | pred right) with a 4px white separator
sep = np.full((pred.shape[0], H, 4, 3), 255, np.uint8)
compare = np.concatenate([gt, sep, pred], axis=2)
write_mp4(out / "compare_gt_left_pred_right.mp4", compare, fps=5)
write_mp4(out / "pred.mp4", pred, fps=5)
write_mp4(out / "gt.mp4", gt, fps=5)
# 2) labeled grid: rows=GT/pred, cols = 1s/3s/6s/10s frames
cols = list(PREFIX_IDX.items())
grid = np.full((2 * H + 3 * 6, len(cols) * W + (len(cols) + 1) * 6, 3), 255, np.uint8)
for c, (name, idx) in enumerate(cols):
x = 6 + c * (W + 6)
grid[6:6 + H, x:x + W] = gt[idx]
grid[12 + H:12 + 2 * H, x:x + W] = pred[idx]
gimg = Image.fromarray(grid)
try:
from PIL import ImageDraw
dr = ImageDraw.Draw(gimg)
for c, (name, idx) in enumerate(cols):
x = 6 + c * (W + 6)
dr.text((x + 4, 0), f"{name} (f{int(frame_ids[idx])})", fill=(0, 0, 0))
dr.text((2, 6 + H // 2), "GT", fill=(255, 0, 0))
dr.text((2, 12 + H + H // 2), "PRED", fill=(255, 0, 0))
except Exception:
pass
gimg.save(out / "grid_GTtop_PREDbottom.png")
print("episode:", episode, " frame_ids:", frame_ids[0], "..", frame_ids[-1])
print("saved:", out / "compare_gt_left_pred_right.mp4")
print("saved:", out / "grid_GTtop_PREDbottom.png")
if __name__ == "__main__":
main()