artur7236 commited on
Commit
8a316d2
·
verified ·
1 Parent(s): feaf29c

scorevision: push artifact

Browse files
Files changed (1) hide show
  1. miner.py +205 -0
miner.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from pathlib import Path
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] | None = None
23
+ polygons: list = []
24
+ keypoints: list[tuple[int, int]] | None = None
25
+
26
+
27
+ def _nms(boxes: ndarray, scores: ndarray, iou_thr: float) -> list[int]:
28
+ order = scores.argsort()[::-1]
29
+ areas = (boxes[:, 2] - boxes[:, 0]).clip(0) * (boxes[:, 3] - boxes[:, 1]).clip(0)
30
+ keep: list[int] = []
31
+ while order.size:
32
+ i = int(order[0])
33
+ keep.append(i)
34
+ if order.size == 1:
35
+ break
36
+ rest = order[1:]
37
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
38
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
39
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
40
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
41
+ inter = (xx2 - xx1).clip(0) * (yy2 - yy1).clip(0)
42
+ iou = inter / (areas[i] + areas[rest] - inter + 1e-9)
43
+ order = rest[iou <= iou_thr]
44
+ return keep
45
+
46
+
47
+ class Miner:
48
+ """Room-dirtiness detector: a single ONNX YOLO model run on CPU.
49
+
50
+ CPU execution keeps chute outputs identical to the validator's CPU compliance run.
51
+ """
52
+
53
+ def __init__(self, path_hf_repo: Path) -> None:
54
+ path_hf_repo = Path(path_hf_repo)
55
+ cfg = json.loads((path_hf_repo / "config.json").read_text())
56
+ self.classes: list[str] = cfg["classes"]
57
+ self.imgsz = int(cfg.get("imgsz", 640))
58
+ self.conf_thr = np.asarray(cfg.get("conf", [0.25] * len(self.classes)), dtype=np.float32)
59
+ nms = cfg.get("nms_iou", 0.6)
60
+ self.nms_iou = [float(v) for v in nms] if isinstance(nms, list) else [float(nms)] * len(self.classes)
61
+ self.max_det = int(cfg.get("max_det", 100))
62
+ mpc = cfg.get("max_per_class", self.max_det)
63
+ self.max_per_class = [int(v) for v in mpc] if isinstance(mpc, list) else [int(mpc)] * len(self.classes)
64
+ # per class: drop a box that lies mostly inside a bigger, higher-scoring box of the same class
65
+ # (one piece of a larger object boxed on its own); null disables
66
+ self.contain_drop = cfg.get("contain_drop", [None] * len(self.classes))
67
+ # size-aware rules: [area %, conf]; a far bin needs more confidence, a floor-wide spill must not be a faint guess,
68
+ # and an image whose best box is weak gets no boxes (without GT every false positive costs 0.1)
69
+ self.small_bin = cfg.get("small_bin")
70
+ self.big_spill = cfg.get("big_spill")
71
+ self.weak_image = float(cfg.get("weak_image", 0.0))
72
+
73
+ # OpenCV's own thread pool is not needed (one resize per frame) and every extra thread counts
74
+ # against the checker sandbox's process limit.
75
+ cv2.setNumThreads(1)
76
+ model_path = str(path_hf_repo / cfg.get("model", "model.onnx"))
77
+ threads = int(cfg.get("threads", 2))
78
+ try:
79
+ self.session = self._make_session(model_path, threads)
80
+ except Exception:
81
+ # The checker sandbox caps processes per user; if no worker thread can be spawned,
82
+ # run the graph on the calling thread instead of failing to load.
83
+ self.session = self._make_session(model_path, 1)
84
+ self.input_name = self.session.get_inputs()[0].name
85
+ warm = np.zeros((self.imgsz, self.imgsz, 3), dtype=np.uint8)
86
+ self._detect(warm)
87
+
88
+ @staticmethod
89
+ def _make_session(model_path: str, threads: int) -> ort.InferenceSession:
90
+ opts = ort.SessionOptions()
91
+ opts.intra_op_num_threads = threads
92
+ opts.inter_op_num_threads = 1
93
+ opts.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
94
+ opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
95
+ return ort.InferenceSession(model_path, sess_options=opts, providers=["CPUExecutionProvider"])
96
+
97
+ def __repr__(self) -> str:
98
+ return f"ONNX YOLO room-dirtiness imgsz={self.imgsz} classes={self.classes}"
99
+
100
+ def _letterbox(self, img: ndarray) -> tuple[ndarray, float, int, int]:
101
+ h, w = img.shape[:2]
102
+ r = min(self.imgsz / h, self.imgsz / w)
103
+ nh, nw = int(round(h * r)), int(round(w * r))
104
+ resized = cv2.resize(img, (nw, nh), interpolation=cv2.INTER_LINEAR)
105
+ top = (self.imgsz - nh) // 2
106
+ left = (self.imgsz - nw) // 2
107
+ canvas = np.full((self.imgsz, self.imgsz, 3), 114, dtype=np.uint8)
108
+ canvas[top : top + nh, left : left + nw] = resized
109
+ return canvas, r, left, top
110
+
111
+ def _postprocess(self, boxes: ndarray, scores: ndarray, cls: ndarray) -> tuple[ndarray, ndarray, ndarray]:
112
+ """Score-ordered dedup: containment rule and a per-class cap."""
113
+ order = np.argsort(-scores, kind="stable")
114
+ keep: list[int] = []
115
+ per_class: dict[int, int] = {}
116
+ for i in order:
117
+ c = int(cls[i])
118
+ frac = self.contain_drop[c] if c < len(self.contain_drop) else None
119
+ if frac is not None:
120
+ bx = boxes[i]
121
+ area_i = max(1e-6, (bx[2] - bx[0]) * (bx[3] - bx[1]))
122
+ dup = False
123
+ for k in keep:
124
+ if int(cls[k]) != c:
125
+ continue
126
+ kb = boxes[k]
127
+ iw = max(0.0, min(bx[2], kb[2]) - max(bx[0], kb[0]))
128
+ ih = max(0.0, min(bx[3], kb[3]) - max(bx[1], kb[1]))
129
+ area_k = max(1e-6, (kb[2] - kb[0]) * (kb[3] - kb[1]))
130
+ if area_k > area_i and iw * ih / area_i >= frac:
131
+ dup = True
132
+ break
133
+ if dup:
134
+ continue
135
+ if per_class.get(c, 0) >= self.max_per_class[c]:
136
+ continue
137
+ per_class[c] = per_class.get(c, 0) + 1
138
+ keep.append(int(i))
139
+ return boxes[keep], scores[keep], cls[keep]
140
+
141
+ def _detect(self, img_bgr: ndarray) -> list[BoundingBox]:
142
+ h, w = img_bgr.shape[:2]
143
+ canvas, r, left, top = self._letterbox(img_bgr)
144
+ x = cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
145
+ x = np.ascontiguousarray(x.transpose(2, 0, 1)[None])
146
+ out = self.session.run(None, {self.input_name: x})[0][0]
147
+
148
+ if out.shape[-1] == 6:
149
+ # End-to-end export (NMS-free): rows of x1, y1, x2, y2, score, cls.
150
+ boxes, scores, cls = out[:, :4], out[:, 4], out[:, 5].astype(np.int64)
151
+ keep = scores >= self.conf_thr[np.clip(cls, 0, len(self.classes) - 1)]
152
+ boxes, scores, cls = boxes[keep], scores[keep], cls[keep]
153
+ else:
154
+ # Raw head: (4 + nc, N) with cx, cy, w, h then per-class scores.
155
+ pred = out.T
156
+ cls_scores = pred[:, 4 : 4 + len(self.classes)] # seg heads append mask coefficients
157
+ # every class of an anchor above its threshold: an ambiguous patch is boxed as both spill and stain,
158
+ # one extra false positive costs far less than missing the class the label used
159
+ anchor, cls = np.nonzero(cls_scores >= self.conf_thr)
160
+ pred, scores = pred[anchor], cls_scores[anchor, cls]
161
+ cx, cy, bw, bh = pred[:, 0], pred[:, 1], pred[:, 2], pred[:, 3]
162
+ boxes = np.stack([cx - bw / 2, cy - bh / 2, cx + bw / 2, cy + bh / 2], axis=1)
163
+ selected: list[int] = []
164
+ for c in np.unique(cls):
165
+ idx = np.where(cls == c)[0]
166
+ selected.extend(idx[_nms(boxes[idx], scores[idx], self.nms_iou[int(c)])].tolist())
167
+ selected.sort(key=lambda i: -float(scores[i]))
168
+ boxes, scores, cls = boxes[selected], scores[selected], cls[selected]
169
+ boxes, scores, cls = self._postprocess(boxes, scores, cls)
170
+
171
+ results: list[BoundingBox] = []
172
+ for (x1, y1, x2, y2), s, c in zip(boxes[: self.max_det], scores[: self.max_det], cls[: self.max_det]):
173
+ x1 = int(np.clip(np.floor((x1 - left) / r), 0, w))
174
+ y1 = int(np.clip(np.floor((y1 - top) / r), 0, h))
175
+ x2 = int(np.clip(np.ceil((x2 - left) / r), 0, w))
176
+ y2 = int(np.clip(np.ceil((y2 - top) / r), 0, h))
177
+ if x2 <= x1 or y2 <= y1:
178
+ continue
179
+ area = (x2 - x1) * (y2 - y1) / (w * h) * 100
180
+ if self.small_bin and c == self.classes.index("bin") and area < self.small_bin[0] and s < self.small_bin[1]:
181
+ continue
182
+ if self.big_spill and c == self.classes.index("liquid spillage") and area > self.big_spill[0] and s < self.big_spill[1]:
183
+ continue
184
+ results.append(BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2, cls_id=int(c), conf=round(float(s), 4)))
185
+ if results and max(b.conf for b in results) < self.weak_image:
186
+ return []
187
+ return results
188
+
189
+ def predict_batch(
190
+ self,
191
+ batch_images: list[ndarray],
192
+ offset: int,
193
+ n_keypoints: int,
194
+ ) -> list[TVFrameResult]:
195
+ results: list[TVFrameResult] = []
196
+ for i, img in enumerate(batch_images):
197
+ results.append(
198
+ TVFrameResult(
199
+ frame_id=offset + i,
200
+ boxes=self._detect(img),
201
+ polygons=[],
202
+ keypoints=[(0, 0)] * n_keypoints,
203
+ )
204
+ )
205
+ return results