AsianPlayer/TriDrive / code /aide /features /pack_features.py
AsianPlayer's picture
download
raw
4.21 kB
"""Pack the per-clip Qwen3-VL-2B features into one cache-aligned array file.
python aide/features/pack_features.py [--src results/feats/qwen3vl2b] [--out results/feats/feats_qwen.npz]
Output results/feats/feats_qwen.npz (row order == aide_kp.npz clip order):
pooled [N,10,4,2048] f16 mean over the full last-layer token sequence (Driver-WM App. B.2)
vis_pooled [N,10,4,2048] f16 mean over the vision tokens only (variant)
clip_id [N] str, split [N] str, views [4] str (front, left, right, incar), frame_idx [N,10] int32
n_tokens / n_vis_tokens [N,10,4] int32, meta (json string of the extractor meta of clip 0)
Asserts: every cache clip has a feature file, the view order equals protocol.VIEWS, and frame_idx
equals the cache's frame_idx10 for every clip (the features belong to the protocol frames).
Writes run_pack_features_<utc>.json next to the output.
"""
from __future__ import annotations
import os
for _v in ("OMP_NUM_THREADS", "MKL_NUM_THREADS", "OPENBLAS_NUM_THREADS", "NUMEXPR_NUM_THREADS"):
os.environ.setdefault(_v, "4")
import argparse # noqa: E402
import json # noqa: E402
import sys # noqa: E402
import time # noqa: E402
from pathlib import Path # noqa: E402
import numpy as np # noqa: E402
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
from aide.data import protocol as P # noqa: E402
from common.runmeta import write_run_json # noqa: E402
FEATS_NPZ = P.RESULTS / "feats" / "feats_qwen.npz"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--src", default=str(P.RESULTS / "feats" / "qwen3vl2b"))
ap.add_argument("--out", default=str(FEATS_NPZ))
a = ap.parse_args()
t0 = time.time()
src = Path(a.src)
c = np.load(P.CACHE_NPZ, allow_pickle=False)
ids, splits, fidx = c["clip_id"], c["split"], c["frame_idx10"]
N, T, V, D = len(ids), P.N_SAMPLE, len(P.VIEWS), 2048
missing = [cid for cid in ids if not (src / f"{cid}.npz").exists()]
assert not missing, f"{len(missing)} clips without features, e.g. {missing[:10]}"
pooled = np.zeros((N, T, V, D), np.float16)
vis = np.zeros((N, T, V, D), np.float16)
ntok = np.zeros((N, T, V), np.int32)
nvis = np.zeros((N, T, V), np.int32)
meta0 = None
for i, cid in enumerate(ids):
z = np.load(src / f"{cid}.npz", allow_pickle=False)
assert str(z["clip_id"]) == cid, (cid, str(z["clip_id"]))
assert tuple(z["views"].tolist()) == tuple(P.VIEWS), z["views"]
assert np.array_equal(z["frame_idx"], fidx[i]), (cid, z["frame_idx"], fidx[i])
assert z["pooled"].shape == (T, V, D) and z["vis_pooled"].shape == (T, V, D)
assert np.isfinite(z["pooled"].astype(np.float32)).all() and np.isfinite(z["vis_pooled"].astype(np.float32)).all(), cid
pooled[i], vis[i] = z["pooled"], z["vis_pooled"]
ntok[i], nvis[i] = z["n_tokens"], z["n_vis_tokens"]
if meta0 is None:
meta0 = str(z["meta"])
if i % 500 == 0:
print(f"[{i}/{N}]", flush=True)
out = Path(a.out)
out.parent.mkdir(parents=True, exist_ok=True)
np.savez(out, pooled=pooled, vis_pooled=vis, clip_id=ids.astype(str), split=splits.astype(str),
views=np.array(P.VIEWS), frame_idx=fidx.astype(np.int32), n_tokens=ntok, n_vis_tokens=nvis,
meta=np.array(meta0))
stats = {
"N": int(N), "shape": [int(N), T, V, D], "bytes": int(out.stat().st_size),
"pooled_norm_mean": float(np.linalg.norm(pooled[:64].astype(np.float32), axis=-1).mean()),
"vis_pooled_norm_mean": float(np.linalg.norm(vis[:64].astype(np.float32), axis=-1).mean()),
"n_tokens_mean": float(ntok.mean()), "n_vis_tokens_mean": float(nvis.mean()),
"per_split": {s: int((splits == s).sum()) for s in ("train", "val", "test")},
"src": str(src), "out": str(out), "meta_clip0": json.loads(meta0) if meta0 else None,
}
print(json.dumps({k: v for k, v in stats.items() if k != "meta_clip0"}, indent=1))
write_run_json(out.parent, "pack_features", [__file__, P.__file__], [P.CACHE_NPZ], seed=None, t_start=t0,
input_listing=False, extra=stats)
if __name__ == "__main__":
main()

Xet Storage Details

Size:
4.21 kB
·
Xet hash:
c981764709b1c522cf046e08a2970f918a4a52cbec881e358c57130806991a15

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.