Recipes / Document & invoice OCR
Fine-tune DeepSeek-OCR-2 for a language or layout (Unsloth)
DeepSeek-OCR-2 (3B) fine-tuned on a 200k-sample synthetic Persian OCR dataset.
- No license stated
Source: Unsloth notebooks ↗ · by Unsloth · last updated Oct 7, 2026
What a local business could build with it
OCR for non-English or unusual paperwork where a stock model misreads. The example data is Persian; bring examples in the language your customers write in.
How to run it
Commands are copied from the source, unchanged except where a notebook cell is cut at 40 lines (marked in the code). Placeholders like <you> are the source's own. Credit goes to the authors named on each block.
Install (as written in the notebook)
%%capture
import os, re
if "COLAB_" not in "".join(os.environ.keys()):
!pip install unsloth # Do this in local & cloud setups
else:
!pip install sentencepiece protobuf "datasets==4.3.0" "huggingface_hub>=0.34.0" hf_transfer
!pip install --no-deps unsloth_zoo bitsandbytes accelerate peft trl triton unsloth
!unsloth install-kernels
!pip install --no-deps --upgrade "torchao>=0.16.0"
!pip install transformers==4.57.6
!pip install --no-deps trl==0.22.2
!pip install jiwer
!pip install einops addict easydict
Load the base model
from unsloth import FastVisionModel # FastLanguageModel for LLMs
import torch
from transformers import AutoModel
import os
os.environ["UNSLOTH_WARN_UNINITIALIZED"] = '0'
# 4bit pre quantized models we support for 4x faster downloading + no OOMs.
fourbit_models = [
"unsloth/Qwen3-VL-8B-Instruct-bnb-4bit", # Qwen 3 vision support
"unsloth/Qwen3-VL-8B-Thinking-bnb-4bit",
"unsloth/Qwen3-VL-32B-Instruct-bnb-4bit",
"unsloth/Qwen3-VL-32B-Thinking-bnb-4bit",
] # More models at https://huggingface.co/unsloth
model, tokenizer = FastVisionModel.from_pretrained(
"./deepseek_ocr2",
load_in_4bit = False, # Use 4bit to reduce memory use. False for 16bit LoRA.
auto_model = AutoModel,
trust_remote_code = True,
unsloth_force_compile = True,
use_gradient_checkpointing = "unsloth", # True or "unsloth" for long context
)
Set up training
from transformers import Trainer, TrainingArguments
from unsloth import is_bf16_supported
FastVisionModel.for_training(model) # Enable for training!
data_collator = DeepSeekOCR2DataCollator(
tokenizer = tokenizer,
model = model,
image_size = 768,
base_size = 1024,
crop_mode = True,
train_on_responses_only = True,
)
trainer = Trainer(
model = model,
tokenizer = tokenizer,
data_collator = data_collator, # Must use!
train_dataset = converted_dataset,
args = TrainingArguments(
per_device_train_batch_size = 2,
gradient_accumulation_steps = 4,
warmup_steps = 5,
max_steps = 60,
# num_train_epochs = 1, # Set this instead of max_steps for full training runs
learning_rate = 2e-4,
logging_steps = 1,
optim = "adamw_8bit",
weight_decay = 0.001,
lr_scheduler_type = "linear",
seed = 3407,
fp16 = not is_bf16_supported(), # Use fp16 if bf16 is not supported
bf16 = is_bf16_supported(), # Use bf16 if supported
output_dir = "outputs",
report_to = "none", # For Weights and Biases
dataloader_num_workers = 2,
# You MUST put the below items for vision finetuning:
remove_unused_columns = False,
),
)
Licenses
License flags to read before commercial use:
- Dataset: No license stated. No license stated on the card
| Part | License, as the source or Hub states it |
|---|---|
| Code | LGPL-3.0 (Unsloth notebooks) |
| Base model | Apache-2.0 |
| Dataset | No license stated on the card |
Checked against the Hugging Face API and each repo on December 4, 2026. A grouping like “$10M revenue cap” is our shorthand, not legal advice.