test_verify_rknn.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """``scripts/verify_rknn.py`` 的纯函数单测(不依赖 rknnlite / onnxruntime,x86 CI 也能跑)。
  2. 校验的是判定逻辑本身:余弦相似度、张量归一化(``(1,C,N)`` → ``(N,C)``)、
  3. 解码框的同类匹配——真值来自 ONNX,脚本只负责「给出可复现的数值证据」。
  4. """
  5. from __future__ import annotations
  6. import importlib.util
  7. import os
  8. import sys
  9. import unittest
  10. import numpy as np
  11. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  12. if ROOT not in sys.path:
  13. sys.path.insert(0, ROOT)
  14. from portcv.schema import Box # noqa: E402
  15. def _load(name: str = "verify_rknn"):
  16. """按路径加载 ``scripts/<name>.py``(scripts 不是包,沿用 test_rknn_ops 的写法)。"""
  17. path = os.path.join(ROOT, "scripts", name + ".py")
  18. spec = importlib.util.spec_from_file_location(name, path)
  19. mod = importlib.util.module_from_spec(spec)
  20. spec.loader.exec_module(mod)
  21. return mod
  22. verify_rknn = _load()
  23. class CosineTests(unittest.TestCase):
  24. def test_identical_and_scaled(self):
  25. a = np.arange(12, dtype=np.float32).reshape(3, 4)
  26. self.assertAlmostEqual(verify_rknn.cosine(a, a), 1.0, places=6)
  27. self.assertAlmostEqual(verify_rknn.cosine(a, a * 3.5), 1.0, places=6)
  28. def test_orthogonal_and_zero(self):
  29. a = np.array([1.0, 0.0, 0.0], dtype=np.float32)
  30. b = np.array([0.0, 2.0, 0.0], dtype=np.float32)
  31. self.assertAlmostEqual(verify_rknn.cosine(a, b), 0.0, places=6)
  32. # 全 0 不能变成 NaN(现场「输出被算成常量」就是要靠这个判出来)
  33. self.assertEqual(verify_rknn.cosine(a, np.zeros(3, dtype=np.float32)), 0.0)
  34. def test_size_mismatch_raises(self):
  35. with self.assertRaises(ValueError):
  36. verify_rknn.cosine(np.zeros(4), np.zeros(5))
  37. class CanonicalTests(unittest.TestCase):
  38. def test_all_layouts_become_n_by_c(self):
  39. c, n = 10, 20 # c = 4 + nc(框 4 行 + 类别 nc 行)
  40. base = np.arange(c * n, dtype=np.float32).reshape(c, n)
  41. expected = np.ascontiguousarray(base.T)
  42. for raw in (
  43. base[None], # (1, C, N) —— RKNN/ONNX 常见
  44. np.ascontiguousarray(base.T)[None], # (1, N, C)
  45. base.T, # (N, C)
  46. base, # (C, N)
  47. ):
  48. got = verify_rknn.canonical(raw, c - 4)
  49. self.assertEqual(got.shape, (n, c))
  50. np.testing.assert_allclose(got, expected)
  51. class MatchBoxesTests(unittest.TestCase):
  52. def _box(self, x1, y1, x2, y2, cls=0):
  53. return Box(x1=x1, y1=y1, x2=x2, y2=y2, score=0.9, class_id=cls, class_name="c%d" % cls)
  54. def test_overlapping_same_class_matches(self):
  55. ref = [self._box(10, 10, 110, 110)]
  56. got = [self._box(12, 11, 108, 112)]
  57. matched, iou = verify_rknn.match_boxes(ref, got)
  58. self.assertEqual(matched, 1)
  59. self.assertGreater(iou, 0.9)
  60. def test_different_class_never_matches(self):
  61. ref = [self._box(10, 10, 110, 110, cls=0)]
  62. got = [self._box(10, 10, 110, 110, cls=1)]
  63. matched, iou = verify_rknn.match_boxes(ref, got)
  64. self.assertEqual((matched, iou), (0, 0.0))
  65. def test_far_apart_box_matches_nothing(self):
  66. ref = [self._box(0, 0, 50, 50)]
  67. got = [self._box(400, 400, 450, 450)]
  68. self.assertEqual(verify_rknn.match_boxes(ref, got), (0, 0.0))
  69. def test_greedy_consumes_each_box_once(self):
  70. ref = [self._box(0, 0, 100, 100), self._box(0, 0, 100, 100)]
  71. got = [self._box(0, 0, 100, 100)]
  72. matched, _ = verify_rknn.match_boxes(ref, got)
  73. self.assertEqual(matched, 1)
  74. if __name__ == "__main__": # pragma: no cover
  75. unittest.main()