subnet_bridge: copy winning miner repo into library
Browse files- README.md +49 -0
- chute_config.yml +29 -0
- class_names.txt +3 -0
- main.py +187 -0
- miner.py +235 -0
- model_type.json +4 -0
- pyproject.toml +17 -0
- weights.onnx +3 -0
README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- element_type:detect
|
| 4 |
+
- model:onnxruntime
|
| 5 |
+
- subnet:winner
|
| 6 |
+
- object:fire
|
| 7 |
+
- object:smoke
|
| 8 |
+
- object:fire extinguisher
|
| 9 |
+
|
| 10 |
+
manako:
|
| 11 |
+
source: winner_fetch
|
| 12 |
+
manifest_element_name: manak0/Detect-fire
|
| 13 |
+
winner_repo_id: meaculpitt/ScoreVision-Fire
|
| 14 |
+
winner_revision: 71ae3d3e59ced8b330eea5e95710318175bb1342
|
| 15 |
+
note: E=0.11785877 (map50=0.600000, size_mb=5.090839)
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# ScoreVision-Fire — meaculpitt v2.1
|
| 19 |
+
|
| 20 |
+
SN44 fire-detection miner for the `manak0/Detect-fire` element.
|
| 21 |
+
|
| 22 |
+
## Pipeline
|
| 23 |
+
- **Architecture**: yolo26n
|
| 24 |
+
- **Resolution**: 1408×768 input → letterbox → 960×960
|
| 25 |
+
- **Preprocessing**: `cv2.dnn.blobFromImage` (fused C++ resize+normalize+transpose)
|
| 26 |
+
- **Inference**: single-pass FP16 ONNX, NMS baked in
|
| 27 |
+
- **Output shape**: `[1, 300, 6]` (xyxy, conf, cls)
|
| 28 |
+
- **Latency**: ~35 ms p95 on RTX 4090 (fits the 50 ms gate)
|
| 29 |
+
|
| 30 |
+
## Classes (validator GT order, NOT the published class_names.txt order)
|
| 31 |
+
- 0: fire
|
| 32 |
+
- 1: smoke
|
| 33 |
+
- 2: fire extinguisher
|
| 34 |
+
|
| 35 |
+
Verified by audit of alfred8995/fire001 (scores 1.00) and navierstocks/fire
|
| 36 |
+
(scores 0.96): both use [fire, smoke, fire_extinguisher] and the validator's
|
| 37 |
+
GT order matches. Our model was trained with [fire, fire_ext, smoke]; miner.py
|
| 38 |
+
applies cls_remap=[0,2,1] to translate model output to validator index.
|
| 39 |
+
|
| 40 |
+
## Training
|
| 41 |
+
- 22,796 training images (validator-synth + Simuletic + D-Fire + z5atr, SHA1 deduped)
|
| 42 |
+
- 2,532 validation images (random 90/10 split, seed=42)
|
| 43 |
+
- 100 epochs, yolo26n, imgsz=960, batch=8, AdamW lr0=0.001 cos_lr
|
| 44 |
+
- CCTV augmentation chain (cctv_aug_patch)
|
| 45 |
+
|
| 46 |
+
## Benchmarks
|
| 47 |
+
- Broader merged val mAP50: 0.785
|
| 48 |
+
- Validator-distribution synth val mAP50: 0.640 (+24.7 pts above 0.393 baseline)
|
| 49 |
+
- Per-class on synth val: fire=0.523, fire_extinguisher=0.647, smoke=0.749
|
chute_config.yml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Image:
|
| 2 |
+
from_base: parachutes/python:3.12
|
| 3 |
+
run_command:
|
| 4 |
+
- pip install --upgrade setuptools wheel
|
| 5 |
+
- pip install 'numpy>=1.23' 'onnxruntime-gpu[cuda,cudnn]>=1.16' 'opencv-python>=4.7' 'pillow>=9.5' 'huggingface_hub>=0.19.4' 'pydantic>=2.0' 'pyyaml>=6.0' 'aiohttp>=3.9'
|
| 6 |
+
- pip install torch torchvision
|
| 7 |
+
|
| 8 |
+
NodeSelector:
|
| 9 |
+
gpu_count: 1
|
| 10 |
+
min_vram_gb_per_gpu: 16
|
| 11 |
+
# SN44 chute platform mandates TEE + pro_6000 include for new elements
|
| 12 |
+
# (verified by crime + beverage deploys 2026-05-04). Cheaper-GPU config
|
| 13 |
+
# caused repeated 500 ContentTypeError on POST /chutes/.
|
| 14 |
+
max_hourly_price_per_gpu: 2.00
|
| 15 |
+
include:
|
| 16 |
+
- "pro_6000"
|
| 17 |
+
exclude:
|
| 18 |
+
- "5090"
|
| 19 |
+
- b200
|
| 20 |
+
- h200
|
| 21 |
+
- h20
|
| 22 |
+
- mi300x
|
| 23 |
+
|
| 24 |
+
Chute:
|
| 25 |
+
concurrency: 4
|
| 26 |
+
max_instances: 5
|
| 27 |
+
scaling_threshold: 0.5
|
| 28 |
+
shutdown_after_seconds: 288000 # 80h idle
|
| 29 |
+
tee: true
|
class_names.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fire
|
| 2 |
+
smoke
|
| 3 |
+
fire extinguisher
|
main.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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v2
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _load_local_miner_class():
|
| 15 |
+
miner_path = Path(__file__).resolve().parent / "miner.py"
|
| 16 |
+
spec = importlib.util.spec_from_file_location("manako_bridge_local_miner", str(miner_path))
|
| 17 |
+
if spec is None or spec.loader is None:
|
| 18 |
+
raise RuntimeError(f"Could not load miner module from {miner_path}")
|
| 19 |
+
module = importlib.util.module_from_spec(spec)
|
| 20 |
+
spec.loader.exec_module(module)
|
| 21 |
+
miner_class = getattr(module, "Miner", None)
|
| 22 |
+
if miner_class is None:
|
| 23 |
+
raise RuntimeError(f"miner.py does not export Miner in {miner_path}")
|
| 24 |
+
return miner_class
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
Miner = _load_local_miner_class()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
CLASS_NAMES = ['fire', 'smoke', 'fire extinguisher']
|
| 31 |
+
MODEL_TYPE = 'onnxruntime'
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _to_dict(value: Any) -> dict[str, Any]:
|
| 35 |
+
if isinstance(value, dict):
|
| 36 |
+
return value
|
| 37 |
+
if hasattr(value, "model_dump") and callable(value.model_dump):
|
| 38 |
+
dumped = value.model_dump()
|
| 39 |
+
if isinstance(dumped, dict):
|
| 40 |
+
return dumped
|
| 41 |
+
if hasattr(value, "__dict__"):
|
| 42 |
+
return dict(value.__dict__)
|
| 43 |
+
return {}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _extract_boxes(frame_result: Any) -> list[Any]:
|
| 47 |
+
frame = _to_dict(frame_result)
|
| 48 |
+
boxes = frame.get("boxes", [])
|
| 49 |
+
if isinstance(boxes, list):
|
| 50 |
+
return boxes
|
| 51 |
+
return []
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _resolve_runtime_class_names(miner: Any) -> list[str]:
|
| 55 |
+
value = getattr(miner, "class_names", None)
|
| 56 |
+
if isinstance(value, (list, tuple)):
|
| 57 |
+
resolved = [str(item) for item in value]
|
| 58 |
+
if resolved:
|
| 59 |
+
return resolved
|
| 60 |
+
return list(CLASS_NAMES)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _to_detection(box: Any, class_names: list[str]) -> dict[str, Any]:
|
| 64 |
+
payload = _to_dict(box)
|
| 65 |
+
cls_id = int(payload.get("cls_id", 0))
|
| 66 |
+
x1 = float(payload.get("x1", 0.0))
|
| 67 |
+
y1 = float(payload.get("y1", 0.0))
|
| 68 |
+
x2 = float(payload.get("x2", 0.0))
|
| 69 |
+
y2 = float(payload.get("y2", 0.0))
|
| 70 |
+
width = max(0.0, x2 - x1)
|
| 71 |
+
height = max(0.0, y2 - y1)
|
| 72 |
+
return {
|
| 73 |
+
"x": x1 + width / 2.0,
|
| 74 |
+
"y": y1 + height / 2.0,
|
| 75 |
+
"width": width,
|
| 76 |
+
"height": height,
|
| 77 |
+
"confidence": float(payload.get("conf", 0.0)),
|
| 78 |
+
"class_id": cls_id,
|
| 79 |
+
"class": class_names[cls_id] if 0 <= cls_id < len(class_names) else str(cls_id),
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def _normalize_image_for_miner(image: Any) -> Any:
|
| 84 |
+
if image is None or hasattr(image, "shape"):
|
| 85 |
+
return image
|
| 86 |
+
if isinstance(image, (bytes, bytearray, memoryview)):
|
| 87 |
+
try:
|
| 88 |
+
buffer = np.frombuffer(bytes(image), dtype=np.uint8)
|
| 89 |
+
decoded = cv2.imdecode(buffer, cv2.IMREAD_COLOR)
|
| 90 |
+
if decoded is not None:
|
| 91 |
+
return decoded
|
| 92 |
+
except Exception:
|
| 93 |
+
return image
|
| 94 |
+
if hasattr(image, "convert") and callable(image.convert):
|
| 95 |
+
try:
|
| 96 |
+
rgb = image.convert("RGB")
|
| 97 |
+
array = np.array(rgb)
|
| 98 |
+
if getattr(array, "ndim", 0) == 3 and array.shape[-1] == 3:
|
| 99 |
+
return cv2.cvtColor(array, cv2.COLOR_RGB2BGR)
|
| 100 |
+
return array
|
| 101 |
+
except Exception:
|
| 102 |
+
return image
|
| 103 |
+
try:
|
| 104 |
+
array = np.asarray(image)
|
| 105 |
+
if getattr(array, "shape", None):
|
| 106 |
+
return array
|
| 107 |
+
except Exception:
|
| 108 |
+
return image
|
| 109 |
+
return image
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def load_model(onnx_path: str | None = None, data_dir: str | None = None):
|
| 113 |
+
del onnx_path
|
| 114 |
+
repo_dir = Path(data_dir) if data_dir else Path(__file__).resolve().parent
|
| 115 |
+
miner = Miner(repo_dir)
|
| 116 |
+
class_names = _resolve_runtime_class_names(miner)
|
| 117 |
+
return {
|
| 118 |
+
"miner": miner,
|
| 119 |
+
"model_type": MODEL_TYPE,
|
| 120 |
+
"class_names": class_names,
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def _candidate_keypoint_counts(miner: Any) -> list[int]:
|
| 125 |
+
counts: list[int] = [0]
|
| 126 |
+
for attr in ("n_keypoints", "num_keypoints", "keypoint_count", "num_joints"):
|
| 127 |
+
value = getattr(miner, attr, None)
|
| 128 |
+
if isinstance(value, int) and value > 0:
|
| 129 |
+
counts.append(value)
|
| 130 |
+
counts.append(32)
|
| 131 |
+
|
| 132 |
+
seen: set[int] = set()
|
| 133 |
+
ordered: list[int] = []
|
| 134 |
+
for count in counts:
|
| 135 |
+
if count in seen:
|
| 136 |
+
continue
|
| 137 |
+
seen.add(count)
|
| 138 |
+
ordered.append(count)
|
| 139 |
+
return ordered
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def _predict_batch_with_fallbacks(miner: Any, image: Any) -> list[Any]:
|
| 143 |
+
normalized_image = _normalize_image_for_miner(image)
|
| 144 |
+
errors: list[str] = []
|
| 145 |
+
for n_keypoints in _candidate_keypoint_counts(miner):
|
| 146 |
+
try:
|
| 147 |
+
return miner.predict_batch([normalized_image], offset=0, n_keypoints=n_keypoints)
|
| 148 |
+
except Exception as exc:
|
| 149 |
+
errors.append(f"n_keypoints={n_keypoints} -> {exc}")
|
| 150 |
+
continue
|
| 151 |
+
raise RuntimeError("predict_batch failed for all keypoint candidates: " + " | ".join(errors))
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def run_model(model: Any, image: Any = None, onnx_path: str | None = None, data_dir: str | None = None):
|
| 155 |
+
del onnx_path
|
| 156 |
+
if image is None:
|
| 157 |
+
image = model
|
| 158 |
+
model = load_model(data_dir=data_dir)
|
| 159 |
+
miner = model["miner"]
|
| 160 |
+
class_names = model.get("class_names")
|
| 161 |
+
if not isinstance(class_names, list):
|
| 162 |
+
class_names = list(CLASS_NAMES)
|
| 163 |
+
results = _predict_batch_with_fallbacks(miner, image)
|
| 164 |
+
if not results:
|
| 165 |
+
return [[]]
|
| 166 |
+
frame_boxes = _extract_boxes(results[0])
|
| 167 |
+
detections = [_to_detection(box, class_names) for box in frame_boxes]
|
| 168 |
+
return [detections]
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def main() -> None:
|
| 172 |
+
if len(sys.argv) < 2:
|
| 173 |
+
print("Usage: main.py <image_path>", file=sys.stderr)
|
| 174 |
+
raise SystemExit(1)
|
| 175 |
+
image_path = sys.argv[1]
|
| 176 |
+
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
|
| 177 |
+
if image is None:
|
| 178 |
+
print(f"Could not read image: {image_path}", file=sys.stderr)
|
| 179 |
+
raise SystemExit(1)
|
| 180 |
+
data_dir = os.path.dirname(os.path.abspath(__file__))
|
| 181 |
+
model = load_model(data_dir=data_dir)
|
| 182 |
+
output = run_model(model, image)
|
| 183 |
+
print(json.dumps(output, indent=2))
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
main()
|
miner.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# build-marker: fire-v2-blob-imgsz960
|
| 2 |
+
"""SN44 fire detection miner — yolo26n single-pass @ imgsz=960.
|
| 3 |
+
|
| 4 |
+
v2 (2026-05-09): trained on merged 25k pool (validator-synth + D-Fire +
|
| 5 |
+
Simuletic + z5atr). FP16 ONNX, ~5 MB. Single forward pass at imgsz=960
|
| 6 |
+
fits the 50 ms p95 latency gate (~35 ms on 4090, blobFromImage preproc).
|
| 7 |
+
|
| 8 |
+
SAHI tiling was tested but blew the latency budget (5x preproc/postproc
|
| 9 |
+
overhead). Code preserved at fire/deploy/miner_sahi.py for later experiments.
|
| 10 |
+
|
| 11 |
+
Classes (validator order from manak0/Detect-fire class_names.txt):
|
| 12 |
+
0=fire, 1=fire extinguisher, 2=smoke
|
| 13 |
+
|
| 14 |
+
Single ONNX expected at path_hf_repo/weights.onnx (yolo26n e2e [1,300,6]).
|
| 15 |
+
"""
|
| 16 |
+
import math
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
import cv2
|
| 20 |
+
import numpy as np
|
| 21 |
+
import onnxruntime as ort
|
| 22 |
+
from pydantic import BaseModel
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class BoundingBox(BaseModel):
|
| 26 |
+
x1: int
|
| 27 |
+
y1: int
|
| 28 |
+
x2: int
|
| 29 |
+
y2: int
|
| 30 |
+
cls_id: int
|
| 31 |
+
conf: float
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class TVFrameResult(BaseModel):
|
| 35 |
+
frame_id: int
|
| 36 |
+
boxes: list[BoundingBox]
|
| 37 |
+
keypoints: list[tuple[int, int]]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class Miner:
|
| 41 |
+
def __init__(self, path_hf_repo) -> None:
|
| 42 |
+
self.path_hf_repo = Path(path_hf_repo)
|
| 43 |
+
# Validator's actual GT class order is [fire, smoke, fire extinguisher]
|
| 44 |
+
# — verified by audit of alfred8995/fire001 (scores 1.00) and
|
| 45 |
+
# navierstocks/fire (scores 0.96), both using this order. The published
|
| 46 |
+
# manak0/Detect-fire class_names.txt list [fire, fire_ext, smoke] does
|
| 47 |
+
# NOT match the actual scoring index.
|
| 48 |
+
# Our model was trained with [fire, fire_ext, smoke] (cls=1=ext, cls=2=smoke).
|
| 49 |
+
# cls_remap translates model output index → validator GT index.
|
| 50 |
+
self.class_names = ["fire", "smoke", "fire extinguisher"]
|
| 51 |
+
model_class_order = ["fire", "fire extinguisher", "smoke"]
|
| 52 |
+
self.cls_remap = np.array(
|
| 53 |
+
[self.class_names.index(n) for n in model_class_order],
|
| 54 |
+
dtype=np.int32,
|
| 55 |
+
) # → [0, 2, 1]: model cls 0→0, 1→2, 2→1
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
ort.preload_dlls()
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
sess_options = ort.SessionOptions()
|
| 63 |
+
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
|
| 64 |
+
try:
|
| 65 |
+
self.session = ort.InferenceSession(
|
| 66 |
+
str(self.path_hf_repo / "weights.onnx"),
|
| 67 |
+
sess_options=sess_options,
|
| 68 |
+
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
|
| 69 |
+
)
|
| 70 |
+
except Exception:
|
| 71 |
+
self.session = ort.InferenceSession(
|
| 72 |
+
str(self.path_hf_repo / "weights.onnx"),
|
| 73 |
+
sess_options=sess_options,
|
| 74 |
+
providers=["CPUExecutionProvider"],
|
| 75 |
+
)
|
| 76 |
+
self.input_name = self.session.get_inputs()[0].name
|
| 77 |
+
self.output_names = [o.name for o in self.session.get_outputs()]
|
| 78 |
+
self.input_dtype = (np.float16
|
| 79 |
+
if 'float16' in self.session.get_inputs()[0].type
|
| 80 |
+
else np.float32)
|
| 81 |
+
|
| 82 |
+
self.input_h = 960
|
| 83 |
+
self.input_w = 960
|
| 84 |
+
self.conf_thres_per_class = np.array([0.20, 0.20, 0.20], dtype=np.float32)
|
| 85 |
+
self.iou_thresh = 0.5
|
| 86 |
+
self.cross_iou_thresh = 0.7
|
| 87 |
+
self.max_det = 100
|
| 88 |
+
self.min_box_area = 64
|
| 89 |
+
self.min_side = 6
|
| 90 |
+
self.max_aspect_ratio = 10.0
|
| 91 |
+
|
| 92 |
+
warm = np.zeros((768, 1408, 3), dtype=np.uint8)
|
| 93 |
+
for _ in range(3):
|
| 94 |
+
try: self._infer_single(warm)
|
| 95 |
+
except Exception: break
|
| 96 |
+
|
| 97 |
+
def __repr__(self):
|
| 98 |
+
thr = ",".join(f"{n[:4]}={t:.2f}" for n, t
|
| 99 |
+
in zip(self.class_names, self.conf_thres_per_class.tolist()))
|
| 100 |
+
return (f"FireMiner v2 yolo26n@{self.input_w} single-pass blob "
|
| 101 |
+
f"conf=[{thr}] iou={self.iou_thresh}")
|
| 102 |
+
|
| 103 |
+
def _preprocess(self, image_bgr):
|
| 104 |
+
"""Letterbox + cv2.dnn.blobFromImage (fused C++ resize/normalize/transpose)."""
|
| 105 |
+
h, w = image_bgr.shape[:2]
|
| 106 |
+
ratio = min(self.input_w / w, self.input_h / h)
|
| 107 |
+
nw, nh = int(round(w * ratio)), int(round(h * ratio))
|
| 108 |
+
if (nw, nh) != (w, h):
|
| 109 |
+
interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
|
| 110 |
+
resized = cv2.resize(image_bgr, (nw, nh), interpolation=interp)
|
| 111 |
+
else:
|
| 112 |
+
resized = image_bgr
|
| 113 |
+
canvas = np.full((self.input_h, self.input_w, 3), 114, dtype=np.uint8)
|
| 114 |
+
dy = (self.input_h - nh) // 2
|
| 115 |
+
dx = (self.input_w - nw) // 2
|
| 116 |
+
canvas[dy:dy+nh, dx:dx+nw] = resized
|
| 117 |
+
# blobFromImage: fused BGR→RGB (swapRB) + /255 + transpose CHW + add batch dim
|
| 118 |
+
blob = cv2.dnn.blobFromImage(
|
| 119 |
+
canvas, scalefactor=1/255.0,
|
| 120 |
+
size=(self.input_w, self.input_h),
|
| 121 |
+
mean=(0, 0, 0), swapRB=True, crop=False,
|
| 122 |
+
)
|
| 123 |
+
if self.input_dtype == np.float16:
|
| 124 |
+
blob = blob.astype(np.float16)
|
| 125 |
+
return blob, ratio, (float(dx), float(dy))
|
| 126 |
+
|
| 127 |
+
def _infer_single(self, image_bgr):
|
| 128 |
+
inp, ratio, (dx, dy) = self._preprocess(image_bgr)
|
| 129 |
+
out = self.session.run(self.output_names, {self.input_name: inp})[0]
|
| 130 |
+
if out.ndim == 3: out = out[0]
|
| 131 |
+
confs_all = out[:, 4].astype(np.float32)
|
| 132 |
+
cls_all = self.cls_remap[out[:, 5].astype(np.int32)]
|
| 133 |
+
cls_idx = np.clip(cls_all, 0, len(self.conf_thres_per_class) - 1)
|
| 134 |
+
keep = confs_all >= self.conf_thres_per_class[cls_idx]
|
| 135 |
+
if not keep.any(): return []
|
| 136 |
+
out = out[keep]
|
| 137 |
+
boxes = out[:, :4].astype(np.float32).copy()
|
| 138 |
+
confs = out[:, 4].astype(np.float32)
|
| 139 |
+
cls_ids = self.cls_remap[out[:, 5].astype(np.int32)]
|
| 140 |
+
boxes[:, [0, 2]] = (boxes[:, [0, 2]] - dx) / ratio
|
| 141 |
+
boxes[:, [1, 3]] = (boxes[:, [1, 3]] - dy) / ratio
|
| 142 |
+
oh, ow = image_bgr.shape[:2]
|
| 143 |
+
boxes[:, [0, 2]] = np.clip(boxes[:, [0, 2]], 0, ow - 1)
|
| 144 |
+
boxes[:, [1, 3]] = np.clip(boxes[:, [1, 3]], 0, oh - 1)
|
| 145 |
+
if len(boxes) > 1:
|
| 146 |
+
keep_idx = self._per_class_hard_nms(boxes, confs, cls_ids, self.iou_thresh)
|
| 147 |
+
keep_idx = keep_idx[: self.max_det]
|
| 148 |
+
boxes, confs, cls_ids = boxes[keep_idx], confs[keep_idx], cls_ids[keep_idx]
|
| 149 |
+
boxes, confs, cls_ids = self._cross_class_dedup(
|
| 150 |
+
boxes, confs, cls_ids, self.cross_iou_thresh)
|
| 151 |
+
return self._to_boundingboxes(boxes, confs, cls_ids, ow, oh)
|
| 152 |
+
|
| 153 |
+
@staticmethod
|
| 154 |
+
def _hard_nms(boxes, scores, iou_thresh):
|
| 155 |
+
n = len(boxes)
|
| 156 |
+
if n == 0: return np.array([], dtype=np.intp)
|
| 157 |
+
order = np.argsort(scores)[::-1]
|
| 158 |
+
keep, suppressed = [], np.zeros(n, dtype=bool)
|
| 159 |
+
for i in range(n):
|
| 160 |
+
idx = order[i]
|
| 161 |
+
if suppressed[idx]: continue
|
| 162 |
+
keep.append(int(idx))
|
| 163 |
+
bi = boxes[idx]
|
| 164 |
+
for k in range(i + 1, n):
|
| 165 |
+
jdx = order[k]
|
| 166 |
+
if suppressed[jdx]: continue
|
| 167 |
+
bj = boxes[jdx]
|
| 168 |
+
xx1, yy1 = max(bi[0], bj[0]), max(bi[1], bj[1])
|
| 169 |
+
xx2, yy2 = min(bi[2], bj[2]), min(bi[3], bj[3])
|
| 170 |
+
inter = max(0.0, xx2-xx1) * max(0.0, yy2-yy1)
|
| 171 |
+
ai = (bi[2]-bi[0])*(bi[3]-bi[1]); aj = (bj[2]-bj[0])*(bj[3]-bj[1])
|
| 172 |
+
iou = inter / (ai + aj - inter + 1e-7)
|
| 173 |
+
if iou > iou_thresh: suppressed[jdx] = True
|
| 174 |
+
return np.array(keep, dtype=np.intp)
|
| 175 |
+
|
| 176 |
+
def _per_class_hard_nms(self, boxes, scores, cls_ids, iou_thresh):
|
| 177 |
+
if len(boxes) == 0: return np.array([], dtype=np.intp)
|
| 178 |
+
all_keep = []
|
| 179 |
+
for c in np.unique(cls_ids):
|
| 180 |
+
mask = cls_ids == c
|
| 181 |
+
indices = np.where(mask)[0]
|
| 182 |
+
keep = self._hard_nms(boxes[mask], scores[mask], iou_thresh)
|
| 183 |
+
all_keep.extend(indices[keep].tolist())
|
| 184 |
+
all_keep.sort()
|
| 185 |
+
return np.array(all_keep, dtype=np.intp)
|
| 186 |
+
|
| 187 |
+
@staticmethod
|
| 188 |
+
def _cross_class_dedup(boxes, scores, cls_ids, iou_thresh):
|
| 189 |
+
n = len(boxes)
|
| 190 |
+
if n <= 1: return boxes, scores, cls_ids
|
| 191 |
+
areas = np.maximum(0.0, boxes[:, 2]-boxes[:, 0]) * np.maximum(0.0, boxes[:, 3]-boxes[:, 1])
|
| 192 |
+
order = np.lexsort((-scores, -areas))
|
| 193 |
+
suppressed = np.zeros(n, dtype=bool); keep = []
|
| 194 |
+
for i in order:
|
| 195 |
+
if suppressed[i]: continue
|
| 196 |
+
keep.append(int(i))
|
| 197 |
+
bi = boxes[i]
|
| 198 |
+
xx1 = np.maximum(bi[0], boxes[:, 0]); yy1 = np.maximum(bi[1], boxes[:, 1])
|
| 199 |
+
xx2 = np.minimum(bi[2], boxes[:, 2]); yy2 = np.minimum(bi[3], boxes[:, 3])
|
| 200 |
+
inter = np.maximum(0.0, xx2-xx1) * np.maximum(0.0, yy2-yy1)
|
| 201 |
+
ai = max(1e-7, float((bi[2]-bi[0])*(bi[3]-bi[1])))
|
| 202 |
+
iou = inter / (ai + areas - inter + 1e-7)
|
| 203 |
+
dup = iou > iou_thresh; dup[i] = False
|
| 204 |
+
suppressed |= dup
|
| 205 |
+
kept = np.array(keep, dtype=np.intp)
|
| 206 |
+
return boxes[kept], scores[kept], cls_ids[kept]
|
| 207 |
+
|
| 208 |
+
def _to_boundingboxes(self, boxes, confs, cls_ids, orig_w, orig_h):
|
| 209 |
+
out = []
|
| 210 |
+
for i in range(len(boxes)):
|
| 211 |
+
x1, y1, x2, y2 = boxes[i]
|
| 212 |
+
ix1 = max(0, min(orig_w, math.floor(x1)))
|
| 213 |
+
iy1 = max(0, min(orig_h, math.floor(y1)))
|
| 214 |
+
ix2 = max(0, min(orig_w, math.ceil(x2)))
|
| 215 |
+
iy2 = max(0, min(orig_h, math.ceil(y2)))
|
| 216 |
+
if ix2 <= ix1 or iy2 <= iy1: continue
|
| 217 |
+
bw, bh = ix2 - ix1, iy2 - iy1
|
| 218 |
+
if bw * bh < self.min_box_area: continue
|
| 219 |
+
if min(bw, bh) < self.min_side: continue
|
| 220 |
+
ar = max(bw / max(bh, 1), bh / max(bw, 1))
|
| 221 |
+
if ar > self.max_aspect_ratio: continue
|
| 222 |
+
out.append(BoundingBox(x1=ix1, y1=iy1, x2=ix2, y2=iy2, cls_id=int(cls_ids[i]),
|
| 223 |
+
conf=max(0.0, min(1.0, float(confs[i])))))
|
| 224 |
+
return out
|
| 225 |
+
|
| 226 |
+
def predict_batch(self, batch_images, offset, n_keypoints):
|
| 227 |
+
results = []
|
| 228 |
+
for idx, image in enumerate(batch_images):
|
| 229 |
+
boxes = self._infer_single(image)
|
| 230 |
+
results.append(TVFrameResult(
|
| 231 |
+
frame_id=offset + idx,
|
| 232 |
+
boxes=boxes,
|
| 233 |
+
keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
|
| 234 |
+
))
|
| 235 |
+
return results
|
model_type.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_type": "object-detection",
|
| 3 |
+
"model_type": "yolov26-nano"
|
| 4 |
+
}
|
pyproject.toml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "miner-element-adapter"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
requires-python = ">=3.9"
|
| 5 |
+
|
| 6 |
+
dependencies = [
|
| 7 |
+
"numpy>=1.23",
|
| 8 |
+
"onnxruntime[cuda,cudnn]>=1.16",
|
| 9 |
+
"opencv-python>=4.7",
|
| 10 |
+
"pillow>=9.5",
|
| 11 |
+
"huggingface_hub>=0.19.4",
|
| 12 |
+
"pydantic>=2.0",
|
| 13 |
+
"pyyaml>=6.0",
|
| 14 |
+
"aiohttp>=3.9",
|
| 15 |
+
"torch",
|
| 16 |
+
"torchvision",
|
| 17 |
+
]
|
weights.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0bfd3fd0b1dca617b05f93fb1ce92aadc8f6ee8e80255c2eb0818b143b4056d6
|
| 3 |
+
size 5077018
|