File size: 2,527 Bytes
f15a766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()