File size: 1,643 Bytes
273b14c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""How pooled/features.npz was made: one (policy steps, 6144) float32 array per episode, pooled from its rollout.npz.

    python pool_features.py <dataset folder with manifest.csv and <dir>/rollout.npz> features.npz

Columns: [0:2048] mean over the base-image tokens, [2048:4096] mean over the wrist-image tokens,
[4096:6144] mean over the non-padded language tokens (language_mask), all averaged in float32.
"""

import csv
import sys
from pathlib import Path

import numpy as np


def pool_rollout(path: Path) -> np.ndarray:
    """(n_policy, 6144) float32 from one rollout.npz."""
    with np.load(path) as z:
        base = z["base_image"].astype(np.float32).mean(axis=1)  # (steps, 88, 2048) -> (steps, 2048)
        wrist = z["wrist_image"].astype(np.float32).mean(axis=1)
        mask = z["language_mask"].astype(np.float32)  # (steps, 200), True for real tokens
        language = (z["language"].astype(np.float32) * mask[..., None]).sum(axis=1) / np.maximum(mask.sum(axis=1, keepdims=True), 1.0)
    return np.concatenate([base, wrist, language], axis=1).astype(np.float32)


def main(root: str, out: str) -> None:
    with open(Path(root) / "manifest.csv", newline="") as f:
        rows = list(csv.DictReader(f))
    arrays = {}
    for r in rows:
        x = pool_rollout(Path(root) / r["dir"] / "rollout.npz")
        assert x.shape == (int(r["n_policy"]), 6144), (r["dir"], x.shape)  # one row per policy step
        arrays[r["dir"]] = x
    np.savez(out, **arrays)  # keys are the `dir` column of manifest.csv
    print(f"wrote {len(arrays)} episodes to {out}")


if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])