car-wash miner v1
Browse files- carwash.onnx +3 -0
- chute_config.yml +21 -0
- miner.py +170 -0
carwash.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:261da6e9f7751c04e7e5aa7e78e0977d444b1ffa31447332c2d79ec1d96cf75d
|
| 3 |
+
size 10606431
|
chute_config.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Image:
|
| 2 |
+
from_base: parachutes/python:3.12
|
| 3 |
+
run_command:
|
| 4 |
+
- pip install --upgrade setuptools wheel
|
| 5 |
+
- pip install onnxruntime==1.26.0 opencv-python-headless numpy pydantic
|
| 6 |
+
set_workdir: /app
|
| 7 |
+
|
| 8 |
+
NodeSelector:
|
| 9 |
+
gpu_count: 1
|
| 10 |
+
min_vram_gb_per_gpu: 8
|
| 11 |
+
exclude:
|
| 12 |
+
- b200
|
| 13 |
+
- h200
|
| 14 |
+
- h20
|
| 15 |
+
- mi300x
|
| 16 |
+
|
| 17 |
+
Chute:
|
| 18 |
+
shutdown_after_seconds: 300
|
| 19 |
+
concurrency: 4
|
| 20 |
+
max_instances: 5
|
| 21 |
+
scaling_threshold: 0.5
|
miner.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
TurboVision miner for element `manak0/Detect-car-wash` — ONNX / CPU-safe.
|
| 3 |
+
|
| 4 |
+
Why ONNX-only: the latency-loop compliance checker (branch `latency-loop`) loads THIS
|
| 5 |
+
miner.py in a sandbox whose image has ONLY onnxruntime + cv2 + numpy + pydantic
|
| 6 |
+
(NO torch, NO ultralytics), forbids `.pt/.pth/.safetensors` files, forbids importing
|
| 7 |
+
socket/urllib/http/subprocess and calling open()/eval/exec, blocks the network during
|
| 8 |
+
inference, caps memory at 8 GiB, and requires the repo to contain a `.onnx` model.
|
| 9 |
+
It times `predict_batch` on CPU and needs p95 <= element.latency_p95_ms (target 100 ms),
|
| 10 |
+
and it re-checks that these outputs match your submitted responses at IoU >= 0.85.
|
| 11 |
+
|
| 12 |
+
So: pure onnxruntime, deterministic, small input size. Classes MUST be in manifest
|
| 13 |
+
order (cls_id == index): 0=broom 1=drainage gate 2=nozzle 3=track.
|
| 14 |
+
"""
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
import os
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
import cv2
|
| 20 |
+
import onnxruntime as ort
|
| 21 |
+
from pydantic import BaseModel
|
| 22 |
+
|
| 23 |
+
CLASSES = ["broom", "drainage gate", "nozzle", "track"]
|
| 24 |
+
|
| 25 |
+
CONF = float(os.environ.get("CARWASH_CONF", "0.15")) # global floor; per-class overrides below
|
| 26 |
+
# Per-class confidence floors (index == cls_id). Each object sits at its own map50/FP
|
| 27 |
+
# sweet spot. Override via CARWASH_CONF_PER_CLASS="0.30,0.45,0.20,0.35".
|
| 28 |
+
_pc = os.environ.get("CARWASH_CONF_PER_CLASS", "")
|
| 29 |
+
PER_CLASS_CONF = ([float(x) for x in _pc.split(",")] if _pc else [0.20, 0.40, 0.20, 0.20])
|
| 30 |
+
IOU_NMS = float(os.environ.get("CARWASH_IOU", "0.6"))
|
| 31 |
+
MAX_DET = int(os.environ.get("CARWASH_MAX_DET", "50"))
|
| 32 |
+
MODEL_FILE = os.environ.get("CARWASH_MODEL", "carwash.onnx")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class BoundingBox(BaseModel):
|
| 36 |
+
x1: int
|
| 37 |
+
y1: int
|
| 38 |
+
x2: int
|
| 39 |
+
y2: int
|
| 40 |
+
cls_id: int
|
| 41 |
+
conf: float
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class Polygon(BaseModel):
|
| 45 |
+
cls_id: int
|
| 46 |
+
conf: float
|
| 47 |
+
points: list[tuple[int, int]]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class TVFrameResult(BaseModel):
|
| 51 |
+
frame_id: int
|
| 52 |
+
boxes: list[BoundingBox] | None = None
|
| 53 |
+
polygons: list[Polygon] | None = None
|
| 54 |
+
keypoints: list[tuple[int, int]] | None = None
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _letterbox(img: np.ndarray, new_shape: tuple[int, int]) -> tuple[np.ndarray, float, float, float]:
|
| 58 |
+
"""Resize+pad BGR image to new_shape (H,W), keep aspect. Return (img, ratio, pad_w, pad_h)."""
|
| 59 |
+
h, w = img.shape[:2]
|
| 60 |
+
nh, nw = new_shape
|
| 61 |
+
r = min(nh / h, nw / w)
|
| 62 |
+
uw, uh = int(round(w * r)), int(round(h * r))
|
| 63 |
+
resized = cv2.resize(img, (uw, uh), interpolation=cv2.INTER_LINEAR)
|
| 64 |
+
pad_w, pad_h = (nw - uw) / 2, (nh - uh) / 2
|
| 65 |
+
top, bottom = int(round(pad_h - 0.1)), int(round(pad_h + 0.1))
|
| 66 |
+
left, right = int(round(pad_w - 0.1)), int(round(pad_w + 0.1))
|
| 67 |
+
out = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114))
|
| 68 |
+
return out, r, left, top
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _nms(boxes: np.ndarray, scores: np.ndarray, iou_thr: float) -> list[int]:
|
| 72 |
+
if len(boxes) == 0:
|
| 73 |
+
return []
|
| 74 |
+
x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
|
| 75 |
+
areas = np.maximum(0, x2 - x1) * np.maximum(0, y2 - y1)
|
| 76 |
+
order = scores.argsort()[::-1]
|
| 77 |
+
keep = []
|
| 78 |
+
while order.size > 0:
|
| 79 |
+
i = order[0]
|
| 80 |
+
keep.append(int(i))
|
| 81 |
+
if order.size == 1:
|
| 82 |
+
break
|
| 83 |
+
xx1 = np.maximum(x1[i], x1[order[1:]])
|
| 84 |
+
yy1 = np.maximum(y1[i], y1[order[1:]])
|
| 85 |
+
xx2 = np.minimum(x2[i], x2[order[1:]])
|
| 86 |
+
yy2 = np.minimum(y2[i], y2[order[1:]])
|
| 87 |
+
inter = np.maximum(0, xx2 - xx1) * np.maximum(0, yy2 - yy1)
|
| 88 |
+
iou = inter / (areas[i] + areas[order[1:]] - inter + 1e-9)
|
| 89 |
+
order = order[1:][iou <= iou_thr]
|
| 90 |
+
return keep
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
class Miner:
|
| 94 |
+
def __init__(self, path_hf_repo: Path) -> None:
|
| 95 |
+
model_path = str(Path(path_hf_repo) / MODEL_FILE)
|
| 96 |
+
providers = os.environ.get("CARWASH_PROVIDERS", "CPUExecutionProvider").split(",")
|
| 97 |
+
avail = ort.get_available_providers()
|
| 98 |
+
providers = [p for p in providers if p in avail] or ["CPUExecutionProvider"]
|
| 99 |
+
so = ort.SessionOptions()
|
| 100 |
+
so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
|
| 101 |
+
so.intra_op_num_threads = int(os.environ.get("CARWASH_THREADS", "0")) # 0 = ORT default
|
| 102 |
+
self.sess = ort.InferenceSession(model_path, sess_options=so, providers=providers)
|
| 103 |
+
self.inp = self.sess.get_inputs()[0]
|
| 104 |
+
shape = self.inp.shape # [1,3,H,W]; may contain strings if dynamic
|
| 105 |
+
self.H = int(shape[2]) if isinstance(shape[2], int) else 640
|
| 106 |
+
self.W = int(shape[3]) if isinstance(shape[3], int) else 640
|
| 107 |
+
self.nc = len(CLASSES)
|
| 108 |
+
# warmup so first real call isn't a cold-start outlier in p95
|
| 109 |
+
dummy = np.zeros((1, 3, self.H, self.W), dtype=np.float32)
|
| 110 |
+
self.sess.run(None, {self.inp.name: dummy})
|
| 111 |
+
print(f"✅ Car-wash ONNX loaded {MODEL_FILE} input={self.H}x{self.W} providers={providers} conf={CONF}")
|
| 112 |
+
|
| 113 |
+
def __repr__(self) -> str:
|
| 114 |
+
return f"CarWash ONNX ({MODEL_FILE}) {self.H}x{self.W} classes={CLASSES} conf={CONF}"
|
| 115 |
+
|
| 116 |
+
def _preprocess(self, img_bgr: np.ndarray):
|
| 117 |
+
lb, r, pad_w, pad_h = _letterbox(img_bgr, (self.H, self.W))
|
| 118 |
+
rgb = lb[:, :, ::-1].astype(np.float32) / 255.0 # BGR->RGB, 0-1 (manifest norm rgb-01)
|
| 119 |
+
chw = np.transpose(rgb, (2, 0, 1))
|
| 120 |
+
return chw, r, pad_w, pad_h
|
| 121 |
+
|
| 122 |
+
def _postprocess(self, out: np.ndarray, r: float, pad_w: float, pad_h: float,
|
| 123 |
+
orig_w: int, orig_h: int) -> list[BoundingBox]:
|
| 124 |
+
# YOLOv8/11 detect ONNX head: (1, 4+nc, N) -> (N, 4+nc), xywh in input pixels
|
| 125 |
+
pred = out[0]
|
| 126 |
+
if pred.shape[0] == (4 + self.nc):
|
| 127 |
+
pred = pred.transpose(1, 0)
|
| 128 |
+
boxes_xywh = pred[:, :4]
|
| 129 |
+
cls_scores = pred[:, 4:4 + self.nc]
|
| 130 |
+
cls_id = cls_scores.argmax(1)
|
| 131 |
+
conf = cls_scores.max(1)
|
| 132 |
+
# per-class confidence floor
|
| 133 |
+
thr = np.array(PER_CLASS_CONF, dtype=np.float32)[cls_id]
|
| 134 |
+
m = conf >= thr
|
| 135 |
+
if not m.any():
|
| 136 |
+
return []
|
| 137 |
+
boxes_xywh, cls_id, conf = boxes_xywh[m], cls_id[m], conf[m]
|
| 138 |
+
cx, cy, w, h = boxes_xywh[:, 0], boxes_xywh[:, 1], boxes_xywh[:, 2], boxes_xywh[:, 3]
|
| 139 |
+
x1 = (cx - w / 2 - pad_w) / r
|
| 140 |
+
y1 = (cy - h / 2 - pad_h) / r
|
| 141 |
+
x2 = (cx + w / 2 - pad_w) / r
|
| 142 |
+
y2 = (cy + h / 2 - pad_h) / r
|
| 143 |
+
xyxy = np.stack([x1, y1, x2, y2], 1)
|
| 144 |
+
xyxy[:, [0, 2]] = xyxy[:, [0, 2]].clip(0, orig_w)
|
| 145 |
+
xyxy[:, [1, 3]] = xyxy[:, [1, 3]].clip(0, orig_h)
|
| 146 |
+
out_boxes: list[BoundingBox] = []
|
| 147 |
+
for c in np.unique(cls_id):
|
| 148 |
+
idx = np.where(cls_id == c)[0]
|
| 149 |
+
keep = _nms(xyxy[idx], conf[idx], IOU_NMS)
|
| 150 |
+
for k in keep:
|
| 151 |
+
j = idx[k]
|
| 152 |
+
out_boxes.append(BoundingBox(
|
| 153 |
+
x1=int(xyxy[j, 0]), y1=int(xyxy[j, 1]),
|
| 154 |
+
x2=int(xyxy[j, 2]), y2=int(xyxy[j, 3]),
|
| 155 |
+
cls_id=int(c), conf=float(conf[j]),
|
| 156 |
+
))
|
| 157 |
+
out_boxes.sort(key=lambda b: b.conf, reverse=True)
|
| 158 |
+
return out_boxes[:MAX_DET]
|
| 159 |
+
|
| 160 |
+
def predict_batch(self, batch_images, offset: int, n_keypoints: int) -> list[TVFrameResult]:
|
| 161 |
+
# Run one frame at a time: the exported ONNX has a fixed batch dim of 1, and
|
| 162 |
+
# per-challenge latency is what the checker measures, so keep each run minimal.
|
| 163 |
+
results: list[TVFrameResult] = []
|
| 164 |
+
for i, img in enumerate(batch_images):
|
| 165 |
+
chw, r, pw, ph = self._preprocess(img)
|
| 166 |
+
inp = np.ascontiguousarray(chw[None], dtype=np.float32)
|
| 167 |
+
out = self.sess.run(None, {self.inp.name: inp})[0] # (1, 4+nc, N)
|
| 168 |
+
boxes = self._postprocess(out, r, pw, ph, img.shape[1], img.shape[0])
|
| 169 |
+
results.append(TVFrameResult(frame_id=offset + i, boxes=boxes, polygons=[], keypoints=[]))
|
| 170 |
+
return results
|