thomaskk2024 commited on
Commit
a4e0487
·
verified ·
1 Parent(s): f0b1b65

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +23 -0
  2. chute_config.yml +19 -0
  3. miner.py +598 -0
  4. weights.onnx +3 -0
README.md ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - element_type:detect
4
+ - model:yolov11-nano
5
+ - object:person
6
+ manako:
7
+ description: Roboflow - generated by element_trainer service to detect person
8
+ source: element_trainer/800e961b-eb64-4380-880c-f1ed67abd563
9
+ prompt_hints: null
10
+ input_payload:
11
+ - name: frame
12
+ type: image
13
+ description: RGB frame
14
+ output_payload:
15
+ - name: detections
16
+ type: detections
17
+ description: List of detections
18
+ evaluation_score: null
19
+ last_benchmark:
20
+ type: synthetic_fixed
21
+ ran_at: '2026-03-06T02:20:51.927289Z'
22
+ result_path: benchmark/synthetic/1ada5b1e-38b8-4bdc-967a-d8a27b0e6afb.json
23
+ ---
chute_config.yml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Image:
2
+ from_base: parachutes/python:3.12
3
+ run_command:
4
+ - pip install --upgrade setuptools wheel
5
+ - pip install 'numpy>=1.23' 'onnxruntime-gpu[cuda,cudnn]>=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 torchvision
7
+
8
+ NodeSelector:
9
+ gpu_count: 1
10
+ min_vram_gb_per_gpu: 24
11
+ min_memory_gb: 32
12
+ min_cpu_count: 32
13
+
14
+ Chute:
15
+ timeout_seconds: 900
16
+ concurrency: 4
17
+ max_instances: 5
18
+ scaling_threshold: 0.5
19
+ shutdown_after_seconds: 288000
miner.py ADDED
@@ -0,0 +1,598 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import math
3
+
4
+ import cv2
5
+ import numpy as np
6
+ import onnxruntime as ort
7
+ from numpy import ndarray
8
+ from pydantic import BaseModel
9
+
10
+
11
+ class BoundingBox(BaseModel):
12
+ x1: int
13
+ y1: int
14
+ x2: int
15
+ y2: int
16
+ cls_id: int
17
+ conf: float
18
+
19
+
20
+ class TVFrameResult(BaseModel):
21
+ frame_id: int
22
+ boxes: list[BoundingBox]
23
+ keypoints: list[tuple[int, int]]
24
+
25
+
26
+ class Miner:
27
+ def __init__(self, path_hf_repo: Path) -> None:
28
+ model_path = path_hf_repo / "weights.onnx"
29
+ self.class_names = ["person"]
30
+ print("ORT version:", ort.__version__)
31
+
32
+ try:
33
+ ort.preload_dlls()
34
+ print("✅ onnxruntime.preload_dlls() success")
35
+ except Exception as e:
36
+ print(f"⚠️ preload_dlls failed: {e}")
37
+
38
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
39
+
40
+ sess_options = ort.SessionOptions()
41
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
42
+
43
+ try:
44
+ self.session = ort.InferenceSession(
45
+ str(model_path),
46
+ sess_options=sess_options,
47
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
48
+ )
49
+ print("✅ Created ORT session with preferred CUDA provider list")
50
+ except Exception as e:
51
+ print(f"⚠️ CUDA session creation failed, falling back to CPU: {e}")
52
+ self.session = ort.InferenceSession(
53
+ str(model_path),
54
+ sess_options=sess_options,
55
+ providers=["CPUExecutionProvider"],
56
+ )
57
+
58
+ print("ORT session providers:", self.session.get_providers())
59
+
60
+ for inp in self.session.get_inputs():
61
+ print("INPUT:", inp.name, inp.shape, inp.type)
62
+
63
+ for out in self.session.get_outputs():
64
+ print("OUTPUT:", out.name, out.shape, out.type)
65
+
66
+ self.input_name = self.session.get_inputs()[0].name
67
+ self.output_names = [output.name for output in self.session.get_outputs()]
68
+ self.input_shape = self.session.get_inputs()[0].shape
69
+
70
+ self.input_height = self._safe_dim(self.input_shape[2], default=1280)
71
+ self.input_width = self._safe_dim(self.input_shape[3], default=1280)
72
+
73
+ # ---------- Scoring-oriented thresholds ----------
74
+ # Low threshold for candidate generation
75
+ self.conf_thres = 0.1
76
+
77
+ # High-confidence boxes can survive without TTA confirmation
78
+ self.conf_high = 0.55
79
+
80
+ # NMS threshold
81
+ self.iou_thres = 0.50
82
+
83
+ # TTA confirmation IoU
84
+ self.tta_match_iou = 0.55
85
+
86
+ self.max_det = 300
87
+ self.use_tta = True
88
+
89
+ # Box sanity filters
90
+ self.min_box_area = 16 * 16
91
+ self.min_w = 6
92
+ self.min_h = 6
93
+ self.max_aspect_ratio = 6.0
94
+ self.max_box_area_ratio = 0.95
95
+
96
+ print(f"✅ ONNX model loaded from: {model_path}")
97
+ print(f"✅ ONNX providers: {self.session.get_providers()}")
98
+ print(f"✅ ONNX input: name={self.input_name}, shape={self.input_shape}")
99
+
100
+ def __repr__(self) -> str:
101
+ return (
102
+ f"ONNXRuntime(session={type(self.session).__name__}, "
103
+ f"providers={self.session.get_providers()})"
104
+ )
105
+
106
+ @staticmethod
107
+ def _safe_dim(value, default: int) -> int:
108
+ return value if isinstance(value, int) and value > 0 else default
109
+
110
+ def _letterbox(
111
+ self,
112
+ image: ndarray,
113
+ new_shape: tuple[int, int],
114
+ color=(114, 114, 114),
115
+ ) -> tuple[ndarray, float, tuple[float, float]]:
116
+ h, w = image.shape[:2]
117
+ new_w, new_h = new_shape
118
+
119
+ ratio = min(new_w / w, new_h / h)
120
+ resized_w = int(round(w * ratio))
121
+ resized_h = int(round(h * ratio))
122
+
123
+ if (resized_w, resized_h) != (w, h):
124
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
125
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
126
+
127
+ dw = new_w - resized_w
128
+ dh = new_h - resized_h
129
+ dw /= 2.0
130
+ dh /= 2.0
131
+
132
+ left = int(round(dw - 0.1))
133
+ right = int(round(dw + 0.1))
134
+ top = int(round(dh - 0.1))
135
+ bottom = int(round(dh + 0.1))
136
+
137
+ padded = cv2.copyMakeBorder(
138
+ image,
139
+ top,
140
+ bottom,
141
+ left,
142
+ right,
143
+ borderType=cv2.BORDER_CONSTANT,
144
+ value=color,
145
+ )
146
+ return padded, ratio, (dw, dh)
147
+
148
+ def _preprocess(
149
+ self, image: ndarray
150
+ ) -> tuple[np.ndarray, float, tuple[float, float], tuple[int, int]]:
151
+ orig_h, orig_w = image.shape[:2]
152
+
153
+ img, ratio, pad = self._letterbox(
154
+ image, (self.input_width, self.input_height)
155
+ )
156
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
157
+ img = img.astype(np.float32) / 255.0
158
+ img = np.transpose(img, (2, 0, 1))[None, ...]
159
+ img = np.ascontiguousarray(img, dtype=np.float32)
160
+
161
+ return img, ratio, pad, (orig_w, orig_h)
162
+
163
+ @staticmethod
164
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
165
+ w, h = image_size
166
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
167
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
168
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
169
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
170
+ return boxes
171
+
172
+ @staticmethod
173
+ def _xywh_to_xyxy(boxes: np.ndarray) -> np.ndarray:
174
+ out = np.empty_like(boxes)
175
+ out[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.0
176
+ out[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.0
177
+ out[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.0
178
+ out[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.0
179
+ return out
180
+
181
+ @staticmethod
182
+ def _hard_nms(
183
+ boxes: np.ndarray,
184
+ scores: np.ndarray,
185
+ iou_thresh: float,
186
+ ) -> np.ndarray:
187
+ if len(boxes) == 0:
188
+ return np.array([], dtype=np.intp)
189
+
190
+ boxes = np.asarray(boxes, dtype=np.float32)
191
+ scores = np.asarray(scores, dtype=np.float32)
192
+ order = np.argsort(scores)[::-1]
193
+ keep = []
194
+
195
+ while len(order) > 0:
196
+ i = order[0]
197
+ keep.append(i)
198
+ if len(order) == 1:
199
+ break
200
+
201
+ rest = order[1:]
202
+
203
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
204
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
205
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
206
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
207
+
208
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
209
+
210
+ area_i = np.maximum(0.0, (boxes[i, 2] - boxes[i, 0])) * np.maximum(0.0, (boxes[i, 3] - boxes[i, 1]))
211
+ area_r = np.maximum(0.0, (boxes[rest, 2] - boxes[rest, 0])) * np.maximum(0.0, (boxes[rest, 3] - boxes[rest, 1]))
212
+
213
+ iou = inter / (area_i + area_r - inter + 1e-7)
214
+ order = rest[iou <= iou_thresh]
215
+
216
+ return np.array(keep, dtype=np.intp)
217
+
218
+ @staticmethod
219
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
220
+ xx1 = np.maximum(box[0], boxes[:, 0])
221
+ yy1 = np.maximum(box[1], boxes[:, 1])
222
+ xx2 = np.minimum(box[2], boxes[:, 2])
223
+ yy2 = np.minimum(box[3], boxes[:, 3])
224
+
225
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
226
+
227
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
228
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
229
+
230
+ return inter / (area_a + area_b - inter + 1e-7)
231
+
232
+ def _filter_sane_boxes(
233
+ self,
234
+ boxes: np.ndarray,
235
+ scores: np.ndarray,
236
+ cls_ids: np.ndarray,
237
+ orig_size: tuple[int, int],
238
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
239
+ if len(boxes) == 0:
240
+ return boxes, scores, cls_ids
241
+
242
+ orig_w, orig_h = orig_size
243
+ image_area = float(orig_w * orig_h)
244
+
245
+ keep = []
246
+ for i, box in enumerate(boxes):
247
+ x1, y1, x2, y2 = box.tolist()
248
+ bw = x2 - x1
249
+ bh = y2 - y1
250
+
251
+ if bw <= 0 or bh <= 0:
252
+ continue
253
+ if bw < self.min_w or bh < self.min_h:
254
+ continue
255
+
256
+ area = bw * bh
257
+ if area < self.min_box_area:
258
+ continue
259
+ if area > self.max_box_area_ratio * image_area:
260
+ continue
261
+
262
+ ar = max(bw / max(bh, 1e-6), bh / max(bw, 1e-6))
263
+ if ar > self.max_aspect_ratio:
264
+ continue
265
+
266
+ keep.append(i)
267
+
268
+ if not keep:
269
+ return (
270
+ np.empty((0, 4), dtype=np.float32),
271
+ np.empty((0,), dtype=np.float32),
272
+ np.empty((0,), dtype=np.int32),
273
+ )
274
+
275
+ keep = np.array(keep, dtype=np.intp)
276
+ return boxes[keep], scores[keep], cls_ids[keep]
277
+
278
+ def _decode_final_dets(
279
+ self,
280
+ preds: np.ndarray,
281
+ ratio: float,
282
+ pad: tuple[float, float],
283
+ orig_size: tuple[int, int],
284
+ ) -> list[BoundingBox]:
285
+ if preds.ndim == 3 and preds.shape[0] == 1:
286
+ preds = preds[0]
287
+
288
+ if preds.ndim != 2 or preds.shape[1] < 6:
289
+ raise ValueError(f"Unexpected ONNX final-det output shape: {preds.shape}")
290
+
291
+ boxes = preds[:, :4].astype(np.float32)
292
+ scores = preds[:, 4].astype(np.float32)
293
+ cls_ids = preds[:, 5].astype(np.int32)
294
+
295
+ # person only
296
+ keep = cls_ids == 0
297
+ boxes = boxes[keep]
298
+ scores = scores[keep]
299
+ cls_ids = cls_ids[keep]
300
+
301
+ # candidate threshold
302
+ keep = scores >= self.conf_thres
303
+ boxes = boxes[keep]
304
+ scores = scores[keep]
305
+ cls_ids = cls_ids[keep]
306
+
307
+ if len(boxes) == 0:
308
+ return []
309
+
310
+ pad_w, pad_h = pad
311
+ orig_w, orig_h = orig_size
312
+
313
+ boxes[:, [0, 2]] -= pad_w
314
+ boxes[:, [1, 3]] -= pad_h
315
+ boxes /= ratio
316
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
317
+
318
+ boxes, scores, cls_ids = self._filter_sane_boxes(boxes, scores, cls_ids, orig_size)
319
+ if len(boxes) == 0:
320
+ return []
321
+
322
+ keep_idx = self._hard_nms(boxes, scores, self.iou_thres)
323
+ keep_idx = keep_idx[: self.max_det]
324
+
325
+ boxes = boxes[keep_idx]
326
+ scores = scores[keep_idx]
327
+ cls_ids = cls_ids[keep_idx]
328
+
329
+ return [
330
+ BoundingBox(
331
+ x1=int(math.floor(box[0])),
332
+ y1=int(math.floor(box[1])),
333
+ x2=int(math.ceil(box[2])),
334
+ y2=int(math.ceil(box[3])),
335
+ cls_id=int(cls_id),
336
+ conf=float(conf),
337
+ )
338
+ for box, conf, cls_id in zip(boxes, scores, cls_ids)
339
+ if box[2] > box[0] and box[3] > box[1]
340
+ ]
341
+
342
+ def _decode_raw_yolo(
343
+ self,
344
+ preds: np.ndarray,
345
+ ratio: float,
346
+ pad: tuple[float, float],
347
+ orig_size: tuple[int, int],
348
+ ) -> list[BoundingBox]:
349
+ if preds.ndim != 3:
350
+ raise ValueError(f"Unexpected raw ONNX output shape: {preds.shape}")
351
+ if preds.shape[0] != 1:
352
+ raise ValueError(f"Unexpected batch dimension in raw output: {preds.shape}")
353
+
354
+ preds = preds[0]
355
+
356
+ # Normalize to [N, C]
357
+ if preds.shape[0] <= 16 and preds.shape[1] > preds.shape[0]:
358
+ preds = preds.T
359
+
360
+ if preds.ndim != 2 or preds.shape[1] < 5:
361
+ raise ValueError(f"Unexpected normalized raw output shape: {preds.shape}")
362
+
363
+ boxes_xywh = preds[:, :4].astype(np.float32)
364
+ tail = preds[:, 4:].astype(np.float32)
365
+
366
+ # Supports:
367
+ # [x,y,w,h,score] single-class
368
+ # [x,y,w,h,obj,cls] YOLO standard single-class
369
+ # [x,y,w,h,obj,cls1,cls2,...] multi-class
370
+ if tail.shape[1] == 1:
371
+ scores = tail[:, 0]
372
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
373
+ elif tail.shape[1] == 2:
374
+ obj = tail[:, 0]
375
+ cls_prob = tail[:, 1]
376
+ scores = obj * cls_prob
377
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
378
+ else:
379
+ obj = tail[:, 0]
380
+ class_probs = tail[:, 1:]
381
+ cls_ids = np.argmax(class_probs, axis=1).astype(np.int32)
382
+ cls_scores = class_probs[np.arange(len(class_probs)), cls_ids]
383
+ scores = obj * cls_scores
384
+
385
+ keep = cls_ids == 0
386
+ boxes_xywh = boxes_xywh[keep]
387
+ scores = scores[keep]
388
+ cls_ids = cls_ids[keep]
389
+
390
+ keep = scores >= self.conf_thres
391
+ boxes_xywh = boxes_xywh[keep]
392
+ scores = scores[keep]
393
+ cls_ids = cls_ids[keep]
394
+
395
+ if len(boxes_xywh) == 0:
396
+ return []
397
+
398
+ boxes = self._xywh_to_xyxy(boxes_xywh)
399
+
400
+ pad_w, pad_h = pad
401
+ orig_w, orig_h = orig_size
402
+
403
+ boxes[:, [0, 2]] -= pad_w
404
+ boxes[:, [1, 3]] -= pad_h
405
+ boxes /= ratio
406
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
407
+
408
+ boxes, scores, cls_ids = self._filter_sane_boxes(boxes, scores, cls_ids, orig_size)
409
+ if len(boxes) == 0:
410
+ return []
411
+
412
+ keep_idx = self._hard_nms(boxes, scores, self.iou_thres)
413
+ keep_idx = keep_idx[: self.max_det]
414
+
415
+ boxes = boxes[keep_idx]
416
+ scores = scores[keep_idx]
417
+ cls_ids = cls_ids[keep_idx]
418
+
419
+ return [
420
+ BoundingBox(
421
+ x1=int(math.floor(box[0])),
422
+ y1=int(math.floor(box[1])),
423
+ x2=int(math.ceil(box[2])),
424
+ y2=int(math.ceil(box[3])),
425
+ cls_id=int(cls_id),
426
+ conf=float(conf),
427
+ )
428
+ for box, conf, cls_id in zip(boxes, scores, cls_ids)
429
+ if box[2] > box[0] and box[3] > box[1]
430
+ ]
431
+
432
+ def _postprocess(
433
+ self,
434
+ output: np.ndarray,
435
+ ratio: float,
436
+ pad: tuple[float, float],
437
+ orig_size: tuple[int, int],
438
+ ) -> list[BoundingBox]:
439
+ if output.ndim == 2 and output.shape[1] >= 6:
440
+ return self._decode_final_dets(output, ratio, pad, orig_size)
441
+
442
+ if output.ndim == 3 and output.shape[0] == 1 and output.shape[2] >= 6:
443
+ return self._decode_final_dets(output, ratio, pad, orig_size)
444
+
445
+ return self._decode_raw_yolo(output, ratio, pad, orig_size)
446
+
447
+ def _predict_single(self, image: np.ndarray) -> list[BoundingBox]:
448
+ if image is None:
449
+ raise ValueError("Input image is None")
450
+ if not isinstance(image, np.ndarray):
451
+ raise TypeError(f"Input is not numpy array: {type(image)}")
452
+ if image.ndim != 3:
453
+ raise ValueError(f"Expected HWC image, got shape={image.shape}")
454
+ if image.shape[0] <= 0 or image.shape[1] <= 0:
455
+ raise ValueError(f"Invalid image shape={image.shape}")
456
+ if image.shape[2] != 3:
457
+ raise ValueError(f"Expected 3 channels, got shape={image.shape}")
458
+
459
+ if image.dtype != np.uint8:
460
+ image = image.astype(np.uint8)
461
+
462
+ input_tensor, ratio, pad, orig_size = self._preprocess(image)
463
+
464
+ expected_shape = (1, 3, self.input_height, self.input_width)
465
+ if input_tensor.shape != expected_shape:
466
+ raise ValueError(
467
+ f"Bad input tensor shape={input_tensor.shape}, expected={expected_shape}"
468
+ )
469
+
470
+ outputs = self.session.run(self.output_names, {self.input_name: input_tensor})
471
+ det_output = outputs[0]
472
+ return self._postprocess(det_output, ratio, pad, orig_size)
473
+
474
+ def _merge_tta_consensus(
475
+ self,
476
+ boxes_orig: list[BoundingBox],
477
+ boxes_flip: list[BoundingBox],
478
+ ) -> list[BoundingBox]:
479
+ """
480
+ Keep:
481
+ - any box with conf >= conf_high
482
+ - low/medium-conf boxes only if confirmed across TTA views
483
+ Then run final hard NMS.
484
+ """
485
+ if not boxes_orig and not boxes_flip:
486
+ return []
487
+
488
+ coords_o = np.array([[b.x1, b.y1, b.x2, b.y2] for b in boxes_orig], dtype=np.float32) if boxes_orig else np.empty((0, 4), dtype=np.float32)
489
+ scores_o = np.array([b.conf for b in boxes_orig], dtype=np.float32) if boxes_orig else np.empty((0,), dtype=np.float32)
490
+
491
+ coords_f = np.array([[b.x1, b.y1, b.x2, b.y2] for b in boxes_flip], dtype=np.float32) if boxes_flip else np.empty((0, 4), dtype=np.float32)
492
+ scores_f = np.array([b.conf for b in boxes_flip], dtype=np.float32) if boxes_flip else np.empty((0,), dtype=np.float32)
493
+
494
+ accepted_boxes = []
495
+ accepted_scores = []
496
+
497
+ # Original view candidates
498
+ for i in range(len(coords_o)):
499
+ score = scores_o[i]
500
+ if score >= self.conf_high:
501
+ accepted_boxes.append(coords_o[i])
502
+ accepted_scores.append(score)
503
+ elif len(coords_f) > 0:
504
+ ious = self._box_iou_one_to_many(coords_o[i], coords_f)
505
+ j = int(np.argmax(ious))
506
+ if ious[j] >= self.tta_match_iou:
507
+ fused_score = max(score, scores_f[j])
508
+ accepted_boxes.append(coords_o[i])
509
+ accepted_scores.append(fused_score)
510
+
511
+ # Flipped-view high-confidence boxes that original missed
512
+ for i in range(len(coords_f)):
513
+ score = scores_f[i]
514
+ if score < self.conf_high:
515
+ continue
516
+
517
+ if len(coords_o) == 0:
518
+ accepted_boxes.append(coords_f[i])
519
+ accepted_scores.append(score)
520
+ continue
521
+
522
+ ious = self._box_iou_one_to_many(coords_f[i], coords_o)
523
+ if np.max(ious) < self.tta_match_iou:
524
+ accepted_boxes.append(coords_f[i])
525
+ accepted_scores.append(score)
526
+
527
+ if not accepted_boxes:
528
+ return []
529
+
530
+ boxes = np.array(accepted_boxes, dtype=np.float32)
531
+ scores = np.array(accepted_scores, dtype=np.float32)
532
+
533
+ keep = self._hard_nms(boxes, scores, self.iou_thres)
534
+ keep = keep[: self.max_det]
535
+
536
+ out = []
537
+ for idx in keep:
538
+ x1, y1, x2, y2 = boxes[idx].tolist()
539
+ out.append(
540
+ BoundingBox(
541
+ x1=int(math.floor(x1)),
542
+ y1=int(math.floor(y1)),
543
+ x2=int(math.ceil(x2)),
544
+ y2=int(math.ceil(y2)),
545
+ cls_id=0,
546
+ conf=float(scores[idx]),
547
+ )
548
+ )
549
+ return out
550
+
551
+ def _predict_tta(self, image: np.ndarray) -> list[BoundingBox]:
552
+ boxes_orig = self._predict_single(image)
553
+
554
+ flipped = cv2.flip(image, 1)
555
+ boxes_flip_raw = self._predict_single(flipped)
556
+
557
+ w = image.shape[1]
558
+ boxes_flip = [
559
+ BoundingBox(
560
+ x1=w - b.x2,
561
+ y1=b.y1,
562
+ x2=w - b.x1,
563
+ y2=b.y2,
564
+ cls_id=b.cls_id,
565
+ conf=b.conf,
566
+ )
567
+ for b in boxes_flip_raw
568
+ ]
569
+
570
+ return self._merge_tta_consensus(boxes_orig, boxes_flip)
571
+
572
+ def predict_batch(
573
+ self,
574
+ batch_images: list[ndarray],
575
+ offset: int,
576
+ n_keypoints: int,
577
+ ) -> list[TVFrameResult]:
578
+ results: list[TVFrameResult] = []
579
+
580
+ for frame_number_in_batch, image in enumerate(batch_images):
581
+ try:
582
+ if self.use_tta:
583
+ boxes = self._predict_tta(image)
584
+ else:
585
+ boxes = self._predict_single(image)
586
+ except Exception as e:
587
+ print(f"⚠️ Inference failed for frame {offset + frame_number_in_batch}: {e}")
588
+ boxes = []
589
+
590
+ results.append(
591
+ TVFrameResult(
592
+ frame_id=offset + frame_number_in_batch,
593
+ boxes=boxes,
594
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
595
+ )
596
+ )
597
+
598
+ return results
weights.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e2308ea9e8784fa26f57b93fe4d66837b07f031f9371e7f988aec0e8d7c9da9
3
+ size 19404973