car-wash miner v1
Browse filesFiles changed (3)
- carwash.onnxAdded+3
- chute_config.ymlAdded+21
- miner.pyAdded+170
123version https://git-lfs.github.com/spec/v1oid sha256:261da6e9f7751c04e7e5aa7e78e0977d444b1ffa31447332c2d79ec1d96cf75dsize 10606431123456789101112131415161718192021Image: from_base: parachutes/python:3.12 run_command: - pip install --upgrade setuptools wheel - pip install onnxruntime==1.26.0 opencv-python-headless numpy pydantic set_workdir: /app
NodeSelector: gpu_count: 1 min_vram_gb_per_gpu: 8 exclude: - b200 - h200 - h20 - mi300x
Chute: shutdown_after_seconds: 300 concurrency: 4 max_instances: 5 scaling_threshold: 0.5123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170"""TurboVision miner for element `manak0/Detect-car-wash` — ONNX / CPU-safe.
Why ONNX-only: the latency-loop compliance checker (branch `latency-loop`) loads THISminer.py in a sandbox whose image has ONLY onnxruntime + cv2 + numpy + pydantic(NO torch, NO ultralytics), forbids `.pt/.pth/.safetensors` files, forbids importingsocket/urllib/http/subprocess and calling open()/eval/exec, blocks the network duringinference, caps memory at 8 GiB, and requires the repo to contain a `.onnx` model.It times `predict_batch` on CPU and needs p95 <= element.latency_p95_ms (target 100 ms),and it re-checks that these outputs match your submitted responses at IoU >= 0.85.
So: pure onnxruntime, deterministic, small input size. Classes MUST be in manifestorder (cls_id == index): 0=broom 1=drainage gate 2=nozzle 3=track."""from pathlib import Pathimport os
import numpy as npimport cv2import onnxruntime as ortfrom pydantic import BaseModel
CLASSES = ["broom", "drainage gate", "nozzle", "track"]
CONF = float(os.environ.get("CARWASH_CONF", "0.15")) # global floor; per-class overrides below# Per-class confidence floors (index == cls_id). Each object sits at its own map50/FP# sweet spot. Override via CARWASH_CONF_PER_CLASS="0.30,0.45,0.20,0.35"._pc = os.environ.get("CARWASH_CONF_PER_CLASS", "")PER_CLASS_CONF = ([float(x) for x in _pc.split(",")] if _pc else [0.20, 0.40, 0.20, 0.20])IOU_NMS = float(os.environ.get("CARWASH_IOU", "0.6"))MAX_DET = int(os.environ.get("CARWASH_MAX_DET", "50"))MODEL_FILE = os.environ.get("CARWASH_MODEL", "carwash.onnx")
class BoundingBox(BaseModel): x1: int y1: int x2: int y2: int cls_id: int conf: float
class Polygon(BaseModel): cls_id: int conf: float points: list[tuple[int, int]]
class TVFrameResult(BaseModel): frame_id: int boxes: list[BoundingBox] | None = None polygons: list[Polygon] | None = None keypoints: list[tuple[int, int]] | None = None
def _letterbox(img: np.ndarray, new_shape: tuple[int, int]) -> tuple[np.ndarray, float, float, float]: """Resize+pad BGR image to new_shape (H,W), keep aspect. Return (img, ratio, pad_w, pad_h).""" h, w = img.shape[:2] nh, nw = new_shape r = min(nh / h, nw / w) uw, uh = int(round(w * r)), int(round(h * r)) resized = cv2.resize(img, (uw, uh), interpolation=cv2.INTER_LINEAR) pad_w, pad_h = (nw - uw) / 2, (nh - uh) / 2 top, bottom = int(round(pad_h - 0.1)), int(round(pad_h + 0.1)) left, right = int(round(pad_w - 0.1)), int(round(pad_w + 0.1)) out = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114)) return out, r, left, top
def _nms(boxes: np.ndarray, scores: np.ndarray, iou_thr: float) -> list[int]: if len(boxes) == 0: return [] x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] areas = np.maximum(0, x2 - x1) * np.maximum(0, y2 - y1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(int(i)) if order.size == 1: break xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) inter = np.maximum(0, xx2 - xx1) * np.maximum(0, yy2 - yy1) iou = inter / (areas[i] + areas[order[1:]] - inter + 1e-9) order = order[1:][iou <= iou_thr] return keep
class Miner: def __init__(self, path_hf_repo: Path) -> None: model_path = str(Path(path_hf_repo) / MODEL_FILE) providers = os.environ.get("CARWASH_PROVIDERS", "CPUExecutionProvider").split(",") avail = ort.get_available_providers() providers = [p for p in providers if p in avail] or ["CPUExecutionProvider"] so = ort.SessionOptions() so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL so.intra_op_num_threads = int(os.environ.get("CARWASH_THREADS", "0")) # 0 = ORT default self.sess = ort.InferenceSession(model_path, sess_options=so, providers=providers) self.inp = self.sess.get_inputs()[0] shape = self.inp.shape # [1,3,H,W]; may contain strings if dynamic self.H = int(shape[2]) if isinstance(shape[2], int) else 640 self.W = int(shape[3]) if isinstance(shape[3], int) else 640 self.nc = len(CLASSES) # warmup so first real call isn't a cold-start outlier in p95 dummy = np.zeros((1, 3, self.H, self.W), dtype=np.float32) self.sess.run(None, {self.inp.name: dummy}) print(f"✅ Car-wash ONNX loaded {MODEL_FILE} input={self.H}x{self.W} providers={providers} conf={CONF}")
def __repr__(self) -> str: return f"CarWash ONNX ({MODEL_FILE}) {self.H}x{self.W} classes={CLASSES} conf={CONF}"
def _preprocess(self, img_bgr: np.ndarray): lb, r, pad_w, pad_h = _letterbox(img_bgr, (self.H, self.W)) rgb = lb[:, :, ::-1].astype(np.float32) / 255.0 # BGR->RGB, 0-1 (manifest norm rgb-01) chw = np.transpose(rgb, (2, 0, 1)) return chw, r, pad_w, pad_h
def _postprocess(self, out: np.ndarray, r: float, pad_w: float, pad_h: float, orig_w: int, orig_h: int) -> list[BoundingBox]: # YOLOv8/11 detect ONNX head: (1, 4+nc, N) -> (N, 4+nc), xywh in input pixels pred = out[0] if pred.shape[0] == (4 + self.nc): pred = pred.transpose(1, 0) boxes_xywh = pred[:, :4] cls_scores = pred[:, 4:4 + self.nc] cls_id = cls_scores.argmax(1) conf = cls_scores.max(1) # per-class confidence floor thr = np.array(PER_CLASS_CONF, dtype=np.float32)[cls_id] m = conf >= thr if not m.any(): return [] boxes_xywh, cls_id, conf = boxes_xywh[m], cls_id[m], conf[m] cx, cy, w, h = boxes_xywh[:, 0], boxes_xywh[:, 1], boxes_xywh[:, 2], boxes_xywh[:, 3] x1 = (cx - w / 2 - pad_w) / r y1 = (cy - h / 2 - pad_h) / r x2 = (cx + w / 2 - pad_w) / r y2 = (cy + h / 2 - pad_h) / r xyxy = np.stack([x1, y1, x2, y2], 1) xyxy[:, [0, 2]] = xyxy[:, [0, 2]].clip(0, orig_w) xyxy[:, [1, 3]] = xyxy[:, [1, 3]].clip(0, orig_h) out_boxes: list[BoundingBox] = [] for c in np.unique(cls_id): idx = np.where(cls_id == c)[0] keep = _nms(xyxy[idx], conf[idx], IOU_NMS) for k in keep: j = idx[k] out_boxes.append(BoundingBox( x1=int(xyxy[j, 0]), y1=int(xyxy[j, 1]), x2=int(xyxy[j, 2]), y2=int(xyxy[j, 3]), cls_id=int(c), conf=float(conf[j]), )) out_boxes.sort(key=lambda b: b.conf, reverse=True) return out_boxes[:MAX_DET]
def predict_batch(self, batch_images, offset: int, n_keypoints: int) -> list[TVFrameResult]: # Run one frame at a time: the exported ONNX has a fixed batch dim of 1, and # per-challenge latency is what the checker measures, so keep each run minimal. results: list[TVFrameResult] = [] for i, img in enumerate(batch_images): chw, r, pw, ph = self._preprocess(img) inp = np.ascontiguousarray(chw[None], dtype=np.float32) out = self.sess.run(None, {self.inp.name: inp})[0] # (1, 4+nc, N) boxes = self._postprocess(out, r, pw, ph, img.shape[1], img.shape[0]) results.append(TVFrameResult(frame_id=offset + i, boxes=boxes, polygons=[], keypoints=[])) return resultscarwash.onnxAdded+3
chute_config.ymlAdded+21
miner.pyAdded+170