Spaces:
Running on Zero

mild-vs-wild / check_examples.py
idaroselm's picture
Upload folder using huggingface_hub
0da203f verified
Raw History Blame Contribute Delete
1.87 kB
"""
Diagnostic: run the app pipeline on every sample photo and compare against the expected answers
(the predictions the same pipeline gives in testing, which match 05_detector_eval.py).
Also re-runs MegaDetector on the CPU to check whether the Mac GPU (MPS) changes the boxes.
Run from the app folder, with the app's .venv active: python check_examples.py
"""
import glob
import os
from megadetector.detection.run_detector import load_detector
import app
EXPECTED = { # top-1 and MegaDetector confidence from testing
"bobcat_day_cam46": ("bobcat", 0.90), "bobcat_gray_cam46": ("bobcat", 0.87), "cat_day_cam105": ("cat", 0.90),
"cat_ir_cam78": ("cat", 0.83), "coyote_day_cam28": ("coyote", 0.93), "raccoon_ir_cam40": ("raccoon", 0.92),
"tricky_bobcat_cam100": ("cat", 0.86), "tricky_bobcat_cam130": ("cat", 0.91),
}
cpu_md = load_detector(app.MD_PATH, force_cpu=True)
def best_box(det, path):
from PIL import Image
with Image.open(path) as im:
r = det.generate_detections_one_image(im.convert("RGB"), "x", detection_threshold=0.01)
a = sorted([d for d in r.get("detections", []) if str(d["category"]) == "1"], key=lambda d: -d["conf"])
return (round(a[0]["conf"], 2), [round(v, 3) for v in a[0]["bbox"]]) if a else (0.0, None)
print(f"{'photo':24s} {'expected':9s} {'app says':22s} {'MD conf (GPU / CPU)':20s} boxes (GPU | CPU)")
for path in sorted(glob.glob(os.path.join(app.EXAMPLES_DIR, "*.jpg"))):
name = os.path.splitext(os.path.basename(path))[0]
_, top3, _, _, st = app.predict(path)
top = max(top3, key=top3.get)
gpu, cpu = best_box(app.DETECTOR, path), best_box(cpu_md, path)
exp = EXPECTED.get(name, ("?", 0))[0]
flag = "" if top == exp else " <-- differs"
print(f"{name:24s} {exp:9s} {top + f' {top3[top]:.2f}':22s} {gpu[0]:.2f} / {cpu[0]:.2f}{'':10s} {gpu[1]} | {cpu[1]}{flag}")