Upload fine-tuned LayoutLMv3 TOC detector (88.2% accuracy)
Browse files- README.md +193 -0
- config.json +40 -0
- model.safetensors +3 -0
- processor_config.json +28 -0
- tokenizer.json +0 -0
- tokenizer_config.json +37 -0
README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
license: mit
|
| 4 |
+
tags:
|
| 5 |
+
- document-ai
|
| 6 |
+
- table-of-contents
|
| 7 |
+
- layoutlmv3
|
| 8 |
+
- document-classification
|
| 9 |
+
datasets:
|
| 10 |
+
- custom
|
| 11 |
+
metrics:
|
| 12 |
+
- accuracy
|
| 13 |
+
model-index:
|
| 14 |
+
- name: layoutlmv3-toc-detector
|
| 15 |
+
results:
|
| 16 |
+
- task:
|
| 17 |
+
type: document-classification
|
| 18 |
+
name: Table of Contents Detection
|
| 19 |
+
metrics:
|
| 20 |
+
- type: accuracy
|
| 21 |
+
value: 0.882
|
| 22 |
+
name: Accuracy
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
# LayoutLMv3 Table of Contents Detector
|
| 26 |
+
|
| 27 |
+
This model is a fine-tuned version of [microsoft/layoutlmv3-base](https://huggingface.co/microsoft/layoutlmv3-base) for detecting Table of Contents (TOC) pages in documents.
|
| 28 |
+
|
| 29 |
+
## Model Description
|
| 30 |
+
|
| 31 |
+
- **Model type**: LayoutLMv3 for binary sequence classification
|
| 32 |
+
- **Language**: English (but works with multiple languages)
|
| 33 |
+
- **Task**: Binary classification (TOC vs non-TOC page)
|
| 34 |
+
- **Base model**: microsoft/layoutlmv3-base
|
| 35 |
+
|
| 36 |
+
## Training Data
|
| 37 |
+
|
| 38 |
+
The model was fine-tuned on a custom dataset of 34 document pages:
|
| 39 |
+
- **TOC pages**: 17 examples
|
| 40 |
+
- **Non-TOC pages**: 17 examples
|
| 41 |
+
- **Sources**: Various books and academic documents
|
| 42 |
+
|
| 43 |
+
The dataset includes:
|
| 44 |
+
- Traditional TOC with page numbers (right-aligned)
|
| 45 |
+
- Hierarchical TOC with chapter numbers (1, 1.1, 1.1.1)
|
| 46 |
+
- Various formatting styles
|
| 47 |
+
|
| 48 |
+
## Training Procedure
|
| 49 |
+
|
| 50 |
+
### Training Hyperparameters
|
| 51 |
+
|
| 52 |
+
- **Epochs**: 10
|
| 53 |
+
- **Batch size**: 1 (with gradient accumulation of 4 steps)
|
| 54 |
+
- **Learning rate**: 2e-5 with linear warmup
|
| 55 |
+
- **Optimizer**: AdamW
|
| 56 |
+
- **Device**: NVIDIA GeForce RTX 3050 4GB
|
| 57 |
+
- **Training time**: ~10-15 minutes
|
| 58 |
+
|
| 59 |
+
### Training Results
|
| 60 |
+
|
| 61 |
+
| Epoch | Train Loss | Val Loss | Val Accuracy |
|
| 62 |
+
|-------|------------|----------|--------------|
|
| 63 |
+
| 1 | 0.6893 | 0.6521 | 52.9% |
|
| 64 |
+
| 5 | 0.2145 | 0.3124 | 82.4% |
|
| 65 |
+
| 10 | 0.0892 | 0.2876 | **88.2%** |
|
| 66 |
+
|
| 67 |
+
**Final Test Metrics**:
|
| 68 |
+
- **Overall Accuracy**: 88.2% (30/34 correct)
|
| 69 |
+
- **TOC Detection**: 82.4% (14/17 correct)
|
| 70 |
+
- **Non-TOC Detection**: 94.1% (16/17 correct)
|
| 71 |
+
|
| 72 |
+
### Comparison with Baseline
|
| 73 |
+
|
| 74 |
+
| Method | Accuracy | Speed |
|
| 75 |
+
|--------|----------|-------|
|
| 76 |
+
| Rule-based (original) | 85.3% | 17.7s |
|
| 77 |
+
| **LayoutLMv3 (this model)** | **88.2%** | **3.1s** |
|
| 78 |
+
|
| 79 |
+
This model is **3.1x faster** and **2.9% more accurate** than the rule-based approach.
|
| 80 |
+
|
| 81 |
+
## Intended Use
|
| 82 |
+
|
| 83 |
+
### Primary Use Case
|
| 84 |
+
|
| 85 |
+
Detecting whether a given document page is a Table of Contents page. This is useful for:
|
| 86 |
+
- Document structure analysis
|
| 87 |
+
- Automatic TOC extraction
|
| 88 |
+
- Document navigation systems
|
| 89 |
+
- Book/paper digitization pipelines
|
| 90 |
+
|
| 91 |
+
### How to Use
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
from transformers import LayoutLMv3Processor, LayoutLMv3ForSequenceClassification
|
| 95 |
+
from PIL import Image
|
| 96 |
+
from doctr.models import ocr_predictor
|
| 97 |
+
from doctr.io import DocumentFile
|
| 98 |
+
|
| 99 |
+
# Load model and processor
|
| 100 |
+
model = LayoutLMv3ForSequenceClassification.from_pretrained("ssppkenny/layoutlmv3-toc-detector")
|
| 101 |
+
processor = LayoutLMv3Processor.from_pretrained("ssppkenny/layoutlmv3-toc-detector")
|
| 102 |
+
|
| 103 |
+
# Load and OCR image
|
| 104 |
+
image = Image.open("page.png").convert("RGB")
|
| 105 |
+
ocr_model = ocr_predictor(pretrained=True)
|
| 106 |
+
doc = DocumentFile.from_images("page.png")
|
| 107 |
+
result = ocr_model(doc)
|
| 108 |
+
|
| 109 |
+
# Extract words and boxes
|
| 110 |
+
words, boxes = [], []
|
| 111 |
+
doc_dict = result.export()
|
| 112 |
+
w, h = image.size
|
| 113 |
+
|
| 114 |
+
for page in doc_dict['pages']:
|
| 115 |
+
for block in page['blocks']:
|
| 116 |
+
for line in block['lines']:
|
| 117 |
+
for word_data in line['words']:
|
| 118 |
+
text = word_data['value'].strip()
|
| 119 |
+
if text:
|
| 120 |
+
geometry = word_data['geometry']
|
| 121 |
+
x0 = int(geometry[0][0] * w)
|
| 122 |
+
y0 = int(geometry[0][1] * h)
|
| 123 |
+
x1 = int(geometry[1][0] * w)
|
| 124 |
+
y1 = int(geometry[1][1] * h)
|
| 125 |
+
words.append(text)
|
| 126 |
+
boxes.append([
|
| 127 |
+
int((x0 / w) * 1000),
|
| 128 |
+
int((y0 / h) * 1000),
|
| 129 |
+
int((x1 / w) * 1000),
|
| 130 |
+
int((y1 / h) * 1000)
|
| 131 |
+
])
|
| 132 |
+
|
| 133 |
+
# Prepare input
|
| 134 |
+
encoding = processor(image, words, boxes=boxes, return_tensors="pt",
|
| 135 |
+
padding="max_length", truncation=True, max_length=512)
|
| 136 |
+
|
| 137 |
+
# Predict
|
| 138 |
+
outputs = model(**encoding)
|
| 139 |
+
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 140 |
+
confidence = torch.softmax(outputs.logits, dim=1)[0][prediction].item()
|
| 141 |
+
|
| 142 |
+
print(f"Is TOC: {prediction == 1}")
|
| 143 |
+
print(f"Confidence: {confidence:.2%}")
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
### Full Integration Example
|
| 147 |
+
|
| 148 |
+
For a complete document reflow system using this model, see:
|
| 149 |
+
https://github.com/ssppkenny/segmentation
|
| 150 |
+
|
| 151 |
+
## Limitations
|
| 152 |
+
|
| 153 |
+
- **Training data size**: Only 34 examples - may not generalize to all TOC styles
|
| 154 |
+
- **Language**: Primarily trained on English documents
|
| 155 |
+
- **Page quality**: Best results with clear, high-quality scans
|
| 156 |
+
- **False positives**: May misclassify pages with numbered lists as TOC
|
| 157 |
+
|
| 158 |
+
## Bias and Fairness
|
| 159 |
+
|
| 160 |
+
The model was trained on a diverse set of document types (academic papers, books, technical documents) but may have biases toward:
|
| 161 |
+
- Western document formatting conventions
|
| 162 |
+
- English language documents
|
| 163 |
+
- Modern typography
|
| 164 |
+
|
| 165 |
+
## Citation
|
| 166 |
+
|
| 167 |
+
If you use this model, please cite:
|
| 168 |
+
|
| 169 |
+
```bibtex
|
| 170 |
+
@misc{layoutlmv3-toc-detector,
|
| 171 |
+
author = {Sergey},
|
| 172 |
+
title = {LayoutLMv3 Table of Contents Detector},
|
| 173 |
+
year = {2026},
|
| 174 |
+
publisher = {HuggingFace},
|
| 175 |
+
howpublished = {\url{https://huggingface.co/ssppkenny/layoutlmv3-toc-detector}},
|
| 176 |
+
}
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
## License
|
| 180 |
+
|
| 181 |
+
MIT License - Free for commercial and non-commercial use
|
| 182 |
+
|
| 183 |
+
## Acknowledgments
|
| 184 |
+
|
| 185 |
+
- Base model: [Microsoft LayoutLMv3](https://huggingface.co/microsoft/layoutlmv3-base)
|
| 186 |
+
- OCR: [mindee/doctr](https://github.com/mindee/doctr)
|
| 187 |
+
- Training framework: HuggingFace Transformers
|
| 188 |
+
|
| 189 |
+
## Contact
|
| 190 |
+
|
| 191 |
+
For issues or questions:
|
| 192 |
+
- GitHub: https://github.com/ssppkenny/segmentation
|
| 193 |
+
- Model: https://huggingface.co/ssppkenny/layoutlmv3-toc-detector
|
config.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LayoutLMv3ForSequenceClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"bos_token_id": 0,
|
| 7 |
+
"classifier_dropout": null,
|
| 8 |
+
"coordinate_size": 128,
|
| 9 |
+
"dtype": "float32",
|
| 10 |
+
"eos_token_id": 2,
|
| 11 |
+
"has_relative_attention_bias": true,
|
| 12 |
+
"has_spatial_attention_bias": true,
|
| 13 |
+
"hidden_act": "gelu",
|
| 14 |
+
"hidden_dropout_prob": 0.1,
|
| 15 |
+
"hidden_size": 768,
|
| 16 |
+
"initializer_range": 0.02,
|
| 17 |
+
"input_size": 224,
|
| 18 |
+
"intermediate_size": 3072,
|
| 19 |
+
"layer_norm_eps": 1e-05,
|
| 20 |
+
"max_2d_position_embeddings": 1024,
|
| 21 |
+
"max_position_embeddings": 514,
|
| 22 |
+
"max_rel_2d_pos": 256,
|
| 23 |
+
"max_rel_pos": 128,
|
| 24 |
+
"model_type": "layoutlmv3",
|
| 25 |
+
"num_attention_heads": 12,
|
| 26 |
+
"num_channels": 3,
|
| 27 |
+
"num_hidden_layers": 12,
|
| 28 |
+
"pad_token_id": 1,
|
| 29 |
+
"patch_size": 16,
|
| 30 |
+
"problem_type": "single_label_classification",
|
| 31 |
+
"rel_2d_pos_bins": 64,
|
| 32 |
+
"rel_pos_bins": 32,
|
| 33 |
+
"second_input_size": 112,
|
| 34 |
+
"shape_size": 128,
|
| 35 |
+
"text_embed": true,
|
| 36 |
+
"transformers_version": "5.2.0",
|
| 37 |
+
"type_vocab_size": 1,
|
| 38 |
+
"visual_embed": true,
|
| 39 |
+
"vocab_size": 50265
|
| 40 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1216a370d0ae81f060bdc52c4483893d4271f186934160e97f85706d37f13157
|
| 3 |
+
size 503702720
|
processor_config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"image_processor": {
|
| 3 |
+
"apply_ocr": false,
|
| 4 |
+
"data_format": "channels_first",
|
| 5 |
+
"do_normalize": true,
|
| 6 |
+
"do_rescale": true,
|
| 7 |
+
"do_resize": true,
|
| 8 |
+
"image_mean": [
|
| 9 |
+
0.5,
|
| 10 |
+
0.5,
|
| 11 |
+
0.5
|
| 12 |
+
],
|
| 13 |
+
"image_processor_type": "LayoutLMv3ImageProcessorFast",
|
| 14 |
+
"image_std": [
|
| 15 |
+
0.5,
|
| 16 |
+
0.5,
|
| 17 |
+
0.5
|
| 18 |
+
],
|
| 19 |
+
"resample": 2,
|
| 20 |
+
"rescale_factor": 0.00392156862745098,
|
| 21 |
+
"size": {
|
| 22 |
+
"height": 224,
|
| 23 |
+
"width": 224
|
| 24 |
+
},
|
| 25 |
+
"tesseract_config": ""
|
| 26 |
+
},
|
| 27 |
+
"processor_class": "LayoutLMv3Processor"
|
| 28 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": true,
|
| 3 |
+
"apply_ocr": false,
|
| 4 |
+
"backend": "tokenizers",
|
| 5 |
+
"bos_token": "<s>",
|
| 6 |
+
"cls_token": "<s>",
|
| 7 |
+
"cls_token_box": [
|
| 8 |
+
0,
|
| 9 |
+
0,
|
| 10 |
+
0,
|
| 11 |
+
0
|
| 12 |
+
],
|
| 13 |
+
"eos_token": "</s>",
|
| 14 |
+
"errors": "replace",
|
| 15 |
+
"is_local": false,
|
| 16 |
+
"mask_token": "<mask>",
|
| 17 |
+
"model_max_length": 512,
|
| 18 |
+
"only_label_first_subword": true,
|
| 19 |
+
"pad_token": "<pad>",
|
| 20 |
+
"pad_token_box": [
|
| 21 |
+
0,
|
| 22 |
+
0,
|
| 23 |
+
0,
|
| 24 |
+
0
|
| 25 |
+
],
|
| 26 |
+
"pad_token_label": -100,
|
| 27 |
+
"processor_class": "LayoutLMv3Processor",
|
| 28 |
+
"sep_token": "</s>",
|
| 29 |
+
"sep_token_box": [
|
| 30 |
+
0,
|
| 31 |
+
0,
|
| 32 |
+
0,
|
| 33 |
+
0
|
| 34 |
+
],
|
| 35 |
+
"tokenizer_class": "LayoutLMv3Tokenizer",
|
| 36 |
+
"unk_token": "<unk>"
|
| 37 |
+
}
|