Download code/tt_meteor2/reference/ort.py from changh95/METEOR-2-p150: direct link, hf CLI and curl.
- Browser
- Download file 4.13 kB
-
https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/tt_meteor2/reference/ort.py
- Command line
-
hf download hf://changh95/METEOR-2-p150/code/tt_meteor2/reference/ort.py
-
curl -L -o ort.py https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/tt_meteor2/reference/ort.py
4.13 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """ONNX Runtime oracle of the released graphs: the 14 outputs plus internal tensors as named taps. | |
| Tests and golden generation only (onnxruntime is not a runtime dependency of the bundle). The graph is modified in | |
| memory (extra graph outputs); the downloaded file is never written. ORT is bit-reproducible at a fixed thread count. | |
| """ | |
| from __future__ import annotations | |
| from typing import Dict, Iterable, Mapping, Optional | |
| import numpy as np | |
| __all__ = ["ORT_TAPS", "OrtOracle", "available"] | |
| # reference tap -> ONNX tensor, per form (the NPU graph numbers its top-level nodes differently) | |
| _COMMON = { | |
| "img.c1": "/stem/stem.4/stem.4.1/relu_1/Relu_output_0", | |
| "img.c2": "/l2/l2.1/relu_1/Relu_output_0", | |
| "img.c3": "/l3/l3.1/relu_1/Relu_output_0", | |
| "img.c4": "/l4/l4.1/relu_1/Relu_output_0", | |
| "img.fpn": "/f_x/Relu_output_0", | |
| "ctx": "/pp_mix/Add_output_0", | |
| "depth.prob": "/Softmax_1_output_0", | |
| "paint.feat": "/paint/paint.2/Relu_output_0", | |
| "lift.polar": "/Transpose_2_output_0", | |
| "bev.b0": "/b0/b0.1/Relu_output_0", | |
| "bev.b1": "/b1/b1.3/Relu_output_0", | |
| "bev.fuse": "/fuse/fuse.1/Relu_output_0", | |
| "det.feat": "/det/det.2/Relu_output_0", | |
| "ego.flat": "/Flatten_output_0", | |
| } | |
| ORT_TAPS: Dict[str, Dict[str, str]] = { | |
| "gather": dict(_COMMON, **{"lift.bev": "/Reshape_6_output_0", "paint.bev": "/Reshape_12_output_0", | |
| "lane.up": "/Add_13_output_0", "ego.kin_in": "/Concat_8_output_0", | |
| "occ.feat": "/occ_mid2/occ_mid2.1/Relu_output_0"}), | |
| "npu": dict(_COMMON, **{"lift.bev": "/vt_lr/Reshape_172_output_0", "paint.bev": "/gvt_lr/Reshape_342_output_0", | |
| "lane.up": "/Add_7_output_0", "ego.kin_in": "/Concat_5_output_0", | |
| "occ.feat": "/Relu_output_0"}), | |
| } | |
| def available() -> bool: | |
| try: | |
| import onnxruntime # noqa: F401 | |
| return True | |
| except ImportError: | |
| return False | |
| class OrtOracle: | |
| """``OrtOracle(path, form, taps=("img.fpn", ...), threads=8)``; ``run(feeds) -> {output or tap: array}``.""" | |
| def __init__(self, path: str, form: str, taps: Iterable[str] = (), threads: int = 8): | |
| import onnx | |
| import onnxruntime as ort | |
| self.form = form | |
| self.taps = {t: ORT_TAPS[form][t] for t in taps} | |
| model = onnx.load(path) | |
| have = {o.name for o in model.graph.output} | |
| for name in self.taps.values(): | |
| if name not in have: | |
| model.graph.output.append(onnx.helper.make_empty_tensor_value_info(name)) | |
| so = ort.SessionOptions() | |
| so.intra_op_num_threads = threads | |
| so.inter_op_num_threads = 1 | |
| so.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL | |
| self.sess = ort.InferenceSession(model.SerializeToString(), so, providers=["CPUExecutionProvider"]) | |
| self.inputs = [i.name for i in self.sess.get_inputs()] | |
| self.outputs = [o.name for o in self.sess.get_outputs()] | |
| def feeds(self, imgs, K=None, T_cam_ego=None, v0=0.0, acc=0.0, ego_hist=None, route=None) -> Dict[str, np.ndarray]: | |
| f = {"imgs": np.asarray(imgs, np.uint8).reshape(1, 8, 3, 648, 1152), | |
| "v0": np.asarray(v0, np.float32).reshape(1), | |
| "acc": np.asarray(acc, np.float32).reshape(1, 1), | |
| "ego_hist": np.zeros((1, 32), np.float32) if ego_hist is None else np.asarray(ego_hist, np.float32).reshape(1, 32), | |
| "route": np.zeros((1, 13), np.float32) if route is None else np.asarray(route, np.float32).reshape(1, 13)} | |
| if "K" in self.inputs: | |
| f["K"] = np.asarray(K, np.float32).reshape(1, 8, 3, 3) | |
| f["T_cam_ego"] = np.asarray(T_cam_ego, np.float32).reshape(1, 8, 4, 4) | |
| return f | |
| def run(self, feeds: Mapping[str, np.ndarray]) -> Dict[str, np.ndarray]: | |
| res = self.sess.run(None, dict(feeds)) | |
| by_name = dict(zip(self.outputs, res)) | |
| out = {n: by_name[n] for n in self.outputs if not n.startswith("/")} | |
| for tap, name in self.taps.items(): | |
| out[tap] = by_name[name] | |
| return out | |