WALDO41-YOLOv26 / inference.py
MirilAI's picture
WALDO 4.1 YOLOv26: N, S, M and L
75add1f
Raw History Blame Contribute Delete
3.19 kB
# SPDX-License-Identifier: MIT
"""Download one WALDO 4.1 YOLOv26 P2 model, check its SHA-256 and run it with Ultralytics.
pip install ultralytics huggingface_hub (add onnxruntime for --format onnx)
python inference.py image.jpg --size s [--format ultralytics|onnx] [--conf 0.25] [--device cpu]
"""
from __future__ import annotations
import argparse
import hashlib
from pathlib import Path
from huggingface_hub import hf_hub_download
REPO_ID = "MirilAI/WALDO41-YOLOv26"
FILES = {
"ultralytics": {
"n": ("ultralytics/waldo41-yolov26n-p2-obnubilated.pt", "cc85cfca58d7e25427930b656578326d4c47c1838b71d8f1fe19a71d1335b7a7"),
"s": ("ultralytics/waldo41-yolov26s-p2-obnubilated.pt", "a41994463f5c6b411501cd34d26f183dac6bd12affd8b464b1f5e35e15c15d38"),
"m": ("ultralytics/waldo41-yolov26m-p2-obnubilated.pt", "1e3680e2ccd2026a8d253af67ddb7cf67dfd1f5585008483f5022277eabc81d3"),
"l": ("ultralytics/waldo41-yolov26l-p2-obnubilated.pt", "40578e65219cae2929053ad8a4f0e2485e5c4e85f8868ceacaf78d7ab9b6d9c6"),
},
"onnx": {
"n": ("onnx/waldo41-yolov26n-p2-obnubilated.onnx", "6b311d31ce545e1c03fb82b1c92486763362a4b82cba597d5700def51e6beb58"),
"s": ("onnx/waldo41-yolov26s-p2-obnubilated.onnx", "03026e4b13f90cc2bac6d6d6ff422f921c67d78b6bb67ae00c32723d97de7aaf"),
"m": ("onnx/waldo41-yolov26m-p2-obnubilated.onnx", "ac25a883230db1a010b4e479fba638f16a9f5af149df38fe69b38a03e798d730"),
"l": ("onnx/waldo41-yolov26l-p2-obnubilated.onnx", "672a2189d50079db7fcb3a5365c2a07b467f8dde3d83466f2b4e4f497b74117f"),
},
}
def sha256_file(path: str | Path) -> str:
digest = hashlib.sha256()
with Path(path).open("rb") as handle:
while chunk := handle.read(1024 * 1024):
digest.update(chunk)
return digest.hexdigest()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("source", help="An image, a folder of images or a video.")
parser.add_argument("--size", choices=("n", "s", "m", "l"), default="s")
parser.add_argument("--format", choices=sorted(FILES), default="ultralytics")
parser.add_argument("--conf", type=float, default=0.25)
parser.add_argument("--device", default=None, help="For example: cpu, mps, 0.")
parser.add_argument("--output", type=Path, default=Path("runs/predict"))
args = parser.parse_args()
if args.size not in FILES[args.format]:
raise SystemExit(f"YOLOv26-{args.size.upper()} is not published as {args.format} yet.")
name, expected = FILES[args.format][args.size]
path = hf_hub_download(REPO_ID, name)
if sha256_file(path) != expected:
raise SystemExit(f"SHA-256 mismatch for {name}: the download is not the published file.")
from ultralytics import YOLO
model = YOLO(path, task="detect")
options = dict(imgsz=640, conf=args.conf, save=True, project=str(args.output), name=args.size, exist_ok=True)
if args.format == "ultralytics":
options["nms"] = False # the model's own NMS-free head, as published
if args.device:
options["device"] = args.device
model.predict(args.source, **options)
if __name__ == "__main__":
main()