File size: 3,939 Bytes
c147de1 | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | # SPDX-License-Identifier: Apache-2.0
"""The three VisionPilot networks: files, inputs, pre-processing and where the device graph ends.
Pre-processing follows VisionPilot (``VisionPilot/modules/sensing/image_preprocessing`` and
``VisionPilot/modules/models/src/inference.cpp`` at vision_pilot commit 87a82ec8):
- AutoSteer and AutoSpeed: the camera frame is top-cropped to 2:1 (``compute_top_crop_2_1``: drop
``round(H - W / 2)`` rows from the top), resized to 1024 x 512 (``cv::INTER_LINEAR``), BGR -> RGB, divided by 255
(``chw_01``).
- AutoDrive: two frames (previous, current) of a 1024 x 512 image warped by the camera homography
(``cv::warpPerspective``, the ``warped`` image), BGR -> RGB, /255, then ImageNet mean / std (``chw_imagenet``).
``cut`` names the tensors where the device graph stops; the few ops after them run on the host
(``host/onnx_numpy.py``): AutoSteer's soft-argmax over 256 columns, AutoSpeed's DFL box decode, AutoDrive's MLP.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Dict, Tuple
import numpy as np
NET_W, NET_H = 1024, 512
IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)
@dataclass(frozen=True)
class NetworkSpec:
name: str # short name, also the folded file name
package: str # Hugging Face package
onnx_file: str # published fp32 file (vision_pilot VisionPilot/modules/models/weights)
inputs: Tuple[str, ...]
two_frames: bool
imagenet: bool # ImageNet mean / std after /255 (else /255 only)
cut: Tuple[str, ...] # device graph outputs (the host tail starts here)
outputs: Tuple[str, ...] = field(default_factory=tuple)
def channel_affine(self) -> Tuple[np.ndarray, np.ndarray]:
"""``x_net = a[c] * x_uint8 + b[c]`` per RGB channel."""
if self.imagenet:
std = np.asarray(IMAGENET_STD, np.float64)
mean = np.asarray(IMAGENET_MEAN, np.float64)
return (1.0 / (255.0 * std)).astype(np.float32), (-mean / std).astype(np.float32)
return np.full(3, 1.0 / 255.0, np.float32), np.zeros(3, np.float32)
NETWORKS: Dict[str, NetworkSpec] = {
"autosteer": NetworkSpec("autosteer", "changh95/auto_steer-p150", "autosteer_fp32.onnx", ("input",), False, False,
cut=("silu_40", "silu_42"), outputs=("lane_value", "height")),
"autospeed": NetworkSpec("autospeed", "changh95/auto_speed-p150", "autospeed_fp32.onnx", ("input",), False, False,
cut=("cat_11", "cat_12", "cat_13"), outputs=("output",)),
"autodrive": NetworkSpec("autodrive", "changh95/auto_drive-p150", "autodrive_fp32.onnx",
("image_prev", "image_curr"), True, True,
cut=("silu_62",), outputs=("distance", "curvature", "flag_logit")),
}
def top_crop_2_1(height: int, width: int) -> int:
"""Rows dropped from the top so the kept band is width x width/2 (VisionPilot ``compute_top_crop_2_1``)."""
return max(0, int(np.round(height - width / 2.0)))
def preprocess_front(bgr: np.ndarray) -> np.ndarray:
"""Camera frame (BGR uint8) -> the 1024 x 512 BGR uint8 network image of AutoSteer / AutoSpeed."""
import cv2
top = top_crop_2_1(bgr.shape[0], bgr.shape[1])
return cv2.resize(bgr[top:], (NET_W, NET_H), interpolation=cv2.INTER_LINEAR)
def network_input(spec: NetworkSpec, bgr_1024x512: np.ndarray) -> np.ndarray:
"""1024 x 512 BGR uint8 -> the float32 NCHW tensor the ONNX file takes (exactly VisionPilot's chw_01 /
chw_imagenet: float32 arithmetic)."""
rgb = bgr_1024x512[..., ::-1].astype(np.float32) * np.float32(1.0 / 255.0)
if spec.imagenet:
rgb = (rgb - np.asarray(IMAGENET_MEAN, np.float32)) / np.asarray(IMAGENET_STD, np.float32)
return np.ascontiguousarray(rgb.transpose(2, 0, 1)[None])
|