alfred8995 commited on
Commit
5620b75
·
verified ·
1 Parent(s): 3ae0e81

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. __pycache__/miner.cpython-310.pyc +0 -0
  2. chute_config.yml +19 -0
  3. miner.py +422 -0
  4. weights.onnx +3 -0
__pycache__/miner.cpython-310.pyc ADDED
Binary file (13.4 kB). View file
 
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
+ set_workdir: /app
8
+
9
+ NodeSelector:
10
+ gpu_count: 1
11
+ min_vram_gb_per_gpu: 16
12
+ max_hourly_price_per_gpu: 1
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,422 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ SIZE = 1280
27
+
28
+
29
+ class Miner:
30
+ def __init__(self, path_hf_repo: Path) -> None:
31
+ model_path = path_hf_repo / "weights.onnx"
32
+ cn_path = model_path.with_name("class_names.txt")
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:
41
+ self.class_names = ["numberplate"]
42
+ print("ORT version:", ort.__version__)
43
+
44
+ try:
45
+ ort.preload_dlls()
46
+ print("onnxruntime.preload_dlls() success")
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
+
62
+ sess_options = ort.SessionOptions()
63
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
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():
85
+ print("OUTPUT:", out.name, out.shape, out.type)
86
+
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
+ self.conf_thres = 0.35
95
+ self.iou_thres = 0.48
96
+ self.sigma = 0.5
97
+ self.max_det = 300
98
+
99
+ self.sparse_threshold = 3 # fire tiles only if primary returns < this
100
+ self.tile_conf = 0.39
101
+ self.tile_overlap = 0.12
102
+ self.novelty_iou = 0.18
103
+ self.final_max_det = 16
104
+ self.tile_use_hflip = False # skip hflip tile pass to save ~4 forwards
105
+
106
+ self.use_tta = True
107
+
108
+ print(f"ONNX model loaded from: {model_path}")
109
+ print(f"ONNX providers: {self.session.get_providers()}")
110
+ print(f"ONNX input: name={self.input_name}, shape={self.input_shape}")
111
+
112
+ def __repr__(self) -> str:
113
+ return (
114
+ f"ONNXRuntime(session={type(self.session).__name__}, "
115
+ f"providers={self.session.get_providers()})"
116
+ )
117
+
118
+ @staticmethod
119
+ def _safe_dim(value, default: int) -> int:
120
+ return value if isinstance(value, int) and value > 0 else default
121
+
122
+ # ---------- image preprocessing ----------
123
+ def _letterbox(
124
+ self,
125
+ image: ndarray,
126
+ new_shape: tuple[int, int],
127
+ color=(114, 114, 114),
128
+ ) -> tuple[ndarray, float, tuple[float, float]]:
129
+ h, w = image.shape[:2]
130
+ new_w, new_h = new_shape
131
+ ratio = min(new_w / w, new_h / h)
132
+ resized_w = int(round(w * ratio))
133
+ resized_h = int(round(h * ratio))
134
+ if (resized_w, resized_h) != (w, h):
135
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
136
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
137
+ dw = (new_w - resized_w) / 2.0
138
+ dh = (new_h - resized_h) / 2.0
139
+ left = int(round(dw - 0.1))
140
+ right = int(round(dw + 0.1))
141
+ top = int(round(dh - 0.1))
142
+ bottom = int(round(dh + 0.1))
143
+ padded = cv2.copyMakeBorder(
144
+ image, top, bottom, left, right,
145
+ borderType=cv2.BORDER_CONSTANT, value=color,
146
+ )
147
+ return padded, ratio, (dw, dh)
148
+
149
+ def _preprocess(self, image: ndarray):
150
+ img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
151
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
152
+ img = np.transpose(img, (2, 0, 1))[None, ...]
153
+ return np.ascontiguousarray(img, dtype=np.float32), ratio, pad
154
+
155
+ @staticmethod
156
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
157
+ w, h = image_size
158
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
159
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
160
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
161
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
162
+ return boxes
163
+
164
+ # ---------- NMS primitives ----------
165
+ @staticmethod
166
+ def _hard_nms(boxes: np.ndarray, scores: np.ndarray, iou_thresh: float) -> np.ndarray:
167
+ N = len(boxes)
168
+ if N == 0:
169
+ return np.array([], dtype=np.intp)
170
+ boxes = np.asarray(boxes, dtype=np.float32)
171
+ scores = np.asarray(scores, dtype=np.float32)
172
+ order = np.argsort(-scores)
173
+ keep: list[int] = []
174
+ while len(order):
175
+ i = int(order[0])
176
+ keep.append(i)
177
+ if len(order) == 1:
178
+ break
179
+ rest = order[1:]
180
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
181
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
182
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
183
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
184
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
185
+ area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
186
+ area_r = (boxes[rest, 2] - boxes[rest, 0]) * (boxes[rest, 3] - boxes[rest, 1])
187
+ iou = inter / (area_i + area_r - inter + 1e-7)
188
+ order = rest[iou <= iou_thresh]
189
+ return np.array(keep, dtype=np.intp)
190
+
191
+ def _soft_nms(
192
+ self,
193
+ boxes: np.ndarray,
194
+ scores: np.ndarray,
195
+ sigma: float,
196
+ score_thresh: float = 0.01,
197
+ ) -> tuple[np.ndarray, np.ndarray]:
198
+ N = len(boxes)
199
+ if N == 0:
200
+ return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
201
+ boxes = boxes.astype(np.float32, copy=True)
202
+ scores = scores.astype(np.float32, copy=True)
203
+ order = np.arange(N)
204
+ for i in range(N):
205
+ max_pos = i + int(np.argmax(scores[i:]))
206
+ boxes[[i, max_pos]] = boxes[[max_pos, i]]
207
+ scores[[i, max_pos]] = scores[[max_pos, i]]
208
+ order[[i, max_pos]] = order[[max_pos, i]]
209
+ if i + 1 >= N:
210
+ break
211
+ xx1 = np.maximum(boxes[i, 0], boxes[i + 1:, 0])
212
+ yy1 = np.maximum(boxes[i, 1], boxes[i + 1:, 1])
213
+ xx2 = np.minimum(boxes[i, 2], boxes[i + 1:, 2])
214
+ yy2 = np.minimum(boxes[i, 3], boxes[i + 1:, 3])
215
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
216
+ area_i = float(
217
+ (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
218
+ )
219
+ areas_j = (
220
+ np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
221
+ * np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
222
+ )
223
+ iou = inter / (area_i + areas_j - inter + 1e-7)
224
+ scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
225
+ mask = scores > score_thresh
226
+ return order[mask], scores[mask]
227
+
228
+ @staticmethod
229
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
230
+ if len(boxes) == 0:
231
+ return np.zeros(0, dtype=np.float32)
232
+ xx1 = np.maximum(box[0], boxes[:, 0])
233
+ yy1 = np.maximum(box[1], boxes[:, 1])
234
+ xx2 = np.minimum(box[2], boxes[:, 2])
235
+ yy2 = np.minimum(box[3], boxes[:, 3])
236
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
237
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
238
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
239
+ return inter / (area_a + area_b - inter + 1e-7)
240
+
241
+ # ---------- raw-dets helper ----------
242
+ def _raw_dets(self, image: ndarray, conf: float) -> np.ndarray:
243
+ """Run a single forward pass and return [N, 5] dets in ORIGINAL image coords."""
244
+ x, ratio, (dw, dh) = self._preprocess(image)
245
+ out = self.session.run(self.output_names, {self.input_name: x})[0]
246
+ if out.ndim == 3:
247
+ out = out[0]
248
+ if out.shape[1] < 5:
249
+ return np.zeros((0, 5), dtype=np.float32)
250
+ boxes = out[:, :4].astype(np.float32)
251
+ scores = out[:, 4].astype(np.float32)
252
+ keep = scores >= conf
253
+ boxes, scores = boxes[keep], scores[keep]
254
+ if len(boxes) == 0:
255
+ return np.zeros((0, 5), dtype=np.float32)
256
+ boxes[:, [0, 2]] -= dw
257
+ boxes[:, [1, 3]] -= dh
258
+ boxes /= ratio
259
+ oh, ow = image.shape[:2]
260
+ boxes = self._clip_boxes(boxes, (ow, oh))
261
+ return np.concatenate([boxes, scores[:, None]], axis=1)
262
+
263
+ # ---------- primary pass: soft-NMS + hflip TTA ----------
264
+ def _primary(self, image: ndarray) -> np.ndarray:
265
+ d1 = self._raw_dets(image, self.conf_thres)
266
+ flipped = cv2.flip(image, 1)
267
+ d2 = self._raw_dets(flipped, self.conf_thres)
268
+ if len(d2):
269
+ w = image.shape[1]
270
+ x1 = w - d2[:, 2]
271
+ x2 = w - d2[:, 0]
272
+ d2 = np.stack([x1, d2[:, 1], x2, d2[:, 3], d2[:, 4]], axis=1)
273
+ all_d = np.concatenate([d1, d2], axis=0) if len(d2) else d1
274
+ if len(all_d) == 0:
275
+ return np.zeros((0, 5), dtype=np.float32)
276
+ # soft-NMS, then hard-NMS
277
+ keep_idx, scores = self._soft_nms(all_d[:, :4].copy(), all_d[:, 4].copy(), sigma=self.sigma)
278
+ if len(keep_idx) == 0:
279
+ return np.zeros((0, 5), dtype=np.float32)
280
+ merged = np.concatenate([all_d[keep_idx, :4], scores[:, None]], axis=1)
281
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
282
+ merged = merged[keep]
283
+ if len(merged) > self.max_det:
284
+ merged = merged[np.argsort(-merged[:, 4])[: self.max_det]]
285
+ return merged
286
+
287
+ # ---------- conditional tile pass ----------
288
+ def _tile_augment(self, image: ndarray, primary: np.ndarray) -> np.ndarray:
289
+ """Run 2x2 overlapping tiles + hflip, novelty-merge into primary."""
290
+ oh, ow = image.shape[:2]
291
+ tw, th = ow // 2, oh // 2
292
+ ox, oy = int(tw * self.tile_overlap), int(th * self.tile_overlap)
293
+ tiles = [
294
+ (0, 0, min(ow, tw + ox), min(oh, th + oy)),
295
+ (max(0, tw - ox), 0, ow, min(oh, th + oy)),
296
+ (0, max(0, th - oy), min(ow, tw + ox), oh),
297
+ (max(0, tw - ox), max(0, th - oy), ow, oh),
298
+ ]
299
+ collected: list[np.ndarray] = []
300
+ for x1, y1, x2, y2 in tiles:
301
+ crop = image[y1:y2, x1:x2]
302
+ if crop.size == 0:
303
+ continue
304
+ d = self._raw_dets(crop, self.tile_conf)
305
+ if len(d):
306
+ d[:, 0] += x1
307
+ d[:, 1] += y1
308
+ d[:, 2] += x1
309
+ d[:, 3] += y1
310
+ collected.append(d)
311
+
312
+ # hflip tile pass (skipped when tile_use_hflip=False — saves 4 ONNX forwards)
313
+ if self.tile_use_hflip:
314
+ flipped = cv2.flip(image, 1)
315
+ for x1, y1, x2, y2 in tiles:
316
+ fx1 = ow - x2
317
+ fx2 = ow - x1
318
+ if fx2 <= fx1:
319
+ continue
320
+ crop = flipped[y1:y2, fx1:fx2]
321
+ if crop.size == 0:
322
+ continue
323
+ d = self._raw_dets(crop, self.tile_conf)
324
+ if len(d):
325
+ d_un = d.copy()
326
+ d_un[:, 0] = (ow - (d[:, 2] + fx1))
327
+ d_un[:, 2] = (ow - (d[:, 0] + fx1))
328
+ d_un[:, 1] = d[:, 1] + y1
329
+ d_un[:, 3] = d[:, 3] + y1
330
+ collected.append(d_un)
331
+
332
+ if not collected:
333
+ return primary
334
+
335
+ tile_dets = np.concatenate(collected, axis=0)
336
+ keep = self._hard_nms(tile_dets[:, :4], tile_dets[:, 4], 0.5)
337
+ tile_dets = tile_dets[keep]
338
+
339
+ # Novelty: drop tile boxes that overlap any primary box at IoU >= novelty_iou
340
+ if len(primary) > 0 and len(tile_dets) > 0:
341
+ mask = np.ones(len(tile_dets), dtype=bool)
342
+ for i in range(len(tile_dets)):
343
+ ious = self._box_iou_one_to_many(tile_dets[i, :4], primary[:, :4])
344
+ if len(ious) and np.max(ious) >= self.novelty_iou:
345
+ mask[i] = False
346
+ tile_dets = tile_dets[mask]
347
+
348
+ if len(tile_dets) == 0:
349
+ return primary
350
+
351
+ # Sanity filter: min/max size, aspect ratio
352
+ w = tile_dets[:, 2] - tile_dets[:, 0]
353
+ h = tile_dets[:, 3] - tile_dets[:, 1]
354
+ area = w * h
355
+ ar = np.maximum(w / np.maximum(h, 1e-6), h / np.maximum(w, 1e-6))
356
+ img_area = float(ow * oh)
357
+ ok = (w >= 7) & (h >= 7) & (area >= 85) & (area <= 0.5 * img_area) & (ar <= 10.0)
358
+ tile_dets = tile_dets[ok]
359
+ if len(tile_dets) == 0:
360
+ return primary
361
+
362
+ merged = np.concatenate([primary, tile_dets], axis=0)
363
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
364
+ merged = merged[keep]
365
+ if len(merged) > self.final_max_det:
366
+ merged = merged[np.argsort(-merged[:, 4])[: self.final_max_det]]
367
+ return merged
368
+
369
+ # ---------- single-image predict ----------
370
+ def _predict_single(self, image: ndarray) -> list[BoundingBox]:
371
+ if image is None or not isinstance(image, np.ndarray) or image.ndim != 3:
372
+ return []
373
+ if image.shape[0] <= 0 or image.shape[1] <= 0 or image.shape[2] != 3:
374
+ return []
375
+ if image.dtype != np.uint8:
376
+ image = image.astype(np.uint8)
377
+
378
+ primary = self._primary(image)
379
+ if len(primary) < self.sparse_threshold:
380
+ dets = self._tile_augment(image, primary)
381
+ else:
382
+ dets = primary
383
+
384
+ results: list[BoundingBox] = []
385
+ for row in dets:
386
+ x1, y1, x2, y2, conf = row.tolist()
387
+ if x2 <= x1 or y2 <= y1:
388
+ continue
389
+ results.append(
390
+ BoundingBox(
391
+ x1=int(math.floor(x1)),
392
+ y1=int(math.floor(y1)),
393
+ x2=int(math.ceil(x2)),
394
+ y2=int(math.ceil(y2)),
395
+ cls_id=0,
396
+ conf=float(conf),
397
+ )
398
+ )
399
+ return results
400
+
401
+ # ---------- chute entrypoint ----------
402
+ def predict_batch(
403
+ self,
404
+ batch_images: list[ndarray],
405
+ offset: int,
406
+ n_keypoints: int,
407
+ ) -> list[TVFrameResult]:
408
+ results: list[TVFrameResult] = []
409
+ for frame_number_in_batch, image in enumerate(batch_images):
410
+ try:
411
+ boxes = self._predict_single(image)
412
+ except Exception as e:
413
+ print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
414
+ boxes = []
415
+ results.append(
416
+ TVFrameResult(
417
+ frame_id=offset + frame_number_in_batch,
418
+ boxes=boxes,
419
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
420
+ )
421
+ )
422
+ return results
weights.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6cbecfb7d55f2a337d173b87ee2c92770b8e041efd4b5f700e59766ebe2af2a5
3
+ size 19405546