Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
**InvoiceReceiptClassifier** is a fine-tuned LayoutLMv2 model that classifies a document to an invoice or receipt.
|
| 2 |
+
|
| 3 |
+
## Quick start: using the raw model
|
| 4 |
+
|
| 5 |
+
```python
|
| 6 |
+
from transformers import (
|
| 7 |
+
AutoModelForSequenceClassification,
|
| 8 |
+
LayoutLMv2FeatureExtractor,
|
| 9 |
+
LayoutLMv2Tokenizer,
|
| 10 |
+
LayoutLMv2Processor,
|
| 11 |
+
)
|
| 12 |
+
model = AutoModelForSequenceClassification.from_pretrained("fedihch/InvoiceReceiptClassifier")
|
| 13 |
+
feature_extractor = LayoutLMv2FeatureExtractor()
|
| 14 |
+
tokenizer = LayoutLMv2Tokenizer.from_pretrained("microsoft/layoutlmv2-base-uncased")
|
| 15 |
+
processor = LayoutLMv2Processor(feature_extractor, tokenizer)
|
| 16 |
+
```
|
| 17 |
+
```python
|
| 18 |
+
from PIL import Image
|
| 19 |
+
input_img = Image.open("*****.jpg")
|
| 20 |
+
w, h = input_img.size
|
| 21 |
+
input_img = input_img.convert("RGB").resize((int(w * 600 / h), 600))
|
| 22 |
+
encoded_inputs = processor(input_img, return_tensors="pt")
|
| 23 |
+
for k, v in encoded_inputs.items():
|
| 24 |
+
encoded_inputs[k] = v.to(model.device)
|
| 25 |
+
outputs = model(**encoded_inputs)
|
| 26 |
+
logits = outputs.logits
|
| 27 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 28 |
+
id2label = {0: "invoice", 1: "receipt"}
|
| 29 |
+
print(id2label[predicted_class_idx])
|
| 30 |
+
```
|