coolroman commited on
Commit
54dcac8
·
verified ·
1 Parent(s): e38ae4a

v1: alfred weights + arabic002 tuning + conditional sparse tile-aug

Browse files
__pycache__/miner.cpython-310.pyc ADDED
Binary file (13.4 kB). View file
 
chute_config.yml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Image:
2
+ from_base: parachutes/python:3.12
3
+ run_command:
4
+ - pip install --upgrade setuptools wheel
5
+ - pip install huggingface_hub==0.19.4 ultralytics==8.2.40 'torch<2.6' opencv-python-headless onnxruntime-gpu
6
+ set_workdir: /app
7
+
8
+ NodeSelector:
9
+ gpu_count: 1
10
+ min_vram_gb_per_gpu: 16
11
+ max_hourly_price_per_gpu: 1
12
+
13
+ Chute:
14
+ shutdown_after_seconds: 300000
15
+ concurrency: 4
16
+ max_instances: 1
17
+ scaling_threshold: 0.5
class_names.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ numberplate
miner.py ADDED
@@ -0,0 +1,426 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Plate-detection miner — v1 "sparse conditional tile-aug".
2
+
3
+ Base weights: alfred8995/arabic002 (YOLO26s @ 1280, fp16, ~19 MB).
4
+
5
+ Inference pipeline:
6
+ 1) Full-image primary pass with arabic002's strict tuning
7
+ (conf=0.27, iou=0.444, sigma=0.5, soft-NMS + hflip TTA, max_det=18).
8
+ 2) If the primary returned fewer than SPARSE_THRESHOLD (5) boxes,
9
+ run a 2x2 overlapping tile pass with higher conf (tile_conf=0.40)
10
+ and novelty-merge: keep a tile box only when it does not overlap
11
+ any primary box at IoU >= 0.10. Tile augmentation is skipped
12
+ entirely on challenges where the primary already has enough
13
+ detections, so the FP score stays intact on dense scenes.
14
+ """
15
+ from pathlib import Path
16
+ import math
17
+
18
+ import cv2
19
+ import numpy as np
20
+ import onnxruntime as ort
21
+ from numpy import ndarray
22
+ from pydantic import BaseModel
23
+
24
+
25
+ class BoundingBox(BaseModel):
26
+ x1: int
27
+ y1: int
28
+ x2: int
29
+ y2: int
30
+ cls_id: int
31
+ conf: float
32
+
33
+
34
+ class TVFrameResult(BaseModel):
35
+ frame_id: int
36
+ boxes: list[BoundingBox]
37
+ keypoints: list[tuple[int, int]]
38
+
39
+
40
+ SIZE = 1280
41
+
42
+
43
+ class Miner:
44
+ def __init__(self, path_hf_repo: Path) -> None:
45
+ model_path = path_hf_repo / "weights.onnx"
46
+ cn_path = model_path.with_name("class_names.txt")
47
+ if cn_path.is_file():
48
+ lines = cn_path.read_text(encoding="utf-8").splitlines()
49
+ self.class_names = [
50
+ ln.strip()
51
+ for ln in lines
52
+ if ln.strip() and not ln.strip().startswith("#")
53
+ ]
54
+ else:
55
+ self.class_names = ["numberplate"]
56
+ print("ORT version:", ort.__version__)
57
+
58
+ try:
59
+ ort.preload_dlls()
60
+ print("onnxruntime.preload_dlls() success")
61
+ except Exception as e:
62
+ print(f"preload_dlls failed: {e}")
63
+
64
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
65
+
66
+ sess_options = ort.SessionOptions()
67
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
68
+
69
+ try:
70
+ self.session = ort.InferenceSession(
71
+ str(model_path),
72
+ sess_options=sess_options,
73
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
74
+ )
75
+ print("Created ORT session with preferred CUDA provider list")
76
+ except Exception as e:
77
+ print(f"CUDA session creation failed, falling back to CPU: {e}")
78
+ self.session = ort.InferenceSession(
79
+ str(model_path),
80
+ sess_options=sess_options,
81
+ providers=["CPUExecutionProvider"],
82
+ )
83
+
84
+ print("ORT session providers:", self.session.get_providers())
85
+
86
+ for inp in self.session.get_inputs():
87
+ print("INPUT:", inp.name, inp.shape, inp.type)
88
+ for out in self.session.get_outputs():
89
+ print("OUTPUT:", out.name, out.shape, out.type)
90
+
91
+ self.input_name = self.session.get_inputs()[0].name
92
+ self.output_names = [o.name for o in self.session.get_outputs()]
93
+ self.input_shape = self.session.get_inputs()[0].shape
94
+
95
+ self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
96
+ self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
97
+
98
+ # Primary pass: arabic002 strict tuning
99
+ self.conf_thres = 0.27
100
+ self.iou_thres = 0.444
101
+ self.sigma = 0.5
102
+ self.max_det = 18
103
+
104
+ # Conditional tile-pass
105
+ self.sparse_threshold = 5 # fire tiles only if primary returns < this
106
+ self.tile_conf = 0.40
107
+ self.tile_overlap = 0.20
108
+ self.novelty_iou = 0.10
109
+ self.final_max_det = 22
110
+
111
+ self.use_tta = True
112
+
113
+ print(f"ONNX model loaded from: {model_path}")
114
+ print(f"ONNX providers: {self.session.get_providers()}")
115
+ print(f"ONNX input: name={self.input_name}, shape={self.input_shape}")
116
+
117
+ def __repr__(self) -> str:
118
+ return (
119
+ f"ONNXRuntime(session={type(self.session).__name__}, "
120
+ f"providers={self.session.get_providers()})"
121
+ )
122
+
123
+ @staticmethod
124
+ def _safe_dim(value, default: int) -> int:
125
+ return value if isinstance(value, int) and value > 0 else default
126
+
127
+ # ---------- image preprocessing ----------
128
+ def _letterbox(
129
+ self,
130
+ image: ndarray,
131
+ new_shape: tuple[int, int],
132
+ color=(114, 114, 114),
133
+ ) -> tuple[ndarray, float, tuple[float, float]]:
134
+ h, w = image.shape[:2]
135
+ new_w, new_h = new_shape
136
+ ratio = min(new_w / w, new_h / h)
137
+ resized_w = int(round(w * ratio))
138
+ resized_h = int(round(h * ratio))
139
+ if (resized_w, resized_h) != (w, h):
140
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
141
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
142
+ dw = (new_w - resized_w) / 2.0
143
+ dh = (new_h - resized_h) / 2.0
144
+ left = int(round(dw - 0.1))
145
+ right = int(round(dw + 0.1))
146
+ top = int(round(dh - 0.1))
147
+ bottom = int(round(dh + 0.1))
148
+ padded = cv2.copyMakeBorder(
149
+ image, top, bottom, left, right,
150
+ borderType=cv2.BORDER_CONSTANT, value=color,
151
+ )
152
+ return padded, ratio, (dw, dh)
153
+
154
+ def _preprocess(self, image: ndarray):
155
+ img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
156
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
157
+ img = np.transpose(img, (2, 0, 1))[None, ...]
158
+ return np.ascontiguousarray(img, dtype=np.float32), ratio, pad
159
+
160
+ @staticmethod
161
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
162
+ w, h = image_size
163
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
164
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
165
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
166
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
167
+ return boxes
168
+
169
+ # ---------- NMS primitives ----------
170
+ @staticmethod
171
+ def _hard_nms(boxes: np.ndarray, scores: np.ndarray, iou_thresh: float) -> np.ndarray:
172
+ N = len(boxes)
173
+ if N == 0:
174
+ return np.array([], dtype=np.intp)
175
+ boxes = np.asarray(boxes, dtype=np.float32)
176
+ scores = np.asarray(scores, dtype=np.float32)
177
+ order = np.argsort(-scores)
178
+ keep: list[int] = []
179
+ while len(order):
180
+ i = int(order[0])
181
+ keep.append(i)
182
+ if len(order) == 1:
183
+ break
184
+ rest = order[1:]
185
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
186
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
187
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
188
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
189
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
190
+ area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
191
+ area_r = (boxes[rest, 2] - boxes[rest, 0]) * (boxes[rest, 3] - boxes[rest, 1])
192
+ iou = inter / (area_i + area_r - inter + 1e-7)
193
+ order = rest[iou <= iou_thresh]
194
+ return np.array(keep, dtype=np.intp)
195
+
196
+ def _soft_nms(
197
+ self,
198
+ boxes: np.ndarray,
199
+ scores: np.ndarray,
200
+ sigma: float,
201
+ score_thresh: float = 0.01,
202
+ ) -> tuple[np.ndarray, np.ndarray]:
203
+ N = len(boxes)
204
+ if N == 0:
205
+ return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
206
+ boxes = boxes.astype(np.float32, copy=True)
207
+ scores = scores.astype(np.float32, copy=True)
208
+ order = np.arange(N)
209
+ for i in range(N):
210
+ max_pos = i + int(np.argmax(scores[i:]))
211
+ boxes[[i, max_pos]] = boxes[[max_pos, i]]
212
+ scores[[i, max_pos]] = scores[[max_pos, i]]
213
+ order[[i, max_pos]] = order[[max_pos, i]]
214
+ if i + 1 >= N:
215
+ break
216
+ xx1 = np.maximum(boxes[i, 0], boxes[i + 1:, 0])
217
+ yy1 = np.maximum(boxes[i, 1], boxes[i + 1:, 1])
218
+ xx2 = np.minimum(boxes[i, 2], boxes[i + 1:, 2])
219
+ yy2 = np.minimum(boxes[i, 3], boxes[i + 1:, 3])
220
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
221
+ area_i = float(
222
+ (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
223
+ )
224
+ areas_j = (
225
+ np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
226
+ * np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
227
+ )
228
+ iou = inter / (area_i + areas_j - inter + 1e-7)
229
+ scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
230
+ mask = scores > score_thresh
231
+ return order[mask], scores[mask]
232
+
233
+ @staticmethod
234
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
235
+ if len(boxes) == 0:
236
+ return np.zeros(0, dtype=np.float32)
237
+ xx1 = np.maximum(box[0], boxes[:, 0])
238
+ yy1 = np.maximum(box[1], boxes[:, 1])
239
+ xx2 = np.minimum(box[2], boxes[:, 2])
240
+ yy2 = np.minimum(box[3], boxes[:, 3])
241
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
242
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
243
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
244
+ return inter / (area_a + area_b - inter + 1e-7)
245
+
246
+ # ---------- raw-dets helper ----------
247
+ def _raw_dets(self, image: ndarray, conf: float) -> np.ndarray:
248
+ """Run a single forward pass and return [N, 5] dets in ORIGINAL image coords."""
249
+ x, ratio, (dw, dh) = self._preprocess(image)
250
+ out = self.session.run(self.output_names, {self.input_name: x})[0]
251
+ if out.ndim == 3:
252
+ out = out[0]
253
+ if out.shape[1] < 5:
254
+ return np.zeros((0, 5), dtype=np.float32)
255
+ boxes = out[:, :4].astype(np.float32)
256
+ scores = out[:, 4].astype(np.float32)
257
+ keep = scores >= conf
258
+ boxes, scores = boxes[keep], scores[keep]
259
+ if len(boxes) == 0:
260
+ return np.zeros((0, 5), dtype=np.float32)
261
+ boxes[:, [0, 2]] -= dw
262
+ boxes[:, [1, 3]] -= dh
263
+ boxes /= ratio
264
+ oh, ow = image.shape[:2]
265
+ boxes = self._clip_boxes(boxes, (ow, oh))
266
+ return np.concatenate([boxes, scores[:, None]], axis=1)
267
+
268
+ # ---------- primary pass: soft-NMS + hflip TTA ----------
269
+ def _primary(self, image: ndarray) -> np.ndarray:
270
+ d1 = self._raw_dets(image, self.conf_thres)
271
+ flipped = cv2.flip(image, 1)
272
+ d2 = self._raw_dets(flipped, self.conf_thres)
273
+ if len(d2):
274
+ w = image.shape[1]
275
+ x1 = w - d2[:, 2]
276
+ x2 = w - d2[:, 0]
277
+ d2 = np.stack([x1, d2[:, 1], x2, d2[:, 3], d2[:, 4]], axis=1)
278
+ all_d = np.concatenate([d1, d2], axis=0) if len(d2) else d1
279
+ if len(all_d) == 0:
280
+ return np.zeros((0, 5), dtype=np.float32)
281
+ # soft-NMS, then hard-NMS
282
+ keep_idx, scores = self._soft_nms(all_d[:, :4].copy(), all_d[:, 4].copy(), sigma=self.sigma)
283
+ if len(keep_idx) == 0:
284
+ return np.zeros((0, 5), dtype=np.float32)
285
+ merged = np.concatenate([all_d[keep_idx, :4], scores[:, None]], axis=1)
286
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
287
+ merged = merged[keep]
288
+ if len(merged) > self.max_det:
289
+ merged = merged[np.argsort(-merged[:, 4])[: self.max_det]]
290
+ return merged
291
+
292
+ # ---------- conditional tile pass ----------
293
+ def _tile_augment(self, image: ndarray, primary: np.ndarray) -> np.ndarray:
294
+ """Run 2x2 overlapping tiles + hflip, novelty-merge into primary."""
295
+ oh, ow = image.shape[:2]
296
+ tw, th = ow // 2, oh // 2
297
+ ox, oy = int(tw * self.tile_overlap), int(th * self.tile_overlap)
298
+ tiles = [
299
+ (0, 0, min(ow, tw + ox), min(oh, th + oy)),
300
+ (max(0, tw - ox), 0, ow, min(oh, th + oy)),
301
+ (0, max(0, th - oy), min(ow, tw + ox), oh),
302
+ (max(0, tw - ox), max(0, th - oy), ow, oh),
303
+ ]
304
+ collected: list[np.ndarray] = []
305
+ for x1, y1, x2, y2 in tiles:
306
+ crop = image[y1:y2, x1:x2]
307
+ if crop.size == 0:
308
+ continue
309
+ d = self._raw_dets(crop, self.tile_conf)
310
+ if len(d):
311
+ d[:, 0] += x1
312
+ d[:, 1] += y1
313
+ d[:, 2] += x1
314
+ d[:, 3] += y1
315
+ collected.append(d)
316
+
317
+ # hflip tile pass
318
+ flipped = cv2.flip(image, 1)
319
+ for x1, y1, x2, y2 in tiles:
320
+ fx1 = ow - x2
321
+ fx2 = ow - x1
322
+ if fx2 <= fx1:
323
+ continue
324
+ crop = flipped[y1:y2, fx1:fx2]
325
+ if crop.size == 0:
326
+ continue
327
+ d = self._raw_dets(crop, self.tile_conf)
328
+ if len(d):
329
+ d_un = d.copy()
330
+ d_un[:, 0] = (ow - (d[:, 2] + fx1))
331
+ d_un[:, 2] = (ow - (d[:, 0] + fx1))
332
+ d_un[:, 1] = d[:, 1] + y1
333
+ d_un[:, 3] = d[:, 3] + y1
334
+ collected.append(d_un)
335
+
336
+ if not collected:
337
+ return primary
338
+
339
+ tile_dets = np.concatenate(collected, axis=0)
340
+ keep = self._hard_nms(tile_dets[:, :4], tile_dets[:, 4], 0.5)
341
+ tile_dets = tile_dets[keep]
342
+
343
+ # Novelty: drop tile boxes that overlap any primary box at IoU >= novelty_iou
344
+ if len(primary) > 0 and len(tile_dets) > 0:
345
+ mask = np.ones(len(tile_dets), dtype=bool)
346
+ for i in range(len(tile_dets)):
347
+ ious = self._box_iou_one_to_many(tile_dets[i, :4], primary[:, :4])
348
+ if len(ious) and np.max(ious) >= self.novelty_iou:
349
+ mask[i] = False
350
+ tile_dets = tile_dets[mask]
351
+
352
+ if len(tile_dets) == 0:
353
+ return primary
354
+
355
+ # Sanity filter: min/max size, aspect ratio
356
+ w = tile_dets[:, 2] - tile_dets[:, 0]
357
+ h = tile_dets[:, 3] - tile_dets[:, 1]
358
+ area = w * h
359
+ ar = np.maximum(w / np.maximum(h, 1e-6), h / np.maximum(w, 1e-6))
360
+ img_area = float(ow * oh)
361
+ ok = (w >= 6) & (h >= 6) & (area >= 36) & (area <= 0.5 * img_area) & (ar <= 10.0)
362
+ tile_dets = tile_dets[ok]
363
+ if len(tile_dets) == 0:
364
+ return primary
365
+
366
+ merged = np.concatenate([primary, tile_dets], axis=0)
367
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
368
+ merged = merged[keep]
369
+ if len(merged) > self.final_max_det:
370
+ merged = merged[np.argsort(-merged[:, 4])[: self.final_max_det]]
371
+ return merged
372
+
373
+ # ---------- single-image predict ----------
374
+ def _predict_single(self, image: ndarray) -> list[BoundingBox]:
375
+ if image is None or not isinstance(image, np.ndarray) or image.ndim != 3:
376
+ return []
377
+ if image.shape[0] <= 0 or image.shape[1] <= 0 or image.shape[2] != 3:
378
+ return []
379
+ if image.dtype != np.uint8:
380
+ image = image.astype(np.uint8)
381
+
382
+ primary = self._primary(image)
383
+ if len(primary) < self.sparse_threshold:
384
+ dets = self._tile_augment(image, primary)
385
+ else:
386
+ dets = primary
387
+
388
+ results: list[BoundingBox] = []
389
+ for row in dets:
390
+ x1, y1, x2, y2, conf = row.tolist()
391
+ if x2 <= x1 or y2 <= y1:
392
+ continue
393
+ results.append(
394
+ BoundingBox(
395
+ x1=int(math.floor(x1)),
396
+ y1=int(math.floor(y1)),
397
+ x2=int(math.ceil(x2)),
398
+ y2=int(math.ceil(y2)),
399
+ cls_id=0,
400
+ conf=float(conf),
401
+ )
402
+ )
403
+ return results
404
+
405
+ # ---------- chute entrypoint ----------
406
+ def predict_batch(
407
+ self,
408
+ batch_images: list[ndarray],
409
+ offset: int,
410
+ n_keypoints: int,
411
+ ) -> list[TVFrameResult]:
412
+ results: list[TVFrameResult] = []
413
+ for frame_number_in_batch, image in enumerate(batch_images):
414
+ try:
415
+ boxes = self._predict_single(image)
416
+ except Exception as e:
417
+ print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
418
+ boxes = []
419
+ results.append(
420
+ TVFrameResult(
421
+ frame_id=offset + frame_number_in_batch,
422
+ boxes=boxes,
423
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
424
+ )
425
+ )
426
+ return results
weights.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:85a9c463abc53cffa7e5607ab1a4ba5e7d60106fee911644e6aec238d436963e
3
+ size 19388678