Views
No views yet
| Epoch | Train Loss | Train Acc | Val Loss | Val Accuracy |
|---|---|---|---|---|
| 1 | 0.6768 | 59.26% | 0.6706 | 57.14% |
| 3 | 0.6045 | 81.48% | 0.6031 | 71.43% |
| 6 | 0.1850 | 92.59% | 0.5292 | 85.71% |
| 7 | 0.1001 | 96.30% | 0.0830 | 100.00% |
| 10 | 0.0048 | 100.00% | 0.0058 | 100.00% |
| Method | Dataset | Accuracy | Speed |
|---|---|---|---|
| Rule-based (original) | N/A | 85.3% | 17.7s |
| LayoutLMv3 (this model) | 54 pages | 100.00% ✨ | 3.1s |
1from transformers import LayoutLMv3Processor, LayoutLMv3ForSequenceClassification
2from PIL import Image
3from doctr.models import ocr_predictor
4from doctr.io import DocumentFile
5
6# Load model and processor
7model = LayoutLMv3ForSequenceClassification.from_pretrained("ssppkenny/layoutlmv3-toc-detector")
8processor = LayoutLMv3Processor.from_pretrained("ssppkenny/layoutlmv3-toc-detector")
9
10# Load and OCR image
11image = Image.open("page.png").convert("RGB")
12ocr_model = ocr_predictor(pretrained=True)
13doc = DocumentFile.from_images("page.png")
14result = ocr_model(doc)
15
16# Extract words and boxes
17words, boxes = [], []
18doc_dict = result.export()
19w, h = image.size
20
21for page in doc_dict['pages']:
22 for block in page['blocks']:
23 for line in block['lines']:
24 for word_data in line['words']:
25 text = word_data['value'].strip()
26 if text:
27 geometry = word_data['geometry']
28 x0 = int(geometry[0][0] * w)
29 y0 = int(geometry[0][1] * h)
30 x1 = int(geometry[1][0] * w)
31 y1 = int(geometry[1][1] * h)
32 words.append(text)
33 boxes.append([
34 int((x0 / w) * 1000),
35 int((y0 / h) * 1000),
36 int((x1 / w) * 1000),
37 int((y1 / h) * 1000)
38 ])
39
40# Prepare input
41encoding = processor(image, words, boxes=boxes, return_tensors="pt",
42 padding="max_length", truncation=True, max_length=512)
43
44# Predict
45outputs = model(**encoding)
46prediction = torch.argmax(outputs.logits, dim=1).item()
47confidence = torch.softmax(outputs.logits, dim=1)[0][prediction].item()
48
49print(f"Is TOC: {prediction == 1}")
50print(f"Confidence: {confidence:.2%}")1@misc{layoutlmv3-toc-detector,
2 author = {Sergey},
3 title = {LayoutLMv3 Table of Contents Detector},
4 year = {2026},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/ssppkenny/layoutlmv3-toc-detector}},
7}