BashirEmran commited on
Commit
cf9704d
·
verified ·
1 Parent(s): e687f30

scorevision: push artifact

Browse files
Files changed (1) hide show
  1. miner.py +149 -0
miner.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Turbo Vision miner for manak0/Detect-beverage-detect (cup / can / bottle).
2
+
3
+ CPU-only ONNX inference with 2 intra-op threads (the compliance checker measures
4
+ p95 latency on 2 vCPUs and re-runs the model to check that outputs are reproducible,
5
+ so everything here is deterministic numpy / onnxruntime CPU).
6
+
7
+ Repo layout:
8
+ miner.py this file
9
+ weights.onnx detector, input [1,3,S,S] RGB float 0..1, output [1, 4+3, N]
10
+ (cx, cy, w, h in input pixels, then per-class sigmoid scores)
11
+ config.json {"conf": [cup, can, bottle], "nms_iou": 0.5, "max_det": 100, "gate": 0.0}
12
+
13
+ No `from __future__ import annotations` here: pydantic must see real types when the
14
+ chute imports this file dynamically.
15
+ """
16
+ import json
17
+ from pathlib import Path
18
+
19
+ import cv2
20
+ import numpy as np
21
+ import onnxruntime as ort
22
+ from numpy import ndarray
23
+ from pydantic import BaseModel
24
+
25
+
26
+ class BoundingBox(BaseModel):
27
+ x1: int
28
+ y1: int
29
+ x2: int
30
+ y2: int
31
+ cls_id: int
32
+ conf: float
33
+
34
+
35
+ class TVFrameResult(BaseModel):
36
+ frame_id: int
37
+ boxes: list[BoundingBox]
38
+ keypoints: list[tuple[int, int]]
39
+
40
+
41
+ def _nms(boxes: ndarray, scores: ndarray, iou: float) -> list[int]:
42
+ order = np.argsort(-scores, kind="stable")
43
+ keep: list[int] = []
44
+ area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
45
+ while order.size:
46
+ i = int(order[0])
47
+ keep.append(i)
48
+ rest = order[1:]
49
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
50
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
51
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
52
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
53
+ inter = np.maximum(0, xx2 - xx1) * np.maximum(0, yy2 - yy1)
54
+ ov = inter / (area[i] + area[rest] - inter + 1e-7)
55
+ order = rest[ov <= iou]
56
+ return keep
57
+
58
+
59
+ class Miner:
60
+ def __init__(self, path_hf_repo: Path) -> None:
61
+ path_hf_repo = Path(path_hf_repo)
62
+ cfg_path = path_hf_repo / "config.json"
63
+ self.cfg = json.loads(cfg_path.read_text()) if cfg_path.exists() else {}
64
+ self.conf = np.array(self.cfg.get("conf", [0.4, 0.4, 0.4]), dtype=np.float32)
65
+ self.nms_iou = float(self.cfg.get("nms_iou", 0.5))
66
+ self.max_det = int(self.cfg.get("max_det", 100))
67
+ self.gate = float(self.cfg.get("gate", 0.0))
68
+ # per class: if no box passes conf, keep the class's best candidate when it scores >= rescue
69
+ # (1.0 = off). map50 averages over the classes in an image, so the first box of a class is worth most.
70
+ self.rescue = np.array(self.cfg.get("rescue", [1.0, 1.0, 1.0]), dtype=np.float32)
71
+ so = ort.SessionOptions()
72
+ so.intra_op_num_threads = 2
73
+ so.inter_op_num_threads = 1
74
+ so.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
75
+ so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
76
+ self.session = ort.InferenceSession(str(path_hf_repo / "weights.onnx"), so, providers=["CPUExecutionProvider"])
77
+ self.input_name = self.session.get_inputs()[0].name
78
+ self.size = int(self.session.get_inputs()[0].shape[-1])
79
+ self.buf = np.empty((1, 3, self.size, self.size), dtype=np.float32)
80
+ cv2.setNumThreads(1)
81
+ warm = np.zeros((1024, 1024, 3), dtype=np.uint8)
82
+ for _ in range(2):
83
+ self._raw(warm)
84
+
85
+ def __repr__(self) -> str:
86
+ return f"ONNX P3 detector {self.size}px conf={self.conf.tolist()}"
87
+
88
+ def _raw(self, image: ndarray):
89
+ """letterbox -> ONNX -> (boxes xyxy in original pixels, per-class scores [N,3])"""
90
+ h, w = image.shape[:2]
91
+ s = self.size
92
+ r = min(s / h, s / w)
93
+ nw, nh = int(round(w * r)), int(round(h * r))
94
+ top, left = (s - nh) // 2, (s - nw) // 2
95
+ img = cv2.resize(image, (nw, nh), interpolation=cv2.INTER_LINEAR) if (nw, nh) != (w, h) else image
96
+ self.buf.fill(114 / 255)
97
+ k = np.float32(1 / 255)
98
+ for c in range(3): # BGR -> RGB, HWC -> CHW, 0..1
99
+ np.multiply(img[:, :, 2 - c], k, out=self.buf[0, c, top:top + nh, left:left + nw], dtype=np.float32)
100
+ out = self.session.run(None, {self.input_name: self.buf})[0][0].T # [N, 4+nc]
101
+ xy, wh, sc = out[:, :2], out[:, 2:4], out[:, 4:]
102
+ boxes = np.concatenate([xy - wh / 2, xy + wh / 2], 1)
103
+ boxes[:, [0, 2]] = (boxes[:, [0, 2]] - left) / r
104
+ boxes[:, [1, 3]] = (boxes[:, [1, 3]] - top) / r
105
+ boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, w - 1)
106
+ boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, h - 1)
107
+ return boxes, sc
108
+
109
+ def candidates(self, image: ndarray, floor: float = 0.05):
110
+ """All per-class NMS survivors above `floor` (used for threshold tuning)."""
111
+ boxes, sc = self._raw(image)
112
+ cls = sc.argmax(1)
113
+ conf = sc[np.arange(len(sc)), cls]
114
+ m = conf >= floor
115
+ boxes, conf, cls = boxes[m], conf[m], cls[m]
116
+ keep: list[int] = []
117
+ for c in range(sc.shape[1]):
118
+ idx = np.flatnonzero(cls == c)
119
+ if idx.size:
120
+ keep.extend(idx[_nms(boxes[idx], conf[idx], self.nms_iou)].tolist())
121
+ keep = sorted(keep, key=lambda i: -conf[i])
122
+ return boxes[keep], conf[keep], cls[keep]
123
+
124
+ def _detect(self, image: ndarray) -> list[BoundingBox]:
125
+ boxes, conf, cls = self.candidates(image, floor=float(min(self.conf.min(), self.rescue.min(), 0.05)))
126
+ if conf.size == 0 or float(conf.max()) < self.gate:
127
+ return []
128
+ m = conf >= self.conf[cls]
129
+ for c in range(len(self.rescue)):
130
+ if self.rescue[c] < 1.0 and not m[cls == c].any():
131
+ idx = np.flatnonzero((cls == c) & (conf >= self.rescue[c]))
132
+ if idx.size:
133
+ m[idx[0]] = True # candidates are sorted by score, so idx[0] is the class's best
134
+ boxes, conf, cls = boxes[m][: self.max_det], conf[m][: self.max_det], cls[m][: self.max_det]
135
+ q = np.round(boxes).astype(int)
136
+ ok = (q[:, 2] > q[:, 0]) & (q[:, 3] > q[:, 1])
137
+ return [BoundingBox(x1=int(a[0]), y1=int(a[1]), x2=int(a[2]), y2=int(a[3]), cls_id=int(c), conf=float(f))
138
+ for a, c, f in zip(q[ok], cls[ok], conf[ok])]
139
+
140
+ def predict_batch(self, batch_images: list[ndarray], offset: int, n_keypoints: int) -> list[TVFrameResult]:
141
+ results = []
142
+ for i, im in enumerate(batch_images):
143
+ try:
144
+ boxes = self._detect(im)
145
+ except Exception as e: # one bad frame must not fail the batch
146
+ print(f"frame {offset + i} failed: {e}")
147
+ boxes = []
148
+ results.append(TVFrameResult(frame_id=offset + i, boxes=boxes, keypoints=[(0, 0)] * n_keypoints))
149
+ return results