HiepOCR-3B 🇻🇳
HiepOCR-3B is an end-to-end Vietnamese Vision-Language OCR model specialized for full-page Vietnamese handwriting, structured Markdown tables, strict-schema JSON extraction, and A4 administrative documents, paired with a pure-Rust pre/post-processing engine (HiepOCR1).
- GitHub Repository (Rust Core + NAFNet v2 + Camera Pipeline): https://github.com/hieple241/HiepOCR1
- Model Family: Hiep3215/HiepOCR (3B) | Hiep3215/HiepOCR-1B (1B Edge) | Hiep3215/HiepOCR-Camera (YOLO26n + General VLM)
Highlights 🌟
- Full-Page Vietnamese Handwriting Mastery: Reduces Character Error Rate (CER) on
manhha2502/Vietnamese_Handwriting_OCR(test split) from72.33%(baseVintern-3B-R-beta) down to10.07%(7.18x error reduction). - Zero-Error Structured Tables & Strict JSON: Achieves
0.00%CER and100%(20/20) schema validity on unseen Markdown tables and postal waybill JSON extraction. - Line-Complete Citizen ID (CCCD) Extraction: Preserves
13/13lines (0dropped lines) on standard/high-resolutionvinhnguyenxyz/CCCD-raw-imagescards. - Pure-Rust Edge Pipeline (
HiepOCR1): Laplacian/Tenengrad Blur Gate (lap_var < 120.0), NAFNet v2 ONNX adaptive deblurring (alpha = 0.65 / 0.35), andVnPostalCorrector(weighted Levenshtein0.35+ 63-province dictionary).
Benchmarks 📈
| Model | Table Markdown (20) | Strict JSON (20) | HF Viet Handwriting Full-Page (20) | HF ViTextVQA (20) |
|---|---|---|---|---|
HiepOCR-3B (v4, Ours) | 0.0000 (0.00%) · 20/20 | 0.0000 (0.00%) · 20/20 | 0.1007 (10.07%) | 1.0869 |
HiepOCR-1B (v2, Ours) | 0.0007 (0.07%) · 20/20 | 0.0467 (4.67%) · 20/20 | 0.8116 | 1.0686 |
5CD-AI/Vintern-3B-R-beta | 0.1166 (11.66%) · 20/20 | 0.0000 · 20/20 | 0.7233 (72.33%) | 0.7761 |
Qwen/Qwen2.5-VL-3B-Instruct | 0.3701 (37.01%) · 20/20 | 0.0746 (7.46%) · 20/20 | 0.3934 (39.34%) | 1.1603 |
Qwen/Qwen2-VL-2B-Instruct | 0.0623 (6.23%) · 20/20 | 0.1138 (11.38%) · 20/20 | 2.1068 | 1.9354 |
Evaluated on NVIDIA RTX 5080 (16 GB), bfloat16, deterministic greedy decoding (do_sample=False, max_new_tokens=1024).
Standard VLM & OCR Benchmarks (vs Vintern-3B-R-beta)
| Benchmark | Split / Samples | HiepOCR-1B (v2) | Vintern-3B-R-beta | HiepOCR-3B (v4) |
|---|---|---|---|---|
vi-MTVQA (MTVQA_VI) | Full (250) | 44.1 (34.7s) | 48.4 (52.2s) | 47.7 (59.9s) |
DocVQAval (lmms-lab/DocVQA) | 300 val | 56.6 (29.8s) | 65.1 (47.3s) | 64.4 (54.4s) |
InfoVQAval (infovqa_val) | 300 val | 40.2 (37.4s) | 50.4 (63.7s) | 49.8 (72.7s) |
TextVQAval (TextVQA_val) | 300 val | 55.9 (19.1s) | 63.8 (32.5s) | 63.0 (34.3s) |
ChartQAtest (ChartQA) | 300 test | 52.0 (17.3s) | 65.0 (33.0s) | 63.3 (34.1s) |
OCRBench (echo840/OCRBench) | Full (1000) | 635 / 1000 (114.3s) | 709 / 1000 (202.1s) | 693 / 1000 (198.5s) |
Use Case Demos & Examples 📝
Use Case 1: Full-Page Vietnamese Handwriting Recognition (Vietnamese_Handwriting_OCR)
Prompt: <image>\nĐọc chính xác toàn bộ văn bản viết tay trong ảnh.
Output (handwriting_demo_1.jpg, HiepOCR-3B v4):
BẢN CHẤT CỦA THÀNH CÔNG Đã bao giờ bạn tự hỏi thành công là gì mà bao kẻ bỏ cả cuộc đời mình theo đuổi? Phải chăng đó là kết quả hoàn hảo trong công việc, sự chính xác đến từng chi tiết? Hay đó là cách nói khác của từ thành đạt, nghĩa là có được cuộc sống giàu sang, được mọi người nể phục? Vậy thì bạn hãy dành chút thời gian để lặng mình suy ngẫm. Cuộc sống sẽ chỉ cho bạn có những người thành đạt được thành công theo một cách giản dị đến bất ngờ.
Output (handwriting_demo_2.jpg, HiepOCR-3B v4):
Thành công là khi bố và con trai có dũng khí bước vào bếp, nấu những món ăn mẹ thích nhân ngày 8 - 3. Món canh có thể hơi mặn, món cá sốt đáng lẽ phải có màu đỏ sậm thì lại ngả sang màu... đen cháy. Nhưng nhìn mâm cơm, mẹ vẫn cười. Bởi vì hai bố con không thể thành công trên " chiến trường " bếp núc, nhưng lại thành công khi tặng mẹ " đoá hồng " của tình yêu. Một món quà ý nghĩa hơn cả những món quà quý giá, hạnh phúc ấy long lanh in trong mắt mẹ.
Use Case 2: Structured Markdown Table & Strict JSON Extraction
Markdown Table Output:
| STT | Dịch vụ | SL | Đơn giá | Thành tiền |
| --- | --- | --- | --- | --- |
| 1 | Phí thu hộ COD | 1 | 15.000đ | 15.000đ |
| 2 | Bảo hiểm hàng hóa | 4 | 25.000đ | 100.000đ |
| 3 | Phụ phí vùng sâu | 2 | 12.000đ | 24.000đ |
Strict JSON Output:
{"ma_buu_gui": "EY150971897VN", "nguoi_gui": {"ten": "Đỗ Quang Hải", "dia_chi": "128 Đình Thôn, Phường Từ Liêm, Hà Nội", "sdt": "0733455127"}, "nguoi_nhan": {"ten": "Nguyễn Văn An", "dia_chi": "17 Nguyễn Đức Cảnh, Xã Hoàn Lão, Quảng Trị", "sdt": "0703023257"}}
Use Case 3: Blur Gate + NAFNet v2 Deblurring (HiepOCR1 Rust Core)
GGUF & Safetensors Artifacts
| File | Format | Size | Recommended Use |
|---|---|---|---|
HiepOCR-3B-Q4_K_M.gguf | GGUF 4-bit | ~2.0 GB | CPU / 4–6 GB VRAM |
HiepOCR-3B-Q5_K_M.gguf | GGUF 5-bit | ~2.3 GB | Balanced speed & accuracy |
HiepOCR-3B-Q6_K.gguf | GGUF 6-bit | ~2.7 GB | Near-FP16 fidelity |
HiepOCR-3B-Q8_0.gguf | GGUF 8-bit | ~3.4 GB | High-precision quantized |
HiepOCR-3B-F16.gguf | GGUF FP16 | ~6.4 GB | Full FP16 reference |
mmproj-HiepOCR-3B-F16.gguf | Vision Projector | ~607 MB | Required for all GGUF files |
bf16/ & root / | Safetensors BF16 | ~7.0 GB | PyTorch (transformers==4.46.3) |
Quickstart 🚀
[!IMPORTANT] Always set
max_new_tokens=1024(or higher) for full-page handwriting, A4 documents, and multi-row tables so long outputs are never truncated.
1. Python (transformers==4.46.3)
import numpy as np
import torch
import torchvision.transforms as T
from PIL import Image
from torchvision.transforms.functional import InterpolationMode
from transformers import AutoModel, AutoTokenizer
IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)
def build_transform(input_size=448):
return T.Compose([
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
T.ToTensor(),
T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
])
def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=True):
orig_width, orig_height = image.size
aspect_ratio = orig_width / max(1, orig_height)
target_ratios = sorted(
{(i, j) for n in range(min_num, max_num + 1)
for i in range(1, n + 1) for j in range(1, n + 1)
if min_num <= i * j <= max_num},
key=lambda x: x[0] * x[1],
)
best_ratio = min(target_ratios, key=lambda r: abs(aspect_ratio - r[0] / r[1]))
target_w, target_h = image_size * best_ratio[0], image_size * best_ratio[1]
resized = image.resize((target_w, target_h))
blocks = []
for i in range(best_ratio[0] * best_ratio[1]):
box = (
(i % best_ratio[0]) * image_size,
(i // best_ratio[0]) * image_size,
((i % best_ratio[0]) + 1) * image_size,
((i // best_ratio[0]) + 1) * image_size,
)
blocks.append(resized.crop(box))
if use_thumbnail and len(blocks) != 1:
blocks.append(image.resize((image_size, image_size)))
return blocks
def load_image(image_file, input_size=448, max_num=6):
image = Image.open(image_file).convert('RGB')
transform = build_transform(input_size=input_size)
images = dynamic_preprocess(image, image_size=input_size, max_num=max_num)
return torch.stack([transform(img) for img in images])
model_id = "Hiep3215/HiepOCR"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModel.from_pretrained(model_id, torch_dtype=torch.bfloat16, trust_remote_code=True).eval().cuda()
pixel_values = load_image("handwriting.jpg", max_num=6).to(torch.bfloat16).cuda()
generation_config = dict(max_new_tokens=1024, do_sample=False)
prompt = "<image>\nĐọc chính xác toàn bộ văn bản viết tay trong ảnh."
response = model.chat(tokenizer, pixel_values, prompt, generation_config)
print(response)
2. llama-server + Rust Core (HiepOCR1)
llama-server -m HiepOCR-3B-Q4_K_M.gguf --mmproj mmproj-HiepOCR-3B-F16.gguf --port 18080 -c 4096
./target/release/hiepocr_deblur_rs --ocr handwriting.jpg models/nafnet_deblur.onnx 127.0.0.1:18080