Image Classification
ONNX
onnxruntime
document-orientation
deskew
ocr
image-preprocessing
cpu
folio-orient / evaluation /REFLECTION.md
foliokestrel's picture
Add optional typewriter reflection checkpoint, reproducible synthetic training and external French archive evaluation
a8c4308 verified
|
Raw History Blame Contribute Delete
7.32 kB

Correcting mirrored print

A horizontal reflection reverses the letters themselves. The optional mirror.onnx estimates both reflection and rotation. Given a decoded page, it returns one of eight operations. For logit index k, flip the visible image left-right if k >= 4, then rotate it clockwise by 90 * (k % 4) degrees. Applying these steps in the other order changes the result on sideways mirrored pages.

Run locally

Install the wheel from the model card. The reflection command prints its proposal even when it sends a page to review. With an output path it writes a new lossless PNG only when the proposal clears the cutoff. Both source and output use the image's EXIF display orientation.

folio-orient-reflect scan.tif
folio-orient-reflect scan.tif --output restored.png
folio-orient-reflect scan.tif --min-agreement 0.70 --output candidate.png

The default agreement cutoff is 0.90. It is deliberately selective. The optional 0.70 setting accepted more ordinary print pages in our evaluation. The number is the mean patch vote for the selected transform. Inspect uncertain, handwritten, typewritten, sparsely printed, or illustrated inputs. Orientation confidence can be misleading on those pages.

from PIL import Image
from folio_orient import ReflectionDetector, correct_transform

detector = ReflectionDetector()
with Image.open("scan.tif") as image:
    proposal = detector.estimate(image)
    if proposal["turn_cw"] is not None and proposal["agreement"] >= 0.90:
        corrected = correct_transform(image, proposal["flip_left_right"], proposal["turn_cw"])
        corrected.save("restored.png")

Evaluation on book scans, newspapers and PDFs

We tested all eight transforms of each source with and without an added 1.50–6.99° tilt. Correctness requires both the correct reflection decision and the correct turn. Multiple views of a page share the same content. The classifier trained entirely on synthetic print. The book, newspaper and contemporary page selections come from the studies in the evaluation notes.

Source pages Views Correct / accepted at 0.90 Correct / accepted at 0.70 Two-pass baseline, correct at matched 0.70 coverage
68 historical printed pages 1,088 234 / 234 847 / 847 802 / 847
48 Finnish newspaper pages 768 142 / 142 655 / 655 589 / 655
139 contemporary PDF pages 2,224 177 / 177 994 / 994 947 / 994

At 0.70, the model accepted 2,496 of 4,080 views (61.2%). All accepted operations were correct. It also sent every one of 448 views of 28 historical plates, covers and blank/show-through pages for review. On the printed inputs, the top proposal before rejection was correct on 1,072 of 1,088 book views, 768 of 768 newspaper views, and 2,062 of 2,224 contemporary views. The evaluation simulates mirror transformations on actual scans. Performance on a population of naturally reversed photographs remains unmeasured. Eight-way predictions

The baseline runs the original four-class model twice on the same selected patches, once as observed and once with each patch mirrored. It picks the strongest of the eight scores. For the table we rank baseline proposals by score within each cohort and keep the same count as the eight-class model at 0.70. The two-pass method normalizes each four-class pass separately and can confidently choose the wrong handedness. At its own 0.90 cutoff, it incorrectly proposed a reflection on 68 unreflected printed views across the three cohorts. Baseline predictions

We set the primary 0.90 cutoff before evaluation. Its low coverage prompted a second operating point. On 24 separate historical development pages we tested 0.70 through 0.90 in increments of 0.05 and chose the lowest threshold with zero accepted errors on print and zero accepted proposals on review pages. At 0.70, the model accepted 207 of 240 printed views and sent all 144 review views for inspection. We examined the original cohorts' primary results before this second analysis. The radio-bulletin check below used the selected cutoff without further tuning. Development predictions

External check on typewritten radio bulletins

We tested every one of the 205 pages in the 1937 radio-bulletin set from the National Library of the Netherlands. These scans include faint type, short entries, strikeouts and handwritten additions. For each source we tested the unaltered view and one hash-assigned transform with no added tilt. The eight assigned labels occur 25 or 26 times each. The reference is the page's orientation as distributed. IMPACT Project, IMPACT KB Ground-truth, KB Lab, The Hague. The library makes the set available in the public domain. Collection and rights

Bulletin condition Views Top proposal correct before rejection Correct / accepted at 0.70 Accepted at 0.90
Original page 205 197 16 / 16 0
Assigned transform 205 160 8 / 9 0

On page 00537249.tif, the 0.70 setting would mirror a rotated, unreflected scan. Coverage on this collection is too low to automate a typewritten archive with the released settings. Page-level results and source archive checksums

For typewritten material, an optional checkpoint adds synthetic monospaced training. On a separate set of 87 French source pages, it accepted 190 of 696 transformed views at 0.70, compared with 57 for the general model. Every accepted transform in both sets was correct. Typewriter checkpoint, limitations and usage

Reproduce

The eight-way model uses the same patch sampler and convolutional backbone as the rotation model, with eight output logits. It trains for 5,000 steps on the synthetic tile bank, with a random left-right flip applied after rotation to half the training crops. The released ONNX is 432,883 bytes, SHA-256 0900da82c9d10b04fc381b1195aa82fbde2adc086b26fd17c43926ccc532f45a. We checked the export against PyTorch for batch sizes 1, 5 and 24.

python training/synthetic.py tiles.npy --font-dir fonts
python training/train.py tiles.npy mirror-checkpoint --steps 5000 --reflection
python training/export.py mirror-checkpoint/weights.pt rebuilt-mirror.onnx --reflection

Prepare the three page folders using the sampling commands in the evaluation notes. The following commands run from the model repository root. The separate bulletin check downloads two public archives totaling about 986 MB to a temporary directory. It writes scores and source identifiers. Add --sheets to inspect source thumbnails locally.

python evaluation/evaluate_reflection.py --historical scans --historical-manifest evaluation/pages.json --modern contemporary --newspapers newspapers --model mirror.onnx scores.json
python evaluation/evaluate_reflection.py --historical scans --historical-manifest evaluation/pages.json --modern contemporary --newspapers newspapers --model model.onnx --two-pass-baseline baseline.json
python evaluation/evaluate_radio.py --baseline model.onnx --model mirror.onnx radio-results