Upload folder using huggingface_hub
Browse files
miner.py
CHANGED
|
@@ -48,17 +48,26 @@ class Miner:
|
|
| 48 |
iou_thres = 0.3
|
| 49 |
cross_iou_thresh = 0.8
|
| 50 |
max_det = 150
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
_conf_thres_array = np.array(
|
| 53 |
-
[0.38, 0.38, 0.22, 0.
|
| 54 |
)
|
| 55 |
_bonus_array = np.array(
|
| 56 |
[0.2, 0.25, 0.12, 0.09, 0.21, 0.06], dtype=np.float32,
|
| 57 |
)
|
| 58 |
|
| 59 |
def __init__(self, path_hf_repo: Path) -> None:
|
| 60 |
-
# model_path = path_hf_repo / "weights-all.onnx"
|
| 61 |
model_path = path_hf_repo / "weights.onnx"
|
|
|
|
| 62 |
print("ORT version:", ort.__version__)
|
| 63 |
|
| 64 |
try:
|
|
@@ -443,20 +452,43 @@ class Miner:
|
|
| 443 |
boxes, scores, cls_ids = self._per_view_pipeline(boxes, scores, cls_ids)
|
| 444 |
return self._build_results(boxes, scores, cls_ids)
|
| 445 |
|
| 446 |
-
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
cls_ids: np.ndarray) -> list[BoundingBox]:
|
|
|
|
| 449 |
results: list[BoundingBox] = []
|
| 450 |
for box, conf, cls_id in zip(boxes, scores, cls_ids):
|
| 451 |
x1, y1, x2, y2 = box.tolist()
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
continue
|
| 454 |
results.append(
|
| 455 |
BoundingBox(
|
| 456 |
-
x1=
|
| 457 |
-
y1=int(math.floor(y1)),
|
| 458 |
-
x2=int(math.ceil(x2)),
|
| 459 |
-
y2=int(math.ceil(y2)),
|
| 460 |
cls_id=int(cls_id),
|
| 461 |
conf=float(conf),
|
| 462 |
)
|
|
|
|
| 48 |
iou_thres = 0.3
|
| 49 |
cross_iou_thresh = 0.8
|
| 50 |
max_det = 150
|
| 51 |
+
|
| 52 |
+
# Final box tightening, applied about each box's own center AFTER NMS /
|
| 53 |
+
# cross-class dedup so the tuned IoU thresholds above still see the raw
|
| 54 |
+
# geometry. `box_shrink` is the fraction of width/height the box keeps:
|
| 55 |
+
# 1.0 = unchanged, 0.96 pulls each edge inward by 2% of that dimension.
|
| 56 |
+
# A box is never shrunk below `min_box_size` px, and never below its own
|
| 57 |
+
# original size.
|
| 58 |
+
box_shrink = 0.98
|
| 59 |
+
min_box_size = 2.0
|
| 60 |
|
| 61 |
_conf_thres_array = np.array(
|
| 62 |
+
[0.38, 0.38, 0.22, 0.20, 0.33, 0.20], dtype=np.float32,
|
| 63 |
)
|
| 64 |
_bonus_array = np.array(
|
| 65 |
[0.2, 0.25, 0.12, 0.09, 0.21, 0.06], dtype=np.float32,
|
| 66 |
)
|
| 67 |
|
| 68 |
def __init__(self, path_hf_repo: Path) -> None:
|
|
|
|
| 69 |
model_path = path_hf_repo / "weights.onnx"
|
| 70 |
+
# model_path = path_hf_repo / "weights.onnx"
|
| 71 |
print("ORT version:", ort.__version__)
|
| 72 |
|
| 73 |
try:
|
|
|
|
| 452 |
boxes, scores, cls_ids = self._per_view_pipeline(boxes, scores, cls_ids)
|
| 453 |
return self._build_results(boxes, scores, cls_ids)
|
| 454 |
|
| 455 |
+
def _shrink_boxes(self, boxes: np.ndarray) -> np.ndarray:
|
| 456 |
+
"""Scale every xyxy box toward its own center by `box_shrink`.
|
| 457 |
+
Center, class and score are untouched; only the extent changes."""
|
| 458 |
+
scale = float(self.box_shrink)
|
| 459 |
+
if len(boxes) == 0 or scale >= 1.0:
|
| 460 |
+
return boxes
|
| 461 |
+
boxes = np.asarray(boxes, dtype=np.float32)
|
| 462 |
+
cx = (boxes[:, 0] + boxes[:, 2]) * 0.5
|
| 463 |
+
cy = (boxes[:, 1] + boxes[:, 3]) * 0.5
|
| 464 |
+
w = np.maximum(0.0, boxes[:, 2] - boxes[:, 0])
|
| 465 |
+
h = np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
|
| 466 |
+
# Shrink, but never past min_box_size and never larger than the original
|
| 467 |
+
# (the floor must not inflate an already-tiny detection).
|
| 468 |
+
new_w = np.minimum(np.maximum(w * scale, self.min_box_size), w)
|
| 469 |
+
new_h = np.minimum(np.maximum(h * scale, self.min_box_size), h)
|
| 470 |
+
out = np.empty_like(boxes)
|
| 471 |
+
out[:, 0] = cx - new_w * 0.5
|
| 472 |
+
out[:, 1] = cy - new_h * 0.5
|
| 473 |
+
out[:, 2] = cx + new_w * 0.5
|
| 474 |
+
out[:, 3] = cy + new_h * 0.5
|
| 475 |
+
return out
|
| 476 |
+
|
| 477 |
+
def _build_results(self, boxes: np.ndarray, scores: np.ndarray,
|
| 478 |
cls_ids: np.ndarray) -> list[BoundingBox]:
|
| 479 |
+
boxes = self._shrink_boxes(boxes)
|
| 480 |
results: list[BoundingBox] = []
|
| 481 |
for box, conf, cls_id in zip(boxes, scores, cls_ids):
|
| 482 |
x1, y1, x2, y2 = box.tolist()
|
| 483 |
+
# Round to nearest instead of floor/ceil: the old outward
|
| 484 |
+
# rounding grew every box by up to 1 px on each of the 4 edges.
|
| 485 |
+
ix1, iy1 = int(round(x1)), int(round(y1))
|
| 486 |
+
ix2, iy2 = int(round(x2)), int(round(y2))
|
| 487 |
+
if ix2 <= ix1 or iy2 <= iy1:
|
| 488 |
continue
|
| 489 |
results.append(
|
| 490 |
BoundingBox(
|
| 491 |
+
x1=ix1, y1=iy1, x2=ix2, y2=iy2,
|
|
|
|
|
|
|
|
|
|
| 492 |
cls_id=int(cls_id),
|
| 493 |
conf=float(conf),
|
| 494 |
)
|