File size: 14,177 Bytes
438565c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | from pathlib import Path
import math
import logging
import cv2
import numpy as np
import onnxruntime as ort
from numpy import ndarray
from pydantic import BaseModel
logger = logging.getLogger(__name__)
# βββ Petrol miner v1.1 βββββββββββββββββββββββββββββββββββββββββββββββ
# Improvements over auto-generated baseline:
# 1. Fix end-to-end ONNX decode (model outputs [1,300,6] post-NMS)
# 2. Spatial co-occurrence scoring (pump+canopy boost, isolated suppress)
# 3. Geometric validation (aspect ratio + size checks per class)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Class IDs
CLS_HOSE = 0
CLS_PUMP = 1
CLS_PRICEBOARD = 2
CLS_CANOPY = 3
# ββ Geometric validation thresholds (derived from 2000-label analysis) ββ
# Canopy: wide/flat, aspect(w/h) mean=2.96. Suppress if aspect < 0.8 (too tall)
CANOPY_MIN_ASPECT = 0.8
# Pump: roughly square/tall, aspect mean=0.91. Suppress if aspect > 4.0 (too wide)
PUMP_MAX_ASPECT = 4.0
# Price board: small. Suppress if area > 15% of image
PRICEBOARD_MAX_AREA_FRAC = 0.15
# Hose: variable. Suppress if area < 0.05% of image (tiny FP)
HOSE_MIN_AREA_FRAC = 0.0005
# ββ Spatial co-occurrence boost/suppress amounts ββ
COOCCUR_BOOST_PUMP_CANOPY = 0.05
COOCCUR_BOOST_PUMP_HOSE = 0.08
COOCCUR_BOOST_CANOPY_HOSE = 0.05
COOCCUR_SUPPRESS_ISOLATED = 0.03 # per missing expected neighbor
# Proximity threshold: normalized distance between box centers
COOCCUR_PROXIMITY = 0.5 # half of image dimension
# ββ Geometric suppress penalty ββ
GEOMETRIC_SUPPRESS_PENALTY = 0.10
class BoundingBox(BaseModel):
x1: int
y1: int
x2: int
y2: int
cls_id: int
conf: float
class TVFrameResult(BaseModel):
frame_id: int
boxes: list[BoundingBox]
keypoints: list[tuple[int, int]]
class Miner:
VERSION = "petrol-v1.1"
def __init__(self, path_hf_repo: Path) -> None:
self.path_hf_repo = path_hf_repo
self.class_names = ['petrol hose', 'petrol pump', 'price board', 'roof canopy']
self.session = ort.InferenceSession(
str(path_hf_repo / "weights.onnx"),
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
)
self.input_name = self.session.get_inputs()[0].name
input_shape = self.session.get_inputs()[0].shape
self.input_h = int(input_shape[2])
self.input_w = int(input_shape[3])
self.conf_threshold = 0.25
self.iou_threshold = 0.45
# Detect output format: end-to-end [1,N,6] vs raw [1,C,N]
out_shape = self.session.get_outputs()[0].shape
# End-to-end: [1, max_dets, 6] where max_dets is small (100-300)
# Raw: [1, 4+nc, N] where N is large (8400+)
if len(out_shape) == 3 and out_shape[2] == 6 and (out_shape[1] or 0) <= 1000:
self._end2end = True
logger.info("[init] End-to-end ONNX output detected")
else:
self._end2end = False
logger.info("[init] Raw ONNX output detected")
logger.info(f"[init] {self.VERSION} loaded, input={self.input_w}x{self.input_h}, "
f"end2end={self._end2end}")
def __repr__(self) -> str:
return f"Petrol Miner {self.VERSION} end2end={self._end2end}"
# βββ Preprocessing ββββββββββββββββββββββββββββββββββββββββββββββββ
def _preprocess(self, image_bgr: ndarray) -> tuple[np.ndarray, tuple[int, int]]:
h, w = image_bgr.shape[:2]
rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
resized = cv2.resize(rgb, (self.input_w, self.input_h))
x = resized.astype(np.float32) / 255.0
x = np.transpose(x, (2, 0, 1))[None, ...]
return x, (h, w)
# βββ NMS (only needed for raw output format) βββββββββββββββββββββ
def _nms(self, dets):
if not dets:
return []
boxes = np.array([[d[0], d[1], d[2], d[3]] for d in dets], dtype=np.float32)
scores = np.array([d[4] for d in dets], dtype=np.float32)
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(boxes[i, 0], boxes[order[1:], 0])
yy1 = np.maximum(boxes[i, 1], boxes[order[1:], 1])
xx2 = np.minimum(boxes[i, 2], boxes[order[1:], 2])
yy2 = np.minimum(boxes[i, 3], boxes[order[1:], 3])
w = np.maximum(0.0, xx2 - xx1)
h = np.maximum(0.0, yy2 - yy1)
inter = w * h
area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
area_rest = (boxes[order[1:], 2] - boxes[order[1:], 0]) * (boxes[order[1:], 3] - boxes[order[1:], 1])
union = np.maximum(area_i + area_rest - inter, 1e-6)
iou = inter / union
remaining = np.where(iou <= self.iou_threshold)[0]
order = order[remaining + 1]
return [dets[idx] for idx in keep]
# βββ Decode: handles both end-to-end and raw formats βββββββββββββ
def _decode_end2end(self, out, orig_h, orig_w):
"""Decode end-to-end [1, N, 6] output: [x1,y1,x2,y2,conf,cls_id] in input coords."""
pred = out[0] # [N, 6]
if pred.ndim != 2 or pred.shape[1] != 6:
return []
confs = pred[:, 4]
keep = confs >= self.conf_threshold
pred = pred[keep]
if pred.shape[0] == 0:
return []
sx = orig_w / float(self.input_w)
sy = orig_h / float(self.input_h)
results = []
for i in range(pred.shape[0]):
x1 = pred[i, 0] * sx
y1 = pred[i, 1] * sy
x2 = pred[i, 2] * sx
y2 = pred[i, 3] * sy
conf = float(pred[i, 4])
cls_id = int(pred[i, 5])
results.append((x1, y1, x2, y2, conf, cls_id))
return results
def _decode_raw(self, out, orig_h, orig_w):
"""Decode raw [1, 4+nc, N] or [1, N, 4+nc] output."""
pred = out[0]
if pred.ndim != 2:
return []
if pred.shape[0] < pred.shape[1]:
pred = pred.T
if pred.shape[1] < 5:
return []
boxes = pred[:, :4]
cls_scores = pred[:, 4:]
if cls_scores.shape[1] == 0:
return []
cls_ids = np.argmax(cls_scores, axis=1)
confs = np.max(cls_scores, axis=1)
keep = confs >= self.conf_threshold
boxes, confs, cls_ids = boxes[keep], confs[keep], cls_ids[keep]
if boxes.shape[0] == 0:
return []
sx = orig_w / float(self.input_w)
sy = orig_h / float(self.input_h)
dets = []
for i in range(boxes.shape[0]):
cx, cy, bw, bh = boxes[i].tolist()
x1 = (cx - bw / 2.0) * sx
y1 = (cy - bh / 2.0) * sy
x2 = (cx + bw / 2.0) * sx
y2 = (cy + bh / 2.0) * sy
dets.append((x1, y1, x2, y2, float(confs[i]), int(cls_ids[i])))
return self._nms(dets)
# βββ Geometric validation ββββββββββββββββββββββββββββββββββββββββ
def _geometric_validate(self, dets, orig_h, orig_w):
"""Suppress detections that fail basic geometric expectations.
Returns list with penalties applied to conf.
- Canopy: must be wide (aspect w/h >= 0.8)
- Pump: must not be extremely wide (aspect w/h <= 4.0)
- Price board: must be small (area <= 15% of image)
- Hose: must not be tiny (area >= 0.05% of image)
"""
img_area = max(orig_h * orig_w, 1)
result = []
for x1, y1, x2, y2, conf, cls_id in dets:
bw = max(x2 - x1, 1)
bh = max(y2 - y1, 1)
aspect = bw / bh
box_area = bw * bh
area_frac = box_area / img_area
penalty = 0.0
if cls_id == CLS_CANOPY:
if aspect < CANOPY_MIN_ASPECT:
penalty = GEOMETRIC_SUPPRESS_PENALTY
elif cls_id == CLS_PUMP:
if aspect > PUMP_MAX_ASPECT:
penalty = GEOMETRIC_SUPPRESS_PENALTY
elif cls_id == CLS_PRICEBOARD:
if area_frac > PRICEBOARD_MAX_AREA_FRAC:
penalty = GEOMETRIC_SUPPRESS_PENALTY
elif cls_id == CLS_HOSE:
if area_frac < HOSE_MIN_AREA_FRAC:
penalty = GEOMETRIC_SUPPRESS_PENALTY
new_conf = max(0.0, conf - penalty)
if new_conf >= self.conf_threshold:
result.append((x1, y1, x2, y2, new_conf, cls_id))
return result
# βββ Spatial co-occurrence scoring βββββββββββββββββββββββββββββββ
def _spatial_cooccurrence(self, dets, orig_h, orig_w):
"""Adjust confidences based on spatial co-occurrence patterns.
Boosts:
- Pump near canopy: both get +0.05
- Pump near hose: hose gets +0.08
- Canopy near hose: hose gets +0.05
Suppresses:
- Low-conf detection with no neighbors of expected class: -0.03
(except price boards, which are 91% solo in training data)
"""
if not dets:
return dets
n = len(dets)
adjustments = [0.0] * n
diag = math.sqrt(orig_h ** 2 + orig_w ** 2)
prox = COOCCUR_PROXIMITY * diag # absolute pixel distance
# Precompute centers
centers = []
for x1, y1, x2, y2, conf, cls_id in dets:
centers.append(((x1 + x2) / 2, (y1 + y2) / 2))
# Build per-class index
cls_map = {}
for i, (_, _, _, _, _, cls_id) in enumerate(dets):
cls_map.setdefault(cls_id, []).append(i)
def near(i, j):
dx = centers[i][0] - centers[j][0]
dy = centers[i][1] - centers[j][1]
return math.sqrt(dx * dx + dy * dy) < prox
# Pump + Canopy boost
for pi in cls_map.get(CLS_PUMP, []):
for ci in cls_map.get(CLS_CANOPY, []):
if near(pi, ci):
adjustments[pi] = max(adjustments[pi], COOCCUR_BOOST_PUMP_CANOPY)
adjustments[ci] = max(adjustments[ci], COOCCUR_BOOST_PUMP_CANOPY)
# Pump + Hose boost (hose gets larger boost)
for pi in cls_map.get(CLS_PUMP, []):
for hi in cls_map.get(CLS_HOSE, []):
if near(pi, hi):
adjustments[hi] = max(adjustments[hi], COOCCUR_BOOST_PUMP_HOSE)
# Canopy + Hose boost
for ci in cls_map.get(CLS_CANOPY, []):
for hi in cls_map.get(CLS_HOSE, []):
if near(ci, hi):
adjustments[hi] = max(adjustments[hi], COOCCUR_BOOST_CANOPY_HOSE)
# Suppress isolated low-confidence detections (not price boards)
for i, (x1, y1, x2, y2, conf, cls_id) in enumerate(dets):
if cls_id == CLS_PRICEBOARD:
continue # price boards are often solo (91% in training)
if conf > 0.60:
continue # high confidence β don't suppress
has_neighbor = False
for j in range(n):
if i == j:
continue
if near(i, j):
has_neighbor = True
break
if not has_neighbor:
adjustments[i] = min(adjustments[i],
adjustments[i] - COOCCUR_SUPPRESS_ISOLATED)
# Apply adjustments
result = []
for i, (x1, y1, x2, y2, conf, cls_id) in enumerate(dets):
new_conf = min(1.0, max(0.0, conf + adjustments[i]))
if new_conf >= self.conf_threshold:
result.append((x1, y1, x2, y2, new_conf, cls_id))
return result
# βββ Main inference ββββββββββββββββββββββββββββββββββββββββββββββ
def _infer_single(self, image_bgr: ndarray) -> list[BoundingBox]:
inp, (orig_h, orig_w) = self._preprocess(image_bgr)
out = self.session.run(None, {self.input_name: inp})[0]
# Decode based on detected output format
if self._end2end:
dets = self._decode_end2end(out, orig_h, orig_w)
else:
dets = self._decode_raw(out, orig_h, orig_w)
if not dets:
return []
# Post-processing pipeline
dets = self._geometric_validate(dets, orig_h, orig_w)
dets = self._spatial_cooccurrence(dets, orig_h, orig_w)
# Convert to BoundingBox
out_boxes = []
for x1, y1, x2, y2, conf, cls_id in dets:
ix1 = max(0, min(orig_w, math.floor(x1)))
iy1 = max(0, min(orig_h, math.floor(y1)))
ix2 = max(0, min(orig_w, math.ceil(x2)))
iy2 = max(0, min(orig_h, math.ceil(y2)))
out_boxes.append(
BoundingBox(
x1=ix1, y1=iy1, x2=ix2, y2=iy2,
cls_id=cls_id,
conf=max(0.0, min(1.0, conf)),
)
)
return out_boxes
def predict_batch(
self,
batch_images: list[ndarray],
offset: int,
n_keypoints: int,
) -> list[TVFrameResult]:
results = []
for idx, image in enumerate(batch_images):
boxes = self._infer_single(image)
keypoints = [(0, 0) for _ in range(max(0, int(n_keypoints)))]
results.append(
TVFrameResult(
frame_id=offset + idx,
boxes=boxes,
keypoints=keypoints,
)
)
return results
|