johnsonkuan commited on
Commit
1ea26b3
·
verified ·
1 Parent(s): 3b74ac0

Shorten README and link detailed usage and evaluation references

Browse files
Files changed (2) hide show
  1. README.md +27 -90
  2. docs/TRAINING_AND_EVALUATION.md +68 -0
README.md CHANGED
@@ -34,56 +34,32 @@ Use its routing score ahead of a larger layout model to identify pages that may
34
 
35
  | Property | Value |
36
  | --- | --- |
37
- | Architecture | ImageNet-pretrained MobileNetV3-Small with a nine-output classification head |
38
  | Parameters | 1,527,081 |
39
- | Deployment format | FP32 ONNX, 6,421,483 bytes (about 6 MB), with inline weights |
40
  | Input | RGB thumbnails, float32 NCHW, `(batch, 3, 224, 224)` |
41
  | Output | Nine raw logits, `(batch, 9)` |
42
- | Routing score | `sigmoid(logits[:, 8])` |
43
- | Operating threshold | `0.20900137722492218`, read from `config.json` |
44
- | License | Apache 2.0 |
45
 
46
- ## What the nine outputs mean
47
 
48
- Glancelot is **multi-label**, rather than a multiclass classifier that chooses one mutually exclusive category. A page can contain a table, a picture, and a formula at the same time. The model returns nine raw logits from one linear output layer. A sigmoid converts each logit to a score; the nine scores do not have to sum to one.
49
 
50
- The first eight outputs are auxiliary structural predictions. Their binary training targets are derived from Heron's recorded detection counts:
51
-
52
- | Output index | Target name | Target is 1 when Heron records… |
53
- | --- | --- | --- |
54
- | 0 | `has_table` | At least one `table` |
55
- | 1 | `has_picture` | At least one `picture` |
56
- | 2 | `has_formula` | At least one `formula` |
57
- | 3 | `has_code` | At least one `code` |
58
- | 4 | `has_doc_index` | At least one `document_index` |
59
- | 5 | `has_checkbox` | At least one `checkbox_selected` **or** `checkbox_unselected` |
60
- | 6 | `has_form` | At least one `form` |
61
- | 7 | `has_kv_region` | At least one `key_value_region` |
62
- | 8 | `has_any_interesting` | Any of targets 0–7 is 1 |
63
-
64
- ### How the routing target is defined
65
-
66
- In the training code, an **“interesting” page** means a page labeled as needing layout analysis under this structural rule. It does not mean that the page is relevant to a user's query or contains important information in a broader sense.
67
-
68
- For each page, the ninth target is constructed as `target[8] = int(any(target[:8]))`. It is **1** when Heron records at least one of the structures above, and **0** otherwise. The code checks whether each recorded detection count is greater than zero; it applies no additional confidence cutoff when constructing these targets.
69
-
70
- Text, page headers, page footers, section headers, list items, footnotes, titles, and captions alone do not make the target positive. Pages with no recorded detections also receive a negative target. That teacher-derived label is a training policy, not proof that native-text extraction will preserve everything on the page.
71
-
72
- ### Training targets versus runtime predictions
73
-
74
- The ninth **training target** is the OR of the eight auxiliary targets. The ninth **prediction** is a separately learned output. Its score is not calculated by combining the eight predicted scores, and the predictions need not agree exactly.
75
-
76
- All nine outputs contribute to training through a shared backbone. For page routing, use `sigmoid(logits[:, 8])` and the operating threshold. The routing score is the ninth learned prediction; it does not require thresholding or combining the auxiliary predictions.
77
 
78
- ## Files
79
 
80
- | File | Purpose |
81
- | --- | --- |
82
- | `triage_fp32.onnx` | Deployed FP32 ONNX model |
83
- | `config.json` | Model settings, preprocessing, operating threshold, and validation statistics; used by the inference example below |
84
- | `threshold.json` | Threshold-only export; its threshold and validation fields are also in `config.json` |
85
- | `best.pt` | PyTorch training checkpoint for re-export or fine-tuning with the model architecture |
86
- | `LICENSE` | Apache 2.0 license text |
87
 
88
  ## Usage
89
 
@@ -142,62 +118,23 @@ The function expects a non-empty list of prepared page thumbnails. Match the ren
142
 
143
  Different rendering or preprocessing can change scores, especially near the routing threshold.
144
 
145
- ## Training and labels
146
-
147
- Glancelot was trained with eight structural outputs and one routing output:
148
 
149
- 1. Run Heron on the source pages and record its per-class detection counts. Build eight structural targets and the ninth routing target using the rule above. This is supervision from **hard binary labels**, not soft-logit or feature distillation.
150
- 2. Prepare RGB page thumbnails at 224×224. Normalize them with the ImageNet mean and standard deviation. Training adds light brightness/contrast jitter and random crops with eight-pixel edge padding, without flips or rotations.
151
- 3. Start from an ImageNet-pretrained MobileNetV3-Small backbone. Add dropout of 0.2 and a `Linear(1024, 9)` output layer. Fine-tune the backbone and output layer together.
152
- 4. Optimize binary cross-entropy with logits across all nine outputs. Each output uses a positive-class weight based on its training-split negative/positive count ratio, clipped to `[0.5, 50]`. The run records specify AdamW, a learning rate of `0.002`, batch size 1,024, and a 25-epoch schedule. The implementation uses weight decay of `0.0001`, cosine learning-rate decay, and FP16 mixed precision on CUDA.
153
- 5. Select the checkpoint using validation recall on the routing output at a false-positive-rate budget of 30%. Then sweep the selected checkpoint's routing threshold to target 99% validation recall. Export the selected weights as a single FP32 ONNX file.
154
 
155
- The combined source pool contains approximately 1.29M PDF page thumbnails, including pages reserved for evaluation. Sources include financial filings, investor-relations materials, research papers, clinical-trial documents, and slides. Training splits are assigned by hashing the source-path prefix, rather than randomly splitting individual pages. The source PDFs and training dataset are not included in this repository.
156
 
157
- ## Evaluation and limits
158
 
159
- These results use Heron-generated labels on the validation split used for checkpoint and threshold selection. They describe agreement with those labels, rather than an independent held-out test of extraction quality.
160
 
161
- | Validation metric | Value |
162
- | --- | --- |
163
- | Pages | 36,605 |
164
- | Pages labeled as needing layout | 29,565 |
165
- | Recall on those pages | 99.002% |
166
- | False-positive rate on pages labeled as not needing layout | 32.074% |
167
- | Pages correctly routed to skip layout | 4,782 |
168
- | Layout-positive pages incorrectly routed to skip layout | 295 |
169
-
170
- The threshold favors recall: missing an important table can damage extraction, while an unnecessary layout call adds computation. At this operating point, about 32% of pages labeled as not needing layout still run through layout analysis.
171
-
172
- Teacher labels can contain errors. The routing score is a sigmoid output; its probability calibration has not been established. Evaluate routing and downstream extraction quality on representative documents before choosing a threshold for another pipeline. The auxiliary predictions indicate possible content types, not their locations on a page.
173
-
174
- ### CPU inference time
175
-
176
- The recorded model-only measurement is **39.2 ms for 32 thumbnails**, or about **1.22 ms per page when batched**, on a Mac mini with an M2 Pro (10-core CPU, 16 GB memory). This is the median of 15 warm ONNX Runtime 1.26 runs. It excludes model loading, PDF parsing, thumbnail rendering, layout analysis, OCR, and assembly.
177
-
178
- This measurement does not establish an end-to-end speedup. Savings depend on the document mix, the cost of preparing thumbnails, and the work skipped downstream. Time the full pipeline and check extraction quality on your workload.
179
 
180
- ## Use with Chowder
181
 
182
- Chowder uses Glancelot for page triage in its `read_documents()` table function.
183
-
184
- Within `read_documents()`, Chowder chooses a path for each PDF page:
185
-
186
- ```text
187
- PDF page
188
- → Rule-based checks
189
- → Empty: return an empty page result
190
- → Scan or other non-empty page without extractable text: hybrid extraction
191
- → Remaining pages: Glancelot
192
- → sigmoid(output[8]) > threshold: hybrid extraction
193
- → sigmoid(output[8]) <= threshold: fast native-text extraction
194
- ```
195
-
196
- **Fast native-text extraction** organizes text already stored in the PDF with geometric rules, skipping layout, text-recognition, and table models. **Hybrid extraction** combines native PDF extraction with layout analysis and other models where needed.
197
-
198
- Layout analysis identifies page structure. OCR recovers text from images. Chowder determines OCR needs separately, so selecting hybrid extraction does not automatically mean running OCR.
199
 
200
- See the [document reading documentation](https://docs.chowder.ai/reference/table-functions/read-documents) for the SQL interface.
201
 
202
  ## License
203
 
 
34
 
35
  | Property | Value |
36
  | --- | --- |
37
+ | Architecture | ImageNet-pretrained MobileNetV3-Small, nine-output head |
38
  | Parameters | 1,527,081 |
39
+ | Deployment | FP32 ONNX, about 6 MB |
40
  | Input | RGB thumbnails, float32 NCHW, `(batch, 3, 224, 224)` |
41
  | Output | Nine raw logits, `(batch, 9)` |
42
+ | Routing | `sigmoid(logits[:, 8]) > config["tau"]` |
 
 
43
 
44
+ ## Outputs
45
 
46
+ Glancelot is **multi-label**: several structures can appear on one page. Apply sigmoid to each logit; the scores do not sum to one.
47
 
48
+ | Index | Target |
49
+ | --- | --- |
50
+ | 0 | `has_table` |
51
+ | 1 | `has_picture` |
52
+ | 2 | `has_formula` |
53
+ | 3 | `has_code` |
54
+ | 4 | `has_doc_index` |
55
+ | 5 | `has_checkbox` |
56
+ | 6 | `has_form` |
57
+ | 7 | `has_kv_region` |
58
+ | 8 | `has_any_interesting` — routing output |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ The first eight targets indicate whether Heron recorded a detection of that structure. The ninth training target is their OR: “interesting” means at least one of these structures, rather than relevance or importance to a reader. Plain text, headers, footers, titles, lists, footnotes, and captions alone are negative under this rule. Pages with no recorded detections are also negative.
61
 
62
+ At inference, the ninth output is **a separately learned prediction**, not the OR of the eight predicted scores. Use it directly with the threshold in `config.json` (currently `0.20900137722492218`). See [target definitions and training details](docs/TRAINING_AND_EVALUATION.md).
 
 
 
 
 
 
63
 
64
  ## Usage
65
 
 
118
 
119
  Different rendering or preprocessing can change scores, especially near the routing threshold.
120
 
121
+ ## Training and evaluation
 
 
122
 
123
+ Glancelot was fine-tuned with binary cross-entropy on Heron-derived hard labels from a source pool of approximately 1.29M page thumbnails, including evaluation pages. The source PDFs and training dataset are not included.
 
 
 
 
124
 
125
+ The checkpoint and threshold were selected on the same **36,605-page validation split**. Routing recall was **99.002%** and false-positive rate **32.074%** against Heron's labels. These are validation agreement figures, not an independent test of extraction quality.
126
 
127
+ A recorded CPU measurement on an M2 Pro was **39.2 ms per 32 thumbnails** (about 1.22 ms/page when batched). It measures warm model inference only; it excludes rendering and extraction and does not establish an end-to-end speedup. See [training, evaluation, and timing conditions](docs/TRAINING_AND_EVALUATION.md).
128
 
129
+ ## Limitations and use with Chowder
130
 
131
+ Teacher labels can be wrong, and probability calibration has not been established. Auxiliary predictions indicate content types, not locations. Check routing and downstream extraction quality on representative documents before changing the threshold.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
+ Chowder uses Glancelot in [`read_documents()`](https://docs.chowder.ai/reference/table-functions/read-documents). Rule-based checks handle empty pages and scans first. Remaining pages go to hybrid extraction when the routing score exceeds the threshold, or fast native-text extraction otherwise. OCR needs are determined separately; selecting hybrid extraction does not automatically run OCR.
134
 
135
+ ## Files
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
+ `triage_fp32.onnx` contains the deployed model; `config.json` records preprocessing and the operating threshold. `threshold.json` is the threshold-only export, and `best.pt` is the training checkpoint for re-export or fine-tuning with the architecture.
138
 
139
  ## License
140
 
docs/TRAINING_AND_EVALUATION.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Glancelot training and evaluation
2
+
3
+ ## What the nine outputs mean
4
+
5
+ Glancelot is **multi-label**, rather than a multiclass classifier that chooses one mutually exclusive category. A page can contain a table, a picture, and a formula at the same time. The model returns nine raw logits from one linear output layer. A sigmoid converts each logit to a score; the nine scores do not have to sum to one.
6
+
7
+ The first eight outputs are auxiliary structural predictions. Their binary training targets are derived from Heron's recorded detection counts:
8
+
9
+ | Output index | Target name | Target is 1 when Heron records… |
10
+ | --- | --- | --- |
11
+ | 0 | `has_table` | At least one `table` |
12
+ | 1 | `has_picture` | At least one `picture` |
13
+ | 2 | `has_formula` | At least one `formula` |
14
+ | 3 | `has_code` | At least one `code` |
15
+ | 4 | `has_doc_index` | At least one `document_index` |
16
+ | 5 | `has_checkbox` | At least one `checkbox_selected` **or** `checkbox_unselected` |
17
+ | 6 | `has_form` | At least one `form` |
18
+ | 7 | `has_kv_region` | At least one `key_value_region` |
19
+ | 8 | `has_any_interesting` | Any of targets 0–7 is 1 |
20
+
21
+ ### How the routing target is defined
22
+
23
+ In the training code, an **“interesting” page** means a page labeled as needing layout analysis under this structural rule. It does not mean that the page is relevant to a user's query or contains important information in a broader sense.
24
+
25
+ For each page, the ninth target is constructed as `target[8] = int(any(target[:8]))`. It is **1** when Heron records at least one of the structures above, and **0** otherwise. The code checks whether each recorded detection count is greater than zero; it applies no additional confidence cutoff when constructing these targets.
26
+
27
+ Text, page headers, page footers, section headers, list items, footnotes, titles, and captions alone do not make the target positive. Pages with no recorded detections also receive a negative target. That teacher-derived label is a training policy, not proof that native-text extraction will preserve everything on the page.
28
+
29
+ ### Training targets versus runtime predictions
30
+
31
+ The ninth **training target** is the OR of the eight auxiliary targets. The ninth **prediction** is a separately learned output. Its score is not calculated by combining the eight predicted scores, and the predictions need not agree exactly.
32
+
33
+ All nine outputs contribute to training through a shared backbone. For page routing, use `sigmoid(logits[:, 8])` and the operating threshold. The routing score is the ninth learned prediction; it does not require thresholding or combining the auxiliary predictions.
34
+
35
+ ## Training and labels
36
+
37
+ Glancelot was trained with eight structural outputs and one routing output:
38
+
39
+ 1. Run Heron on the source pages and record its per-class detection counts. Build eight structural targets and the ninth routing target using the rule above. This is supervision from **hard binary labels**, not soft-logit or feature distillation.
40
+ 2. Prepare RGB page thumbnails at 224×224. Normalize them with the ImageNet mean and standard deviation. Training adds light brightness/contrast jitter and random crops with eight-pixel edge padding, without flips or rotations.
41
+ 3. Start from an ImageNet-pretrained MobileNetV3-Small backbone. Add dropout of 0.2 and a `Linear(1024, 9)` output layer. Fine-tune the backbone and output layer together.
42
+ 4. Optimize binary cross-entropy with logits across all nine outputs. Each output uses a positive-class weight based on its training-split negative/positive count ratio, clipped to `[0.5, 50]`. The run records specify AdamW, a learning rate of `0.002`, batch size 1,024, and a 25-epoch schedule. The implementation uses weight decay of `0.0001`, cosine learning-rate decay, and FP16 mixed precision on CUDA.
43
+ 5. Select the checkpoint using validation recall on the routing output at a false-positive-rate budget of 30%. Then sweep the selected checkpoint's routing threshold to target 99% validation recall. Export the selected weights as a single FP32 ONNX file.
44
+
45
+ The combined source pool contains approximately 1.29M PDF page thumbnails, including pages reserved for evaluation. Sources include financial filings, investor-relations materials, research papers, clinical-trial documents, and slides. Training splits are assigned by hashing the source-path prefix, rather than randomly splitting individual pages. The source PDFs and training dataset are not included in this repository.
46
+
47
+ ## Evaluation and limits
48
+
49
+ These results use Heron-generated labels on the validation split used for checkpoint and threshold selection. They describe agreement with those labels, rather than an independent held-out test of extraction quality.
50
+
51
+ | Validation metric | Value |
52
+ | --- | --- |
53
+ | Pages | 36,605 |
54
+ | Pages labeled as needing layout | 29,565 |
55
+ | Recall on those pages | 99.002% |
56
+ | False-positive rate on pages labeled as not needing layout | 32.074% |
57
+ | Pages correctly routed to skip layout | 4,782 |
58
+ | Layout-positive pages incorrectly routed to skip layout | 295 |
59
+
60
+ The threshold favors recall: missing an important table can damage extraction, while an unnecessary layout call adds computation. At this operating point, about 32% of pages labeled as not needing layout still run through layout analysis.
61
+
62
+ Teacher labels can contain errors. The routing score is a sigmoid output; its probability calibration has not been established. Evaluate routing and downstream extraction quality on representative documents before choosing a threshold for another pipeline. The auxiliary predictions indicate possible content types, not their locations on a page.
63
+
64
+ ### CPU inference time
65
+
66
+ The recorded model-only measurement is **39.2 ms for 32 thumbnails**, or about **1.22 ms per page when batched**, on a Mac mini with an M2 Pro (10-core CPU, 16 GB memory). This is the median of 15 warm ONNX Runtime 1.26 runs. It excludes model loading, PDF parsing, thumbnail rendering, layout analysis, OCR, and assembly.
67
+
68
+ This measurement does not establish an end-to-end speedup. Savings depend on the document mix, the cost of preparing thumbnails, and the work skipped downstream. Time the full pipeline and check extraction quality on your workload.