Đồ án fine-tuning mô hình DeepSeek-OCR cho tác vụ nhận dạng chữ viết tay tiếng Việt (Vietnamese Handwriting Recognition) sử dụng bộ dữ liệu UIT-HWDB.
Mô hình được fine-tune bằng kỹ thuật LoRA (Low-Rank Adaptation) với thư viện Unsloth để tối ưu hóa hiệu năng training trên GPU hạn chế.
- Fine-tune mô hình DeepSeek-OCR trên dữ liệu chữ viết tay tiếng Việt
- Đánh giá và so sánh hiệu năng giữa mô hình gốc (Baseline) và mô hình Fine-tuned
- Phân tích chi tiết các loại lỗi OCR: Insertion, Deletion, Substitution
| Metric | Baseline | Fine-tuned | Cải thiện |
|---|---|---|---|
| Mean CER | ~40% | ~12% | ~70% ↓ |
| Median CER | ~33% | ~8% | ~75% ↓ |
- Baseline: ~0% mẫu có CER = 0%
- Fine-tuned: ~25% mẫu có CER = 0%
root/
├── kaggle_notebook/
│ └── deepseek-ocr-fine-tuning.ipynb # Notebook training trên Kaggle
├── results/
│ ├── baseline_evaluation.json # Kết quả đánh giá mô hình gốc
│ ├── finetuned_evaluation.json # Kết quả đánh giá mô hình fine-tuned
│ ├── deepseek-ocr_logs.txt # Log quá trình training
│ └── outputs/
│ ├── checkpoint-400/ # Checkpoint tại step 400
│ └── checkpoint-534/ # Checkpoint cuối cùng (1 epoch)
│ ├── adapter_config.json # Cấu hình LoRA adapter
│ ├── adapter_model.safetensors # Trọng số LoRA adapter
│ ├── tokenizer.json # Tokenizer
│ └── trainer_state.json # Trạng thái training
├── visualizations/ # Thư mục lưu biểu đồ phân tích
├── analyze_results.py # Script phân tích kết quả
└── README.md
| Parameter | Value |
|---|---|
| Rank (r) | 16 |
| Alpha | 16 |
| Dropout | 0 |
| Bias | none |
| Task Type | CAUSAL_LM |
target_modules = [
"q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"
]- Base Model:
unsloth/DeepSeek-OCR - Quantization: 4-bit (QLoRA)
- Epochs: 1
- Total Steps: 534
- Hardware: Tesla T4 GPU (2x)
UIT-HWDB (Vietnamese Handwriting Database):
UIT_HWDB_line: Dữ liệu dòng chữUIT_HWDB_paragraph: Dữ liệu đoạn vănUIT_HWDB_word: Dữ liệu từ đơn
- Training: ~10,000+ samples từ nhiều writer khác nhau
- Testing: 200+ samples
Sử dụng notebook kaggle_notebook/deepseek-ocr-fine-tuning.ipynb:
- Upload notebook lên Kaggle
- Thêm dataset
uit-hwdb-dataset - Bật GPU accelerator (Tesla T4)
- Chạy toàn bộ notebook
python analyze_results.pyScript sẽ:
- In thống kê so sánh Baseline vs Fine-tuned
- Phân tích loại lỗi (Insertion/Deletion/Substitution)
- Tạo biểu đồ trực quan trong thư mục
visualizations/
from unsloth import FastVisionModel
from transformers import AutoModel
from peft import PeftModel
# Load base model
model, tokenizer = FastVisionModel.from_pretrained(
"unsloth/DeepSeek-OCR",
load_in_4bit=True,
auto_model=AutoModel,
trust_remote_code=True,
)
# Load LoRA adapter
model = PeftModel.from_pretrained(model, "results/outputs/checkpoint-534")
# Inference
FastVisionModel.for_inference(model)
result = model.infer(
tokenizer,
prompt="<image>\nFree OCR. ",
image_file="path/to/image.jpg",
)- CER (Character Error Rate): Tỷ lệ lỗi ký tự
- Perfect Match Rate: Tỷ lệ mẫu nhận dạng chính xác 100%
- Insertion Rate: Tỷ lệ lỗi chèn ký tự thừa
- Deletion Rate: Tỷ lệ lỗi bỏ sót ký tự
- Substitution Rate: Tỷ lệ lỗi thay thế ký tự sai
- Framework: Unsloth - Fast fine-tuning
- Base Model: DeepSeek-OCR
- Fine-tuning: LoRA/QLoRA với PEFT
- Training: Hugging Face Transformers + TRL
- Evaluation: jiwer (WER/CER metrics)
- Visualization: Matplotlib
Bang My Linh -- 23122009 -- FIT@HCMUS
MIT License