Aryan7500 commited on
Commit
73d90e4
Β·
verified Β·
1 Parent(s): b6f2618

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +110 -0
  2. config.json +44 -0
  3. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LayoutLMv1 Quantized Model for Form Understanding (FUNSD)
2
+
3
+ This repository hosts a quantized version of the LayoutLMv1 model, fine-tuned for document information extraction on the FUNSD dataset. The model is optimized for extracting entities such as questions, answers, and headers from scanned forms while maintaining efficient performance for deployment in production environments.
4
+
5
+ ## Model Details
6
+
7
+ - **Model Architecture:** LayoutLMv1 (Hugging Face Transformers)
8
+ - **Task:** Token Classification (Form Understanding / Information Extraction)
9
+ - **Dataset:** [nielsr/funsd](https://huggingface.co/datasets/nielsr/funsd)
10
+ - **Quantization:** Dynamic Quantization (INT8 for Linear layers)
11
+ - **Fine-tuning Framework:** PyTorch & Hugging Face Transformers
12
+
13
+ ---
14
+
15
+ ## Usage
16
+
17
+ ### Installation
18
+
19
+ ```sh
20
+ pip install transformers datasets torch torchvision
21
+ ```
22
+
23
+ ### Loading the Quantized Model
24
+
25
+ ```python
26
+ from transformers import LayoutLMForTokenClassification, LayoutLMTokenizer
27
+ import torch
28
+
29
+ # Load quantized model
30
+ model = LayoutLMForTokenClassification.from_pretrained("saved_model_quantized/")
31
+ model.load_state_dict(torch.load("saved_model_quantized/pytorch_model.bin"))
32
+ model.eval()
33
+
34
+ # Load tokenizer
35
+ tokenizer = LayoutLMTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Performance Metrics
41
+
42
+ | Epoch | Training Loss | Validation Loss |
43
+ |-------|----------------|------------------|
44
+ | 0 | 73.67 | 22.15 |
45
+ | 1 | 37.13 | 15.65 |
46
+ | 2 | 26.30 | 16.39 |
47
+ | 3 | 17.68 | 14.45 |
48
+ | 4 | 11.12 | 17.16 |
49
+
50
+ > πŸ“Š The model steadily improves until Epoch 3, indicating that the best checkpoint may be around this point.
51
+
52
+ ---
53
+
54
+ ## Fine-Tuning Details
55
+
56
+ ### Dataset
57
+
58
+ - **Name:** `nielsr/funsd`
59
+ - **Description:** A benchmark dataset for form understanding tasks with manually labeled fields like questions, answers, headers, etc.
60
+
61
+ ### Training Configuration
62
+
63
+ - **Epochs:** 5
64
+ - **Batch Size:** As per available memory
65
+ - **Learning Rate:** 4e-5
66
+ - **Optimizer:** AdamW
67
+ - **Loss Function:** CrossEntropyLoss (token classification)
68
+
69
+ ---
70
+
71
+ ## Quantization
72
+
73
+ Dynamic post-training quantization was applied using PyTorch:
74
+
75
+ ```python
76
+ import torch
77
+ quantized_model = torch.quantization.quantize_dynamic(
78
+ model_fp32,
79
+ {torch.nn.Linear},
80
+ dtype=torch.qint8
81
+ )
82
+ torch.save(quantized_model.state_dict(), "saved_model_quantized/pytorch_model.bin")
83
+ ```
84
+
85
+ ---
86
+
87
+ ## Repository Structure
88
+
89
+ ```
90
+ .
91
+ β”œβ”€β”€ saved_model/ # Original fine-tuned LayoutLMv1
92
+ β”œβ”€β”€ saved_model_quantized/ # INT8 quantized model files
93
+ β”‚ β”œβ”€β”€ config.json
94
+ β”‚ β”œβ”€β”€ pytorch_model.bin
95
+ β”œβ”€β”€ README.md # Project documentation
96
+ ```
97
+
98
+ ---
99
+
100
+ ## Limitations
101
+
102
+ - Trained only on the FUNSD dataset; may not generalize to other document formats.
103
+ - Quantized version may have slight degradation in performance compared to the full-precision model.
104
+ - Requires OCR and bounding boxes as input for proper operation.
105
+
106
+ ---
107
+
108
+ ## Contributing
109
+
110
+ Contributions are welcome! If you have suggestions, improvements, or find bugs, feel free to open an issue or submit a pull request.
config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_attn_implementation_autoset": true,
3
+ "architectures": [
4
+ "LayoutLMForTokenClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "hidden_act": "gelu",
8
+ "hidden_dropout_prob": 0.1,
9
+ "hidden_size": 768,
10
+ "id2label": {
11
+ "0": "LABEL_0",
12
+ "1": "LABEL_1",
13
+ "2": "LABEL_2",
14
+ "3": "LABEL_3",
15
+ "4": "LABEL_4",
16
+ "5": "LABEL_5",
17
+ "6": "LABEL_6"
18
+ },
19
+ "initializer_range": 0.02,
20
+ "intermediate_size": 3072,
21
+ "label2id": {
22
+ "LABEL_0": 0,
23
+ "LABEL_1": 1,
24
+ "LABEL_2": 2,
25
+ "LABEL_3": 3,
26
+ "LABEL_4": 4,
27
+ "LABEL_5": 5,
28
+ "LABEL_6": 6
29
+ },
30
+ "layer_norm_eps": 1e-12,
31
+ "max_2d_position_embeddings": 1024,
32
+ "max_position_embeddings": 512,
33
+ "model_type": "layoutlm",
34
+ "num_attention_heads": 12,
35
+ "num_hidden_layers": 12,
36
+ "output_past": true,
37
+ "pad_token_id": 0,
38
+ "position_embedding_type": "absolute",
39
+ "torch_dtype": "float32",
40
+ "transformers_version": "4.51.3",
41
+ "type_vocab_size": 2,
42
+ "use_cache": true,
43
+ "vocab_size": 30522
44
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a0f4b6415488ff5eb308d2f6eda1ac1c2cef440ff7a1788265931911edcb71e9
3
+ size 194086670