Forager-Field-Notes / tests /test_two_photo.py
HomesteaderLabs's picture
Safety fix: never return an edible verdict from a single photo
6f38f56 verified
Raw History Blame
4.44 kB
"""Two-photo safety protocol tests, against the real ONNX models.
The load-bearing guarantee: no edible verdict is ever surfaced from a single
photo, and two disagreeing photos abstain rather than commit.
"""
import os
import sys
import pytest
from PIL import Image
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from pipeline.infer import Pipeline # noqa: E402
from pipeline.session import resolve_session # noqa: E402
ML = "/home/alex/Documents/Forager/forager_ml"
@pytest.fixture(scope="module")
def pipe():
return Pipeline()
def _img(path):
return Image.open(path)
def _first(cls_dir, n=1):
files = sorted(f for f in os.listdir(cls_dir)
if f.lower().endswith((".jpg", ".jpeg", ".png")))
return [os.path.join(cls_dir, f) for f in files[:n]]
# ── Policy: single photo never surfaces an edible verdict ────────────────────
def test_single_photo_never_commits_edible(pipe):
"""A single photo never surfaces an edible (SAFE/CAUTION) verdict, at any
confidence: it is withheld and routed to a second photo."""
safe_dir = f"{ML}/berry_dataset_split/val/blackberry_common"
if not os.path.isdir(safe_dir):
pytest.skip("val data not present")
for path in _first(safe_dir, 12):
r = resolve_session(pipe, [_img(path)])
assert not (not r.abstained and r.safety in ("SAFE", "CAUTION")), (
f"single photo committed {r.safety} at {r.confidence:.3f} for {path}")
if r.abstained:
assert r.reason in ("need_second_photo", "low_confidence",
"uncertain_domain", "off_domain")
def test_confident_deadly_mushroom_is_not_shown_safe(pipe):
"""The case that removed the 0.85 shortcut: toxic-catch-all photos the
mushroom expert calls lion's mane (0.91) and chanterelle (0.87)."""
d = f"{ML}/high_value_dataset_split/val/high_value_toxics"
paths = [f"{d}/599814795.jpg", f"{d}/616602478.jpg"]
if not all(os.path.isfile(p) for p in paths):
pytest.skip("val data not present")
for path in paths:
r = resolve_session(pipe, [_img(path)])
assert r.abstained and r.reason == "need_second_photo", (
f"{path} surfaced as {r.species} {r.safety} at {r.confidence:.3f}")
def test_two_agreeing_photos_can_commit_safe(pipe):
"""Two photos of the same SAFE class should be allowed to commit (the whole
point β€” the second photo unlocks an edible verdict)."""
safe_dir = f"{ML}/berry_dataset_split/val/blackberry_common"
if not os.path.isdir(safe_dir):
pytest.skip("val data not present")
committed = 0
paths = _first(safe_dir, 12)
# try a few pairs; at least one agreeing pair should commit SAFE
for i in range(0, len(paths) - 1, 2):
r = resolve_session(pipe, [_img(paths[i]), _img(paths[i + 1])])
if not r.abstained and r.safety == "SAFE":
committed += 1
assert committed >= 1, "no agreeing SAFE pair ever committed β€” fusion too strict"
def test_deadly_can_surface_on_single_photo(pipe):
"""Over-warning is safe: a DEADLY read on one photo is allowed through."""
deadly_dir = f"{ML}/berry_dataset_split/val/pokeweed_toxic"
if not os.path.isdir(deadly_dir):
pytest.skip("val data not present")
# We don't require every pokeweed to read DEADLY on one shot, only that the
# policy PERMITS a single-photo DEADLY (never converts it to need_second).
for path in _first(deadly_dir, 8):
r = resolve_session(pipe, [_img(path)])
if r.safety == "DEADLY":
assert not r.abstained
return
pytest.skip("no single-shot DEADLY in sample (still safe: they abstained)")
def test_two_photos_no_deadly_shown_edible(pipe):
"""The core claim: fuse two photos of DEADLY berry specimens; none may be
surfaced as edible (SAFE/CAUTION committed)."""
for cls in ("pokeweed_toxic", "canada_moonseed_deadly"):
d = f"{ML}/berry_dataset_split/val/{cls}"
if not os.path.isdir(d):
pytest.skip("val data not present")
paths = _first(d, 12)
for i in range(0, len(paths) - 1, 2):
r = resolve_session(pipe, [_img(paths[i]), _img(paths[i + 1])])
assert not (not r.abstained and r.safety in ("SAFE", "CAUTION")), (
f"{cls} pair surfaced as {r.safety} β€” toxic-as-edible at K=2")