MTerryJack commited on
Commit
b0b7da1
·
verified ·
1 Parent(s): ccc3ae0

subnet_bridge: copy winning miner repo into library

Browse files
README.md CHANGED
@@ -1,33 +1,26 @@
1
  ---
2
  tags:
3
  - element_type:detect
4
- - model:onnxruntime
5
  - subnet:winner
6
  - object:person
 
7
  manako:
8
  source: winner_fetch
9
  manifest_element_name: manak0/Detect-Person
10
- winner_repo_id: dan2143/ScoreVision20
11
- winner_revision: 7b8bd6afab32eebf339e06a3cbc2bba085768e0d
12
  bridge_sha: aa95434205d1b11a181750fc0731b1cf890dc5c4
13
- score:
14
- size_mb: 9.788236
15
- performance_map50: 0.8036822714196793
16
- E: 0.08210695690415304
17
- last_benchmark:
18
- type: synthetic_fixed
19
- ran_at: '2026-11-14T06:56:17.407230Z'
20
- result_path: benchmark/synthetic/f2b3a0a0-3c7a-40c2-82e7-86005ced7e92.json
21
  ---
22
 
23
  # Detect-Person-winner
24
 
25
  Published winning miner converted into a library element.
26
 
27
- - Source winner repo: `dan2143/ScoreVision20`
28
- - Source revision: `7b8bd6afab32eebf339e06a3cbc2bba085768e0d`
29
  - Published by subnet bridge: `aa95434205d1b11a181750fc0731b1cf890dc5c4`
30
  - Manifest element: `manak0/Detect-Person`
31
  - Element type: `Detect`
32
  - Objects: person
33
- - Runtime model type: `onnxruntime`
 
1
  ---
2
  tags:
3
  - element_type:detect
4
+ - model:ultralytics-yolo
5
  - subnet:winner
6
  - object:person
7
+
8
  manako:
9
  source: winner_fetch
10
  manifest_element_name: manak0/Detect-Person
11
+ winner_repo_id: crimedetector/person1
12
+ winner_revision: 09422b654befaefb81680161c3b2f31dc78d977f
13
  bridge_sha: aa95434205d1b11a181750fc0731b1cf890dc5c4
 
 
 
 
 
 
 
 
14
  ---
15
 
16
  # Detect-Person-winner
17
 
18
  Published winning miner converted into a library element.
19
 
20
+ - Source winner repo: `crimedetector/person1`
21
+ - Source revision: `09422b654befaefb81680161c3b2f31dc78d977f`
22
  - Published by subnet bridge: `aa95434205d1b11a181750fc0731b1cf890dc5c4`
23
  - Manifest element: `manak0/Detect-Person`
24
  - Element type: `Detect`
25
  - Objects: person
26
+ - Runtime model type: `ultralytics-yolo`
benchmark/synthetic/f2b3a0a0-3c7a-40c2-82e7-86005ced7e92.json DELETED
@@ -1,147 +0,0 @@
1
- {
2
- "meta": {
3
- "element_id": "manako/Detect-Person-winner",
4
- "run_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
5
- "benchmark_type": "synthetic_fixed",
6
- "saved_at": "2026-11-14T06:56:17.407230Z",
7
- "result_path": "benchmark/synthetic/f2b3a0a0-3c7a-40c2-82e7-86005ced7e92.json",
8
- "split_ref": "private_final"
9
- },
10
- "results": {
11
- "overall_iou": 0.8017030040944356,
12
- "per_class_iou": {
13
- "person": 0.8017030040944356
14
- },
15
- "map_50": 0.8036822714196793,
16
- "map_50_90": 0.6692305279275813,
17
- "precision": 0.931899641577061,
18
- "recall": 0.816692842719318,
19
- "per_class_map_50": {
20
- "person": 0.8036822714196793
21
- },
22
- "per_class_map_50_90": {
23
- "person": 0.6692305279275813
24
- },
25
- "per_class_precision": {
26
- "person": 0.931899641577061
27
- },
28
- "per_class_recall": {
29
- "person": 0.816692842719318
30
- },
31
- "adaptive_map_50": 0.8123780856174476,
32
- "ffpi": 2.5384615384615383,
33
- "false_positive_score": 0.7461538461538462,
34
- "foundry_score": 0.785888389832007,
35
- "composite_score": 0.785888389832007,
36
- "scoring_policy": "subnet-style-composite-v1",
37
- "matched_images": 91,
38
- "gt_count": 4457,
39
- "pred_count": 3906,
40
- "classes": [
41
- "person"
42
- ],
43
- "evaluated_element_revision": null,
44
- "dataset_id": "bdf4d5b4-7dec-4b72-8747-ab005ca92b3b",
45
- "dataset_version_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
46
- "run_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
47
- "split_ref": "private_final",
48
- "selected_frame_count": 91,
49
- "selected_frame_indices": [
50
- 5,
51
- 11,
52
- 16,
53
- 20,
54
- 24,
55
- 26,
56
- 28,
57
- 32,
58
- 41,
59
- 49,
60
- 55,
61
- 59,
62
- 63,
63
- 67,
64
- 70,
65
- 73,
66
- 79,
67
- 80,
68
- 81,
69
- 84,
70
- 86,
71
- 95,
72
- 103,
73
- 104,
74
- 110,
75
- 116,
76
- 118,
77
- 120,
78
- 121,
79
- 129,
80
- 130,
81
- 138,
82
- 139,
83
- 146,
84
- 151,
85
- 156,
86
- 173,
87
- 175,
88
- 183,
89
- 184,
90
- 185,
91
- 187,
92
- 193,
93
- 197,
94
- 200,
95
- 201,
96
- 205,
97
- 206,
98
- 218,
99
- 222,
100
- 223,
101
- 226,
102
- 227,
103
- 234,
104
- 235,
105
- 241,
106
- 242,
107
- 244,
108
- 247,
109
- 249,
110
- 250,
111
- 251,
112
- 257,
113
- 259,
114
- 262,
115
- 266,
116
- 267,
117
- 282,
118
- 296,
119
- 302,
120
- 307,
121
- 314,
122
- 315,
123
- 331,
124
- 333,
125
- 341,
126
- 345,
127
- 346,
128
- 354,
129
- 357,
130
- 361,
131
- 363,
132
- 364,
133
- 367,
134
- 370,
135
- 374,
136
- 377,
137
- 382,
138
- 383,
139
- 387,
140
- 399
141
- ],
142
- "evaluated_version_ids": [
143
- "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92"
144
- ],
145
- "skipped_missing_pairs": 0
146
- }
147
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
benchmark/synthetic/index.json DELETED
@@ -1,16 +0,0 @@
1
- [
2
- {
3
- "run_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
4
- "saved_at": "2026-11-14T06:56:17.407230Z",
5
- "result_path": "benchmark/synthetic/f2b3a0a0-3c7a-40c2-82e7-86005ced7e92.json",
6
- "overall_iou": 0.8017030040944356,
7
- "map_50": 0.8036822714196793,
8
- "map_50_90": 0.6692305279275813,
9
- "precision": 0.931899641577061,
10
- "recall": 0.816692842719318,
11
- "matched_images": 91,
12
- "gt_count": 4457,
13
- "pred_count": 3906,
14
- "split_ref": "private_final"
15
- }
16
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
benchmark/synthetic/latest.json DELETED
@@ -1,147 +0,0 @@
1
- {
2
- "meta": {
3
- "element_id": "manako/Detect-Person-winner",
4
- "run_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
5
- "benchmark_type": "synthetic_fixed",
6
- "saved_at": "2026-11-14T06:56:17.407230Z",
7
- "result_path": "benchmark/synthetic/f2b3a0a0-3c7a-40c2-82e7-86005ced7e92.json",
8
- "split_ref": "private_final"
9
- },
10
- "results": {
11
- "overall_iou": 0.8017030040944356,
12
- "per_class_iou": {
13
- "person": 0.8017030040944356
14
- },
15
- "map_50": 0.8036822714196793,
16
- "map_50_90": 0.6692305279275813,
17
- "precision": 0.931899641577061,
18
- "recall": 0.816692842719318,
19
- "per_class_map_50": {
20
- "person": 0.8036822714196793
21
- },
22
- "per_class_map_50_90": {
23
- "person": 0.6692305279275813
24
- },
25
- "per_class_precision": {
26
- "person": 0.931899641577061
27
- },
28
- "per_class_recall": {
29
- "person": 0.816692842719318
30
- },
31
- "adaptive_map_50": 0.8123780856174476,
32
- "ffpi": 2.5384615384615383,
33
- "false_positive_score": 0.7461538461538462,
34
- "foundry_score": 0.785888389832007,
35
- "composite_score": 0.785888389832007,
36
- "scoring_policy": "subnet-style-composite-v1",
37
- "matched_images": 91,
38
- "gt_count": 4457,
39
- "pred_count": 3906,
40
- "classes": [
41
- "person"
42
- ],
43
- "evaluated_element_revision": null,
44
- "dataset_id": "bdf4d5b4-7dec-4b72-8747-ab005ca92b3b",
45
- "dataset_version_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
46
- "run_id": "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92",
47
- "split_ref": "private_final",
48
- "selected_frame_count": 91,
49
- "selected_frame_indices": [
50
- 5,
51
- 11,
52
- 16,
53
- 20,
54
- 24,
55
- 26,
56
- 28,
57
- 32,
58
- 41,
59
- 49,
60
- 55,
61
- 59,
62
- 63,
63
- 67,
64
- 70,
65
- 73,
66
- 79,
67
- 80,
68
- 81,
69
- 84,
70
- 86,
71
- 95,
72
- 103,
73
- 104,
74
- 110,
75
- 116,
76
- 118,
77
- 120,
78
- 121,
79
- 129,
80
- 130,
81
- 138,
82
- 139,
83
- 146,
84
- 151,
85
- 156,
86
- 173,
87
- 175,
88
- 183,
89
- 184,
90
- 185,
91
- 187,
92
- 193,
93
- 197,
94
- 200,
95
- 201,
96
- 205,
97
- 206,
98
- 218,
99
- 222,
100
- 223,
101
- 226,
102
- 227,
103
- 234,
104
- 235,
105
- 241,
106
- 242,
107
- 244,
108
- 247,
109
- 249,
110
- 250,
111
- 251,
112
- 257,
113
- 259,
114
- 262,
115
- 266,
116
- 267,
117
- 282,
118
- 296,
119
- 302,
120
- 307,
121
- 314,
122
- 315,
123
- 331,
124
- 333,
125
- 341,
126
- 345,
127
- 346,
128
- 354,
129
- 357,
130
- 361,
131
- 363,
132
- 364,
133
- 367,
134
- 370,
135
- 374,
136
- 377,
137
- 382,
138
- 383,
139
- 387,
140
- 399
141
- ],
142
- "evaluated_version_ids": [
143
- "f2b3a0a0-3c7a-40c2-82e7-86005ced7e92"
144
- ],
145
- "skipped_missing_pairs": 0
146
- }
147
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
chute_config.yml CHANGED
@@ -2,19 +2,20 @@ Image:
2
  from_base: parachutes/python:3.12
3
  run_command:
4
  - pip install --upgrade setuptools wheel
5
- - pip install 'numpy>=1.23,<3' 'onnxruntime==1.26.0' 'opencv-python-headless>=4.8' 'pydantic>=2.0' 'pyyaml>=6.0' 'huggingface_hub>=0.24' 'aiohttp>=3.9'
6
- - pip install torch==2.8.0 --index-url https://download.pytorch.org/whl/cu128
7
- set_workdir: /app
8
 
9
  NodeSelector:
10
  gpu_count: 1
11
  min_vram_gb_per_gpu: 16
 
12
  include:
13
  - pro_6000
14
 
15
  Chute:
16
- tee: true
17
- shutdown_after_seconds: 86400
18
  concurrency: 4
19
- max_instances: 1
20
- scaling_threshold: 0.75
 
 
 
2
  from_base: parachutes/python:3.12
3
  run_command:
4
  - pip install --upgrade setuptools wheel
5
+ - pip install 'numpy>=1.23' 'onnxruntime-gpu>=1.16' 'opencv-python>=4.7' 'pillow>=9.5' 'huggingface_hub>=0.19.4' 'pydantic>=2.0' 'pyyaml>=6.0' 'aiohttp>=3.9'
6
+ - pip install torch==2.8.0 torchvision==0.23.0 torchaudio==2.8.0 --index-url https://download.pytorch.org/whl/cu128
 
7
 
8
  NodeSelector:
9
  gpu_count: 1
10
  min_vram_gb_per_gpu: 16
11
+ max_hourly_price_per_gpu: 2
12
  include:
13
  - pro_6000
14
 
15
  Chute:
16
+ timeout_seconds: 900
 
17
  concurrency: 4
18
+ max_instances: 5
19
+ scaling_threshold: 0.5
20
+ shutdown_after_seconds: 288000
21
+ tee: true
main.py CHANGED
@@ -28,7 +28,7 @@ Miner = _load_local_miner_class()
28
 
29
 
30
  CLASS_NAMES = ['person']
31
- MODEL_TYPE = 'onnxruntime'
32
 
33
 
34
  def _to_dict(value: Any) -> dict[str, Any]:
 
28
 
29
 
30
  CLASS_NAMES = ['person']
31
+ MODEL_TYPE = 'ultralytics-yolo'
32
 
33
 
34
  def _to_dict(value: Any) -> dict[str, Any]:
miner.py CHANGED
@@ -1,5 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
 
 
2
  from pathlib import Path
 
3
 
4
  import cv2
5
  import numpy as np
@@ -7,31 +28,31 @@ import onnxruntime as ort
7
  from numpy import ndarray
8
  from pydantic import BaseModel
9
 
10
- MODEL_FILE = "weights.onnx"
11
- INPUT_SIZE = 736
12
- THREADS = 2
13
- PAD_VALUE = 114
14
- MIN_SIDE = 2
15
- K = 15.0
16
- MISS_PRIOR = 1.0
17
- P_FLOOR = 0.05
18
- RAW_FLOOR = 0.33
19
- WARMUP_RUNS = 3
20
- CALIB_Y = (
21
- 0.0000, 0.0308, 0.0545, 0.0832, 0.1035, 0.1102, 0.1726, 0.1726, 0.1985, 0.2042,
22
- 0.2042, 0.2634, 0.2903, 0.2996, 0.3099, 0.3099, 0.3414, 0.3414, 0.3414, 0.3414,
23
- 0.4028, 0.4028, 0.4353, 0.4353, 0.4571, 0.4667, 0.5123, 0.5123, 0.5315, 0.5357,
24
- 0.5646, 0.5646, 0.5646, 0.5824, 0.5978, 0.6239, 0.6413, 0.6413, 0.6413, 0.6465,
25
- 0.6667, 0.6667, 0.6667, 0.7010, 0.7485, 0.7485, 0.7485, 0.7485, 0.7485, 0.7778,
26
- 0.7778, 0.7882, 0.7882, 0.8214, 0.8214, 0.8664, 0.8664, 0.8684, 0.8772, 0.8772,
27
- 0.8772, 0.8933, 0.8933, 0.9231, 0.9261, 0.9261, 0.9261, 0.9261, 0.9261, 0.9342,
28
- 0.9342, 0.9342, 0.9342, 0.9342, 0.9645, 0.9692, 0.9692, 0.9692, 0.9692, 0.9692,
29
- 0.9692, 0.9694, 0.9694, 0.9764, 0.9764, 0.9764, 0.9798, 0.9836, 0.9836, 0.9843,
30
- 0.9890, 0.9890, 0.9890, 0.9890, 0.9890, 0.9928, 1.0000, 1.0000, 1.0000, 1.0000,
31
- 1.0000,
32
- )
33
- _CALIB_X = np.linspace(0.0, 1.0, len(CALIB_Y))
34
- _CALIB_Y = np.asarray(CALIB_Y, dtype=np.float64)
35
 
36
 
37
  class BoundingBox(BaseModel):
@@ -43,110 +64,271 @@ class BoundingBox(BaseModel):
43
  conf: float
44
 
45
 
46
- class Polygon(BaseModel):
47
- cls_id: int
48
- conf: float
49
- points: list[tuple[int, int]]
50
-
51
-
52
  class TVFrameResult(BaseModel):
53
  frame_id: int
54
- boxes: list[BoundingBox] | None = None
55
- polygons: list[Polygon] | None = None
56
- keypoints: list[tuple[int, int]] | None = None
57
-
58
-
59
- def _letterbox(img: ndarray, size: int) -> tuple:
60
- h, w = img.shape[:2]
61
- r = min(size / h, size / w)
62
- nw, nh = int(round(w * r)), int(round(h * r))
63
- if (w, h) != (nw, nh):
64
- img = cv2.resize(img, (nw, nh), interpolation=cv2.INTER_LINEAR)
65
- dw, dh = (size - nw) / 2, (size - nh) / 2
66
- top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
67
- left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
68
- if top or bottom or left or right:
69
- img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT,
70
- value=(PAD_VALUE, PAD_VALUE, PAD_VALUE))
71
- x = img[:, :, ::-1].transpose(2, 0, 1)[None].astype(np.float32) / 255.0
72
- return np.ascontiguousarray(x), r, left, top
73
-
74
-
75
-
76
- def _sanitise(img) -> ndarray:
77
- if not isinstance(img, np.ndarray) or img.ndim not in (2, 3) or img.size == 0:
78
- raise ValueError(f"unsupported frame: {type(img).__name__} {getattr(img, 'shape', None)}")
79
- if img.dtype != np.uint8:
80
- img = np.clip(np.nan_to_num(img), 0, 255).astype(np.uint8)
81
- if img.ndim == 2 or img.shape[2] == 1:
82
- img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
83
- elif img.shape[2] == 4:
84
- img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
85
- elif img.shape[2] != 3:
86
- raise ValueError(f"unsupported channel count {img.shape[2]}")
87
- return img
88
 
89
 
90
  class Miner:
 
 
 
91
  def __init__(self, path_hf_repo: Path) -> None:
92
- so = ort.SessionOptions()
93
- so.intra_op_num_threads = THREADS
94
- so.inter_op_num_threads = 1
95
- so.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
96
- so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  cv2.setNumThreads(1)
98
- self.session = ort.InferenceSession(str(Path(path_hf_repo) / MODEL_FILE), sess_options=so,
99
- providers=["CPUExecutionProvider"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  self.input_name = self.session.get_inputs()[0].name
101
- self.inference_failures = 0
102
- dummy = np.zeros((1024, 1024, 3), np.uint8)
103
- for _ in range(WARMUP_RUNS):
104
- self._gate(*self._detect(dummy))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- def __repr__(self) -> str:
107
- return f"Person detector {MODEL_FILE} @ {INPUT_SIZE}px (CPU, {THREADS} threads)"
108
-
109
- def _detect(self, img: ndarray) -> tuple[ndarray, ndarray]:
110
- h, w = img.shape[:2]
111
- x, r, px, py = _letterbox(img, INPUT_SIZE)
112
- out = self.session.run(None, {self.input_name: x})[0][0]
113
- out = out[out[:, 4] > 0.01]
114
- out = out[np.isfinite(out).all(1)]
115
- boxes = out[:, :4].astype(np.float64)
116
- boxes[:, [0, 2]] = ((boxes[:, [0, 2]] - px) / r).clip(0, w)
117
- boxes[:, [1, 3]] = ((boxes[:, [1, 3]] - py) / r).clip(0, h)
118
- return boxes, out[:, 4].astype(np.float64)
119
 
120
  @staticmethod
121
- def _gate(boxes: ndarray, scores: ndarray) -> tuple[ndarray, ndarray]:
122
- side = np.minimum(boxes[:, 2] - boxes[:, 0], boxes[:, 3] - boxes[:, 1])
123
- keep = side >= MIN_SIDE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  boxes, scores = boxes[keep], scores[keep]
125
- p = np.interp(scores, _CALIB_X, _CALIB_Y)
126
- n_hat = float(p.sum()) + MISS_PRIOR
127
- keep = (p > max(n_hat / (K + n_hat), P_FLOOR)) & (scores >= RAW_FLOOR)
128
- return boxes[keep], scores[keep]
129
-
130
- def _frame_boxes(self, img) -> list[BoundingBox]:
131
- boxes, scores = self._gate(*self._detect(_sanitise(img)))
132
- return [BoundingBox(x1=int(round(b[0])), y1=int(round(b[1])), x2=int(round(b[2])),
133
- y2=int(round(b[3])), cls_id=0, conf=float(s))
134
- for b, s in zip(boxes, scores)]
135
-
136
- def predict_batch(self, batch_images: list[ndarray], offset: int, n_keypoints: int) -> list[TVFrameResult]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  results: list[TVFrameResult] = []
138
- for i, img in enumerate(batch_images):
 
139
  try:
140
- boxes = self._frame_boxes(img)
141
  except Exception as exc:
 
 
142
  self.inference_failures += 1
143
- print(json.dumps({"event": "inference_failure", "frame_id": offset + i,
144
- "error": f"{type(exc).__name__}: {exc}"}))
 
 
 
 
 
145
  boxes = []
146
- results.append(TVFrameResult(
147
- frame_id=offset + i,
148
- boxes=boxes,
149
- polygons=[],
150
- keypoints=[(0, 0)] * int(n_keypoints) if n_keypoints else [],
151
- ))
152
  return results
 
 
 
 
1
+ """Strict consumer for an Ultralytics end-to-end-NMS ONNX (person element).
2
+
3
+ Contract: static batch-1 NCHW input, one ``[1, 300, 6]`` output whose rows are
4
+ ``x1, y1, x2, y2, confidence, model_class_id``. NMS runs inside the graph, so
5
+ this module only validates the tensors, applies the confidence gate, undoes the
6
+ letterbox, drops geometrically impossible boxes and caps the result count.
7
+
8
+ No ``dataclass`` declarations: ScoreVision's ``spec_from_file_location`` loader
9
+ does not register the module in ``sys.modules`` before executing it, which makes
10
+ dataclass decoration fail at import.
11
+
12
+ ELEMENT_CONFIG below is written by ``scripts/build_submission.py`` from the
13
+ policy chosen on the validation split -- do not hand-edit it without re-running
14
+ ``scripts/optimize_policy.py``, since ``nms_iou`` in particular must match the
15
+ value frozen into the ONNX graph at export time.
16
+ """
17
+
18
+ import ast
19
  import json
20
+ import math
21
+ import os
22
  from pathlib import Path
23
+ from typing import Any
24
 
25
  import cv2
26
  import numpy as np
 
28
  from numpy import ndarray
29
  from pydantic import BaseModel
30
 
31
+ API_VERSION = "c2-strict-v1"
32
+ STRICT_INFERENCE_ENV = "SV_STRICT_INFERENCE"
33
+ OUTPUT_ROWS = 300
34
+ OUTPUT_COLUMNS = 6
35
+
36
+ ELEMENT_CONFIG: dict[str, Any] = {
37
+ "element_id": "person",
38
+ "classes": ["person"],
39
+ "coco_ids": [0],
40
+ "imgsz": 704,
41
+ "conf": 0.45,
42
+ "nms_iou": 0.45, # frozen into the ONNX graph at export
43
+ "max_det": 300,
44
+ "min_side": 3,
45
+ "max_aspect": 10,
46
+ "max_area_frac": 0.95,
47
+ "scale_w": 1, # box geometry calibration to the validator standard
48
+ "scale_h": 1,
49
+ "warp": None,
50
+ "conf_steps": ((30, 0.45), (60, 0.50), (None, 0.55)),
51
+ }
52
+
53
+
54
+ class InferenceInvariantError(RuntimeError):
55
+ """A model tensor or decoded detection violated the serving contract."""
56
 
57
 
58
  class BoundingBox(BaseModel):
 
64
  conf: float
65
 
66
 
 
 
 
 
 
 
67
  class TVFrameResult(BaseModel):
68
  frame_id: int
69
+ boxes: list[BoundingBox]
70
+ keypoints: list[tuple[int, int]]
71
+
72
+
73
+ def _conf_for(n_raw: int) -> float:
74
+ """Confidence gate, optionally raised on crowded frames.
75
+
76
+ The wrong-box penalty is a per-image absolute count that saturates at ten,
77
+ so a frame holding ninety people can lose the whole term while a frame with
78
+ six cannot. When CONF_STEPS is non-empty the gate is spent where that
79
+ exposure actually is. Empty steps means a flat gate.
80
+ """
81
+ for limit, conf in ELEMENT_CONFIG.get("conf_steps", ()):
82
+ if limit is None or n_raw < limit:
83
+ return float(conf)
84
+ return float(ELEMENT_CONFIG["conf"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
 
87
  class Miner:
88
+ api_version = API_VERSION
89
+ strict_inference = False
90
+
91
  def __init__(self, path_hf_repo: Path) -> None:
92
+ cfg = ELEMENT_CONFIG
93
+ self.config = cfg
94
+ self.class_names = list(cfg["classes"])
95
+ self.conf_threshold = float(cfg["conf"])
96
+ self.max_det = int(cfg["max_det"])
97
+ self.min_side = float(cfg.get("min_side", 3.0))
98
+ self.max_aspect_ratio = float(cfg.get("max_aspect", 10.0))
99
+ self.max_area_frac = float(cfg.get("max_area_frac", 0.95))
100
+ self.conf_steps = tuple(tuple(s) for s in cfg.get("conf_steps", ()))
101
+ # box geometry calibration: the validator draws wider/taller boxes than
102
+ # the training sources; scale about the centre before the IoU match.
103
+ self.scale_w = float(cfg.get("scale_w", 1.0))
104
+ self.scale_h = float(cfg.get("scale_h", 1.0))
105
+ # optional top-half perspective warp (S, D), applied only to SQUARE frames: source rows
106
+ # [0, S*h) are resampled to input rows [0, D*s), the rest to [D*s, s); columns uniform.
107
+ # The model must have been trained on identically warped images; boxes are un-warped
108
+ # back to original pixels before any geometry filter or output.
109
+ warp = cfg.get("warp")
110
+ self.warp = (float(warp[0]), float(warp[1])) if warp else None
111
+ if self.warp is not None and not (0.0 < self.warp[0] < 1.0 and 0.0 < self.warp[1] < 1.0):
112
+ raise ValueError(f"warp must be two fractions in (0, 1), got {warp!r}")
113
+ raw = os.environ.get(STRICT_INFERENCE_ENV, "")
114
+ if raw not in ("", "0", "1"):
115
+ raise ValueError(f"{STRICT_INFERENCE_ENV} must be '1' or unset/'0', got {raw!r}")
116
+ self.strict_inference = raw == "1"
117
+ self.inference_failures = 0
118
+
119
+ # The referee grants one physical core (two SMT threads). Let ORT own
120
+ # both for the convolution graph and keep OpenCV single-threaded: a
121
+ # second OpenCV worker is slower and adds tail jitter on two siblings.
122
  cv2.setNumThreads(1)
123
+ options = ort.SessionOptions()
124
+ options.intra_op_num_threads = 2
125
+ options.inter_op_num_threads = 1
126
+ options.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
127
+ options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
128
+ model_path = Path(path_hf_repo) / "weights.onnx"
129
+ try:
130
+ self.session = ort.InferenceSession(
131
+ str(model_path), sess_options=options,
132
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
133
+ )
134
+ except Exception as exc:
135
+ print(f"CUDA session failed, falling back to CPU: {exc}")
136
+ self.session = ort.InferenceSession(
137
+ str(model_path), sess_options=options, providers=["CPUExecutionProvider"]
138
+ )
139
+
140
  self.input_name = self.session.get_inputs()[0].name
141
+ self.output_names = [o.name for o in self.session.get_outputs()]
142
+ shape = self.session.get_inputs()[0].shape
143
+ if len(shape) != 4 or shape[1] != 3:
144
+ raise ValueError(f"model input must be rank-4 NCHW with 3 channels, got {shape!r}")
145
+ if not all(isinstance(v, int) and v > 0 for v in shape[2:]):
146
+ raise ValueError(f"e2e export must have static spatial dims, got {shape[2:]!r}")
147
+ if (shape[2], shape[3]) != (cfg["imgsz"], cfg["imgsz"]):
148
+ raise ValueError(f"model input {shape[2]}x{shape[3]} != config imgsz {cfg['imgsz']}")
149
+ self.input_height = self.input_width = int(cfg["imgsz"])
150
+ out_shape = self.session.get_outputs()[0].shape
151
+ if list(out_shape) != [1, OUTPUT_ROWS, OUTPUT_COLUMNS]:
152
+ raise ValueError(f"e2e output must be [1, {OUTPUT_ROWS}, {OUTPUT_COLUMNS}], got {out_shape!r}")
153
+ self.model_names = self._read_model_names()
154
+ self._warmup(3)
155
+ print(
156
+ f"api_version={self.api_version} element={cfg['element_id']} imgsz={cfg['imgsz']} "
157
+ f"conf={self.conf_threshold} steps={self.conf_steps} "
158
+ f"providers={self.session.get_providers()}"
159
+ )
160
 
161
+ def _read_model_names(self) -> tuple[str, ...]:
162
+ try:
163
+ raw = self.session.get_modelmeta().custom_metadata_map["names"]
164
+ parsed = ast.literal_eval(raw)
165
+ if not isinstance(parsed, dict):
166
+ raise TypeError("names metadata is not a dict")
167
+ keys = sorted(parsed)
168
+ if keys != list(range(len(keys))):
169
+ raise ValueError(f"names metadata keys not consecutive from zero: {keys[:8]}")
170
+ return tuple(str(parsed[i]) for i in keys)
171
+ except Exception as exc:
172
+ raise ValueError(f"e2e ONNX requires valid names metadata: {exc}") from exc
 
173
 
174
  @staticmethod
175
+ def _letterbox(image: ndarray, new_shape: tuple[int, int], color=(114, 114, 114)):
176
+ height, width = image.shape[:2]
177
+ new_width, new_height = new_shape
178
+ ratio = min(new_width / width, new_height / height)
179
+ rw, rh = int(round(width * ratio)), int(round(height * ratio))
180
+ if (rw, rh) != (width, height):
181
+ image = cv2.resize(
182
+ image, (rw, rh),
183
+ interpolation=cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR,
184
+ )
185
+ dw, dh = (new_width - rw) / 2.0, (new_height - rh) / 2.0
186
+ padded = cv2.copyMakeBorder(
187
+ image,
188
+ int(round(dh - 0.1)), int(round(dh + 0.1)),
189
+ int(round(dw - 0.1)), int(round(dw + 0.1)),
190
+ cv2.BORDER_CONSTANT, value=color,
191
+ )
192
+ return padded, ratio, (dw, dh)
193
+
194
+ def _warp_prep(self, image: ndarray):
195
+ h = image.shape[0]
196
+ s_w, s_h = self.input_width, self.input_height
197
+ hs, hd = int(round(self.warp[0] * h)), int(round(self.warp[1] * s_h))
198
+ top = cv2.resize(image[:hs], (s_w, hd), interpolation=cv2.INTER_LINEAR)
199
+ bot = cv2.resize(image[hs:], (s_w, s_h - hd), interpolation=cv2.INTER_LINEAR)
200
+ return np.ascontiguousarray(np.concatenate([top, bot], 0)), hs, hd
201
+
202
+ def _unwarp(self, boxes: ndarray, width: int, height: int, hs: int, hd: int) -> ndarray:
203
+ s_w, s_h = self.input_width, self.input_height
204
+ boxes = boxes.astype(np.float32, copy=True)
205
+ boxes[:, [0, 2]] *= np.float32(width / s_w)
206
+ for j in (1, 3):
207
+ y = boxes[:, j]
208
+ boxes[:, j] = np.where(y < hd, y * (hs / hd), hs + (y - hd) * ((height - hs) / (s_h - hd))).astype(np.float32)
209
+ return boxes
210
+
211
+ def _decode(self, output, ratio, pad, original_size, unwarp=None):
212
+ rows = output[0]
213
+ boxes = rows[:, :4].astype(np.float32, copy=True)
214
+ scores = rows[:, 4].astype(np.float32, copy=False)
215
+ raw_classes = rows[:, 5]
216
+ if (scores < 0).any() or (scores > 1).any():
217
+ raise InferenceInvariantError("e2e confidence outside [0, 1]")
218
+ rounded = np.rint(raw_classes)
219
+ if not np.array_equal(raw_classes, rounded):
220
+ raise InferenceInvariantError("e2e model class ids must be exact integers")
221
+ model_classes = rounded.astype(np.int64)
222
+ if (model_classes < 0).any() or (model_classes >= len(self.model_names)).any():
223
+ raise InferenceInvariantError("e2e model class id outside range")
224
+
225
+ active = scores > 0.0
226
+ reversed_rows = active & ((boxes[:, 2] < boxes[:, 0]) | (boxes[:, 3] < boxes[:, 1]))
227
+ if reversed_rows.any():
228
+ raise InferenceInvariantError("e2e output contains reversed boxes")
229
+
230
+ # A floor below every reachable gate; nothing under it can be emitted.
231
+ floor = min([self.conf_threshold] + [float(c) for _, c in self.conf_steps])
232
+ keep = active & (model_classes == 0) & (scores >= np.float32(floor))
233
+ if not keep.any():
234
+ return np.empty((0, 4), np.float32), np.empty((0,), np.float32)
235
  boxes, scores = boxes[keep], scores[keep]
236
+
237
+ boxes[:, [0, 2]] = (boxes[:, [0, 2]] - pad[0]) / ratio
238
+ boxes[:, [1, 3]] = (boxes[:, [1, 3]] - pad[1]) / ratio
239
+ if unwarp is not None:
240
+ boxes = self._unwarp(boxes, *unwarp)
241
+ width, height = original_size
242
+ if self.scale_w != 1.0 or self.scale_h != 1.0:
243
+ cx = (boxes[:, 0] + boxes[:, 2]) * 0.5
244
+ cy = (boxes[:, 1] + boxes[:, 3]) * 0.5
245
+ hw = (boxes[:, 2] - boxes[:, 0]) * 0.5 * np.float32(self.scale_w)
246
+ hh = (boxes[:, 3] - boxes[:, 1]) * 0.5 * np.float32(self.scale_h)
247
+ boxes = np.stack([cx - hw, cy - hh, cx + hw, cy + hh], 1).astype(np.float32)
248
+ boxes[:, [0, 2]] = np.clip(boxes[:, [0, 2]], 0, width - 1)
249
+ boxes[:, [1, 3]] = np.clip(boxes[:, [1, 3]], 0, height - 1)
250
+
251
+ bw = boxes[:, 2] - boxes[:, 0]
252
+ bh = boxes[:, 3] - boxes[:, 1]
253
+ aspect = np.maximum(bw / np.maximum(bh, 1e-6), bh / np.maximum(bw, 1e-6))
254
+ sane = (
255
+ (bw >= self.min_side)
256
+ & (bh >= self.min_side)
257
+ & (bw * bh <= self.max_area_frac * float(width * height))
258
+ & (aspect <= self.max_aspect_ratio)
259
+ )
260
+ boxes, scores = boxes[sane], scores[sane]
261
+ if not len(scores):
262
+ return boxes, scores
263
+ order = np.argsort(-scores, kind="stable")[: self.max_det]
264
+ boxes, scores = boxes[order], scores[order]
265
+ gate = _conf_for(len(scores)) if self.conf_steps else self.conf_threshold
266
+ emit = scores >= np.float32(gate)
267
+ return boxes[emit], scores[emit]
268
+
269
+ def _predict_single(self, image: np.ndarray) -> list[BoundingBox]:
270
+ if not isinstance(image, np.ndarray) or image.ndim != 3 or image.shape[2] != 3:
271
+ raise ValueError(f"expected HWC 3-channel image, got {getattr(image, 'shape', type(image))}")
272
+ if image.dtype != np.uint8:
273
+ image = image.astype(np.uint8)
274
+ original_height, original_width = image.shape[:2]
275
+ unwarp = None
276
+ if self.warp is not None and original_height == original_width:
277
+ padded, hs, hd = self._warp_prep(image)
278
+ ratio, pad, unwarp = 1.0, (0.0, 0.0), (original_width, original_height, hs, hd)
279
+ else:
280
+ padded, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
281
+ blob = cv2.dnn.blobFromImage(padded, scalefactor=1 / 255.0, swapRB=True)
282
+ outputs = self.session.run(self.output_names, {self.input_name: blob})
283
+ output = outputs[0]
284
+ if not np.isfinite(output).all():
285
+ raise InferenceInvariantError("model output must be finite everywhere")
286
+ if list(output.shape) != [1, OUTPUT_ROWS, OUTPUT_COLUMNS]:
287
+ raise ValueError(f"unexpected output shape {list(output.shape)}")
288
+ boxes, scores = self._decode(output, ratio, pad, (original_width, original_height), unwarp)
289
+ return [
290
+ BoundingBox(
291
+ x1=int(math.floor(b[0])), y1=int(math.floor(b[1])),
292
+ x2=int(math.ceil(b[2])), y2=int(math.ceil(b[3])),
293
+ cls_id=0, conf=float(s),
294
+ )
295
+ for b, s in zip(boxes, scores)
296
+ if b[2] > b[0] and b[3] > b[1]
297
+ ]
298
+
299
+ def _warmup(self, iterations: int) -> None:
300
+ dummy = np.zeros((720, 1280, 3), dtype=np.uint8)
301
+ original = self.strict_inference
302
+ self.strict_inference = True
303
+ try:
304
+ for _ in range(iterations):
305
+ self.predict_batch(batch_images=[dummy], offset=0, n_keypoints=0)
306
+ except Exception as exc:
307
+ raise RuntimeError(f"warmup failed, refusing to serve: {type(exc).__name__}: {exc}") from exc
308
+ finally:
309
+ self.strict_inference = original
310
+
311
+ def predict_batch(self, batch_images, offset: int, n_keypoints: int) -> list[TVFrameResult]:
312
+ _ = n_keypoints
313
  results: list[TVFrameResult] = []
314
+ for index, image in enumerate(batch_images):
315
+ frame_id = offset + index
316
  try:
317
+ boxes = self._predict_single(image)
318
  except Exception as exc:
319
+ if self.strict_inference:
320
+ raise
321
  self.inference_failures += 1
322
+ print(json.dumps({
323
+ "event": "inference_failure",
324
+ "frame_id": frame_id,
325
+ "error_type": type(exc).__name__,
326
+ "error": str(exc),
327
+ "inference_failures": self.inference_failures,
328
+ }, sort_keys=True))
329
  boxes = []
330
+ results.append(TVFrameResult(frame_id=frame_id, boxes=boxes, keypoints=[]))
 
 
 
 
 
331
  return results
332
+
333
+ def __repr__(self) -> str:
334
+ return f"E2EONNXRuntime(providers={self.session.get_providers()})"
pyproject.toml CHANGED
@@ -4,12 +4,15 @@ version = "0.1.0"
4
  requires-python = ">=3.12"
5
 
6
  dependencies = [
7
- "numpy>=1.23,<3",
8
- "onnxruntime==1.26.0",
9
- "opencv-python-headless>=4.8",
 
 
10
  "pydantic>=2.0",
11
  "pyyaml>=6.0",
12
- "huggingface_hub>=0.24",
13
  "aiohttp>=3.9",
14
  "torch==2.8.0",
 
 
15
  ]
 
4
  requires-python = ">=3.12"
5
 
6
  dependencies = [
7
+ "numpy>=1.23",
8
+ "onnxruntime>=1.16",
9
+ "opencv-python>=4.7",
10
+ "pillow>=9.5",
11
+ "huggingface_hub>=0.19.4",
12
  "pydantic>=2.0",
13
  "pyyaml>=6.0",
 
14
  "aiohttp>=3.9",
15
  "torch==2.8.0",
16
+ "torchvision==0.23.0",
17
+ "torchaudio==2.8.0",
18
  ]
weights.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:9646f2df701ea23424c1115aa98dfda5c9b377b875658e5a9b9f3dbf7ddbcd87
3
- size 9768988
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7d6189d3c3e26bc8fc8bd30ecfb6bbaaef1d87973356ae4bf4c7c76c865a8b30
3
+ size 9860183