| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- """``scripts/verify_rknn.py`` 的纯函数单测(不依赖 rknnlite / onnxruntime,x86 CI 也能跑)。
- 校验的是判定逻辑本身:余弦相似度、张量归一化(``(1,C,N)`` → ``(N,C)``)、
- 解码框的同类匹配——真值来自 ONNX,脚本只负责「给出可复现的数值证据」。
- """
- from __future__ import annotations
- import importlib.util
- import os
- import sys
- import unittest
- import numpy as np
- ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- if ROOT not in sys.path:
- sys.path.insert(0, ROOT)
- from portcv.schema import Box # noqa: E402
- def _load(name: str = "verify_rknn"):
- """按路径加载 ``scripts/<name>.py``(scripts 不是包,沿用 test_rknn_ops 的写法)。"""
- path = os.path.join(ROOT, "scripts", name + ".py")
- spec = importlib.util.spec_from_file_location(name, path)
- mod = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(mod)
- return mod
- verify_rknn = _load()
- class CosineTests(unittest.TestCase):
- def test_identical_and_scaled(self):
- a = np.arange(12, dtype=np.float32).reshape(3, 4)
- self.assertAlmostEqual(verify_rknn.cosine(a, a), 1.0, places=6)
- self.assertAlmostEqual(verify_rknn.cosine(a, a * 3.5), 1.0, places=6)
- def test_orthogonal_and_zero(self):
- a = np.array([1.0, 0.0, 0.0], dtype=np.float32)
- b = np.array([0.0, 2.0, 0.0], dtype=np.float32)
- self.assertAlmostEqual(verify_rknn.cosine(a, b), 0.0, places=6)
- # 全 0 不能变成 NaN(现场「输出被算成常量」就是要靠这个判出来)
- self.assertEqual(verify_rknn.cosine(a, np.zeros(3, dtype=np.float32)), 0.0)
- def test_size_mismatch_raises(self):
- with self.assertRaises(ValueError):
- verify_rknn.cosine(np.zeros(4), np.zeros(5))
- class CanonicalTests(unittest.TestCase):
- def test_all_layouts_become_n_by_c(self):
- c, n = 10, 20 # c = 4 + nc(框 4 行 + 类别 nc 行)
- base = np.arange(c * n, dtype=np.float32).reshape(c, n)
- expected = np.ascontiguousarray(base.T)
- for raw in (
- base[None], # (1, C, N) —— RKNN/ONNX 常见
- np.ascontiguousarray(base.T)[None], # (1, N, C)
- base.T, # (N, C)
- base, # (C, N)
- ):
- got = verify_rknn.canonical(raw, c - 4)
- self.assertEqual(got.shape, (n, c))
- np.testing.assert_allclose(got, expected)
- class MatchBoxesTests(unittest.TestCase):
- def _box(self, x1, y1, x2, y2, cls=0):
- return Box(x1=x1, y1=y1, x2=x2, y2=y2, score=0.9, class_id=cls, class_name="c%d" % cls)
- def test_overlapping_same_class_matches(self):
- ref = [self._box(10, 10, 110, 110)]
- got = [self._box(12, 11, 108, 112)]
- matched, iou = verify_rknn.match_boxes(ref, got)
- self.assertEqual(matched, 1)
- self.assertGreater(iou, 0.9)
- def test_different_class_never_matches(self):
- ref = [self._box(10, 10, 110, 110, cls=0)]
- got = [self._box(10, 10, 110, 110, cls=1)]
- matched, iou = verify_rknn.match_boxes(ref, got)
- self.assertEqual((matched, iou), (0, 0.0))
- def test_far_apart_box_matches_nothing(self):
- ref = [self._box(0, 0, 50, 50)]
- got = [self._box(400, 400, 450, 450)]
- self.assertEqual(verify_rknn.match_boxes(ref, got), (0, 0.0))
- def test_greedy_consumes_each_box_once(self):
- ref = [self._box(0, 0, 100, 100), self._box(0, 0, 100, 100)]
- got = [self._box(0, 0, 100, 100)]
- matched, _ = verify_rknn.match_boxes(ref, got)
- self.assertEqual(matched, 1)
- if __name__ == "__main__": # pragma: no cover
- unittest.main()
|