scorevision: push artifact
Browse files
miner.py
CHANGED
|
@@ -1,21 +1,14 @@
|
|
| 1 |
-
"""Plate-detection miner —
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
- softnms(conf=0.30, iou=0.45, sigma=0.5, max_det=16)
|
| 13 |
-
- Bench: gated 0.436, fp/img 0.51, ms_p95 ~160 locally (A4000)
|
| 14 |
-
- On pro_6000 + TEE: expect ~2-3s p95 including network/attest overhead
|
| 15 |
-
Compared to:
|
| 16 |
-
plate_v2 best: gated=0.424
|
| 17 |
-
hermestech best: gated=0.422
|
| 18 |
-
5GRAm best: gated=0.401
|
| 19 |
"""
|
| 20 |
from pathlib import Path
|
| 21 |
import math
|
|
@@ -52,8 +45,7 @@ class Miner:
|
|
| 52 |
if cn_path.is_file():
|
| 53 |
lines = cn_path.read_text(encoding="utf-8").splitlines()
|
| 54 |
self.class_names = [
|
| 55 |
-
ln.strip()
|
| 56 |
-
for ln in lines
|
| 57 |
if ln.strip() and not ln.strip().startswith("#")
|
| 58 |
]
|
| 59 |
else:
|
|
@@ -66,15 +58,11 @@ class Miner:
|
|
| 66 |
except Exception as e:
|
| 67 |
print(f"preload_dlls failed: {e}")
|
| 68 |
|
| 69 |
-
print("ORT available providers BEFORE session:", ort.get_available_providers())
|
| 70 |
-
|
| 71 |
try:
|
| 72 |
import torch
|
| 73 |
if torch.cuda.is_available():
|
| 74 |
print(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 75 |
print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
|
| 76 |
-
else:
|
| 77 |
-
print("GPU: CUDA not available via torch")
|
| 78 |
except Exception as e:
|
| 79 |
print(f"GPU detection failed: {e}")
|
| 80 |
|
|
@@ -83,21 +71,17 @@ class Miner:
|
|
| 83 |
|
| 84 |
try:
|
| 85 |
self.session = ort.InferenceSession(
|
| 86 |
-
str(model_path),
|
| 87 |
-
sess_options=sess_options,
|
| 88 |
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
|
| 89 |
)
|
| 90 |
-
print("Created ORT session with
|
| 91 |
except Exception as e:
|
| 92 |
print(f"CUDA session creation failed, falling back to CPU: {e}")
|
| 93 |
self.session = ort.InferenceSession(
|
| 94 |
-
str(model_path),
|
| 95 |
-
sess_options=sess_options,
|
| 96 |
providers=["CPUExecutionProvider"],
|
| 97 |
)
|
| 98 |
|
| 99 |
-
print("ORT session providers:", self.session.get_providers())
|
| 100 |
-
|
| 101 |
for inp in self.session.get_inputs():
|
| 102 |
print("INPUT:", inp.name, inp.shape, inp.type)
|
| 103 |
for out in self.session.get_outputs():
|
|
@@ -106,80 +90,55 @@ class Miner:
|
|
| 106 |
self.input_name = self.session.get_inputs()[0].name
|
| 107 |
self.output_names = [o.name for o in self.session.get_outputs()]
|
| 108 |
self.input_shape = self.session.get_inputs()[0].shape
|
| 109 |
-
# plate_v3 export is fp16 static [1,3,1280,1280]
|
| 110 |
self.input_dtype = (
|
| 111 |
-
np.float16
|
| 112 |
-
if "float16" in self.session.get_inputs()[0].type
|
| 113 |
else np.float32
|
| 114 |
)
|
| 115 |
-
|
| 116 |
self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
|
| 117 |
self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# recovers recall on hard shards where it matters most.
|
| 126 |
-
# plate_v4 bench winner: softnms(c30,md16) at gated=0.436, mAP=0.980
|
| 127 |
-
self.conf_thres = 0.30
|
| 128 |
-
self.iou_thres = 0.45
|
| 129 |
-
self.sigma = 0.5
|
| 130 |
-
self.max_det = 16
|
| 131 |
-
self.use_tta = True # hflip TTA — bench-verified for mAP gain
|
| 132 |
|
|
|
|
|
|
|
| 133 |
print(f"ONNX model loaded from: {model_path}")
|
| 134 |
-
print(f"ONNX providers: {self.session.get_providers()}")
|
| 135 |
-
print(f"ONNX input: name={self.input_name}, shape={self.input_shape}, dtype={self.input_dtype}")
|
| 136 |
-
print(f"Preset: conf={self.conf_thres} iou={self.iou_thres} sigma={self.sigma} max_det={self.max_det}")
|
| 137 |
|
| 138 |
def __repr__(self) -> str:
|
| 139 |
-
return (
|
| 140 |
-
f"ONNXRuntime(session={type(self.session).__name__}, "
|
| 141 |
-
f"providers={self.session.get_providers()})"
|
| 142 |
-
)
|
| 143 |
|
| 144 |
@staticmethod
|
| 145 |
def _safe_dim(value, default: int) -> int:
|
| 146 |
return value if isinstance(value, int) and value > 0 else default
|
| 147 |
|
| 148 |
-
# ----------
|
| 149 |
-
def _letterbox(
|
| 150 |
-
self,
|
| 151 |
-
image: ndarray,
|
| 152 |
-
new_shape: tuple[int, int],
|
| 153 |
-
color=(114, 114, 114),
|
| 154 |
-
) -> tuple[ndarray, float, tuple[float, float]]:
|
| 155 |
h, w = image.shape[:2]
|
| 156 |
new_w, new_h = new_shape
|
| 157 |
ratio = min(new_w / w, new_h / h)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
if (resized_w, resized_h) != (w, h):
|
| 161 |
interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
|
| 162 |
-
image = cv2.resize(image, (
|
| 163 |
-
dw = (new_w -
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
bottom = int(round(dh + 0.1))
|
| 169 |
-
padded = cv2.copyMakeBorder(
|
| 170 |
-
image, top, bottom, left, right,
|
| 171 |
-
borderType=cv2.BORDER_CONSTANT, value=color,
|
| 172 |
-
)
|
| 173 |
return padded, ratio, (dw, dh)
|
| 174 |
|
| 175 |
-
def _preprocess(self, image
|
| 176 |
img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
|
| 177 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
|
| 178 |
img = np.transpose(img, (2, 0, 1))[None, ...]
|
| 179 |
return np.ascontiguousarray(img, dtype=self.input_dtype), ratio, pad
|
| 180 |
|
| 181 |
@staticmethod
|
| 182 |
-
def _clip_boxes(boxes
|
| 183 |
w, h = image_size
|
| 184 |
boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
|
| 185 |
boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
|
|
@@ -187,20 +146,32 @@ class Miner:
|
|
| 187 |
boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
|
| 188 |
return boxes
|
| 189 |
|
| 190 |
-
# ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
@staticmethod
|
| 192 |
-
def _hard_nms(boxes
|
| 193 |
N = len(boxes)
|
| 194 |
if N == 0:
|
| 195 |
return np.array([], dtype=np.intp)
|
| 196 |
-
boxes = np.asarray(boxes, dtype=np.float32)
|
| 197 |
-
scores = np.asarray(scores, dtype=np.float32)
|
| 198 |
order = np.argsort(-scores)
|
| 199 |
-
keep
|
| 200 |
while len(order):
|
| 201 |
i = int(order[0])
|
| 202 |
keep.append(i)
|
| 203 |
-
if len(order) == 1:
|
| 204 |
break
|
| 205 |
rest = order[1:]
|
| 206 |
xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
|
|
@@ -208,52 +179,13 @@ class Miner:
|
|
| 208 |
xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
|
| 209 |
yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
|
| 210 |
inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
iou = inter / (
|
| 214 |
order = rest[iou <= iou_thresh]
|
| 215 |
return np.array(keep, dtype=np.intp)
|
| 216 |
|
| 217 |
-
def
|
| 218 |
-
self,
|
| 219 |
-
boxes: np.ndarray,
|
| 220 |
-
scores: np.ndarray,
|
| 221 |
-
sigma: float,
|
| 222 |
-
score_thresh: float = 0.01,
|
| 223 |
-
) -> tuple[np.ndarray, np.ndarray]:
|
| 224 |
-
N = len(boxes)
|
| 225 |
-
if N == 0:
|
| 226 |
-
return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
|
| 227 |
-
boxes = boxes.astype(np.float32, copy=True)
|
| 228 |
-
scores = scores.astype(np.float32, copy=True)
|
| 229 |
-
order = np.arange(N)
|
| 230 |
-
for i in range(N):
|
| 231 |
-
max_pos = i + int(np.argmax(scores[i:]))
|
| 232 |
-
boxes[[i, max_pos]] = boxes[[max_pos, i]]
|
| 233 |
-
scores[[i, max_pos]] = scores[[max_pos, i]]
|
| 234 |
-
order[[i, max_pos]] = order[[max_pos, i]]
|
| 235 |
-
if i + 1 >= N:
|
| 236 |
-
break
|
| 237 |
-
xx1 = np.maximum(boxes[i, 0], boxes[i + 1:, 0])
|
| 238 |
-
yy1 = np.maximum(boxes[i, 1], boxes[i + 1:, 1])
|
| 239 |
-
xx2 = np.minimum(boxes[i, 2], boxes[i + 1:, 2])
|
| 240 |
-
yy2 = np.minimum(boxes[i, 3], boxes[i + 1:, 3])
|
| 241 |
-
inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
|
| 242 |
-
area_i = float(
|
| 243 |
-
(boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
|
| 244 |
-
)
|
| 245 |
-
areas_j = (
|
| 246 |
-
np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
|
| 247 |
-
* np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
|
| 248 |
-
)
|
| 249 |
-
iou = inter / (area_i + areas_j - inter + 1e-7)
|
| 250 |
-
scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
|
| 251 |
-
mask = scores > score_thresh
|
| 252 |
-
return order[mask], scores[mask]
|
| 253 |
-
|
| 254 |
-
# ---------- raw-dets helper ----------
|
| 255 |
-
def _raw_dets(self, image: ndarray, conf: float) -> np.ndarray:
|
| 256 |
-
"""Run a single forward pass and return [N, 5] dets in ORIGINAL image coords."""
|
| 257 |
x, ratio, (dw, dh) = self._preprocess(image)
|
| 258 |
out = self.session.run(self.output_names, {self.input_name: x})[0]
|
| 259 |
if out.ndim == 3:
|
|
@@ -273,32 +205,51 @@ class Miner:
|
|
| 273 |
boxes = self._clip_boxes(boxes, (ow, oh))
|
| 274 |
return np.concatenate([boxes, scores[:, None]], axis=1)
|
| 275 |
|
| 276 |
-
# ----------
|
| 277 |
-
def
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
return np.zeros((0, 5), dtype=np.float32)
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
merged = merged[np.argsort(-merged[:, 4])[: self.max_det]]
|
| 301 |
-
return merged
|
| 302 |
|
| 303 |
# ---------- single-image predict ----------
|
| 304 |
def _predict_single(self, image: ndarray) -> list[BoundingBox]:
|
|
@@ -309,32 +260,26 @@ class Miner:
|
|
| 309 |
if image.dtype != np.uint8:
|
| 310 |
image = image.astype(np.uint8)
|
| 311 |
|
| 312 |
-
dets = self.
|
| 313 |
|
| 314 |
results: list[BoundingBox] = []
|
| 315 |
for row in dets:
|
| 316 |
x1, y1, x2, y2, conf = row.tolist()
|
| 317 |
if x2 <= x1 or y2 <= y1:
|
| 318 |
continue
|
| 319 |
-
results.append(
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
)
|
| 328 |
-
)
|
| 329 |
return results
|
| 330 |
|
| 331 |
# ---------- chute entrypoint ----------
|
| 332 |
-
def predict_batch(
|
| 333 |
-
|
| 334 |
-
batch_images: list[ndarray],
|
| 335 |
-
offset: int,
|
| 336 |
-
n_keypoints: int,
|
| 337 |
-
) -> list[TVFrameResult]:
|
| 338 |
results: list[TVFrameResult] = []
|
| 339 |
for frame_number_in_batch, image in enumerate(batch_images):
|
| 340 |
try:
|
|
@@ -342,11 +287,9 @@ class Miner:
|
|
| 342 |
except Exception as e:
|
| 343 |
print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
|
| 344 |
boxes = []
|
| 345 |
-
results.append(
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
)
|
| 351 |
-
)
|
| 352 |
return results
|
|
|
|
| 1 |
+
"""Plate-detection miner — plate_v6 + consensus-TTA inference.
|
| 2 |
+
|
| 3 |
+
Weights: plate_v6 (resumed plate_v5 + difficulty-weighted scraped real challenges
|
| 4 |
+
+ synth CCTV; 18 epochs, peak val mAP50 0.930).
|
| 5 |
+
|
| 6 |
+
Inference (smile0123-style consensus-TTA, our bench winner at gated 0.443):
|
| 7 |
+
- low conf (0.15) for high recall, super-high-conf (>=0.90) passes directly
|
| 8 |
+
- hflip cross-view consensus: low-conf boxes must match a flipped-view box at IoU>=0.01
|
| 9 |
+
- final hard-NMS at iou=0.32, max_det=150
|
| 10 |
+
Bench on 221-shard live pseudo-GT pool: gated 0.443 mAP 0.975 fp/img 0.25 ms_p95 ~150 (A4000)
|
| 11 |
+
On pro_6000 + TEE expect ~2-3s p95 including network/attest overhead.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
from pathlib import Path
|
| 14 |
import math
|
|
|
|
| 45 |
if cn_path.is_file():
|
| 46 |
lines = cn_path.read_text(encoding="utf-8").splitlines()
|
| 47 |
self.class_names = [
|
| 48 |
+
ln.strip() for ln in lines
|
|
|
|
| 49 |
if ln.strip() and not ln.strip().startswith("#")
|
| 50 |
]
|
| 51 |
else:
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
print(f"preload_dlls failed: {e}")
|
| 60 |
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
import torch
|
| 63 |
if torch.cuda.is_available():
|
| 64 |
print(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 65 |
print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
print(f"GPU detection failed: {e}")
|
| 68 |
|
|
|
|
| 71 |
|
| 72 |
try:
|
| 73 |
self.session = ort.InferenceSession(
|
| 74 |
+
str(model_path), sess_options=sess_options,
|
|
|
|
| 75 |
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
|
| 76 |
)
|
| 77 |
+
print("Created ORT session with CUDA provider")
|
| 78 |
except Exception as e:
|
| 79 |
print(f"CUDA session creation failed, falling back to CPU: {e}")
|
| 80 |
self.session = ort.InferenceSession(
|
| 81 |
+
str(model_path), sess_options=sess_options,
|
|
|
|
| 82 |
providers=["CPUExecutionProvider"],
|
| 83 |
)
|
| 84 |
|
|
|
|
|
|
|
| 85 |
for inp in self.session.get_inputs():
|
| 86 |
print("INPUT:", inp.name, inp.shape, inp.type)
|
| 87 |
for out in self.session.get_outputs():
|
|
|
|
| 90 |
self.input_name = self.session.get_inputs()[0].name
|
| 91 |
self.output_names = [o.name for o in self.session.get_outputs()]
|
| 92 |
self.input_shape = self.session.get_inputs()[0].shape
|
|
|
|
| 93 |
self.input_dtype = (
|
| 94 |
+
np.float16 if "float16" in self.session.get_inputs()[0].type
|
|
|
|
| 95 |
else np.float32
|
| 96 |
)
|
|
|
|
| 97 |
self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
|
| 98 |
self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
|
| 99 |
|
| 100 |
+
# Consensus-TTA preset (bench winner — gated 0.443)
|
| 101 |
+
self.conf_thres = 0.15 # low — collect MANY candidates
|
| 102 |
+
self.conf_high = 0.90 # >= this → pass through without TTA match
|
| 103 |
+
self.tta_match_iou = 0.01 # very permissive cross-view match
|
| 104 |
+
self.iou_thres = 0.32 # final hard-NMS
|
| 105 |
+
self.max_det = 150
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
print(f"Preset: conf={self.conf_thres} conf_high={self.conf_high} "
|
| 108 |
+
f"tta_match_iou={self.tta_match_iou} iou={self.iou_thres} max_det={self.max_det}")
|
| 109 |
print(f"ONNX model loaded from: {model_path}")
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
def __repr__(self) -> str:
|
| 112 |
+
return f"ONNXRuntime(providers={self.session.get_providers()})"
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
@staticmethod
|
| 115 |
def _safe_dim(value, default: int) -> int:
|
| 116 |
return value if isinstance(value, int) and value > 0 else default
|
| 117 |
|
| 118 |
+
# ---------- preprocessing ----------
|
| 119 |
+
def _letterbox(self, image, new_shape, color=(114, 114, 114)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
h, w = image.shape[:2]
|
| 121 |
new_w, new_h = new_shape
|
| 122 |
ratio = min(new_w / w, new_h / h)
|
| 123 |
+
rw, rh = int(round(w * ratio)), int(round(h * ratio))
|
| 124 |
+
if (rw, rh) != (w, h):
|
|
|
|
| 125 |
interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
|
| 126 |
+
image = cv2.resize(image, (rw, rh), interpolation=interp)
|
| 127 |
+
dw, dh = (new_w - rw) / 2.0, (new_h - rh) / 2.0
|
| 128 |
+
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
|
| 129 |
+
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
|
| 130 |
+
padded = cv2.copyMakeBorder(image, top, bottom, left, right,
|
| 131 |
+
borderType=cv2.BORDER_CONSTANT, value=color)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return padded, ratio, (dw, dh)
|
| 133 |
|
| 134 |
+
def _preprocess(self, image):
|
| 135 |
img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
|
| 136 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
|
| 137 |
img = np.transpose(img, (2, 0, 1))[None, ...]
|
| 138 |
return np.ascontiguousarray(img, dtype=self.input_dtype), ratio, pad
|
| 139 |
|
| 140 |
@staticmethod
|
| 141 |
+
def _clip_boxes(boxes, image_size):
|
| 142 |
w, h = image_size
|
| 143 |
boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
|
| 144 |
boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
|
|
|
|
| 146 |
boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
|
| 147 |
return boxes
|
| 148 |
|
| 149 |
+
# ---------- detection helpers ----------
|
| 150 |
+
@staticmethod
|
| 151 |
+
def _iou_one_to_many(box, boxes):
|
| 152 |
+
if len(boxes) == 0:
|
| 153 |
+
return np.zeros(0, dtype=np.float32)
|
| 154 |
+
xx1 = np.maximum(box[0], boxes[:, 0])
|
| 155 |
+
yy1 = np.maximum(box[1], boxes[:, 1])
|
| 156 |
+
xx2 = np.minimum(box[2], boxes[:, 2])
|
| 157 |
+
yy2 = np.minimum(box[3], boxes[:, 3])
|
| 158 |
+
inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
|
| 159 |
+
ai = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
|
| 160 |
+
aj = (np.maximum(0.0, boxes[:, 2] - boxes[:, 0])
|
| 161 |
+
* np.maximum(0.0, boxes[:, 3] - boxes[:, 1]))
|
| 162 |
+
return inter / (ai + aj - inter + 1e-7)
|
| 163 |
+
|
| 164 |
@staticmethod
|
| 165 |
+
def _hard_nms(boxes, scores, iou_thresh, max_det):
|
| 166 |
N = len(boxes)
|
| 167 |
if N == 0:
|
| 168 |
return np.array([], dtype=np.intp)
|
|
|
|
|
|
|
| 169 |
order = np.argsort(-scores)
|
| 170 |
+
keep = []
|
| 171 |
while len(order):
|
| 172 |
i = int(order[0])
|
| 173 |
keep.append(i)
|
| 174 |
+
if len(order) == 1 or len(keep) >= max_det:
|
| 175 |
break
|
| 176 |
rest = order[1:]
|
| 177 |
xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
|
|
|
|
| 179 |
xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
|
| 180 |
yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
|
| 181 |
inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
|
| 182 |
+
ai = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
|
| 183 |
+
ar = (boxes[rest, 2] - boxes[rest, 0]) * (boxes[rest, 3] - boxes[rest, 1])
|
| 184 |
+
iou = inter / (ai + ar - inter + 1e-7)
|
| 185 |
order = rest[iou <= iou_thresh]
|
| 186 |
return np.array(keep, dtype=np.intp)
|
| 187 |
|
| 188 |
+
def _raw_dets(self, image, conf):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
x, ratio, (dw, dh) = self._preprocess(image)
|
| 190 |
out = self.session.run(self.output_names, {self.input_name: x})[0]
|
| 191 |
if out.ndim == 3:
|
|
|
|
| 205 |
boxes = self._clip_boxes(boxes, (ow, oh))
|
| 206 |
return np.concatenate([boxes, scores[:, None]], axis=1)
|
| 207 |
|
| 208 |
+
# ---------- consensus-TTA prediction ----------
|
| 209 |
+
def _predict_consensus_tta(self, image):
|
| 210 |
+
d_o = self._raw_dets(image, self.conf_thres)
|
| 211 |
+
flipped = cv2.flip(image, 1)
|
| 212 |
+
d_f = self._raw_dets(flipped, self.conf_thres)
|
| 213 |
+
if len(d_f):
|
| 214 |
+
w = image.shape[1]
|
| 215 |
+
d_f = np.stack([w - d_f[:, 2], d_f[:, 1], w - d_f[:, 0],
|
| 216 |
+
d_f[:, 3], d_f[:, 4]], axis=1)
|
| 217 |
+
|
| 218 |
+
accepted_boxes = []
|
| 219 |
+
accepted_scores = []
|
| 220 |
+
# Original-view candidates
|
| 221 |
+
for i in range(len(d_o)):
|
| 222 |
+
s = float(d_o[i, 4])
|
| 223 |
+
if s >= self.conf_high:
|
| 224 |
+
accepted_boxes.append(d_o[i, :4])
|
| 225 |
+
accepted_scores.append(s)
|
| 226 |
+
elif len(d_f) > 0:
|
| 227 |
+
ious = self._iou_one_to_many(d_o[i, :4], d_f[:, :4])
|
| 228 |
+
j = int(np.argmax(ious))
|
| 229 |
+
if ious[j] >= self.tta_match_iou:
|
| 230 |
+
fused = max(s, float(d_f[j, 4]))
|
| 231 |
+
accepted_boxes.append(d_o[i, :4])
|
| 232 |
+
accepted_scores.append(fused)
|
| 233 |
+
# Flip-view high-conf boxes that original missed
|
| 234 |
+
for i in range(len(d_f)):
|
| 235 |
+
s = float(d_f[i, 4])
|
| 236 |
+
if s < self.conf_high:
|
| 237 |
+
continue
|
| 238 |
+
if len(d_o) == 0:
|
| 239 |
+
accepted_boxes.append(d_f[i, :4])
|
| 240 |
+
accepted_scores.append(s)
|
| 241 |
+
continue
|
| 242 |
+
ious = self._iou_one_to_many(d_f[i, :4], d_o[:, :4])
|
| 243 |
+
if np.max(ious) < self.tta_match_iou:
|
| 244 |
+
accepted_boxes.append(d_f[i, :4])
|
| 245 |
+
accepted_scores.append(s)
|
| 246 |
+
|
| 247 |
+
if not accepted_boxes:
|
| 248 |
return np.zeros((0, 5), dtype=np.float32)
|
| 249 |
+
boxes = np.array(accepted_boxes, dtype=np.float32)
|
| 250 |
+
scores = np.array(accepted_scores, dtype=np.float32)
|
| 251 |
+
keep = self._hard_nms(boxes, scores, self.iou_thres, self.max_det)
|
| 252 |
+
return np.concatenate([boxes[keep], scores[keep][:, None]], axis=1)
|
|
|
|
|
|
|
| 253 |
|
| 254 |
# ---------- single-image predict ----------
|
| 255 |
def _predict_single(self, image: ndarray) -> list[BoundingBox]:
|
|
|
|
| 260 |
if image.dtype != np.uint8:
|
| 261 |
image = image.astype(np.uint8)
|
| 262 |
|
| 263 |
+
dets = self._predict_consensus_tta(image)
|
| 264 |
|
| 265 |
results: list[BoundingBox] = []
|
| 266 |
for row in dets:
|
| 267 |
x1, y1, x2, y2, conf = row.tolist()
|
| 268 |
if x2 <= x1 or y2 <= y1:
|
| 269 |
continue
|
| 270 |
+
results.append(BoundingBox(
|
| 271 |
+
x1=int(math.floor(x1)),
|
| 272 |
+
y1=int(math.floor(y1)),
|
| 273 |
+
x2=int(math.ceil(x2)),
|
| 274 |
+
y2=int(math.ceil(y2)),
|
| 275 |
+
cls_id=0,
|
| 276 |
+
conf=float(conf),
|
| 277 |
+
))
|
|
|
|
|
|
|
| 278 |
return results
|
| 279 |
|
| 280 |
# ---------- chute entrypoint ----------
|
| 281 |
+
def predict_batch(self, batch_images: list[ndarray], offset: int,
|
| 282 |
+
n_keypoints: int) -> list[TVFrameResult]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
results: list[TVFrameResult] = []
|
| 284 |
for frame_number_in_batch, image in enumerate(batch_images):
|
| 285 |
try:
|
|
|
|
| 287 |
except Exception as e:
|
| 288 |
print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
|
| 289 |
boxes = []
|
| 290 |
+
results.append(TVFrameResult(
|
| 291 |
+
frame_id=offset + frame_number_in_batch,
|
| 292 |
+
boxes=boxes,
|
| 293 |
+
keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
|
| 294 |
+
))
|
|
|
|
|
|
|
| 295 |
return results
|