Image Classification
ONNX
onnxruntime
document-orientation
deskew
ocr
image-preprocessing
cpu
foliokestrel's picture
Add a local searchable-PDF orientation reader, a stale OCR example, and verified results on 343 USGS and 553 Bengali archive pages
88248d0 verified
Raw History Blame Contribute Delete
3.88 kB
"""Independently recompute published counts and ranks from the per-page records."""
from collections import Counter
import json
from pathlib import Path
import sys
from unblind import reference
def verify(path):
pages = list(map(json.loads, (path / "pages.jsonl").read_text().splitlines()))
report = json.loads((path / "results.json").read_text())
refs = reference()
assert len(pages) == len(refs) == 343
for row, ref in zip(pages, refs):
assert all(row[k] == value for k, value in ref.items())
for method in ("leave_as_is", "text", "osd"):
action = row[method]
if method == "text":
votes = action["votes"]
total = sum(votes)
assert action["accepted"] == (total >= 24 and 5*max(votes) >= 4*total)
assert action["predicted"] == (90*votes.index(max(votes)) if total else None)
if method == "osd":
assert action["accepted"] == (action["score"] is not None and action["score"] >= 15)
truth = row["native_cw"]
state = "unclear" if truth is None else ("turned" if truth else "upright")
move = action["predicted"]
if not action["accepted"]:
kind = "referred"
elif truth is None:
kind = "changed" if move else "kept"
elif move == truth:
kind = "corrected" if truth else "correct_keep"
else:
kind = "wrong_change" if move else "wrong_keep"
assert action["outcome"] == state+"_"+kind
for series, result in report["review"].items():
group = [r for r in pages if r["series"] == int(series)]
positives = [r for r in group if r["native_cw"]]
eligible = [r for r in group if r["ink"] is not None and .01 <= r["ink"] <= .55]
expected = sorted(eligible, key=lambda r: (-r["vertical_score"], r["page"]))
assert [r["rank"] for r in expected] == list(range(1,len(expected)+1))
assert len(group) == result["pages"]
assert {(r["page"],r["rank"]) for r in positives} == {(r["page"],r["rank"]) for r in result["turns"]}
assert sorted(r["page"] for r in group if r["rank"] is None) == sorted(r["page"] for r in result["excluded"])
assert result["extra_reviews"] == len(group)-len(eligible)
assert result["extra_turns"] == sum(r["rank"] is None for r in positives)
assert [b["k"] for b in result["budgets"]] == [5,10,20]
for b in result["budgets"]:
shortlist = expected[:b["k"]]
n = sum(bool(r["native_cw"]) for r in shortlist)
assert (b["reviewed"],b["available"],b["found"]) == (len(shortlist),len(positives),n)
assert b["recall"] == (n/len(positives) if positives else None)
assert b["fraction_with_known_turn"] == (n/len(shortlist) if shortlist else None)
assert abs(b["random_expected_found"] - len(positives)*len(shortlist)/len(group)) < 1e-9
def check_counts(group, stats):
for m in ("text","osd","leave_as_is"):
assert stats[m]["pages"] == len(group)
assert stats[m]["outcomes"] == Counter(r[m]["outcome"] for r in group)
assert stats[m]["accepted"] == sum(r[m]["accepted"] for r in group)
both = [r for r in group if all(r[m]["accepted"] for m in ("text","osd"))]
assert stats["agreement"] == dict(both_accepted=len(both),conflict=sum(r["text"]["predicted"]!=r["osd"]["predicted"] for r in both))
check_counts(pages, report["all"])
for field in ("series","content"):
for value, stats in report[field].items():
check_counts([r for r in pages if str(r[field]) == value], stats)
print(f"Verified {len(pages)} references, actions, review ranks and counts.")
if __name__ == "__main__":
verify(Path(sys.argv[1]))