Mosal1985 commited on
Commit
f90f751
·
1 Parent(s): 75459e5

v9: header-synonym augmentation (BUYER_NAME 0.764→0.800, macroF1 0.566→0.588) + ONNX INT8

Browse files
Files changed (5) hide show
  1. README.md +32 -40
  2. config.json +64 -46
  3. label2id.json +32 -23
  4. model_int8.onnx +3 -0
  5. pytorch_model.bin +2 -2
README.md CHANGED
@@ -7,58 +7,50 @@ tags:
7
  - invoice-extraction
8
  - gst
9
  - pytorch
 
10
  datasets:
11
  - Mosal1985/gst-smoke-dataset
12
  pipeline_tag: token-classification
13
- metrics:
14
- - precision
15
- - recall
16
- - f1
17
  ---
18
 
19
- # GstEngine — Tiny LayoutLM for GST Invoice Extraction
20
 
21
- A **from-scratch tiny LayoutLM** (LayoutLM v1 architecture, 6 layers / 256 hidden / 4
22
- attention heads, **13.08M params**) fine-tuned for **BIOES token classification** on
23
- Indian GST invoice line items. Extracts seller, buyer, GSTIN, invoice number/date,
24
- HSN/SAC, quantities, prices, and tax amounts to emit TallyPrime import vouchers.
25
 
26
- Trained on the GstEngine smoke dataset: 16 labeled invoice pages across 11 vendors
27
- (synthetic + real internet invoices), `MAX_LEN=768` (tail-truncated, v4 baseline).
28
 
29
- **This is a baseline/smoke model.** Header fields (seller, buyer, GSTIN, invoice
30
- number, date) are reasonably extracted on similar layouts; line-item numbers and
31
- tax amounts remain noisy on unseen layouts until more training invoices are added.
 
 
 
 
32
 
33
- ## Usage
34
-
35
- ```python
36
- from transformers import LayoutLMForTokenClassification, AutoTokenizer
37
-
38
- model = LayoutLMForTokenClassification.from_pretrained("Mosal1985/gst-layoutlm-tiny")
39
- tok = AutoTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
40
 
41
- # tokenize words + normalized 0-1000 bounding boxes, predict argmax per token,
42
- # map through label2id.json, merge same-field spans in reading order.
43
- ```
 
 
44
 
45
- See [`sanprat/GstEngine`](https://github.com/sanprat/GstEngine) for the full
46
- pipeline: dataset builder, training runs (`smoke_run_v2/v3/v4`), OCR
47
- integration, and Tally XML generation.
48
 
49
- ## Files
 
50
 
51
- | File | Description |
52
- |---|---|
53
- | `pytorch_model.bin` | v4 checkpoint (52MB, `best_state.pt`) |
54
- | `config.json` | LayoutLM config + `id2label` (23 BIOES labels) |
55
- | `label2id.json` | label → id map for decoding |
56
 
57
- ## Details
58
 
59
- - Architecture: LayoutLM v1, 6L/256H/4A, max_position_embeddings 768
60
- - Labels: 23 BIOES (SELLER_NAME, BUYER_NAME, GSTIN, INVOICE_NUMBER, INVOICE_DATE,
61
- HSN_SAC, ITEM_NAME, QUANTITY, UNIT_PRICE, DISCOUNT, TAXABLE_VALUE,
62
- CGST/SGST/IGST_AMOUNT, CGST/SGST/IGST_RATE, TOTAL, O...)
63
- - Data: 16 pages, 11 vendors — see the GstEngine repo for the builder
64
- - License: CC BY-NC 4.0
 
7
  - invoice-extraction
8
  - gst
9
  - pytorch
10
+ - onnx
11
  datasets:
12
  - Mosal1985/gst-smoke-dataset
13
  pipeline_tag: token-classification
 
 
 
 
14
  ---
15
 
16
+ # GST LayoutLM Tiny (v9)
17
 
18
+ Fine-tuned tiny LayoutLM student for extracting fields from Indian GST invoices
19
+ (6L/256H/4A, ~13M params). Trained on 1,942 pages (199 original + 1,761
20
+ header-synonym augmented) across 40 vendors; evaluated on 4 held-out vendor
21
+ families (18 pages, identical to the v8 eval split for A/B comparison).
22
 
23
+ ## v9 highlights
 
24
 
25
+ - **BUYER_NAME F1: 0.800** (v8: 0.764) — robust to header synonyms:
26
+ `Buyer`, `Customer`, `Purchaser`, `Bill To`, `Consignee`, `Recipient`,
27
+ `Party (Buyer)`, `Customer Details`, `Billed To`, `Party Name` (+ email
28
+ distractors so usernames like `saif.mr` are not picked up as buyer names)
29
+ - **Macro Field F1: 0.588** (v8: 0.566) · **Entity F1: 0.649** (v8: 0.642)
30
+ - Big gains on tax fields: TAX_AMOUNT 0.727 (+0.30), SGST_AMOUNT 0.800 (+0.13),
31
+ QUANTITY 0.606 (+0.08), TAXABLE_VALUE 0.615 (+0.07)
32
 
33
+ ## Files
 
 
 
 
 
 
34
 
35
+ | File | Size | Use |
36
+ |---|---|---|
37
+ | `pytorch_model.bin` | 52 MB | PyTorch `from_pretrained` (LayoutLMForTokenClassification) |
38
+ | `model_int8.onnx` | 13.17 MB | Android / onnxruntime INT8 (99.48% token parity, ~126 ms CPU @ seq 768) |
39
+ | `config.json` / `label2id.json` | — | label map + config |
40
 
41
+ ## Quickstart
 
 
42
 
43
+ ```python
44
+ from transformers import LayoutLMConfig, LayoutLMForTokenClassification
45
 
46
+ config = LayoutLMConfig.from_pretrained("Mosal1985/gst-layoutlm-tiny")
47
+ model = LayoutLMForTokenClassification.from_pretrained(
48
+ "Mosal1985/gst-layoutlm-tiny", config=config
49
+ )
50
+ ```
51
 
52
+ ## Training
53
 
54
+ `gst_finetune_smoke.py` (canonical trainer, MAX_LEN=768, 40 epochs, T4 ~25 min)
55
+ with dataset augmentation from `augment_header_synonyms.py`
56
+ (v9 = `data/gst_smoke_dataset_v9.jsonl`, 1,960 records).
 
 
 
config.json CHANGED
@@ -6,56 +6,74 @@
6
  "hidden_dropout_prob": 0.1,
7
  "hidden_size": 256,
8
  "id2label": {
9
- "0": "B-SELLER_NAME",
10
- "1": "E-SELLER_NAME",
11
- "2": "I-SELLER_NAME",
12
- "3": "O",
13
- "4": "S-BUYER_NAME",
14
- "5": "S-CGST_AMOUNT",
15
- "6": "S-CGST_RATE",
16
- "7": "S-DISCOUNT",
17
- "8": "S-HSN_SAC",
18
- "9": "S-IGST_AMOUNT",
19
- "10": "S-IGST_RATE",
20
- "11": "S-INVOICE_DATE",
21
- "12": "S-INVOICE_NUMBER",
22
- "13": "S-ITEM_NAME",
23
- "14": "S-PLACE_OF_SUPPLY",
24
- "15": "S-QUANTITY",
25
- "16": "S-SELLER_GSTIN",
26
- "17": "S-SGST_AMOUNT",
27
- "18": "S-SGST_RATE",
28
- "19": "S-TAXABLE_VALUE",
29
- "20": "S-TAX_AMOUNT",
30
- "21": "S-TOTAL",
31
- "22": "S-UNIT_PRICE"
 
 
 
 
 
 
 
 
 
32
  },
33
  "initializer_range": 0.02,
34
  "intermediate_size": 768,
35
  "label2id": {
36
- "B-SELLER_NAME": 0,
37
- "E-SELLER_NAME": 1,
38
- "I-SELLER_NAME": 2,
39
- "O": 3,
40
- "S-BUYER_NAME": 4,
41
- "S-CGST_AMOUNT": 5,
42
- "S-CGST_RATE": 6,
43
- "S-DISCOUNT": 7,
44
- "S-HSN_SAC": 8,
45
- "S-IGST_AMOUNT": 9,
46
- "S-IGST_RATE": 10,
47
- "S-INVOICE_DATE": 11,
48
- "S-INVOICE_NUMBER": 12,
49
- "S-ITEM_NAME": 13,
50
- "S-PLACE_OF_SUPPLY": 14,
51
- "S-QUANTITY": 15,
52
- "S-SELLER_GSTIN": 16,
53
- "S-SGST_AMOUNT": 17,
54
- "S-SGST_RATE": 18,
55
- "S-TAXABLE_VALUE": 19,
56
- "S-TAX_AMOUNT": 20,
57
- "S-TOTAL": 21,
58
- "S-UNIT_PRICE": 22
 
 
 
 
 
 
 
 
 
59
  },
60
  "layer_norm_eps": 1e-12,
61
  "max_2d_position_embeddings": 1024,
 
6
  "hidden_dropout_prob": 0.1,
7
  "hidden_size": 256,
8
  "id2label": {
9
+ "0": "B-BUYER_NAME",
10
+ "1": "B-INVOICE_DATE",
11
+ "2": "B-ITEM_NAME",
12
+ "3": "B-SELLER_NAME",
13
+ "4": "E-BUYER_NAME",
14
+ "5": "E-INVOICE_DATE",
15
+ "6": "E-ITEM_NAME",
16
+ "7": "E-SELLER_NAME",
17
+ "8": "I-BUYER_NAME",
18
+ "9": "I-INVOICE_DATE",
19
+ "10": "I-ITEM_NAME",
20
+ "11": "I-SELLER_NAME",
21
+ "12": "O",
22
+ "13": "S-BUYER_NAME",
23
+ "14": "S-CGST_AMOUNT",
24
+ "15": "S-CGST_RATE",
25
+ "16": "S-DISCOUNT",
26
+ "17": "S-HSN_SAC",
27
+ "18": "S-IGST_AMOUNT",
28
+ "19": "S-IGST_RATE",
29
+ "20": "S-INVOICE_DATE",
30
+ "21": "S-INVOICE_NUMBER",
31
+ "22": "S-ITEM_NAME",
32
+ "23": "S-PLACE_OF_SUPPLY",
33
+ "24": "S-QUANTITY",
34
+ "25": "S-SELLER_GSTIN",
35
+ "26": "S-SGST_AMOUNT",
36
+ "27": "S-SGST_RATE",
37
+ "28": "S-TAXABLE_VALUE",
38
+ "29": "S-TAX_AMOUNT",
39
+ "30": "S-TOTAL",
40
+ "31": "S-UNIT_PRICE"
41
  },
42
  "initializer_range": 0.02,
43
  "intermediate_size": 768,
44
  "label2id": {
45
+ "B-BUYER_NAME": 0,
46
+ "B-INVOICE_DATE": 1,
47
+ "B-ITEM_NAME": 2,
48
+ "B-SELLER_NAME": 3,
49
+ "E-BUYER_NAME": 4,
50
+ "E-INVOICE_DATE": 5,
51
+ "E-ITEM_NAME": 6,
52
+ "E-SELLER_NAME": 7,
53
+ "I-BUYER_NAME": 8,
54
+ "I-INVOICE_DATE": 9,
55
+ "I-ITEM_NAME": 10,
56
+ "I-SELLER_NAME": 11,
57
+ "O": 12,
58
+ "S-BUYER_NAME": 13,
59
+ "S-CGST_AMOUNT": 14,
60
+ "S-CGST_RATE": 15,
61
+ "S-DISCOUNT": 16,
62
+ "S-HSN_SAC": 17,
63
+ "S-IGST_AMOUNT": 18,
64
+ "S-IGST_RATE": 19,
65
+ "S-INVOICE_DATE": 20,
66
+ "S-INVOICE_NUMBER": 21,
67
+ "S-ITEM_NAME": 22,
68
+ "S-PLACE_OF_SUPPLY": 23,
69
+ "S-QUANTITY": 24,
70
+ "S-SELLER_GSTIN": 25,
71
+ "S-SGST_AMOUNT": 26,
72
+ "S-SGST_RATE": 27,
73
+ "S-TAXABLE_VALUE": 28,
74
+ "S-TAX_AMOUNT": 29,
75
+ "S-TOTAL": 30,
76
+ "S-UNIT_PRICE": 31
77
  },
78
  "layer_norm_eps": 1e-12,
79
  "max_2d_position_embeddings": 1024,
label2id.json CHANGED
@@ -1,25 +1,34 @@
1
  {
2
- "B-SELLER_NAME": 0,
3
- "E-SELLER_NAME": 1,
4
- "I-SELLER_NAME": 2,
5
- "O": 3,
6
- "S-BUYER_NAME": 4,
7
- "S-CGST_AMOUNT": 5,
8
- "S-CGST_RATE": 6,
9
- "S-DISCOUNT": 7,
10
- "S-HSN_SAC": 8,
11
- "S-IGST_AMOUNT": 9,
12
- "S-IGST_RATE": 10,
13
- "S-INVOICE_DATE": 11,
14
- "S-INVOICE_NUMBER": 12,
15
- "S-ITEM_NAME": 13,
16
- "S-PLACE_OF_SUPPLY": 14,
17
- "S-QUANTITY": 15,
18
- "S-SELLER_GSTIN": 16,
19
- "S-SGST_AMOUNT": 17,
20
- "S-SGST_RATE": 18,
21
- "S-TAXABLE_VALUE": 19,
22
- "S-TAX_AMOUNT": 20,
23
- "S-TOTAL": 21,
24
- "S-UNIT_PRICE": 22
 
 
 
 
 
 
 
 
 
25
  }
 
1
  {
2
+ "B-BUYER_NAME": 0,
3
+ "B-INVOICE_DATE": 1,
4
+ "B-ITEM_NAME": 2,
5
+ "B-SELLER_NAME": 3,
6
+ "E-BUYER_NAME": 4,
7
+ "E-INVOICE_DATE": 5,
8
+ "E-ITEM_NAME": 6,
9
+ "E-SELLER_NAME": 7,
10
+ "I-BUYER_NAME": 8,
11
+ "I-INVOICE_DATE": 9,
12
+ "I-ITEM_NAME": 10,
13
+ "I-SELLER_NAME": 11,
14
+ "O": 12,
15
+ "S-BUYER_NAME": 13,
16
+ "S-CGST_AMOUNT": 14,
17
+ "S-CGST_RATE": 15,
18
+ "S-DISCOUNT": 16,
19
+ "S-HSN_SAC": 17,
20
+ "S-IGST_AMOUNT": 18,
21
+ "S-IGST_RATE": 19,
22
+ "S-INVOICE_DATE": 20,
23
+ "S-INVOICE_NUMBER": 21,
24
+ "S-ITEM_NAME": 22,
25
+ "S-PLACE_OF_SUPPLY": 23,
26
+ "S-QUANTITY": 24,
27
+ "S-SELLER_GSTIN": 25,
28
+ "S-SGST_AMOUNT": 26,
29
+ "S-SGST_RATE": 27,
30
+ "S-TAXABLE_VALUE": 28,
31
+ "S-TAX_AMOUNT": 29,
32
+ "S-TOTAL": 30,
33
+ "S-UNIT_PRICE": 31
34
  }
model_int8.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:76680802d9f8cc20b5b0f1e0102b8b23fabfed4c99825a0c76e808018747b739
3
+ size 13807989
pytorch_model.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:38e7b0a2864f02450bc9fc1ef1a545f38f74e46158e2a3cbffe2a1aff0034d38
3
- size 52374334
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:05a10d81c1bac409274c8963becf2d301cd03308c5a39cae9986586f0b1edac5
3
+ size 52383614