licensy commited on
Commit
2c7f87e
·
verified ·
1 Parent(s): 99d4464

scorevision: push artifact

Browse files
Files changed (1) hide show
  1. miner.py +252 -123
miner.py CHANGED
@@ -1,15 +1,3 @@
1
- """Plate-detection miner — plate_v6 + consensus-TTA inference.
2
-
3
- Weights: plate_v6 (resumed plate_v5 + difficulty-weighted scraped real challenges
4
- + synth CCTV; 18 epochs, peak val mAP50 0.930).
5
-
6
- Inference (smile0123-style consensus-TTA, our bench winner at gated 0.443):
7
- - low conf (0.15) for high recall, super-high-conf (>=0.90) passes directly
8
- - hflip cross-view consensus: low-conf boxes must match a flipped-view box at IoU>=0.01
9
- - final hard-NMS at iou=0.32, max_det=150
10
- Bench on 221-shard live pseudo-GT pool: gated 0.443 mAP 0.975 fp/img 0.25 ms_p95 ~150 (A4000)
11
- On pro_6000 + TEE expect ~2-3s p95 including network/attest overhead.
12
- """
13
  from pathlib import Path
14
  import math
15
 
@@ -45,7 +33,8 @@ class Miner:
45
  if cn_path.is_file():
46
  lines = cn_path.read_text(encoding="utf-8").splitlines()
47
  self.class_names = [
48
- ln.strip() for ln in lines
 
49
  if ln.strip() and not ln.strip().startswith("#")
50
  ]
51
  else:
@@ -58,11 +47,15 @@ class Miner:
58
  except Exception as e:
59
  print(f"preload_dlls failed: {e}")
60
 
 
 
61
  try:
62
  import torch
63
  if torch.cuda.is_available():
64
  print(f"GPU: {torch.cuda.get_device_name(0)}")
65
- print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
 
 
66
  except Exception as e:
67
  print(f"GPU detection failed: {e}")
68
 
@@ -71,17 +64,21 @@ class Miner:
71
 
72
  try:
73
  self.session = ort.InferenceSession(
74
- str(model_path), sess_options=sess_options,
 
75
  providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
76
  )
77
- print("Created ORT session with CUDA provider")
78
  except Exception as e:
79
  print(f"CUDA session creation failed, falling back to CPU: {e}")
80
  self.session = ort.InferenceSession(
81
- str(model_path), sess_options=sess_options,
 
82
  providers=["CPUExecutionProvider"],
83
  )
84
 
 
 
85
  for inp in self.session.get_inputs():
86
  print("INPUT:", inp.name, inp.shape, inp.type)
87
  for out in self.session.get_outputs():
@@ -90,55 +87,75 @@ class Miner:
90
  self.input_name = self.session.get_inputs()[0].name
91
  self.output_names = [o.name for o in self.session.get_outputs()]
92
  self.input_shape = self.session.get_inputs()[0].shape
93
- self.input_dtype = (
94
- np.float16 if "float16" in self.session.get_inputs()[0].type
95
- else np.float32
96
- )
97
  self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
98
  self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
99
 
100
- # Consensus-TTA preset (bench winner gated 0.443)
101
- self.conf_thres = 0.15 # low — collect MANY candidates
102
- self.conf_high = 0.90 # >= this → pass through without TTA match
103
- self.tta_match_iou = 0.01 # very permissive cross-view match
104
- self.iou_thres = 0.32 # final hard-NMS
105
- self.max_det = 150
 
 
 
 
 
 
 
 
 
106
 
107
- print(f"Preset: conf={self.conf_thres} conf_high={self.conf_high} "
108
- f"tta_match_iou={self.tta_match_iou} iou={self.iou_thres} max_det={self.max_det}")
109
  print(f"ONNX model loaded from: {model_path}")
 
 
110
 
111
  def __repr__(self) -> str:
112
- return f"ONNXRuntime(providers={self.session.get_providers()})"
 
 
 
113
 
114
  @staticmethod
115
  def _safe_dim(value, default: int) -> int:
116
  return value if isinstance(value, int) and value > 0 else default
117
 
118
- # ---------- preprocessing ----------
119
- def _letterbox(self, image, new_shape, color=(114, 114, 114)):
 
 
 
 
 
120
  h, w = image.shape[:2]
121
  new_w, new_h = new_shape
122
  ratio = min(new_w / w, new_h / h)
123
- rw, rh = int(round(w * ratio)), int(round(h * ratio))
124
- if (rw, rh) != (w, h):
 
125
  interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
126
- image = cv2.resize(image, (rw, rh), interpolation=interp)
127
- dw, dh = (new_w - rw) / 2.0, (new_h - rh) / 2.0
128
- left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
129
- top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
130
- padded = cv2.copyMakeBorder(image, top, bottom, left, right,
131
- borderType=cv2.BORDER_CONSTANT, value=color)
 
 
 
 
 
132
  return padded, ratio, (dw, dh)
133
 
134
- def _preprocess(self, image):
135
  img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
136
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
137
  img = np.transpose(img, (2, 0, 1))[None, ...]
138
- return np.ascontiguousarray(img, dtype=self.input_dtype), ratio, pad
139
 
140
  @staticmethod
141
- def _clip_boxes(boxes, image_size):
142
  w, h = image_size
143
  boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
144
  boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
@@ -146,32 +163,20 @@ class Miner:
146
  boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
147
  return boxes
148
 
149
- # ---------- detection helpers ----------
150
  @staticmethod
151
- def _iou_one_to_many(box, boxes):
152
- if len(boxes) == 0:
153
- return np.zeros(0, dtype=np.float32)
154
- xx1 = np.maximum(box[0], boxes[:, 0])
155
- yy1 = np.maximum(box[1], boxes[:, 1])
156
- xx2 = np.minimum(box[2], boxes[:, 2])
157
- yy2 = np.minimum(box[3], boxes[:, 3])
158
- inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
159
- ai = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
160
- aj = (np.maximum(0.0, boxes[:, 2] - boxes[:, 0])
161
- * np.maximum(0.0, boxes[:, 3] - boxes[:, 1]))
162
- return inter / (ai + aj - inter + 1e-7)
163
-
164
- @staticmethod
165
- def _hard_nms(boxes, scores, iou_thresh, max_det):
166
  N = len(boxes)
167
  if N == 0:
168
  return np.array([], dtype=np.intp)
 
 
169
  order = np.argsort(-scores)
170
- keep = []
171
  while len(order):
172
  i = int(order[0])
173
  keep.append(i)
174
- if len(order) == 1 or len(keep) >= max_det:
175
  break
176
  rest = order[1:]
177
  xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
@@ -179,13 +184,65 @@ class Miner:
179
  xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
180
  yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
181
  inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
182
- ai = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
183
- ar = (boxes[rest, 2] - boxes[rest, 0]) * (boxes[rest, 3] - boxes[rest, 1])
184
- iou = inter / (ai + ar - inter + 1e-7)
185
  order = rest[iou <= iou_thresh]
186
  return np.array(keep, dtype=np.intp)
187
 
188
- def _raw_dets(self, image, conf):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  x, ratio, (dw, dh) = self._preprocess(image)
190
  out = self.session.run(self.output_names, {self.input_name: x})[0]
191
  if out.ndim == 3:
@@ -205,51 +262,111 @@ class Miner:
205
  boxes = self._clip_boxes(boxes, (ow, oh))
206
  return np.concatenate([boxes, scores[:, None]], axis=1)
207
 
208
- # ---------- consensus-TTA prediction ----------
209
- def _predict_consensus_tta(self, image):
210
- d_o = self._raw_dets(image, self.conf_thres)
211
  flipped = cv2.flip(image, 1)
212
- d_f = self._raw_dets(flipped, self.conf_thres)
213
- if len(d_f):
214
  w = image.shape[1]
215
- d_f = np.stack([w - d_f[:, 2], d_f[:, 1], w - d_f[:, 0],
216
- d_f[:, 3], d_f[:, 4]], axis=1)
217
-
218
- accepted_boxes = []
219
- accepted_scores = []
220
- # Original-view candidates
221
- for i in range(len(d_o)):
222
- s = float(d_o[i, 4])
223
- if s >= self.conf_high:
224
- accepted_boxes.append(d_o[i, :4])
225
- accepted_scores.append(s)
226
- elif len(d_f) > 0:
227
- ious = self._iou_one_to_many(d_o[i, :4], d_f[:, :4])
228
- j = int(np.argmax(ious))
229
- if ious[j] >= self.tta_match_iou:
230
- fused = max(s, float(d_f[j, 4]))
231
- accepted_boxes.append(d_o[i, :4])
232
- accepted_scores.append(fused)
233
- # Flip-view high-conf boxes that original missed
234
- for i in range(len(d_f)):
235
- s = float(d_f[i, 4])
236
- if s < self.conf_high:
237
- continue
238
- if len(d_o) == 0:
239
- accepted_boxes.append(d_f[i, :4])
240
- accepted_scores.append(s)
241
- continue
242
- ious = self._iou_one_to_many(d_f[i, :4], d_o[:, :4])
243
- if np.max(ious) < self.tta_match_iou:
244
- accepted_boxes.append(d_f[i, :4])
245
- accepted_scores.append(s)
246
-
247
- if not accepted_boxes:
248
  return np.zeros((0, 5), dtype=np.float32)
249
- boxes = np.array(accepted_boxes, dtype=np.float32)
250
- scores = np.array(accepted_scores, dtype=np.float32)
251
- keep = self._hard_nms(boxes, scores, self.iou_thres, self.max_det)
252
- return np.concatenate([boxes[keep], scores[keep][:, None]], axis=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
  # ---------- single-image predict ----------
255
  def _predict_single(self, image: ndarray) -> list[BoundingBox]:
@@ -260,26 +377,36 @@ class Miner:
260
  if image.dtype != np.uint8:
261
  image = image.astype(np.uint8)
262
 
263
- dets = self._predict_consensus_tta(image)
 
 
 
 
264
 
265
  results: list[BoundingBox] = []
266
  for row in dets:
267
  x1, y1, x2, y2, conf = row.tolist()
268
  if x2 <= x1 or y2 <= y1:
269
  continue
270
- results.append(BoundingBox(
271
- x1=int(math.floor(x1)),
272
- y1=int(math.floor(y1)),
273
- x2=int(math.ceil(x2)),
274
- y2=int(math.ceil(y2)),
275
- cls_id=0,
276
- conf=float(conf),
277
- ))
 
 
278
  return results
279
 
280
  # ---------- chute entrypoint ----------
281
- def predict_batch(self, batch_images: list[ndarray], offset: int,
282
- n_keypoints: int) -> list[TVFrameResult]:
 
 
 
 
283
  results: list[TVFrameResult] = []
284
  for frame_number_in_batch, image in enumerate(batch_images):
285
  try:
@@ -287,9 +414,11 @@ class Miner:
287
  except Exception as e:
288
  print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
289
  boxes = []
290
- results.append(TVFrameResult(
291
- frame_id=offset + frame_number_in_batch,
292
- boxes=boxes,
293
- keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
294
- ))
 
 
295
  return results
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from pathlib import Path
2
  import math
3
 
 
33
  if cn_path.is_file():
34
  lines = cn_path.read_text(encoding="utf-8").splitlines()
35
  self.class_names = [
36
+ ln.strip()
37
+ for ln in lines
38
  if ln.strip() and not ln.strip().startswith("#")
39
  ]
40
  else:
 
47
  except Exception as e:
48
  print(f"preload_dlls failed: {e}")
49
 
50
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
51
+
52
  try:
53
  import torch
54
  if torch.cuda.is_available():
55
  print(f"GPU: {torch.cuda.get_device_name(0)}")
56
+ print(f"GPU memory: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB")
57
+ else:
58
+ print("GPU: CUDA not available via torch")
59
  except Exception as e:
60
  print(f"GPU detection failed: {e}")
61
 
 
64
 
65
  try:
66
  self.session = ort.InferenceSession(
67
+ str(model_path),
68
+ sess_options=sess_options,
69
  providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
70
  )
71
+ print("Created ORT session with preferred CUDA provider list")
72
  except Exception as e:
73
  print(f"CUDA session creation failed, falling back to CPU: {e}")
74
  self.session = ort.InferenceSession(
75
+ str(model_path),
76
+ sess_options=sess_options,
77
  providers=["CPUExecutionProvider"],
78
  )
79
 
80
+ print("ORT session providers:", self.session.get_providers())
81
+
82
  for inp in self.session.get_inputs():
83
  print("INPUT:", inp.name, inp.shape, inp.type)
84
  for out in self.session.get_outputs():
 
87
  self.input_name = self.session.get_inputs()[0].name
88
  self.output_names = [o.name for o in self.session.get_outputs()]
89
  self.input_shape = self.session.get_inputs()[0].shape
90
+
 
 
 
91
  self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
92
  self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
93
 
94
+ # Primary pass: alfred001 weights + lower conf (bench-verified +0.007 gated vs alfred stock c=0.26)
95
+ self.conf_thres = 0.22
96
+ self.iou_thres = 0.39
97
+ self.sigma = 0.465
98
+ self.max_det = 300
99
+
100
+ # Conditional tile-pass (trimmed for latency: no hflip, tighter sparse)
101
+ self.sparse_threshold = 3 # fire tiles only if primary returns < this
102
+ self.tile_conf = 0.57
103
+ self.tile_overlap = 0.20
104
+ self.novelty_iou = 0.10
105
+ self.final_max_det = 17
106
+ self.tile_use_hflip = False # skip hflip tile pass to save ~4 forwards
107
+
108
+ self.use_tta = True
109
 
 
 
110
  print(f"ONNX model loaded from: {model_path}")
111
+ print(f"ONNX providers: {self.session.get_providers()}")
112
+ print(f"ONNX input: name={self.input_name}, shape={self.input_shape}")
113
 
114
  def __repr__(self) -> str:
115
+ return (
116
+ f"ONNXRuntime(session={type(self.session).__name__}, "
117
+ f"providers={self.session.get_providers()})"
118
+ )
119
 
120
  @staticmethod
121
  def _safe_dim(value, default: int) -> int:
122
  return value if isinstance(value, int) and value > 0 else default
123
 
124
+ # ---------- image preprocessing ----------
125
+ def _letterbox(
126
+ self,
127
+ image: ndarray,
128
+ new_shape: tuple[int, int],
129
+ color=(114, 114, 114),
130
+ ) -> tuple[ndarray, float, tuple[float, float]]:
131
  h, w = image.shape[:2]
132
  new_w, new_h = new_shape
133
  ratio = min(new_w / w, new_h / h)
134
+ resized_w = int(round(w * ratio))
135
+ resized_h = int(round(h * ratio))
136
+ if (resized_w, resized_h) != (w, h):
137
  interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
138
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
139
+ dw = (new_w - resized_w) / 2.0
140
+ dh = (new_h - resized_h) / 2.0
141
+ left = int(round(dw - 0.1))
142
+ right = int(round(dw + 0.1))
143
+ top = int(round(dh - 0.1))
144
+ bottom = int(round(dh + 0.1))
145
+ padded = cv2.copyMakeBorder(
146
+ image, top, bottom, left, right,
147
+ borderType=cv2.BORDER_CONSTANT, value=color,
148
+ )
149
  return padded, ratio, (dw, dh)
150
 
151
+ def _preprocess(self, image: ndarray):
152
  img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
153
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
154
  img = np.transpose(img, (2, 0, 1))[None, ...]
155
+ return np.ascontiguousarray(img, dtype=np.float32), ratio, pad
156
 
157
  @staticmethod
158
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
159
  w, h = image_size
160
  boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
161
  boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
 
163
  boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
164
  return boxes
165
 
166
+ # ---------- NMS primitives ----------
167
  @staticmethod
168
+ def _hard_nms(boxes: np.ndarray, scores: np.ndarray, iou_thresh: float) -> np.ndarray:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  N = len(boxes)
170
  if N == 0:
171
  return np.array([], dtype=np.intp)
172
+ boxes = np.asarray(boxes, dtype=np.float32)
173
+ scores = np.asarray(scores, dtype=np.float32)
174
  order = np.argsort(-scores)
175
+ keep: list[int] = []
176
  while len(order):
177
  i = int(order[0])
178
  keep.append(i)
179
+ if len(order) == 1:
180
  break
181
  rest = order[1:]
182
  xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
 
184
  xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
185
  yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
186
  inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
187
+ area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
188
+ area_r = (boxes[rest, 2] - boxes[rest, 0]) * (boxes[rest, 3] - boxes[rest, 1])
189
+ iou = inter / (area_i + area_r - inter + 1e-7)
190
  order = rest[iou <= iou_thresh]
191
  return np.array(keep, dtype=np.intp)
192
 
193
+ def _soft_nms(
194
+ self,
195
+ boxes: np.ndarray,
196
+ scores: np.ndarray,
197
+ sigma: float,
198
+ score_thresh: float = 0.01,
199
+ ) -> tuple[np.ndarray, np.ndarray]:
200
+ N = len(boxes)
201
+ if N == 0:
202
+ return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
203
+ boxes = boxes.astype(np.float32, copy=True)
204
+ scores = scores.astype(np.float32, copy=True)
205
+ order = np.arange(N)
206
+ for i in range(N):
207
+ max_pos = i + int(np.argmax(scores[i:]))
208
+ boxes[[i, max_pos]] = boxes[[max_pos, i]]
209
+ scores[[i, max_pos]] = scores[[max_pos, i]]
210
+ order[[i, max_pos]] = order[[max_pos, i]]
211
+ if i + 1 >= N:
212
+ break
213
+ xx1 = np.maximum(boxes[i, 0], boxes[i + 1:, 0])
214
+ yy1 = np.maximum(boxes[i, 1], boxes[i + 1:, 1])
215
+ xx2 = np.minimum(boxes[i, 2], boxes[i + 1:, 2])
216
+ yy2 = np.minimum(boxes[i, 3], boxes[i + 1:, 3])
217
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
218
+ area_i = float(
219
+ (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
220
+ )
221
+ areas_j = (
222
+ np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
223
+ * np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
224
+ )
225
+ iou = inter / (area_i + areas_j - inter + 1e-7)
226
+ scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
227
+ mask = scores > score_thresh
228
+ return order[mask], scores[mask]
229
+
230
+ @staticmethod
231
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
232
+ if len(boxes) == 0:
233
+ return np.zeros(0, dtype=np.float32)
234
+ xx1 = np.maximum(box[0], boxes[:, 0])
235
+ yy1 = np.maximum(box[1], boxes[:, 1])
236
+ xx2 = np.minimum(box[2], boxes[:, 2])
237
+ yy2 = np.minimum(box[3], boxes[:, 3])
238
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
239
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
240
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
241
+ return inter / (area_a + area_b - inter + 1e-7)
242
+
243
+ # ---------- raw-dets helper ----------
244
+ def _raw_dets(self, image: ndarray, conf: float) -> np.ndarray:
245
+ """Run a single forward pass and return [N, 5] dets in ORIGINAL image coords."""
246
  x, ratio, (dw, dh) = self._preprocess(image)
247
  out = self.session.run(self.output_names, {self.input_name: x})[0]
248
  if out.ndim == 3:
 
262
  boxes = self._clip_boxes(boxes, (ow, oh))
263
  return np.concatenate([boxes, scores[:, None]], axis=1)
264
 
265
+ # ---------- primary pass: soft-NMS + hflip TTA ----------
266
+ def _primary(self, image: ndarray) -> np.ndarray:
267
+ d1 = self._raw_dets(image, self.conf_thres)
268
  flipped = cv2.flip(image, 1)
269
+ d2 = self._raw_dets(flipped, self.conf_thres)
270
+ if len(d2):
271
  w = image.shape[1]
272
+ x1 = w - d2[:, 2]
273
+ x2 = w - d2[:, 0]
274
+ d2 = np.stack([x1, d2[:, 1], x2, d2[:, 3], d2[:, 4]], axis=1)
275
+ all_d = np.concatenate([d1, d2], axis=0) if len(d2) else d1
276
+ if len(all_d) == 0:
277
+ return np.zeros((0, 5), dtype=np.float32)
278
+ # soft-NMS, then hard-NMS
279
+ keep_idx, scores = self._soft_nms(all_d[:, :4].copy(), all_d[:, 4].copy(), sigma=self.sigma)
280
+ if len(keep_idx) == 0:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  return np.zeros((0, 5), dtype=np.float32)
282
+ merged = np.concatenate([all_d[keep_idx, :4], scores[:, None]], axis=1)
283
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
284
+ merged = merged[keep]
285
+ if len(merged) > self.max_det:
286
+ merged = merged[np.argsort(-merged[:, 4])[: self.max_det]]
287
+ return merged
288
+
289
+ # ---------- conditional tile pass ----------
290
+ def _tile_augment(self, image: ndarray, primary: np.ndarray) -> np.ndarray:
291
+ """Run 2x2 overlapping tiles + hflip, novelty-merge into primary."""
292
+ oh, ow = image.shape[:2]
293
+ tw, th = ow // 2, oh // 2
294
+ ox, oy = int(tw * self.tile_overlap), int(th * self.tile_overlap)
295
+ tiles = [
296
+ (0, 0, min(ow, tw + ox), min(oh, th + oy)),
297
+ (max(0, tw - ox), 0, ow, min(oh, th + oy)),
298
+ (0, max(0, th - oy), min(ow, tw + ox), oh),
299
+ (max(0, tw - ox), max(0, th - oy), ow, oh),
300
+ ]
301
+ collected: list[np.ndarray] = []
302
+ for x1, y1, x2, y2 in tiles:
303
+ crop = image[y1:y2, x1:x2]
304
+ if crop.size == 0:
305
+ continue
306
+ d = self._raw_dets(crop, self.tile_conf)
307
+ if len(d):
308
+ d[:, 0] += x1
309
+ d[:, 1] += y1
310
+ d[:, 2] += x1
311
+ d[:, 3] += y1
312
+ collected.append(d)
313
+
314
+ # hflip tile pass (skipped when tile_use_hflip=False — saves 4 ONNX forwards)
315
+ if self.tile_use_hflip:
316
+ flipped = cv2.flip(image, 1)
317
+ for x1, y1, x2, y2 in tiles:
318
+ fx1 = ow - x2
319
+ fx2 = ow - x1
320
+ if fx2 <= fx1:
321
+ continue
322
+ crop = flipped[y1:y2, fx1:fx2]
323
+ if crop.size == 0:
324
+ continue
325
+ d = self._raw_dets(crop, self.tile_conf)
326
+ if len(d):
327
+ d_un = d.copy()
328
+ d_un[:, 0] = (ow - (d[:, 2] + fx1))
329
+ d_un[:, 2] = (ow - (d[:, 0] + fx1))
330
+ d_un[:, 1] = d[:, 1] + y1
331
+ d_un[:, 3] = d[:, 3] + y1
332
+ collected.append(d_un)
333
+
334
+ if not collected:
335
+ return primary
336
+
337
+ tile_dets = np.concatenate(collected, axis=0)
338
+ keep = self._hard_nms(tile_dets[:, :4], tile_dets[:, 4], 0.5)
339
+ tile_dets = tile_dets[keep]
340
+
341
+ # Novelty: drop tile boxes that overlap any primary box at IoU >= novelty_iou
342
+ if len(primary) > 0 and len(tile_dets) > 0:
343
+ mask = np.ones(len(tile_dets), dtype=bool)
344
+ for i in range(len(tile_dets)):
345
+ ious = self._box_iou_one_to_many(tile_dets[i, :4], primary[:, :4])
346
+ if len(ious) and np.max(ious) >= self.novelty_iou:
347
+ mask[i] = False
348
+ tile_dets = tile_dets[mask]
349
+
350
+ if len(tile_dets) == 0:
351
+ return primary
352
+
353
+ # Sanity filter: min/max size, aspect ratio
354
+ w = tile_dets[:, 2] - tile_dets[:, 0]
355
+ h = tile_dets[:, 3] - tile_dets[:, 1]
356
+ area = w * h
357
+ ar = np.maximum(w / np.maximum(h, 1e-6), h / np.maximum(w, 1e-6))
358
+ img_area = float(ow * oh)
359
+ ok = (w >= 7) & (h >= 7) & (area >= 85) & (area <= 0.5 * img_area) & (ar <= 10.0)
360
+ tile_dets = tile_dets[ok]
361
+ if len(tile_dets) == 0:
362
+ return primary
363
+
364
+ merged = np.concatenate([primary, tile_dets], axis=0)
365
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
366
+ merged = merged[keep]
367
+ if len(merged) > self.final_max_det:
368
+ merged = merged[np.argsort(-merged[:, 4])[: self.final_max_det]]
369
+ return merged
370
 
371
  # ---------- single-image predict ----------
372
  def _predict_single(self, image: ndarray) -> list[BoundingBox]:
 
377
  if image.dtype != np.uint8:
378
  image = image.astype(np.uint8)
379
 
380
+ primary = self._primary(image)
381
+ if len(primary) < self.sparse_threshold:
382
+ dets = self._tile_augment(image, primary)
383
+ else:
384
+ dets = primary
385
 
386
  results: list[BoundingBox] = []
387
  for row in dets:
388
  x1, y1, x2, y2, conf = row.tolist()
389
  if x2 <= x1 or y2 <= y1:
390
  continue
391
+ results.append(
392
+ BoundingBox(
393
+ x1=int(math.floor(x1)),
394
+ y1=int(math.floor(y1)),
395
+ x2=int(math.ceil(x2)),
396
+ y2=int(math.ceil(y2)),
397
+ cls_id=0,
398
+ conf=float(conf),
399
+ )
400
+ )
401
  return results
402
 
403
  # ---------- chute entrypoint ----------
404
+ def predict_batch(
405
+ self,
406
+ batch_images: list[ndarray],
407
+ offset: int,
408
+ n_keypoints: int,
409
+ ) -> list[TVFrameResult]:
410
  results: list[TVFrameResult] = []
411
  for frame_number_in_batch, image in enumerate(batch_images):
412
  try:
 
414
  except Exception as e:
415
  print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
416
  boxes = []
417
+ results.append(
418
+ TVFrameResult(
419
+ frame_id=offset + frame_number_in_batch,
420
+ boxes=boxes,
421
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
422
+ )
423
+ )
424
  return results