Download code/tt_autoe2e/tests/dev_api.py from changh95/auto_e2e-p150: direct link, hf CLI and curl.
- Browser
- Download file 5.57 kB
-
https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/tests/dev_api.py
- Command line
-
hf download hf://changh95/auto_e2e-p150/code/tt_autoe2e/tests/dev_api.py
-
curl -L -o dev_api.py https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/tests/dev_api.py
5.57 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """The Python API (``api.AutoE2E``) on the golden sample's current frame (bin/devrun). | |
| bin/devrun python -m tt_autoe2e.tests.dev_api [--out /logs/api/dev_api.json] | |
| The API takes camera images with K / T_ref_from_camera; the golden sample has the loader's normalised tiles and | |
| 3 x 4 projections. The test turns them back into what a client sends (uint8 images: the tiles are decoded JPEGs, so | |
| this is exact up to float rounding; K and T from an RQ decomposition of each projection, image size = the tile | |
| size), calls the API with a reset stream, and compares with the traced runner driven directly with the golden | |
| arrays in the same state (history = the current frame 7 times): the calibration -> projection path and the image | |
| pre-processing must reproduce the same controls. Then two more calls on the stream check the history bookkeeping. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import glob | |
| import json | |
| import os | |
| import numpy as np | |
| WEIGHTS = "/ae2e/881e6a8367746da1eaae044a68077d0e7ce6fc1b" | |
| def to_uint8(tile: np.ndarray) -> np.ndarray: | |
| from ..api import IMAGENET_MEAN, IMAGENET_STD | |
| x = tile * IMAGENET_STD.reshape(3, 1, 1) + IMAGENET_MEAN.reshape(3, 1, 1) | |
| return np.clip(np.round(x * 255.0), 0, 255).astype(np.uint8).transpose(1, 2, 0) | |
| def k_t_from_p(P: np.ndarray): | |
| """P = K [R | t] -> (K, T_ref_from_camera) with K upper-triangular, positive diagonal.""" | |
| M = np.asarray(P[:, :3], np.float64) | |
| J = np.flipud(np.eye(3)) | |
| Qm, Rm = np.linalg.qr((J @ M).T) # RQ through QR of the row-reversed transpose | |
| K, R = J @ Rm.T @ J, J @ Qm.T | |
| D = np.diag(np.sign(np.diag(K))) | |
| K, R = K @ D, D @ R | |
| t = np.linalg.solve(K, np.asarray(P[:, 3], np.float64)) | |
| T_cam_from_ref = np.eye(4) | |
| T_cam_from_ref[:3, :3], T_cam_from_ref[:3, 3] = R, t | |
| return K, np.linalg.inv(T_cam_from_ref) | |
| def main(argv=None) -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", default="/logs/api/dev_api.json") | |
| args = ap.parse_args(argv) | |
| from ..api import AutoE2E, COLUMNS | |
| from ..host import head as H | |
| from ..host.geometry import bev_geometry, frame_geometry | |
| from ..tt.fusion import FRONT_CAP | |
| from ..tt.image import backbone_input | |
| g = np.load(glob.glob("/ae2e/ref/autoe2e_kitscenes_epoch5_*f000045.npz")[0]) | |
| names = AutoE2E.CAMERA_ORDER | |
| P = g["camera_projection_matrix"][0] | |
| Pf = g["front_camera_projection_matrix"][0, 0] | |
| images, cal = {}, {} | |
| for i, n in enumerate(names): | |
| if i == 0: | |
| images[n] = to_uint8(g["front_camera_tile"][0]) | |
| K, T = k_t_from_p(Pf) | |
| else: | |
| images[n] = to_uint8(g["camera_tiles"][0, i]) | |
| K, T = k_t_from_p(P[i]) | |
| cal[n] = {"intrinsics": K.tolist(), "T_ref_from_camera": T.tolist()} | |
| inputs = {"egomotion_history": g["egomotion_history_raw"][0], "map_context": g["map_context"][0], | |
| "route_mask": g["route_mask"][0]} | |
| res = {} | |
| model = AutoE2E.from_pretrained(weights_dir=WEIGHTS) | |
| try: | |
| call = lambda reset: model(images=images, calibration={"cameras": cal}, | |
| stream={"id": "golden", "reset": reset, "T_world_from_ego": np.eye(4).tolist()}, | |
| inputs=inputs) | |
| out = call(True) | |
| res["timing_ms"] = {k: round(v, 1) for k, v in out.timing_ms.items()} | |
| res["meta"] = out.meta | |
| api_ctrl = out.poses[:, 4:6] | |
| # the runner driven with the golden arrays, same state (ring = the current frame 7 times) | |
| cams = backbone_input(g["camera_tiles"][0]) | |
| for _ in range(7): | |
| model.dev.push(cams) | |
| geo = bev_geometry(P, 512) | |
| gf = frame_geometry(g["front_camera_projection_matrix"][0], 1024, r_cap=FRONT_CAP, k_cams=1) | |
| nav, _ = H.navigation(model.state, g["map_context"][0], g["route_mask"][0], bool(g["map_valid"][0]), | |
| bool(g["route_valid"][0])) | |
| fused = model.dev.step(cams, backbone_input(g["front_camera_tile"]), [geo] * 8, gf, | |
| np.ascontiguousarray(nav.reshape(256, -1).T)) | |
| ref_ctrl = H.planner(model.state, fused, H.normalize_egomotion(g["egomotion_history"][0])) | |
| res["api_vs_runner_controls_max_abs"] = float(np.abs(api_ctrl - ref_ctrl).max()) | |
| res["api_vs_runner_controls_rel_l2"] = float(np.linalg.norm(api_ctrl - ref_ctrl) / np.linalg.norm(ref_ctrl)) | |
| res["projection_max_abs_err_px"] = None | |
| model.stream_id = None # the direct runner calls changed the ring: start the stream over | |
| outs = [call(True)] + [call(False) for _ in range(3)] | |
| res["history_steps_sequence"] = [o.meta["history_steps"] for o in outs[:3]] | |
| # a streaming call (no reset: one image branch, 8 BEV frames, the navigation BEV overlapped) | |
| res["stream_timing_ms"] = {k: round(float(np.median([o.timing_ms[k] for o in outs[1:]])), 1) | |
| for k in outs[1].timing_ms} | |
| res["columns"] = list(COLUMNS) | |
| res["first_poses"] = out.poses[:3].round(4).tolist() | |
| finally: | |
| model.close() | |
| ok = res["api_vs_runner_controls_rel_l2"] < 2e-2 and res["history_steps_sequence"] == [0, 1, 2] | |
| res["verdict"] = "PASS" if ok else "FAIL" | |
| print(json.dumps(res, indent=1)) | |
| os.makedirs(os.path.dirname(args.out), exist_ok=True) | |
| with open(args.out, "w") as f: | |
| json.dump(res, f, indent=1) | |
| return 0 if ok else 1 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |