streampetr-p150 / code /tt_streampetr /tests /test_host_preprocess.py
changh95's picture
tt-model push streampetr-p150 (container)
ac1609a verified
Raw History Blame Contribute Delete
3.73 kB
# SPDX-License-Identifier: Apache-2.0
"""Host pre-processing (``host.preprocess`` over the vendored C14 ``ttaw.image_triangle``) against the research
reference: bit-identical network inputs for the shipped sample (the sha256 of every float32 camera tensor of the
research goldens), float-rounding agreement with the SPEC's dense numpy port, the presets.
TT_VISIBLE_DEVICES=none python -m pytest -q code/tt_streampetr/tests/test_host_preprocess.py
"""
from __future__ import annotations
import hashlib
import json
import numpy as np
import pytest
from tt_streampetr.host import inputs as hin
from tt_streampetr.host.preprocess import image_geometry, preprocess_images
from tt_streampetr.reference.config import CAMERA_ORDER, default_config
from tt_streampetr.tests import paths
CFG = default_config()
def _sample_frame(frame: int):
paths.need_pandaset()
return hin.prepare_frame(**hin.load_sample(paths.pandaset_sample(frame)), cfg=CFG)
def test_geometry_of_the_sample_rig():
g = image_geometry(1080, 1920)
assert g.resized_hw == (480, 853) and g.roi_start == (0, 106) and g.roi_hw == (480, 640)
@pytest.mark.parametrize("frame", [40, 41, 42, 43])
def test_sample_inputs_bit_identical_to_research_goldens(frame):
"""The float32 input tensors of the research CPU goldens (``inputs_sha256.json``, preset autoware_main)."""
gold = paths.public_golden("ps019_f40_43_cold")
if gold is None:
pytest.skip("research/streampetr/public_data not present")
hashes = {r["frame"]: r["img_f32_sha256"] for r in json.loads((gold / "inputs_sha256.json").read_text())}[frame]
img = preprocess_images(_sample_frame(frame).images, CFG, workers=2)
assert img.shape == (1, 5, 3, 480, 640) and img.dtype == np.float32
for i, cam in enumerate(CAMERA_ORDER):
assert hashlib.sha256(img[0, i].tobytes()).hexdigest() == hashes[cam], cam
def test_matches_the_spec_numpy_port():
"""``sp_common.resize_crop_normalize`` (dense separable form; SPEC 3.1 "exact numpy port") agrees to float
rounding: it sums the taps in another order."""
sp = paths.import_research()
if sp is None:
pytest.skip("research/streampetr not present")
rng = np.random.default_rng(0)
for hw in [(720, 1280), (540, 640), (432, 768)]:
img = rng.integers(0, 256, size=hw + (3,), dtype=np.uint8)
ours = preprocess_images([img] * 5, CFG)[0, 0]
ref, p = sp.resize_crop_normalize(img)
assert (p["new_h"], p["new_w"]) == image_geometry(*hw).resized_hw
assert np.abs(ours - ref).max() < 2e-5, hw
def test_presets_and_channel_order():
rng = np.random.default_rng(1)
imgs = [rng.integers(0, 256, size=(240, 320, 3), dtype=np.uint8) for _ in range(5)]
main = preprocess_images(imgs, CFG)
awml = preprocess_images(imgs, CFG, preset="awml_training")
a052 = preprocess_images(imgs, CFG, preset="autoware_0.52")
m_rgb = np.asarray([123.675, 116.28, 103.53], np.float32)[:, None, None]
s_rgb = np.asarray([58.395, 57.12, 57.375], np.float32)[:, None, None]
m_bgr, s_bgr = m_rgb[::-1], s_rgb[::-1]
v = (main[0] * s_rgb + m_rgb) # the weighted means back (float error ~1e-5)
assert np.allclose(awml[0], (v[:, ::-1] - m_bgr) / s_bgr, atol=1e-4)
assert np.allclose(a052[0], (v - m_bgr) / s_bgr, atol=1e-4)
# a bgr8 camera (OpenCV order) gives the same tensor as its rgb8 twin under autoware_main (the node swaps first)
assert np.array_equal(preprocess_images([i[:, :, ::-1].copy() for i in imgs], CFG, channels="bgr"), main)
assert np.array_equal(preprocess_images(imgs, CFG, workers=3), main)
with pytest.raises(ValueError):
preprocess_images(imgs[:4], CFG)