File size: 2,134 Bytes
0f131e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""fp32 CPU reference taps + outputs of input npz files -> goldens npz (host only; the device tests read them).

    python make_goldens.py --form gather --out ~/ttaw-phase0/data/meteor2_port/goldens INPUT.npz ...
"""
import argparse
import os
import sys

import numpy as np

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
from tt_meteor2.host.ego import EgoHead  # noqa: E402
from tt_meteor2.reference import config as C  # noqa: E402
from tt_meteor2.reference.model import Meteor2Net  # noqa: E402
from tt_meteor2.reference.weights import Meteor2Weights  # noqa: E402

KEEP = ("img.c1", "img.c2", "img.c3", "img.c4", "img.fpn", "ctx", "depth.prob", "paint.feat", "lift.polar",
        "lift.bev", "paint.bev", "bev.b0", "bev.b1", "bev.fuse", "det.feat", "lane.up", "occ.feat", "ego.flat")


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--model-dir", default=os.path.expanduser(f"~/data/tt-autoware/meteor2/{C.WEIGHTS_REVISION}"))
    ap.add_argument("--form", choices=C.FORMS, required=True)
    ap.add_argument("--out", required=True)
    ap.add_argument("--threads", type=int, default=8)
    ap.add_argument("inputs", nargs="+")
    a = ap.parse_args()
    import torch
    torch.set_num_threads(a.threads)
    net = Meteor2Net(Meteor2Weights(os.path.join(a.model_dir, C.ONNX_FILES[a.form])))
    os.makedirs(os.path.join(a.out, a.form), exist_ok=True)
    for f in a.inputs:
        z = dict(np.load(f))
        taps = {}
        out = net(**{k: z[k] for k in ("imgs", "K", "T_cam_ego", "v0", "acc", "ego_hist", "route")}, taps=taps)
        d = {"out." + k: v.astype(np.float32) for k, v in out.items() if k not in ("depth_logit", "seg2d_logit", "hm2d", "reg2d")}
        for k in KEEP:
            v = taps[k]
            d["tap." + k] = (v.numpy() if hasattr(v, "numpy") else np.asarray(v)).astype(np.float32)
        dst = os.path.join(a.out, a.form, os.path.basename(f))
        np.savez(dst, **d)
        print(dst, sum(v.nbytes for v in d.values()) >> 20, "MB")


if __name__ == "__main__":
    main()