Invoice Classifier

This is the receipt/invoice classifier model, based on MobileNet V3. Its purpose is to check if an input image is a valid invoice or receipt.

Trained using the following datasets:

Usage

To use the model, first, install some neccessary dependencies:

pip install huggingface_hub onnxruntime pillow numpy

Then run the code:

import json, time, numpy as np
from pathlib import Path
from PIL import Image, ImageOps
import onnxruntime as ort
from huggingface_hub import hf_hub_download

REPO_ID = "huytd189/invoice-classifier"
MODEL_FN = "model.onnx"
LABELS_FN = "class_mapping.json"
IMG = "demo.jpg"
IMG_SIZE = 192

# pull files
model_path = hf_hub_download(REPO_ID, MODEL_FN)
labels_path = hf_hub_download(REPO_ID, LABELS_FN)

# labels
labels = {"0":"invalid","1":"valid"}
if Path(labels_path).exists():
    labels = json.loads(Path(labels_path).read_text())

# session
sess = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
in_name, out_name = sess.get_inputs()[0].name, sess.get_outputs()[0].name

# preprocess image input
img = ImageOps.exif_transpose(Image.open(IMG)).convert("RGB").resize((IMG_SIZE, IMG_SIZE))
x = (np.asarray(img, np.float32) / 255.0)
x = np.transpose(x, (2,0,1))[None, ...]

# run
t0 = time.time()
(logits,) = sess.run([out_name], {in_name: x})
probs = np.exp(logits - logits.max()) / np.exp(logits - logits.max()).sum(-1, keepdims=True)
idx = int(probs.argmax())
print(f"pred={labels.get(str(idx), f'class{idx}')}, probs={probs[0].round(4).tolist()}, {1000*(time.time()-t0):.1f}ms")

Demo

image

image

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Datasets used to train huytd189/invoice-classifier