Detect and confirm page orientation during automatic splitting

Bundle the trained heatmap/image orientation model and run it after provisional
blank-page and split analysis. Confirm candidates with Gemini using low thinking;
resolve mixed A3 sheet ordering with the complete statement and medium thinking.
Keep A4 scans independent and export paired A3 scans in booklet reading order.

Reanalyse uniformly inverted source sheets, validate ordering permutations, and
preserve source provenance, corrected rotations and reading slots in manual review.
Document the experiments, rejected approaches, calibration and runtime limits.

Validation: focused splitting/orientation regression suites pass; bundled model
checksum and wheel assets verified. Full Interro07 offline run: 44.26 seconds,
eight candidates, mocked upright confirmations, source hashes and geometry intact.
This commit is contained in:
sebastien committed 2026-11-04 21:07:20 +01:00
1 parent 78f3e4b2f0
commit 0a4f4a80d0
21 files changed
+1574 -51

No files matched your search

+77 -3
View File
@@ -5,7 +5,9 @@
The “Séparer et réordonner les pages” step now offers **Manuellement** and
**Automatiquement**. Automatic processing runs in the background using the
standard splitter, publishes PDFs in `Copies`, and preserves source scans in
`Copies Originales`. It retains source-page order. The parallel-copy setting
`Copies Originales`. A4 scans keep their positions; consecutive A3 scans are paired
and exported in booklet reading order (outer right, inner left, inner right,
outer left). The parallel-copy setting
appears only for automatic processing. Existing single-copy and marked-copy
selection work in both modes.
@@ -14,7 +16,7 @@ preserved original. The editor displays independent overlapping output crops
and crosses out removed blank pages. Advancing accepts the displayed decision;
the existing keep/discard shortcuts override it. Moving a splitter resumes
manual splitting on that page. Reviewing automatic proposals preserves their
source-page order; the original manual workflow retains its booklet ordering.
saved reading order, source rotations and individual-half corrections.
Proposals are reused only while both source and output fingerprints still match.
Each automatic run saves its inputs, report/gallery, and previous outputs under
@@ -59,10 +61,82 @@ run may leave partial outputs; use a new output directory for a subsequent run.
## Decisions
### Orientation and physical sheets
After provisional blank/middle detection, each nonblank logical page is screened
using the bundled conservative orientation model. Model weights and dependencies
are included in the installed package; no runtime model download is required.
Gemini confirms only candidates, returning `upright`, `inverted` or `inconclusive`.
An inconclusive reply is treated as upright for the remainder of the run.
`MODEL_FOR_PAGE_ORIENTATION` can select the confirmation model; its default `None`
inherits `MODEL_FOR_LABEL_ID`. Orientation confirmations use low thinking;
page-ordering requests use medium thinking. Both use structured responses.
A4 scans are independent, even when adjacent: correct a confirmed inversion and
keep its position. A3 scans are paired only when consecutive; an unpaired A3 scan
is flagged for manual review. Once an A3 inversion is confirmed, Gemini checks
all other nonblank pages on that sheet, including pages the local model did not
flag. Blank logical slots remain available until sheet ordering is resolved.
- If every logical page is blank or confirmed inverted, turn both source faces
180°, recompute blank/middle/overlap decisions, then apply normal booklet order.
- Otherwise turn only the inverted logical pages. Send the full evaluation
`enonce.pdf` (including supported filename aliases) and all sheet images,
including blanks, to Gemini for a reading-order permutation. Validate that every
slot occurs exactly once, then remove blank pages from the final export.
A request failure, malformed orientation reply, invalid permutation or missing
énoncé for mixed-sheet ordering aborts analysis before publication. These errors
are not interpreted as inconclusive handwriting. Existing copies and originals
remain untouched. The report stores screen scores, replies, source/logical
rotations, physical-sheet grouping and final page numbers. Manual acceptance
preserves that order; reinstating a blank proposal restores its reading-order slot.
The standalone analysis tool remains a geometry-only baseline by default. To
exercise orientation and booklet ordering without publishing into `Copies`:
```sh
python -m copienator.auto_page_split 'EVALUATION/Copies Originales' tmp/oriented-review \
--workers 4 --orientation --statement EVALUATION/enonce.pdf
```
See [the decision record](page_orientation.md) for the trained model, rejected
approaches and calibration limits. Its earlier timings exclude live Gemini latency.
### Integration verification (2026-11-04)
The maintained implementation passes 65 focused tests, including A4 independence,
A3 booklet order, blank siblings, inconclusive replies, full-source reanalysis,
mixed-sheet image rotation and full-statement ordering payloads, invalid response
rejection, failure preservation, and manual acceptance/reinstatement/secondary cuts.
Wheel inspection confirms that weights, settings, ONNX model and license ship with
the package.
An isolated full Interro07 run took **44.26 seconds** with four workers: 47 PDFs,
214 scans, 347 kept pages, 73 blank halves removed, eight detector candidates.
Gemini replies were mocked as upright; no live requests were sent. Source hashes,
blank decisions and boundaries match the reference; output page mappings match
the new reading order. This checks local computation and request routing, not
live Gemini accuracy or latency. The evidence is under
`tmp/orientation-implemented-interro07-verified/` (`timing.json`, `report.json`,
`verification.json`).
A subsequent one-off live check of precisely those eight candidates used
`gemini-3.8-flash`, thinking `low`, four concurrent workers and no retries.
Gemini classified **all eight as upright**, with no inconclusive or inverted
verdicts. Request wall time was **9.64 s**, with individual calls taking
**3.33–5.96 s**. No sibling investigations or ordering calls were needed, and no
evaluation PDFs were modified. This was a separate confirmation-only run, not a
measured complete splitting run with live latency included. Exact source crops,
per-page scores/verdicts/times and unchanged-source verification are saved under
`tmp/orientation-eight-live-flash/` (`results.json`).
Displayed physical dimensions distinguish A3 landscape spreads from A4 sheets,
with a 5% tolerance. A4 landscape sheets are also retained whole. Unexpected
sizes and portrait A3 pages are retained whole and flagged. PDF rotation is
respected; handwriting orientation is not inferred.
respected. Automatic evaluation processing additionally applies the orientation
and physical-sheet rules above; standalone analysis requires `--orientation`.
The standard splitter renders a 75 dpi overview, accepts clearly written pages,
and analyses a central 10% strip at the requested 200 dpi for overflow ownership.
+62
View File
@@ -0,0 +1,62 @@
# Interro07 orientation experiment — 2026-11-04
Historical experiment. Start with the [orientation decision record](page_orientation.md)
for the current recommendation, rejected approaches and links to later validation.
All scripts, manifests, model hashes, raw scores, test PDFs and galleries are in `tmp/orientation-benchmark/`. Paths below are relative to that experiment directory.
The best tested screen combines the official PaddlePaddle PP-LCNet_x1_0_doc_ori ONNX model on a full page and an ink-bounding crop, each in both orientations. At exploratory threshold 0.52: 61/64 inverted examples detected, 1/64 upright false positives; 24/24 inverted sparse pages detected. On the randomly mixed set, all 33 inverted pages detected and 1/31 upright false positives. Across 347 unrotated pages, 16 flags (4.61%). The 18 flags at threshold 0.5 were visually inspected and appear upright.
## Dataset
Source: saved automatic split outputs in ../auto-page-split-interro07-fast-final-20261103/pdfs (47 copies, 347 retained logical pages). Full sheets at 75 dpi, before margin cropping. 24 pages with largest blank vertical gaps, plus 40 seeded random remaining pages (seed 73107), spanning 36 copies. All 64 selected originals visually checked in contact-0.jpg through contact-3.jpg. Sparse subset includes a single exercise heading, very faint notes, equations, and writing occupying only the top or bottom of a sheet. Selected maximum blank runs range from 60% to 97% of height. Gap detection is a simple dark-ink projection, not a ground-truth whitespace segmentation.
sample.json records a random 0/180° assignment (33 rotated, 31 unchanged). rotated-pdfs/ contains copies of the selected PDFs with those actual PDF rotations applied. Other pages in those PDFs are unchanged. Mixed sample gallery: index.html. Paired testing additionally evaluates every selected page at both 0° and 180° (128 examples). This avoids drawing conclusions solely from lucky random assignment.
Original evaluation PDFs were only read. All artifacts are isolated here. Input PDFs for the measured splitter trial are Interro07/Copies Originales; its experimental exports go to batch-with-orientation/.
## Results
| Screen | Rotated detected /64 | Upright false alarms /64 | Sparse rotations detected /24 |
|---|---:|---:|---:|
| Official page preprocessing (short side 256, centre crop 224) | 51 | 4 | 16 |
| Whole sheet resized to 224 square | 50 | 1 | 19 |
| Up to five detected writing components, small text-line classifier | 30 | 7 | 10 |
| Top/bottom ink-margin asymmetry | 49 | 16 | 23 |
| Ink crop resized to square | 47 | 0 | 19 |
| Whole sheet, both orientations | 55 | 1 | 19 |
| Ink crop, both orientations | 55 | 0 | 23 |
| Maximum of whole-sheet and ink-crop paired scores, threshold .52 | **61** | **1** | **24** |
| Tesseract OSD (180° output) | 9 | 23 | — |
Tesseract returned no orientation on 29/64 upright and 28/64 inverted examples; 128 calls took 6.07 seconds with four workers. These failures are counted as missed rotations, not upright successes. Its sample runtime extrapolates to about 16.5 seconds for 347 pages, but that is not a measured full-batch time.
All neural and layout scores above use threshold .5 except layout (>0) and combined (.52). Combined .5 catches the same 61, with the same one sample false alarm, but flags 18/347 original pages (5.19%); .52 reduces that to 16/347. Threshold .55 catches 60/64 with 13/347 full-batch flags.
Missed by the combined .52 screen: Copie01-p03, Copie03-p07, Copie36-p08. They are predominantly formula-heavy; none belongs to the sparse subset. These may still be flagged through a companion half of the same scanned spread, but no credit for that was included.
## Algorithm and timing boundaries
ONNX Runtime 1.31.1 CPU execution provider; Ryzen 7 PRO 7840U; one inference thread per session. Both sessions load in 0.034 seconds. Models are official PaddlePaddle Hugging Face exports; models/*.yml contain their supplied preprocessing configuration, model-hashes.json pins actual downloaded bytes.
Paired score = (P180(image) + P0(rotated image))/2. Combined score = max(paired full-page score, paired ink-crop score). This is an empirical ranking score, not a calibrated probability. Crop bounds use .5/99.5 percentiles of pixels with min RGB <100, with 12-pixel padding; 15-pixel borders are excluded. The line model receives up to five large horizontally connected ink components resized to its official 160x80 input. This is a cheap extraction baseline, not a full text detector; its poor result does not establish that every line-based approach fails.
Separate full-corpus kernel timings: whole-page forward screen 1.43 seconds, resized-sheet screen 1.35 seconds, line extraction/classification 5.69 seconds, layout projection 3.91 seconds. Four combined model passes take about 6.52 seconds sequentially for 347 pages, excluding image loading and shared ink extraction/crop creation. Rendering the already split PDFs separately took 52.49 seconds and is deliberately avoided by reusing splitting previews.
integration.py temporarily wraps the splitter inside the benchmark process to reuse its already rendered RGB overview for each retained output. It records orientation_score in experimental reports and performs normal splitting/export/preview generation. No application source was edited. Its inputs include all 214 original scanned pages. See batch-with-orientation/timing.json for measured total time including process startup, detection, splitting, exports and previews. Scores from source overviews can differ slightly from separately rendered output PDFs due to raster alignment and overlapping crop boundaries; classification accuracy above is measured on the saved split-page test set.
## Interpretation
The combined model is the best measured candidate. At this exploratory operating point it satisfies the desired low referral volume on this batch and catches 95.3% of paired rotations, including all sparse examples tested. LLM confirmation should check every flag; no LLM requests were made in this experiment.
The variants and .52 threshold were explored on this same evaluation. These numbers are descriptive, not independent validation or a guarantee for another evaluation. Full-corpus referral counts use the existing scans as the presumed upright baseline; selected examples and all combined flags were visually reviewed, not every other page. New evaluation/student holdout data should determine the eventual threshold. Referral rate also depends on the true proportion of inverted pages: when testing a deliberately half-inverted set it should exceed 5%.
## Reproduce
Use a Python environment with project dependencies and onnxruntime. Run prepare.py, bench.py, variants.py, report.py. bench.py overwrites results.json; variants.py appends its extra scores. `python integration.py with-orientation` runs the full splitter trial; its output directory must not already exist. baseline runs the same splitter without the wrapper. Model URLs: https://huggingface.co/PaddlePaddle/PP-LCNet_x1_0_doc_ori_onnx and https://huggingface.co/PaddlePaddle/PP-LCNet_x0_25_textline_ori_onnx .
## Measured full splitting run
Matched runs with four worker processes: baseline 36.55 seconds; combined orientation screen 39.62 seconds; added time 3.07 seconds. One run of each, sequentially, on the same machine, not a statistical timing distribution. An earlier baseline attempt was interrupted and excluded.
Integrated screen flagged 15/347 retained pages (4.32%) at .52. The separate saved-PDF screen flagged 16/347 (4.61%); small raster/crop differences explain why these are distinct measurements. Export boundaries and all 420 keep/remove decisions match the existing reference report exactly. Verified all 33 PDF rotations and all 47 original source hashes (verification.json). LLM confirmation latency is not included.
+80
View File
@@ -0,0 +1,80 @@
# Expanded orientation experiments — 2026-11-04
Historical experiment. Start with the [orientation decision record](page_orientation.md)
for the current recommendation, rejected approaches and links to later validation.
Artifacts, models, scripts and raw scores: `tmp/orientation-expanded/`.
The main improvement is substantially more labelled-by-construction training data and a richer spatial representation. The useful heatmap records both absolute page layout and layout after cropping around the writing; a second dark-ink threshold exposes fainter strokes. A learned combination of heatmap scores and the pretrained orientation model's full output distribution is stronger than the previous maximum-of-two-scores rule.
## Data and evaluation
Added Interro05's 406 processed pages and Interro06's 390 processed pages (796 total). Their existing CropBoxes were reset to the full MediaBox in memory before rendering at 75 dpi, preserving the stored PDF rotations. The files themselves were not modified. This recovers blank margins needed for layout analysis. Interro07 uses the same 347 saved automatic-split pages as previous experiments. Every page supplies an original and a synthetic 180° rotation: 2,286 examples total.
Primary experiment: fit models on Interro05, select thresholds using Interro06, evaluate Interro07. Thus Interro07 is absent from fitting and threshold calibration in this round. It is not an untouched final benchmark: earlier experiments and model-family choices already used Interro07. The assessments also come from the same cohort, so this is not an unseen-writer evaluation.
For fusion training, the heatmap classifier produces five-fold out-of-copy predictions within Interro05. The fusion model trains on those held-out heatmap scores and the pretrained neural outputs. The heatmap classifier is then refitted to all Interro05 pages. This avoids training the fusion on unrealistically perfect in-sample heatmap scores. A separate full assessment calibrates thresholds.
The original images are presumed upright. The prior 64-page sample and 24 sparse examples were visually reviewed previously. A new 24-page random sample from each added assessment was visually checked; all 17 flags of the earlier fusion candidate were also reviewed and appear upright. This includes the final fusion candidate's ten separate-image flags. Every page in the expanded corpus has not been independently annotated, so full-population counts retain this ground-truth limitation. Formula-only pages can be intrinsically ambiguous.
## Separate-image results and runtime
Times below are measured detection time for all 347 Interro07 previews, excluding image loading, PDF rendering, model loading and one-time training. The complete feature extraction and per-page prediction calls are included. Image-file loading added 2.77 s in this standalone benchmark; integration reuses the existing splitter previews. The previous fixed image-rule time is represented by the neural feature cost; it has no material learned-head cost.
| Method | Calibration false-alarm target | Rotations detected /347 | Upright flags /347 | Sparse detected /24 | Prior sample detected /64 | Detection time /347 pages |
|---|---:|---:|---:|---:|---:|---:|
| Previous fixed image score | fixed .52 score | 313 | 16 (4.6%) | 24 | 61 | 8.16 s |
| Global heatmaps | 4% | 325 | 12 (3.5%) | 20 | 55 | 2.28 s |
| Global + writing-crop heatmaps | 4% | 343 | 16 (4.6%) | 22 | 61 | 2.85 s |
| Global + writing-crop + faint-ink heatmaps | 4% | 343 | 8 (2.3%) | 23 | 62 | 4.14 s |
| Learned combination of neural outputs | 2% | 328 | 15 (4.3%) | 24 | 64 | 8.16 s |
| Rich heatmap + image outputs, tree fusion | 2% | 346 | 10 (2.9%) | 24 | 64 | 13.51 s |
The new tree fusion detects 346/347 synthetic rotations (99.7%) with ten upright flags (2.9%). Its remaining separate-image miss is Interro07 Copie05, logical page 1. The rich heatmap alone detects 343/347 with eight flags; it is the inexpensive alternative.
Both richer heatmaps and the fusion recover the three misses of the original classifier on separate images. This is stronger evidence than the earlier 64-page experiment, although most of the larger population still relies on presumed orientation labels.
The second ink threshold is 140 rather than 100 on the minimum RGB channel. Every map uses normalized square-root ink density on 8×12 and 16×24 grids. The rich vector concatenates the two grids for the full dark-ink page, the dark-ink writing crop, and the full page at threshold 140. It has 1,440 features. Long-line grid removal was not used.
The heatmap classifier is a standardized RBF SVM (C=3). Neural features retain all four orientation probabilities for full-page and writing-crop views, each supplied and rotated: sixteen numbers. Fusion appends one out-of-fold heatmap score to those sixteen outputs. The final classifier is ExtraTrees with 200 trees, minimum leaf size 5, random seed 514. It predicts a screening score, not a calibrated probability. The primary fusion threshold is 0.2769861111, selected from the upright Interro06 score tail for a 2% calibration target; the rich heatmap threshold is -0.0486107703, from the 4% calibration target.
A learned logistic head over neural outputs alone also improves over the previous arbitrary maximum rule, at essentially the same inference cost. The complete exploratory threshold curves and other rejected candidates are in results.json and multi-fusion-results.json; no unsuccessful models were removed from the raw logs.
## Full splitting trial
The experimental wrapper ran the standard splitter on all 47 original PDFs / 214 scanned pages, adding both the rich heatmap and fused scores to each retained output. It also scored a simulated 180° version of every retained page without repeating neural inference: the four neural view outputs are reused in reversed order. Thus the full timing includes orientation auditing for both directions. No LLM calls are included.
| Full Interro07 run | Rotations detected /347 | Upright flags /347 | Total runtime |
|---|---:|---:|---:|
| Earlier splitting baseline | — | — | 36.55 s |
| Earlier fixed image screen | — | 15 | 39.62 s |
| New combined trial, rich heatmap scores | 343 | 8 | 48.58 s (shared trial) |
| New combined trial, fused scores | 345 | 11 | 48.58 s (shared trial) |
These are individual timing runs, not statistical estimates. The two new rows come from one run that calculates both screens; no standalone rich-heatmap-only full-pipeline timing was made in this round. The earlier rows come from preceding experiments, so the time difference should not be treated as a precise isolated overhead measurement.
In the actual splitter previews, the fused screen catches 345/347 rotations with 11 upright flags (3.2%); it catches 63/64 of the prior sample and all 24 sparse rotations. The two misses are Copie01-p03 and Copie05-p01. Separate-image rendering detects 346/347 with ten flags. Small raster phase/crop differences change borderline outcomes. The rich heatmap is stable at 343 detected and eight upright flags across the two rendering paths.
All 420 keep/remove decisions and all output rectangle coordinates match the prior baseline exactly. All 47 original source hashes still match; 347 logical pages remain, with 73 blank halves removed. Scores were recorded only in isolated experimental outputs. Application code and evaluation PDFs were not changed.
A simple union of rich-heatmap and fusion flags on live previews catches all 347 rotations, but gives 18 upright flags (5.19%), just above the requested 5% volume. A stricter union using the heatmap's 2% calibration threshold catches 346 with 15 flags (4.32%). These post-hoc unions are diagnostic comparisons, not independently selected deployment thresholds. A grid search calibrated entirely on Interro06 did not improve on the fusion screen at useful held-out referral levels (union-results.json); it selected effectively fusion-only rules.
## Rotating the assessment roles
The richer fusion was additionally tested with each assessment excluded from fitting and threshold selection in turn. Interro07 calibration was used for the Interro05 and Interro06 test runs. The following table uses the same 2% calibration-target policy. Interro05/06 detection times are estimates from the measured Interro07 per-page cost; full-pipeline runtimes were not measured for those two assessments.
| Test assessment | Training assessment | Threshold assessment | Rotations caught | Upright flags | Detection time |
|---|---|---|---:|---:|---:|
| Interro05 | Interro06 | Interro07 | 402/406 | 5/406 (1.2%) | ≈15.8 s (estimate) |
| Interro06 | Interro05 | Interro07 | 389/390 | 4/390 (1.0%) | ≈15.2 s (estimate) |
| Interro07 | Interro05 | Interro06 | 346/347 | 10/347 (2.9%) | 13.51 s (measured) |
This supports generalization across these three assessments, but not across unrelated paper layouts, cohorts, scanners or handwriting populations. Rotating examination roles is not the same as an untouched final test after model selection. All training, calibration and test roles and raw scores are retained for audit.
## Reproduce and inspect
`prepare.py` renders the two additional assessments. `extract.py` creates features.npz and timings.json. `evaluate.py` fits the initial candidates; `evaluate_multi.py` fits richer fusion. `reverse.py` and `reverse_multi.py` rotate assessment roles. `benchmark.py` measures complete per-preview screening code paths; this corrected an omitted faint-map resize cost in the initial extraction instrumentation. Use benchmark-timings.json, not early multi_heat timing estimates, for quoted final detection times. `integration.py fused_multi_trees` runs the full experimental splitter into a fresh output directory. It refuses to overwrite an existing result directory.
Model files are local trusted joblib artifacts; features, logits, thresholds, complete predictions, runtime logs and split reports remain under tmp/orientation-expanded. The representative assessment contact sheets are Interro05-sample.jpg and Interro06-sample.jpg. integrated-scores.json records each live-preview upright and rotated score. The separate-image missed page is Interro07-Copie05-p01-missed.png. Previous false-positive review contact sheets are fused-flags-0.jpg and fused-flags-1.jpg.
Recommendation: the rich heatmap is an attractive fast local screen. The learned fusion gives the strongest measured accuracy while staying below one minute and below 5% upright referrals in the actual Interro07 splitter trial. Use LLM confirmation on flagged pages; keep unresolved cases visible rather than rotating solely on this heuristic. A further assessment with reviewed orientation labels would be the appropriate next validation step.
+80
View File
@@ -0,0 +1,80 @@
# Orientation model trained across several interros — 2026-11-04
Detailed final training record. The [orientation decision record](page_orientation.md)
also explains the chosen approach, rejected alternatives and integration status.
The reusable model and standalone predictor are in `tmp/orientation-final/bundle/`; a ZIP is in `tmp/orientation-final/orientation-detector.zip`. The recommended default is the conservative profile. The experimental full Interro07 splitting run takes 50.92 seconds, including exports/previews and orientation checks, and refers eight upright logical pages (2.3%) for confirmation.
## Dataset and frozen design
2,762 full logical pages across the seven original assessments, each supplying an upright and a synthetic 180° example. Alternate Interro03 pilot folders were excluded as duplicates.
- Training: Interro01 (392), Interro02 (423), Interro03 (431), Interro05 (406): **1,652 pages**.
- Threshold calibration: Interro06: **390 pages**.
- Held-out assessment: Interro04: **373 pages**.
- Secondary comparison: Interro07: **347 pages**.
Processed PDFs were rendered at 75 dpi after restoring the full MediaBox in memory, retaining stored rotations. Source PDFs were not saved or modified. Interro07 uses the previously saved full logical-page outputs. Existing cached Interro05/06/07 features were reused; 1,619 additional pages were rendered/extracted for Interro01–04. Parallel extraction of the additional features took 19.19 seconds; PDF rendering took 27.85 seconds. These are one-time preparation costs.
The architecture was held fixed from the preceding experiment. A standardized RBF SVM (C=3, gamma=scale) learns the 1,440 spatial features. Those concatenate normalized square-root ink-density grids of 8×12 and 16×24, for (1) the full sheet with threshold 100, (2) a crop around that ink, (3) the full sheet with threshold 140 for fainter strokes. Borders of 15 pixels are excluded from ink extraction.
The pretrained PP-LCNet_x1_0_doc_ori ONNX model supplies sixteen probabilities: all four orientation classes from full and writing-crop views, each in its supplied and reversed orientation. An ExtraTrees classifier with 200 trees, minimum leaf size 5 and seed 514 learns to combine those probabilities with one heatmap SVM score. The fusion learns from **leave-one-training-assessment-out heatmap scores**, rather than optimistic in-sample SVM scores. This is stronger separation than leaving out individual pages. The final SVM is then fitted on all four training assessments. Learned model fitting took 20.76 seconds in total.
The design was saved before fitting; weights, threshold and hashes were saved in frozen-model.json before predicting Interro04. Neither Interro04 nor Interro07 was used for fitting or numeric threshold calibration. Previous work had used Interro07 to choose the architecture; it is consequently only a secondary comparison. The same cohort appears across assessments, so these are held-out-assessment tests, not unseen-writer tests.
## Direct prediction on actually rotated images
The final accuracy check runs the reusable predictor directly on original and 180°-rotated RGB page arrays. It recomputes the writing crop after rotation. This matters: percentile rounding can shift a crop edge by one pixel, so swapping the cached neural outputs is an approximation. An attempted exact equality check on inverted scores exposed that difference. All **720 original-page predictions** match the saved feature/reference scores within 1e-7; inverted-page results below are direct fresh predictions, not assumed equivalence.
Timing is measured screening time for the specified number of pages, averaged over one upright and one inverted pass. It includes complete ink extraction, crop creation, four neural views and learned prediction; it excludes file decoding, initial model loading and PDF rendering. File-loading timings and the separate upright/inverted passes are retained in benchmark-timings.json and direct-rotation-results.json.
| Profile | Test assessment | Rotations detected | Upright flags | Detection time for that assessment |
|---|---|---:|---:|---:|
| balanced | Interro04 | 373/373 (100.0%) | 19/373 (5.1%) | 14.91 s |
| balanced | Interro07 | 346/347 (99.7%) | 16/347 (4.6%) | 13.48 s |
| conservative | Interro04 | 372/373 (99.7%) | 11/373 (2.9%) | 14.91 s |
| conservative | Interro07 | 345/347 (99.4%) | 6/347 (1.7%) | 13.48 s |
The originally frozen **balanced** profile targets 2% upright flags on Interro06; threshold 0.1732301587. Its untouched Interro04 result catches all 373 rotations but flags 19 upright pages (5.1%). That is one page above a strict 5% budget of eighteen.
The **conservative** fallback targets 1% upright flags on Interro06; threshold 0.3044920635. Its numeric threshold uses only Interro06, but the decision to evaluate this fallback policy followed seeing the balanced holdout referral rate. Therefore its Interro04 result is an exploratory follow-up, not a second untouched validation. It is the recommended default because it stays below the desired referral volume on both measured assessments while retaining high recall. The weights are identical between profiles; only the decision threshold changes. No threshold was fitted directly to Interro04 or Interro07.
Conservative direct-image misses: Interro04 Copie16-p10; Interro07 Copie05-p01 and Copie48-p01. Rotated PNGs are saved with `-remaining-rotation.png` suffix. The original 64-page Interro07 sample and all 24 sparse-page rotations are detected by the conservative predictor.
A fixed previous model (trained on Interro05, calibrated on Interro06) was also evaluated using the stored feature augmentation: on Interro04 it caught 371/373 with 28 upright flags; the new balanced model caught 373 with nineteen. This comparison used cached rotated crops, not the final direct recropping test, and should not be substituted for the direct metrics above.
## Full automatic splitting trial
The experimental wrapper reuses the existing RGB overview and records scores for each retained logical page, without changing splitting or blank-removal behaviour. It also computes an inverted-feature score for auditing, reusing the four neural views. These inverted-feature scores approximate a direct re-render/recrop, as explained above. Timing therefore includes both normal screening and this inexpensive audit.
| Full Interro07 trial | Simulated rotations detected | Upright flags | Total time including splitting, previews, exports and startup |
|---|---:|---:|---:|
| balanced | 346/347 | 22/347 (6.3%) | 50.92 s (same run) |
| conservative | 345/347 | 8/347 (2.3%) | 50.92 s (same run) |
For the conservative profile, the actual splitter previews catch the same 345 rotations, refer eight upright pages, and preserve detection of all 64 prior sample pages and all 24 sparse pages. Differences between six separate-image flags and eight integrated flags come from raster phase and overlapping crop boundaries. The balanced profile produces 22 integrated flags and is above 5% for this run; this reinforces the conservative recommendation.
The complete measured run meets the one-minute budget on this machine (Ryzen 7 PRO 7840U, four copy workers, one neural inference thread per worker). This is a single run, not a guaranteed latency bound. An earlier attempt was interrupted with exit 143 and excluded from timing; the uninterrupted retry is the source of the quoted result. Initial bundled model loading took about 0.81 seconds in a separate standalone run. Training is not repeated during splitting. LLM confirmation time is not included.
All 214 source pages and 47 PDFs were processed, retaining 347 logical pages and removing 73 blank halves. All 420 keep/remove decisions and all output rectangles match the earlier baseline. All 47 original Interro07 hashes still match. No application source or evaluation PDF was changed. verification.json records these checks.
## Ground-truth limits and review
Original pages are presumed upright from the existing corrected workflow. A 24-page random sample from each newly added training assessment was visually reviewed. All nineteen balanced-profile Interro04 flags were visually inspected after the model was frozen and appear upright, including substantial clusters from heavily ruled monochrome copies. The prior Interro05/06 samples and Interro07 reviewed examples remain available. Every original page in the complete 2,762-page corpus has not been independently labelled; the full counts retain that limitation. Synthetic 180° rotations also do not cover every real scanner artifact. The model remains a screening stage whose flags should receive LLM confirmation.
More interros improved assessment generalization, but did not uniformly lower every error rate: the higher-recall profile has more referrals on Interro07 than the previous narrower model. The recommended conservative profile instead favours a manageable confirmation queue. The remaining errors are formula-heavy pages with weak or misleading layout cues.
## Saved artifacts and use
- `bundle/model.joblib`: final SVM and fusion classifier, trained on four assessments.
- `bundle/doc.onnx`: official PaddlePaddle PP-LCNet_x1_0_doc_ori neural model.
- `bundle/predict.py`: self-contained `Detector`; input is a full logical-page RGB uint8 array at 75 dpi. Reuse one detector per worker. Default profile is conservative.
- `bundle/settings.json`: profiles, thresholds, data roles, hashes and validation results.
- `bundle/requirements.txt`: runtime dependencies; scikit-learn and ONNX Runtime pinned to the versions used.
- `orientation-detector.zip`: complete standalone bundle.
Minimal usage: import Detector from predict.py, instantiate Detector(), then call detector.predict(image_rgb). The result contains flag, score, threshold and heatmap_score. Scores are screening signals, not calibrated probabilities. The bundle does not make LLM requests or alter PDFs.
The model files are locally generated joblib artifacts. PP-LCNet_x1_0_doc_ori is Apache-2.0 and comes from https://huggingface.co/PaddlePaddle/PP-LCNet_x1_0_doc_ori_onnx .
Preparation, fitting and validation scripts are in tmp/orientation-final. All data roles and the frozen architecture are in design.json; frozen-model.json records the primary model/threshold before holdout prediction; profiles.json explicitly records the later fallback policy. Training uses leave-one-training-exam-out stacking, not any held-out assessment. all-corpus.json, features.npz, results.json and direct-rotation-results.json retain the complete corpus and score provenance. integration.py reproduces the experimental full splitting run into a new directory; existing-output refusal applies.
+65
View File
@@ -0,0 +1,65 @@
# Writing-density orientation experiments — 2026-11-04
Historical experiment. Start with the [orientation decision record](page_orientation.md)
for the current recommendation, rejected approaches and links to later validation.
Artifacts and runnable scripts: `tmp/orientation-heatmaps/`. This extends the previous experiment in `tmp/orientation-benchmark/`.
The density-map classifier recovers all three rotations missed by the previous image classifier. It is complementary rather than a replacement: at the selected thresholds it catches fewer rotations overall. A simple union catches all 64 paired test rotations, but exceeds the desired referral volume.
## Accuracy and time
Detection time means measured feature extraction and prediction for all 347 logical pages, sequential CPU, excluding rendering, image-file decoding, training and application startup. The fusion times sum independently measured components and conservatively count their ink extraction twice. They are not measured full-pipeline times.
| Method | Detected /64 rotations | False alarms /64 upright | Sparse rotations /24 | Flags /347 original pages | Detection time for 347 pages |
|---|---:|---:|---:|---:|---:|
| Coarse heatmap, linear | 47 | 4 | 16 | 15 | 0.97 s |
| Fine heatmap, linear | 40 | 4 | 14 | 14 | 0.72 s |
| Two-scale heatmaps, nonlinear | 55 | 3 | 19 | 12 | 1.52 s |
| Two-scale heatmaps, grid-line cleanup | 54 | 3 | 19 | 12 | 2.01 s |
| Heatmap + line endpoints, nonlinear | 49 | 7 | 16 | 13 | 2.31 s |
| Heatmap + line endpoints, trees | 54 | 4 | 21 | 15 | 2.07 s |
| Previous image classifier | 61 | 1 | 24 | 16 | 11.17 s |
| Flag if either image or heatmap model flags | 64 | 4 | 24 | 28 | 12.70 s (sum) |
| Combined rule, recall-oriented calibration | 63 | 3 | 23 | 19 | 12.70 s (sum) |
| Combined rule, stricter calibration | 60 | 1 | 22 | 14 | 12.70 s (sum) |
The original-page flags correspond to 3.46% for the nonlinear heatmap alone, 4.61% for the previous image screen, 8.07% for their union, 5.48% for recall-oriented fusion, and 4.03% for stricter fusion. An intentionally inverted population naturally produces many more flags; these rates describe the presumed-upright originals.
## Full splitting runtime
| Run | Input | Time including splitting, previews, exports and startup |
|---|---|---:|
| Previous baseline without orientation | 47 PDFs / 214 source scans | 36.55 s |
| Previous image orientation screen | Same batch | 39.62 s |
| New heatmap screen | Same batch | 40.80 s |
The first heatmap timing was 40.32 s but overlapped a separate neural benchmark, so it was superseded by the isolated 40.80 s run. Timings are individual runs, not confidence intervals; the previous baseline and image-screen times come from the preceding experiment. The new screen has not demonstrated a lower complete splitting time despite its cheaper detection kernel. File decoding during the feature experiment was an additional 2.63 s; separate neural-screen file loading was 2.68 s. Normal integration reuses already rendered previews. No LLM requests were made.
## Features and evaluation
A fixed min-channel <100 ink mask excludes 15-pixel image borders. This is cheap dark-ink extraction, not the application's expensive full content analysis. Normalized square-root ink-density grids at 8×12 and 16×24 form the best heatmap representation. Coarse/fine linear models use 8×12 and 24×32 grids. Normalization removes total-ink variation while retaining relative spatial distribution. Grid cleanup removes long horizontal/vertical strokes; it slightly worsened quality. Layout models add 48 horizontal bands describing left/right ink limits, occupied columns and ink density, plus 16×24 histograms of line start/end positions after inexpensive horizontal component grouping.
Models: standardized logistic regression C=.1; standardized RBF SVM C=3, gamma=scale; ExtraTrees with 160 trees, min leaf size 3, max_features=.6. Models are intentionally small. Paired orientation scoring was also tested (results.json): it gave no improvement for heatmap-only models and worsened the layout SVM.
All 347 saved logical pages and their synthetic 180-degree rotations supply training/evaluation features. Five folds separate entire student copies. Inside each outer training fold, a separate 25% of student groups calibrates the threshold; the remaining groups fit the classifier. Outer test students appear in neither fitting nor threshold calibration. The primary 64-page test subset is the previously visually reviewed sample, containing the 24 largest vertical whitespace gaps plus 40 random pages; each tested both ways. Thus every primary reported heatmap prediction is from a model that has never trained on that student.
Calibration for individual models targets at most about 4% upright flags on the inner calibration subset (finite-sample rounding applies). Fusion tries a small fixed grid of neural thresholds and heatmap calibration quantiles, selecting maximum calibration recall under a 4% or 2% upright-flag constraint. Calibration controls the observed inner subset only; it does not guarantee a held-out rate. fusion-target4.json and fusion.json record the fold-specific selected thresholds. In one fold the recall-oriented fusion selected heatmap-only screening.
Quality results are out-of-fold. The separately saved timing-model.joblib is fit on all pages solely to measure integration speed; none of its in-sample predictions contributes to the quality results. Training a heatmap model takes a fraction of a second; all five SVM fold fits together took 0.27 s.
On the larger, provisionally labelled 347-page population, the nonlinear heatmap catches 299/347 rotations; the previous image screen 313/347; union 342/347; recall-oriented fusion 338/347; stricter fusion 319/347. These figures rely on the original orientation assumption. The 64 selected pages and previous model flags were visually checked in the preceding experiment, but every other page was not independently annotated.
## Interpretation and limitations
Heatmaps carry orientation information even when letters are unreadable. They recover Copie01-p03, Copie03-p07, and Copie36-p08, all three previous misses. They also miss other sparse pages: their 19/24 sparse recall is below the prior image screen's 24/24. No tested rule simultaneously improves the prior primary-sample recall and stays below 5% referrals over the entire original corpus: recall-oriented fusion is close at 5.48%, while the stricter variant has lower sample recall.
These are exploratory comparisons on one evaluation, with relatively few students per inner calibration set and many tried feature variants. Student grouping prevents direct student leakage, but choosing a preferred method after looking at these results still needs validation on another evaluation. Training/evaluation on the same assessment can learn its paper/layout conventions; there is no evidence yet for generalization to other assessments. The original synthetic pairing makes layout reversal systematic; real scanner artifacts and naturally inverted scans need later validation.
![Heatmap examples](../tmp/orientation-heatmaps/heatmap-examples.png)
## Artifacts and reproduction
`experiment.py` extracts all features and runs six primary models; `paired.py` evaluates symmetric scoring; `compare_neural.py` recomputes full neural scores and complete preprocessing/prediction runtime; `fusion.py` calibrates combined rules (currently target 2%); `features.npz` holds fixed feature vectors; `results.json` holds every out-of-fold score, fold assignment, flag, threshold and timing. `fusion-target4.json` retains the 4% fusion experiment. `visualize.py` creates the heatmap comparison. `integration.py heatmap-isolated` reproduces the full splitter timing into a new directory (existing-output refusal applies). The model is loaded with joblib; only load trusted locally generated models.
No application source was changed. Full-run boundaries and all keep/remove decisions were verified against the prior splitter reference; all 47 source hashes still match.
+283
View File
@@ -0,0 +1,283 @@
# Detecting upside-down handwritten pages
Decision record, 2026-11-04. The recommended experiment combines writing-density
heatmaps with a small pretrained image-orientation classifier, then sends flagged
pages for LLM confirmation. The trained bundle defaults to a conservative
threshold. It is now enabled in the automatic evaluation splitting step; see
[the integration rules](auto_page_split.md#orientation-and-physical-sheets).
The experiment results below predate live confirmation integration and exclude
Gemini latency.
## Requirements and result
The target is to process all of Interro07 in less than a minute, including normal
splitting. Recognition of the handwriting is unnecessary. Some missed rotations
are acceptable; false alarms can be checked individually, provided referrals stay
around or below 5% on an ordinarily upright batch. That percentage is a workload
target, not a cap to enforce by silently dropping low-ranked candidates. A batch
with many genuinely inverted pages should produce more referrals.
The final experimental splitting run processed 47 original PDFs / 214 scans,
retaining 347 logical pages and removing 73 blank halves, in **50.92 seconds**.
The conservative screen flagged eight upright logical pages (2.3%). Its simulated
inversion audit detected 345/347 rotations, including all 24 sparse examples and
all 64 pages from the initial test sample. Output rectangles, keep/remove decisions
and original source hashes matched the baseline.
Direct checks of the reusable predictor on actual rotated RGB arrays give:
| Profile | Assessment | Rotations detected | Upright false alarms | Screening time for that assessment |
|---|---|---:|---:|---:|
| Conservative, recommended | Interro04 | 372/373 | 11/373 (2.9%) | 14.91 s |
| Conservative, recommended | Interro07 | 345/347 | 6/347 (1.7%) | 13.48 s |
| Balanced | Interro04 | 373/373 | 19/373 (5.1%) | 14.91 s |
| Balanced | Interro07 | 346/347 | 16/347 (4.6%) | 13.48 s |
Screening times average one upright and one rotated pass, excluding file loading,
PDF rendering and model startup. Both profiles use the same weights and computation.
The 50.92-second full-run timing includes splitting, previews, exports, startup
and an inexpensive inverted-feature audit, but excludes LLM confirmation. It was
measured once on a Ryzen 7 PRO 7840U with four copy workers and one ONNX inference
thread per worker; it is not a latency guarantee. Separate-image and splitter
preview counts differ because their raster alignment and clip boundaries differ.
## Recommended algorithm
Operate on each retained logical A4 page independently, reusing the splitter's
existing **75 dpi RGB overview**. A complete A3 spread makes writing too small and
can mix orientation evidence from its two halves. Preserve the source-page
grouping for later confirmation and repair.
1. Respect the PDF's displayed rotation and retain the full logical page area.
Existing margin crops would remove useful layout evidence. During dataset
preparation, full MediaBoxes were restored in memory without saving changes
to the PDFs.
2. Extract a cheap ink mask: a pixel counts as ink when its minimum RGB channel
is below 100. Exclude the outer 15 pixels on each edge. This is not the full
content/blank detector; do not use this orientation mask to discard pages.
3. Define a writing crop using the 0.5th and 99.5th percentiles of ink coordinates,
with 12 pixels of padding. The implementation floors both percentile endpoints
and uses an exclusive upper bound of `endpoint + 13`. If fewer than five ink
pixels exist, use the full page. See the rounding caveat below.
4. Build density grids at **8×12 and 16×24** for three views: the full threshold-100
mask, its writing crop, and the full-page mask at threshold 140 for faint ink.
Area-resize each mask, take the square root of the densities, and divide each
grid by its L2 norm plus `1e-6`. Concatenate the six grids: **1,440 features**.
5. A standardized RBF SVM (`C=3`, `gamma="scale"`) converts those features into
one layout score. Global maps preserve margins and blank space; crop-relative
maps preserve line/block arrangement when writing occupies a small area.
6. Run **PP-LCNet_x1_0_doc_ori** on the RGB full page and writing crop, each in its
supplied orientation and rotated 180°. Resize each view directly to 224×224;
normalize RGB channels with means `[.485, .456, .406]` and standard deviations
`[.229, .224, .225]`. Retain all four orientation probabilities from each view:
**16 neural features**. No text recognition is performed.
7. Append the SVM score to those 16 features. An ExtraTrees classifier with
200 trees, minimum leaf size 5, `max_features=1.0`, and seed 514 predicts the
final screening score. Use `score > threshold`, not `>=`.
8. The conservative threshold is **0.3044920634920634**; the balanced threshold is
**0.17323015873015868**. These values belong to the saved weights and feature
pipeline. Retraining or changing preprocessing requires recalibration.
The final score is not a calibrated probability. Confirm flagged pages before
rotating them. Formula-only, blank and unreadable pages can remain unresolved.
Do not let an uncertain orientation score change blank-page decisions.
An upside-down A3 source reverses its two halves as well as their orientation.
Retain provenance and use the existing orientation-repair logic when applying
confirmed corrections; rotating each exported half alone can leave their order
wrong. The automatic splitter now applies the confirmed-sheet rules before export.
## Training and validation
All seven original interros contributed to preparation: **2,762 logical pages**,
each with a synthetic 180° counterpart. Duplicate Interro03 pilot folders were
excluded.
- Fit on Interro01 (392), Interro02 (423), Interro03 (431), and Interro05 (406):
**1,652 original pages**.
- Set numeric thresholds using Interro06: **390 pages**.
- Reserve Interro04: **373 pages**. Check Interro07 separately: **347 pages**.
To train the fusion, fit the heatmap SVM on three training assessments and score
the fourth, repeating for all four. Train ExtraTrees using these held-out-assessment
SVM scores and the pretrained neural features. Finally refit the SVM on all four
training assessments. In-sample SVM scores would give the fusion an unrealistically
easy training problem. Complete learned-model fitting took **20.76 seconds**;
training is not repeated during splitting.
The balanced weights and threshold were frozen before predicting Interro04.
The conservative fallback was evaluated after the balanced profile slightly
exceeded the holdout referral budget. Its numeric threshold still comes solely
from Interro06, but its policy selection is a follow-up experiment, not a second
untouched holdout validation. Interro07 had already informed earlier architecture
choices. These assessments share a cohort, so the result does not establish
unseen-writer generalization.
Originals are presumed upright from the corrected workflow. Representative
samples, the initial 64-page set, its 24 sparse examples, and all nineteen balanced
Interro04 false alarms were visually reviewed. The entire corpus was not
independently labelled. Synthetic rotations do not cover every real scanner fault.
## What was tried and why it was not selected
These stages used different training populations and thresholds. Their figures
are diagnostic records, not one common leaderboard. Historical rotated-feature
results are also subject to the crop-rounding caveat below.
### OCR, whitespace and pretrained classifiers
- **Tesseract OSD:** caught only 9/64 rotations and flagged 23/64 upright pages.
It returned no orientation for 29 upright and 28 rotated examples. Four workers
processed all 128 calls in **6.07 seconds**. It was fast enough but poorly matched
to sparse handwritten mathematics; unknown outputs must count as misses.
- **Top/bottom whitespace alone:** caught 49/64 rotations, but flagged 16/64 upright
pages and 139/347 corpus originals. Its full-corpus computation took **3.91 s**.
A single margin-asymmetry number discards line layout and mistakes legitimate
bottom-heavy writing for inverted pages.
- **Pretrained whole-page orientation:** the official short-side resize/centre-crop
preprocessing caught 51/64 rotations with four false alarms; resizing the entire
page to a square caught 50 with one. Their early measured wrapper times were
**1.43 s** and **1.35 s** for 347 pages, excluding shared file loading. Centre
cropping can hide sparse writing. These early partial timings must not be
compared directly with the final complete screening times.
- **Small text-line classifier:** voting over up to five cheaply extracted writing
components caught 30/64 rotations with seven false alarms; extraction and
classification took **5.69 s** for 347 originals. Equation fragments and ruling
make these imperfect text-line crops. This rejects that cheap baseline, not all
possible line classifiers.
- **Fixed whole-page/crop combination:** averaging `P180(image)` with
`P0(rotated image)` for each view, then taking the larger view score, caught
61/64 rotations with one false alarm. A complete later screen took **11.17 s**
for 347 pages. It recovered all sparse examples but missed three formula-heavy
rotations. Learning from all 16 neural outputs and a layout score worked better
than this hand-chosen maximum rule.
See [the first benchmark](orientation_benchmark.md) for all early variants.
### Initial heatmap experiments
Five-fold out-of-copy evaluation within Interro07 used the original 64-page sample.
Each time below includes feature extraction and prediction for 347 pages, excluding
file loading, rendering, training and startup.
| Variant | Rotations caught /64 | Upright false alarms /64 | Sparse rotations /24 | Screening time /347 pages |
|---|---:|---:|---:|---:|
| Coarse heatmap, linear classifier | 47 | 4 | 16 | 0.97 s |
| Finer heatmap, linear classifier | 40 | 4 | 14 | 0.72 s |
| Two scales, nonlinear SVM | 55 | 3 | 19 | 1.52 s |
| Two scales plus long-grid-line removal | 54 | 3 | 19 | 2.01 s |
| Heatmap plus line endpoints, nonlinear SVM | 49 | 7 | 16 | 2.31 s |
| Heatmap plus line endpoints, tree classifier | 54 | 4 | 21 | 2.07 s |
Finer grids alone did not compensate for a weak classifier. Removing long ruling
strokes slightly hurt quality and cost more time. Explicit line starts/ends did
not improve on the simpler two-scale SVM. Symmetric scoring of both orientations
gave no gain for heatmap-only models and worsened the line-layout SVM.
Heatmaps recovered all three original image-classifier misses, establishing useful
complementarity. However, a simple OR of both screens flagged **28/347 originals
(8.1%)** despite catching all 64 sample rotations. Its summed measured component
time was **12.70 s**. Inner-calibrated OR rules either retained too many referrals
or lost recall; a grid search on Interro06 later selected effectively fusion-only
rules. Catching every sample rotation was not sufficient to select a method.
See [the heatmap benchmark](orientation_heatmap_benchmark.md) for those folds and
threshold experiments.
### Adding other assessments and richer features
Training on Interro05 and calibrating on Interro06 gave the following Interro07
results. Times are complete measured per-page screening loops for 347 pages,
excluding file loading, rendering and startup.
| Variant | Rotations caught /347 | Upright flags /347 | Screening time /347 pages |
|---|---:|---:|---:|
| Global heatmaps | 325 | 12 | 2.28 s |
| Global plus writing-crop heatmaps | 343 | 16 | 2.85 s |
| Global, crop-relative and faint-ink heatmaps | 343 | 8 | 4.14 s |
| Learned neural-output combination alone | 328 | 15 | 8.16 s |
| Rich heatmap plus neural outputs, tree fusion | 346 | 10 | 13.51 s |
The crop-relative maps and second ink threshold were retained. Pure heatmaps were
an attractive inexpensive alternative, but neural evidence recovered additional
cases. Logistic and SVM fusion candidates did not offer a better overall
recall/referral tradeoff than the selected tree fusion at the tested calibration
policies. Their complete threshold curves remain in the raw experiment results.
That narrower tree-fusion model produced **345 detections and 11 upright flags**
in a **48.58 s** full splitting trial. Adding the richer heatmap flags by simple
OR caught all 347 simulated rotations but produced **18 flags (5.19%)**, just above
the workload target. Such post-hoc unions were not independently validated.
More training data improved the held-out-assessment result, but did not uniformly
reduce every error rate. The final balanced model has more Interro07 referrals
than this narrower model. Prefer the final conservative profile for the measured
workload, rather than assuming that more training always wins at a fixed threshold.
See [the expanded benchmark](orientation_expanded_benchmark.md) and
[the final training record](orientation_final_training.md) for the complete evidence.
## Measurement lessons and reproducibility
- **Actually rotate and recrop test images.** Flooring percentile bounds makes
crop creation non-equivariant by a pixel under 180° rotation. Swapping cached
views therefore does not exactly reproduce a fresh detector call on a rotated
page. The final direct-image results above supersede that approximation for
rotation accuracy; the full splitter audit remains explicitly labelled simulated.
- **Do not mix render paths silently.** Rendering an exported clipped PDF and
slicing the source overview can shift pixels. Measure the integrated path too.
- **Separate timing scopes.** Model-only inference, full screening, image decoding,
training and full splitting are different measurements. An initially omitted
faint-map resize cost was corrected in the expanded standalone benchmark.
Interrupted runs and a run overlapping another CPU benchmark were excluded from
the selected timings. Faster classifier kernels did not always produce faster
complete splitting runs.
- **Preserve full-page geometry.** Training on already cropped content would hide
the margins and blank vertical spaces the heatmap is intended to analyse.
- **Keep assessment/copy groups together.** A page and its rotated counterpart
must never straddle training and test sets. Use held-out scores when stacking
classifiers. Report which data selected thresholds and which selected policies.
- **Confirm before repair.** Keep source-page provenance, A3 half ordering and
unresolved verdicts intact; orientation screening is not proof of a safe repair.
The detailed records above are repository documentation. Large generated datasets,
models, galleries and most experiment scripts are under ignored `tmp/` and are
local artifacts, not assets guaranteed by a fresh checkout:
- `tmp/orientation-benchmark/`: initial sample, actual randomly rotated PDF copies,
paired scores, Tesseract results and reference full splitting runs.
- `tmp/orientation-heatmaps/`: first heatmap features, grouped folds and rejected
layout/fusion variants.
- `tmp/orientation-expanded/`: Interro05/06 features, cross-assessment candidates,
live-preview scores and timing corrections.
- `tmp/orientation-final/`: corpus, frozen design/weights, profiles, direct rotated
predictions, package checks, final full-run report and source verification.
The final folder's `prepare.py`, `extract.py`, `train.py`, `profiles.py`,
`benchmark.py`, `direct_rotations.py` and `integration.py` document the executable
workflow. They rely on earlier experiment artifacts and local assessment files;
they are not a clean-checkout training command. Existing-output refusal applies to
the full splitting experiments. `frozen-model.json` and `bundle/sha256.json`
identify the saved weights; `profiles.json` records the conservative follow-up.
The standalone bundle contains `predict.py`, `model.joblib`, `doc.onnx`,
`settings.json`, dependencies and its validation record. Instantiate one `Detector`
per worker and reuse it; pass full logical-page RGB `uint8` images at 75 dpi.
The returned fields are `flag`, `score`, `threshold` and `heatmap_score`. The
pretrained ONNX model is PaddlePaddle's
[PP-LCNet_x1_0_doc_ori](https://huggingface.co/PaddlePaddle/PP-LCNet_x1_0_doc_ori_onnx),
Apache-2.0. The recorded runtime uses scikit-learn 1.9.1 and ONNX Runtime 1.31.1.
## Integration into the maintained workflow
The runtime is maintained in `copienator/page_orientation_detector.py`, with assets
under `copienator/data/page_orientation/` and declared wheel dependencies. It no
longer depends on the local `tmp/` experiment folders. `copienator/split_orientation.py`
handles Gemini verdicts, physical-sheet grouping and validated ordering. The GUI
and `page-split --automatic` enable this after provisional geometry/blank analysis.
The standalone geometry tool enables it through `--orientation --statement ...`.
See [automatic splitting](auto_page_split.md#orientation-and-physical-sheets) for
A4/A3 rules, failure handling, provenance and manual-review behaviour. Unit tests
use mocked Gemini responses; local full-batch benchmarks can do the same. Such
benchmarks establish local computation time, not live API latency or LLM accuracy.