Instructions to use TianchenGuan/FireSmockDetect-models with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ultralytics
How to use TianchenGuan/FireSmockDetect-models with ultralytics:
from huggingface_hub import hf_hub_download from ultralytics import YOLO # pick the weights file from this repo's "Files and versions" tab weights = hf_hub_download("TianchenGuan/FireSmockDetect-models", "<weights>.pt") model = YOLO(weights) source = 'http://images.cocodataset.org/val2017/000000039769.jpg' model.predict(source=source, save=True) - Notebooks
- Google Colab
- Kaggle
v6 edge release: YOLO11n real-only model A (pt/ONNX/TFLite int8+fp16) + offline inference script
Browse files- v6A_y11n/EXPORT_REPORT.json +18 -0
- v6A_y11n/infer.py +200 -0
- v6A_y11n/v6A_y11n.onnx +3 -0
- v6A_y11n/v6A_y11n_best.pt +3 -0
- v6A_y11n/v6A_y11n_fp16.tflite +3 -0
- v6A_y11n/v6A_y11n_int8.tflite +3 -0
v6A_y11n/EXPORT_REPORT.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"ckpt": "models/v6/runs/expV6A_realn_s0/seg03/best.pt",
|
| 3 |
+
"imgsz": 1280,
|
| 4 |
+
"params_m": 2.59,
|
| 5 |
+
"onnx_mb": 11.11,
|
| 6 |
+
"parity": {
|
| 7 |
+
"n_images": 24,
|
| 8 |
+
"raw_max_abs_diff": 0.0157470703125,
|
| 9 |
+
"boxes_torch": 76,
|
| 10 |
+
"boxes_score_matched": 76
|
| 11 |
+
},
|
| 12 |
+
"latency_cpu_onnx_ms": 116.0,
|
| 13 |
+
"tflite_fp16_mb": 5.69,
|
| 14 |
+
"tflite_int8_mb": 3.6,
|
| 15 |
+
"latency_cpu_tflite_fp16_ms": 90.4,
|
| 16 |
+
"latency_cpu_tflite_int8_ms": 45.8,
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Offline smoke-detection inference — single command, no network needed.
|
| 3 |
+
|
| 4 |
+
Examples:
|
| 5 |
+
python infer.py --model model_int8.tflite --source image.jpg
|
| 6 |
+
python infer.py --model model.onnx --source folder/ --conf 0.62
|
| 7 |
+
python infer.py --model best.pt --source video.mp4 --out out/
|
| 8 |
+
|
| 9 |
+
Backends by file extension: .pt (ultralytics), .onnx (onnxruntime CPU),
|
| 10 |
+
.tflite (ai-edge-litert / tflite-runtime). Input letterboxed to 1280.
|
| 11 |
+
Default conf 0.62 = the sdis-val threshold at 10% negative-image FP for
|
| 12 |
+
model A; use 0.68 for ~5%. Latency printed per image is END-TO-END
|
| 13 |
+
(preprocess + inference + NMS) on this machine, batch=1.
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import json
|
| 19 |
+
import time
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
|
| 22 |
+
import numpy as np
|
| 23 |
+
|
| 24 |
+
IMGSZ = 1280
|
| 25 |
+
IMG_EXT = {".jpg", ".jpeg", ".png", ".bmp", ".webp"}
|
| 26 |
+
VID_EXT = {".mp4", ".avi", ".mov", ".mkv"}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def letterbox(im, size=IMGSZ):
|
| 30 |
+
import cv2
|
| 31 |
+
h, w = im.shape[:2]
|
| 32 |
+
s = size / max(h, w)
|
| 33 |
+
nh, nw = round(h * s), round(w * s)
|
| 34 |
+
r = cv2.resize(im, (nw, nh))
|
| 35 |
+
top, left = (size - nh) // 2, (size - nw) // 2
|
| 36 |
+
out = np.full((size, size, 3), 114, np.uint8)
|
| 37 |
+
out[top:top + nh, left:left + nw] = r
|
| 38 |
+
return out, s, left, top
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def nms(boxes, scores, iou_t=0.7, max_det=100):
|
| 42 |
+
idx = scores.argsort()[::-1][:1000]
|
| 43 |
+
keep = []
|
| 44 |
+
while len(idx):
|
| 45 |
+
i = idx[0]
|
| 46 |
+
keep.append(i)
|
| 47 |
+
if len(keep) >= max_det:
|
| 48 |
+
break
|
| 49 |
+
xx0 = np.maximum(boxes[i, 0], boxes[idx[1:], 0])
|
| 50 |
+
yy0 = np.maximum(boxes[i, 1], boxes[idx[1:], 1])
|
| 51 |
+
xx1 = np.minimum(boxes[i, 2], boxes[idx[1:], 2])
|
| 52 |
+
yy1 = np.minimum(boxes[i, 3], boxes[idx[1:], 3])
|
| 53 |
+
inter = np.clip(xx1 - xx0, 0, None) * np.clip(yy1 - yy0, 0, None)
|
| 54 |
+
a = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
|
| 55 |
+
b = (boxes[idx[1:], 2] - boxes[idx[1:], 0]) * (boxes[idx[1:], 3] - boxes[idx[1:], 1])
|
| 56 |
+
iou = inter / (a + b - inter + 1e-9)
|
| 57 |
+
idx = idx[1:][iou < iou_t]
|
| 58 |
+
return keep
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def decode(raw, conf):
|
| 62 |
+
# raw: (1, 5, N) -> cxcywh + score
|
| 63 |
+
p = raw[0].T
|
| 64 |
+
sc = p[:, 4]
|
| 65 |
+
m = sc >= conf
|
| 66 |
+
p, sc = p[m], sc[m]
|
| 67 |
+
if not len(p):
|
| 68 |
+
return np.zeros((0, 4)), np.zeros(0)
|
| 69 |
+
xy = np.stack([p[:, 0] - p[:, 2] / 2, p[:, 1] - p[:, 3] / 2,
|
| 70 |
+
p[:, 0] + p[:, 2] / 2, p[:, 1] + p[:, 3] / 2], 1)
|
| 71 |
+
k = nms(xy, sc)
|
| 72 |
+
return xy[k], sc[k]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class Runner:
|
| 76 |
+
def __init__(self, path):
|
| 77 |
+
self.path = Path(path)
|
| 78 |
+
self.kind = self.path.suffix
|
| 79 |
+
if self.kind == ".pt":
|
| 80 |
+
from ultralytics import YOLO
|
| 81 |
+
self.m = YOLO(str(path))
|
| 82 |
+
elif self.kind == ".onnx":
|
| 83 |
+
import onnxruntime as ort
|
| 84 |
+
self.s = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"])
|
| 85 |
+
self.inp = self.s.get_inputs()[0].name
|
| 86 |
+
elif self.kind == ".tflite":
|
| 87 |
+
try:
|
| 88 |
+
from ai_edge_litert.interpreter import Interpreter
|
| 89 |
+
except ImportError:
|
| 90 |
+
from tflite_runtime.interpreter import Interpreter
|
| 91 |
+
self.it = Interpreter(model_path=str(path))
|
| 92 |
+
self.it.allocate_tensors()
|
| 93 |
+
self.ii = self.it.get_input_details()[0]
|
| 94 |
+
self.oo = self.it.get_output_details()[0]
|
| 95 |
+
|
| 96 |
+
def __call__(self, bgr, conf):
|
| 97 |
+
if self.kind == ".pt":
|
| 98 |
+
r = self.m.predict(bgr, imgsz=IMGSZ, conf=conf, verbose=False)[0]
|
| 99 |
+
return r.boxes.xyxy.cpu().numpy(), r.boxes.conf.cpu().numpy(), None
|
| 100 |
+
lb, s, dx, dy = letterbox(bgr)
|
| 101 |
+
x = lb[:, :, ::-1].astype(np.float32) / 255.0
|
| 102 |
+
x = x.transpose(2, 0, 1)[None]
|
| 103 |
+
if self.kind == ".onnx":
|
| 104 |
+
raw = self.s.run(None, {self.inp: x})[0]
|
| 105 |
+
else:
|
| 106 |
+
xi = x.transpose(0, 2, 3, 1)
|
| 107 |
+
if self.ii["dtype"] == np.int8:
|
| 108 |
+
sc_, zp = self.ii["quantization"]
|
| 109 |
+
xi = (xi / sc_ + zp).round().clip(-128, 127).astype(np.int8)
|
| 110 |
+
self.it.set_tensor(self.ii["index"], xi)
|
| 111 |
+
self.it.invoke()
|
| 112 |
+
raw = self.it.get_tensor(self.oo["index"])
|
| 113 |
+
if self.oo["dtype"] == np.int8:
|
| 114 |
+
sc_, zp = self.oo["quantization"]
|
| 115 |
+
raw = (raw.astype(np.float32) - zp) * sc_
|
| 116 |
+
if raw.shape[1] != 5:
|
| 117 |
+
raw = raw.transpose(0, 2, 1)
|
| 118 |
+
raw = raw.copy()
|
| 119 |
+
raw[:, :4] *= IMGSZ # tflite head is normalized
|
| 120 |
+
boxes, scores = decode(raw, conf)
|
| 121 |
+
boxes[:, [0, 2]] = (boxes[:, [0, 2]] - dx) / s
|
| 122 |
+
boxes[:, [1, 3]] = (boxes[:, [1, 3]] - dy) / s
|
| 123 |
+
return boxes, scores, None
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def main():
|
| 127 |
+
import cv2
|
| 128 |
+
ap = argparse.ArgumentParser()
|
| 129 |
+
ap.add_argument("--model", required=True)
|
| 130 |
+
ap.add_argument("--source", required=True, help="image / folder / video")
|
| 131 |
+
ap.add_argument("--conf", type=float, default=0.62)
|
| 132 |
+
ap.add_argument("--out", default="infer_out")
|
| 133 |
+
args = ap.parse_args()
|
| 134 |
+
run = Runner(args.model)
|
| 135 |
+
outd = Path(args.out)
|
| 136 |
+
outd.mkdir(parents=True, exist_ok=True)
|
| 137 |
+
src = Path(args.source)
|
| 138 |
+
imgs = []
|
| 139 |
+
if src.is_dir():
|
| 140 |
+
imgs = [p for p in sorted(src.iterdir()) if p.suffix.lower() in IMG_EXT]
|
| 141 |
+
elif src.suffix.lower() in IMG_EXT:
|
| 142 |
+
imgs = [src]
|
| 143 |
+
|
| 144 |
+
results, lats = [], []
|
| 145 |
+
if imgs:
|
| 146 |
+
for p in imgs:
|
| 147 |
+
im = cv2.imread(str(p))
|
| 148 |
+
t0 = time.perf_counter()
|
| 149 |
+
boxes, scores, _ = run(im, args.conf)
|
| 150 |
+
lat = (time.perf_counter() - t0) * 1000
|
| 151 |
+
lats.append(lat)
|
| 152 |
+
for b, s_ in zip(boxes, scores):
|
| 153 |
+
cv2.rectangle(im, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),
|
| 154 |
+
(0, 0, 255), 3)
|
| 155 |
+
cv2.putText(im, f"smoke {s_:.2f}", (int(b[0]), int(b[1]) - 6),
|
| 156 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
|
| 157 |
+
cv2.imwrite(str(outd / p.name), im)
|
| 158 |
+
results.append({"image": str(p), "n_det": len(boxes),
|
| 159 |
+
"boxes": boxes.tolist(),
|
| 160 |
+
"scores": scores.tolist(), "latency_ms": round(lat, 1)})
|
| 161 |
+
print(f"{p.name}: {len(boxes)} det, {lat:.0f} ms")
|
| 162 |
+
elif src.suffix.lower() in VID_EXT:
|
| 163 |
+
cap = cv2.VideoCapture(str(src))
|
| 164 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 25
|
| 165 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 166 |
+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 167 |
+
vw = cv2.VideoWriter(str(outd / (src.stem + "_det.mp4")),
|
| 168 |
+
cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
|
| 169 |
+
n = 0
|
| 170 |
+
while True:
|
| 171 |
+
ok, im = cap.read()
|
| 172 |
+
if not ok:
|
| 173 |
+
break
|
| 174 |
+
t0 = time.perf_counter()
|
| 175 |
+
boxes, scores, _ = run(im, args.conf)
|
| 176 |
+
lats.append((time.perf_counter() - t0) * 1000)
|
| 177 |
+
for b, s_ in zip(boxes, scores):
|
| 178 |
+
cv2.rectangle(im, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),
|
| 179 |
+
(0, 0, 255), 3)
|
| 180 |
+
vw.write(im)
|
| 181 |
+
n += 1
|
| 182 |
+
vw.release()
|
| 183 |
+
print(f"video: {n} frames -> {outd / (src.stem + '_det.mp4')}")
|
| 184 |
+
else:
|
| 185 |
+
raise SystemExit(f"unsupported source: {src}")
|
| 186 |
+
|
| 187 |
+
if lats:
|
| 188 |
+
rep = {"model": str(args.model),
|
| 189 |
+
"model_mb": round(Path(args.model).stat().st_size / 1e6, 2),
|
| 190 |
+
"imgsz": IMGSZ, "conf": args.conf, "n": len(lats),
|
| 191 |
+
"latency_ms_median_e2e_batch1": round(float(np.median(lats)), 1),
|
| 192 |
+
"note": "end-to-end (preprocess+inference+NMS) on THIS machine; "
|
| 193 |
+
"edge-device latency unmeasured unless run on the device"}
|
| 194 |
+
(outd / "RUN_REPORT.json").write_text(json.dumps(
|
| 195 |
+
{"report": rep, "results": results}, indent=1))
|
| 196 |
+
print(json.dumps(rep, indent=1))
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
if __name__ == "__main__":
|
| 200 |
+
main()
|
v6A_y11n/v6A_y11n.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a57792ebf357366d93c1ab01f9e9054c306ed2fad6cddf8df5fc2bd2df9005bf
|
| 3 |
+
size 11108812
|
v6A_y11n/v6A_y11n_best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:36384c830fc4ba8a08993724e58170b51d1d7f0db5c6c12cf21054d15de87d77
|
| 3 |
+
size 5536154
|
v6A_y11n/v6A_y11n_fp16.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6e3ea161450efecb9899ad9bab9ba99aeb865588506824c76117d1c01445786e
|
| 3 |
+
size 5690984
|
v6A_y11n/v6A_y11n_int8.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6f193b89072b3538d0e62df1806f16cc9ffdead5b62a880b95aa13c08689ff02
|
| 3 |
+
size 3604222
|