Views
No views yet
microsoft/layoutlmv3-baseLayoutLMv3ForTokenClassification)1from transformers import LayoutLMv3ForTokenClassification, LayoutLMv3Processor
2from PIL import Image
3import torch
4
5model = LayoutLMv3ForTokenClassification.from_pretrained("your-username/layoutlmv3-cord-invoice")
6processor = LayoutLMv3Processor.from_pretrained("your-username/layoutlmv3-cord-invoice", apply_ocr=False)
7
8image = Image.open("receipt.jpg").convert("RGB")
9
10# words and boxes must come from your own OCR step (e.g. PaddleOCR),
11# with boxes normalized to a 0-1000 scale
12words = [...] # list[str]
13boxes = [...] # list[[x0, y0, x1, y1]], normalized 0-1000
14
15encoding = processor(
16 image, words, boxes=boxes,
17 truncation=True, padding="max_length", max_length=512, return_tensors="pt"
18)
19
20with torch.no_grad():
21 outputs = model(**encoding)
22
23predictions = outputs.logits.argmax(-1).squeeze().tolist()
24labels = [model.config.id2label[p] for p in predictions]⚠️ This model does not include an OCR engine. You must run OCR (e.g. PaddleOCR, Tesseract) first to obtainwordsandboxes, sinceapply_ocr=False.
MENU.NM, MENU.CNT, MENU.PRICE, MENU.UNITPRICE, MENU.DISCOUNTPRICE,
MENU.SUB_NM, MENU.SUB_CNT, MENU.SUB_PRICE, MENU.SUB_UNITPRICE, MENU.ETC,
MENU.SUB_ETC, MENU.NUM, MENU.VATYN,
SUB_TOTAL.SUBTOTAL_PRICE, SUB_TOTAL.TAX_PRICE, SUB_TOTAL.SERVICE_PRICE,
SUB_TOTAL.DISCOUNT_PRICE, SUB_TOTAL.ETC, SUB_TOTAL.OTHERSVC_PRICE,
TOTAL.TOTAL_PRICE, TOTAL.CASHPRICE, TOTAL.CHANGEPRICE, TOTAL.CREDITCARDPRICE,
TOTAL.EMONEYPRICE, TOTAL.MENUQTY_CNT, TOTAL.MENUTYPE_CNT, TOTAL.TOTAL_ETC,
VOID_MENU.NM, OB- / I- per standard BIO tagging — 54 labels total)valid_line, dontcare) into word-level (text, bbox, label) triples, preserving original word order within each entity for correct BIO sequencing.LayoutLMv3Processor, aligning sub-word tokens to their parent word's label (-100 for non-first sub-tokens, per standard token classification practice).LayoutLMv3ForTokenClassification using the 🤗 Trainer API.2e-5seqeval (entity-level Precision, Recall, F1). See training logs for per-label breakdown.