File size: 2,922 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 51 52 53 54 55 56 57 58 59 | #!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""The fp32 CPU-reference ``/predict`` body of a sample (host only: torch reference, no device), stored next to the
sample as ``<stem>.<form>.reference.json`` for the smoke test (``server/smoke_test.py``) and the device tests.
python make_reference.py --form gather code/tt_meteor2/samples/synthetic_8cam.json
"""
import argparse
import json
import os
import sys
from pathlib import Path
import numpy as np
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
from tt_meteor2.api import load_sample # noqa: E402
from tt_meteor2.host.ego import EgoHead # noqa: E402
from tt_meteor2.host.inputs import prepare_request, resize_camera # noqa: E402
from tt_meteor2.host.outputs import Meteor2Output # noqa: E402
from tt_meteor2.host.postprocess import decode_dense, occ_from_logits # 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
from tt_meteor2 import io as tio # noqa: E402
def reference_output(net: Meteor2Net, kw: dict, form: str) -> Meteor2Output:
f = prepare_request(kw["images"], kw.get("calibration"), ego_speed=kw["ego_speed"], acc=kw.get("acc"),
ego_hist=kw.get("ego_hist"), route=kw.get("route"), need_calibration=form == "gather")
cams = tio.load_cameras(kw["images"], kw.get("calibration"), order=C.CAMERAS, require_calibration=False)
imgs = np.stack([resize_camera(c.image).transpose(2, 0, 1) for c in cams])[None]
o = net(imgs, f.K, f.T_cam_ego, f.v0, f.acc, f.ego_hist, f.route)
boxes = decode_dense(o["hm"], o["reg"], o["stationary"], o["risk"], o["traj"])
cls, p = occ_from_logits(o["occ_logit"])
return Meteor2Output(boxes, o["ego"][0], o["ego_path_6s"][0], o["tl"][0], o["lane_logit"][0].argmax(0).astype(np.uint8),
cls.astype(np.uint8), p.astype(np.float32), o["occ_logit"][0].argmax(0).astype(np.uint8),
form=form, meta={"reference": "fp32 CPU (torch)"})
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("sample", type=Path)
a = ap.parse_args()
import torch
torch.set_num_threads(8)
net = Meteor2Net(Meteor2Weights(os.path.join(a.model_dir, C.ONNX_FILES[a.form])))
out = reference_output(net, load_sample(a.sample), a.form)
body = out.to_dict()
dst = a.sample.with_name(f"{a.sample.stem}.{a.form}.reference.json")
dst.write_text(json.dumps(body) + "\n")
print(dst, "boxes", body["num_detections"], "tl", body["traffic_light"]["state"], "mode", body["plan"]["mode"])
if __name__ == "__main__":
main()
|