Nikos Livathinos
commited on
Commit
·
e6bfdf7
1
Parent(s):
72d9495
docs: Update the model card with inference code example and references
Browse files
README.md
CHANGED
@@ -1,3 +1,107 @@
|
|
1 |
-
---
|
2 |
-
license: cdla-permissive-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: cdla-permissive-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
# Docling Layout Model
|
6 |
+
|
7 |
+
`docling-layout-heron` is the Layout Model of [Docling project](https://github.com/docling-project/docling).
|
8 |
+
|
9 |
+
This model uses the [RT-DETRv2](https://github.com/lyuwenyu/RT-DETR/tree/main/rtdetrv2_pytorch) architecture and has been trained from scratch on a variety of document datasets.
|
10 |
+
|
11 |
+
|
12 |
+
# Inference code example
|
13 |
+
|
14 |
+
Prerequisites:
|
15 |
+
|
16 |
+
```bash
|
17 |
+
pip install transformers Pillow torch requests
|
18 |
+
```
|
19 |
+
|
20 |
+
Prediction:
|
21 |
+
|
22 |
+
```python
|
23 |
+
import requests
|
24 |
+
from transformers import RTDetrV2ForObjectDetection, RTDetrImageProcessor
|
25 |
+
import torch
|
26 |
+
from PIL import Image
|
27 |
+
|
28 |
+
|
29 |
+
classes_map = {
|
30 |
+
0: "Caption",
|
31 |
+
1: "Footnote",
|
32 |
+
2: "Formula",
|
33 |
+
3: "List-item",
|
34 |
+
4: "Page-footer",
|
35 |
+
5: "Page-header",
|
36 |
+
6: "Picture",
|
37 |
+
7: "Section-header",
|
38 |
+
8: "Table",
|
39 |
+
9: "Text",
|
40 |
+
10: "Title",
|
41 |
+
11: "Document Index",
|
42 |
+
12: "Code",
|
43 |
+
13: "Checkbox-Selected",
|
44 |
+
14: "Checkbox-Unselected",
|
45 |
+
15: "Form",
|
46 |
+
16: "Key-Value Region",
|
47 |
+
}
|
48 |
+
image_url = "https://huggingface.co/spaces/ds4sd/SmolDocling-256M-Demo/resolve/main/example_images/annual_rep_14.png"
|
49 |
+
model_name = "ds4sd/docling-layout-heron"
|
50 |
+
threshold = 0.6
|
51 |
+
|
52 |
+
|
53 |
+
# Download the image
|
54 |
+
image = Image.open(requests.get(image_url, stream=True).raw)
|
55 |
+
image = image.convert("RGB")
|
56 |
+
|
57 |
+
# Initialize the model
|
58 |
+
image_processor = RTDetrImageProcessor.from_pretrained(model_name)
|
59 |
+
model = RTDetrV2ForObjectDetection.from_pretrained(model_name)
|
60 |
+
|
61 |
+
# Run the prediction pipeline
|
62 |
+
inputs = image_processor(images=[image], return_tensors="pt")
|
63 |
+
with torch.no_grad():
|
64 |
+
outputs = model(**inputs)
|
65 |
+
results = image_processor.post_process_object_detection(
|
66 |
+
outputs,
|
67 |
+
target_sizes=torch.tensor([image.size[::-1]]),
|
68 |
+
threshold=threshold,
|
69 |
+
)
|
70 |
+
|
71 |
+
# Get the results
|
72 |
+
for result in results:
|
73 |
+
for score, label_id, box in zip(
|
74 |
+
result["scores"], result["labels"], result["boxes"]
|
75 |
+
):
|
76 |
+
score = round(score.item(), 2)
|
77 |
+
label = classes_map[label_id.item()]
|
78 |
+
box = [round(i, 2) for i in box.tolist()]
|
79 |
+
print(f"{label}:{score} {box}")
|
80 |
+
```
|
81 |
+
|
82 |
+
|
83 |
+
# References
|
84 |
+
|
85 |
+
```
|
86 |
+
@techreport{Docling,
|
87 |
+
author = {Deep Search Team},
|
88 |
+
month = {8},
|
89 |
+
title = {Docling Technical Report},
|
90 |
+
url = {https://arxiv.org/abs/2408.09869v4},
|
91 |
+
eprint = {2408.09869},
|
92 |
+
doi = {10.48550/arXiv.2408.09869},
|
93 |
+
version = {1.0.0},
|
94 |
+
year = {2024}
|
95 |
+
}
|
96 |
+
|
97 |
+
@misc{lv2024rtdetrv2improvedbaselinebagoffreebies,
|
98 |
+
title={RT-DETRv2: Improved Baseline with Bag-of-Freebies for Real-Time Detection Transformer},
|
99 |
+
author={Wenyu Lv and Yian Zhao and Qinyao Chang and Kui Huang and Guanzhong Wang and Yi Liu},
|
100 |
+
year={2024},
|
101 |
+
eprint={2407.17140},
|
102 |
+
archivePrefix={arXiv},
|
103 |
+
primaryClass={cs.CV},
|
104 |
+
url={https://arxiv.org/abs/2407.17140},
|
105 |
+
}
|
106 |
+
|
107 |
+
```
|