Update RADME.md
Browse files
README.md
CHANGED
@@ -1,200 +1,184 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
|
4 |
-
-
|
|
|
|
|
|
|
|
|
|
|
5 |
---
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
- **
|
22 |
-
- **
|
23 |
-
- **
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
- **
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
[
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
**APA:**
|
181 |
-
|
182 |
-
[More Information Needed]
|
183 |
-
|
184 |
-
## Glossary [optional]
|
185 |
-
|
186 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
187 |
-
|
188 |
-
[More Information Needed]
|
189 |
-
|
190 |
-
## More Information [optional]
|
191 |
-
|
192 |
-
[More Information Needed]
|
193 |
-
|
194 |
-
## Model Card Authors [optional]
|
195 |
-
|
196 |
-
[More Information Needed]
|
197 |
-
|
198 |
-
## Model Card Contact
|
199 |
-
|
200 |
-
[More Information Needed]
|
|
|
1 |
---
|
2 |
+
base_model: Qwen/Qwen2.5-VL-3B-Instruct
|
3 |
+
datasets:
|
4 |
+
- zackriya/diagramJSON
|
5 |
+
library_name: peft
|
6 |
+
tags:
|
7 |
+
- diagram
|
8 |
+
- structured-data
|
9 |
+
- image-processing
|
10 |
---
|
11 |
|
12 |
+
# πΌοΈπ Diagram-to-Graph Model
|
13 |
+
|
14 |
+
<div align="center">
|
15 |
+
<img src="https://github.com/Zackriya-Solutions/diagram2graph/blob/main/docs/diagram2graph_cmpr.png?raw=true" width="800" style="border-radius:10px;" alt="Diagram to Graph Header"/>
|
16 |
+
</div>
|
17 |
+
|
18 |
+
This model is a research-driven project built during an internship at [Zackariya Solution](https://www.zackriya.com/). It specializes in extracting **structured data(JSON)** from images, particularly **nodes, edges, and their sub-attributes** to represent visual information as knowledge graphs.
|
19 |
+
|
20 |
+
> π **Note:** This model is intended for **learning purposes** only and not for production applications. The extracted structured data may vary based on project needs.
|
21 |
+
|
22 |
+
## π Model Details
|
23 |
+
|
24 |
+
- **Developed by:** Zackariya Solution Internship Team(Mohammed Safvan)
|
25 |
+
- **Fine Tuned from:** `Qwen/Qwen2.5-VL-3B-Instruct`
|
26 |
+
- **License:** Apache 2.0
|
27 |
+
- **Language(s):** Multilingual (focus on structured extraction)
|
28 |
+
- **Model type:** Vision-Language Transformer (PEFT fine-tuned)
|
29 |
+
|
30 |
+
## π― Use Cases
|
31 |
+
|
32 |
+
### β
Direct Use
|
33 |
+
- Experimenting with **diagram-to-graph conversion** π
|
34 |
+
- Understanding **AI-driven structured extraction** from images
|
35 |
+
|
36 |
+
### π Downstream Use (Potential)
|
37 |
+
- Enhancing **BPMN/Flowchart** analysis ποΈ
|
38 |
+
- Supporting **automated document processing** π
|
39 |
+
|
40 |
+
### β Out-of-Scope Use
|
41 |
+
- Not designed for **real-world production** deployment β οΈ
|
42 |
+
- May not generalize well across **all diagram types**
|
43 |
+
|
44 |
+
## π How to Use
|
45 |
+
```python
|
46 |
+
%pip install -q "transformers>=4.49.0" accelerate datasets "qwen-vl-utils[decord]==0.0.8"
|
47 |
+
|
48 |
+
import os
|
49 |
+
import PIL
|
50 |
+
import torch
|
51 |
+
from qwen_vl_utils import process_vision_info
|
52 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
|
53 |
+
|
54 |
+
|
55 |
+
MODEL_ID="zackriya/diagram2graph"
|
56 |
+
MAX_PIXELS = 1280 * 28 * 28
|
57 |
+
MIN_PIXELS = 256 * 28 * 28
|
58 |
+
|
59 |
+
|
60 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
61 |
+
MODEL_ID,
|
62 |
+
device_map="auto",
|
63 |
+
torch_dtype=torch.bfloat16
|
64 |
+
)
|
65 |
+
|
66 |
+
processor = Qwen2_5_VLProcessor.from_pretrained(
|
67 |
+
MODEL_ID,
|
68 |
+
min_pixels=MIN_PIXELS,
|
69 |
+
max_pixels=MAX_PIXELS
|
70 |
+
)
|
71 |
+
|
72 |
+
|
73 |
+
SYSTEM_MESSAGE = """You are a Vision Language Model specialized in extracting structured data from visual representations of process and flow diagrams.
|
74 |
+
Your task is to analyze the provided image of a diagram and extract the relevant information into a well-structured JSON format.
|
75 |
+
The diagram includes details such as nodes and edges. each of them have their own attributes.
|
76 |
+
Focus on identifying key data fields and ensuring the output adheres to the requested JSON structure.
|
77 |
+
Provide only the JSON output based on the extracted information. Avoid additional explanations or comments."""
|
78 |
+
|
79 |
+
def run_inference(image):
|
80 |
+
messages= [
|
81 |
+
{
|
82 |
+
"role": "system",
|
83 |
+
"content": [{"type": "text", "text": SYSTEM_MESSAGE}],
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"role": "user",
|
87 |
+
"content": [
|
88 |
+
{
|
89 |
+
"type": "image",
|
90 |
+
# this image is handled by qwen_vl_utils's process_visio_Info so no need to worry about pil image or path
|
91 |
+
"image": image,
|
92 |
+
},
|
93 |
+
{
|
94 |
+
"type": "text",
|
95 |
+
"text": "Extract data in JSON format, Only give the JSON",
|
96 |
+
},
|
97 |
+
],
|
98 |
+
},
|
99 |
+
]
|
100 |
+
|
101 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
102 |
+
image_inputs, _ = process_vision_info(messages)
|
103 |
+
|
104 |
+
inputs = processor(
|
105 |
+
text=[text],
|
106 |
+
images=image_inputs,
|
107 |
+
return_tensors="pt",
|
108 |
+
)
|
109 |
+
inputs = inputs.to('cuda')
|
110 |
+
|
111 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
112 |
+
generated_ids_trimmed = [
|
113 |
+
out_ids[len(in_ids):]
|
114 |
+
for in_ids, out_ids
|
115 |
+
in zip(inputs.input_ids, generated_ids)
|
116 |
+
]
|
117 |
+
|
118 |
+
output_text = processor.batch_decode(
|
119 |
+
generated_ids_trimmed,
|
120 |
+
skip_special_tokens=True,
|
121 |
+
clean_up_tokenization_spaces=False
|
122 |
+
)
|
123 |
+
return output_text
|
124 |
+
image = eval_dataset[9]['image'] # PIL image
|
125 |
+
# `image` could be URL or relative path to the image
|
126 |
+
output = run_inference(image)
|
127 |
+
|
128 |
+
# JSON loading
|
129 |
+
import json
|
130 |
+
json.loads(output[0])
|
131 |
+
```
|
132 |
+
|
133 |
+
|
134 |
+
## ποΈ Training Details
|
135 |
+
- **Dataset:** Internally curated diagram dataset πΌοΈ
|
136 |
+
- **Fine-tuning:** LoRA-based optimization β‘
|
137 |
+
- **Precision:** bf16 mixed-precision training π―
|
138 |
+
|
139 |
+
## π Evaluation
|
140 |
+
|
141 |
+
- **Metrics:** F1-score π
|
142 |
+
- **Limitations:** May struggle with **complex, dense diagrams** β οΈ
|
143 |
+
## Results
|
144 |
+
|
145 |
+
- **+14% improvement in node detection**
|
146 |
+
- **+23% improvement in edge detection**
|
147 |
+
|
148 |
+
| Samples | (Base)Node F1 | (Fine)Node F1 | (Base)Edge F1 | (Fine)Edge F1 |
|
149 |
+
| --------------- | ------------- | ------------- | ------------- | ------------- |
|
150 |
+
| image_sample_1 | 0.46 | 1.0 | 0.59 | 0.71 |
|
151 |
+
| image_sample_2 | 0.67 | 0.57 | 0.25 | 0.25 |
|
152 |
+
| image_sample_3 | 1.0 | 1.0 | 0.25 | 0.75 |
|
153 |
+
| image_sample_4 | 0.5 | 0.83 | 0.15 | 0.62 |
|
154 |
+
| image_sample_5 | 0.72 | 0.78 | 0.0 | 0.48 |
|
155 |
+
| image_sample_6 | 0.75 | 0.75 | 0.29 | 0.67 |
|
156 |
+
| image_sample_7 | 0.6 | 1.0 | 1.0 | 1.0 |
|
157 |
+
| image_sample_8 | 0.6 | 1.0 | 1.0 | 1.0 |
|
158 |
+
| image_sample_9 | 1.0 | 1.0 | 0.55 | 0.77 |
|
159 |
+
| image_sample_10 | 0.67 | 0.8 | 0.0 | 1.0 |
|
160 |
+
| image_sample_11 | 0.8 | 0.8 | 0.5 | 1.0 |
|
161 |
+
| image_sample_12 | 0.67 | 1.0 | 0.62 | 0.75 |
|
162 |
+
| image_sample_13 | 1.0 | 1.0 | 0.73 | 0.67 |
|
163 |
+
| image_sample_14 | 0.74 | 0.95 | 0.56 | 0.67 |
|
164 |
+
| image_sample_15 | 0.86 | 0.71 | 0.67 | 0.67 |
|
165 |
+
| image_sample_16 | 0.75 | 1.0 | 0.8 | 0.75 |
|
166 |
+
| image_sample_17 | 0.8 | 1.0 | 0.63 | 0.73 |
|
167 |
+
| image_sample_18 | 0.83 | 0.83 | 0.33 | 0.43 |
|
168 |
+
| image_sample_19 | 0.75 | 0.8 | 0.06 | 0.22 |
|
169 |
+
| image_sample_20 | 0.81 | 1.0 | 0.23 | 0.75 |
|
170 |
+
| **Mean** | 0.749 | **0.891** | 0.4605 | **0.6945** |
|
171 |
+
|
172 |
+
|
173 |
+
## π€ Collaboration
|
174 |
+
Are you interested in fine tuning your own model for your use case or want to explore how we can help you? Let's collaborate.
|
175 |
+
|
176 |
+
[Zackriya Solutions](https://www.zackriya.com/collaboration-form)
|
177 |
+
|
178 |
+
## π References
|
179 |
+
- [Roboflow](https://github.com/roboflow/notebooks/blob/main/notebooks/how-to-finetune-qwen2-5-vl-for-json-data-extraction.ipynb)
|
180 |
+
- [Qwen](https://huggingface.co/Qwen/Qwen2.5-VL-3B-Instruct)
|
181 |
+
|
182 |
+
<h3 align='center'>
|
183 |
+
πStay Curious & Keep Exploring!π
|
184 |
+
</h3>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|