Skip to content

gracebml/deepseek-ocr_fine-tuning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DeepSeek-OCR Fine-tuning

Giới thiệu

Đồ á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ế.

Mục tiêu

  • 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

Kết quả

Cải thiện CER (Character Error Rate)

Metric Baseline Fine-tuned Cải thiện
Mean CER ~40% ~12% ~70% ↓
Median CER ~33% ~8% ~75% ↓

Tỉ lệ khớp hoàn hảo (Perfect Match Rate)

  • Baseline: ~0% mẫu có CER = 0%
  • Fine-tuned: ~25% mẫu có CER = 0%

🗂️ Cấu trúc thư mục

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                              

Cấu hình Fine-tuning

LoRA Configuration

Parameter Value
Rank (r) 16
Alpha 16
Dropout 0
Bias none
Task Type CAUSAL_LM

Target Modules

target_modules = [
    "q_proj", "k_proj", "v_proj", "o_proj",
    "gate_proj", "up_proj", "down_proj"
]

Training Configuration

  • Base Model: unsloth/DeepSeek-OCR
  • Quantization: 4-bit (QLoRA)
  • Epochs: 1
  • Total Steps: 534
  • Hardware: Tesla T4 GPU (2x)

Dataset

UIT-HWDB (Vietnamese Handwriting Database):

  • UIT_HWDB_line: Dữ liệu dòng chữ
  • UIT_HWDB_paragraph: Dữ liệu đoạn văn
  • UIT_HWDB_word: Dữ liệu từ đơn

Data Split

  • Training: ~10,000+ samples từ nhiều writer khác nhau
  • Testing: 200+ samples

Hướng dẫn sử dụng

1. Training trên Kaggle

Sử dụng notebook kaggle_notebook/deepseek-ocr-fine-tuning.ipynb:

  1. Upload notebook lên Kaggle
  2. Thêm dataset uit-hwdb-dataset
  3. Bật GPU accelerator (Tesla T4)
  4. Chạy toàn bộ notebook

2. Phân tích kết quả

python analyze_results.py

Script 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/

4. Sử dụng mô hình Fine-tuned

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",
)

Evaluation Metrics

  • 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

Công nghệ sử dụng

  • 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

Tài liệu tham khảo

👤 Tác giả

Bang My Linh -- 23122009 -- FIT@HCMUS

📄 License

MIT License

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors