# DeepSeek-OCR 2：运行与微调指南

**DeepSeek-OCR 2** 是 DeepSeek 于 2026 年 1 月 27 日发布的用于最先进视觉与文档理解的 3B 参数新模型。该模型侧重于图像到文本的转换，并具有更强的视觉推理能力，而不仅仅是文本提取。

DeepSeek-OCR 2 引入了 DeepEncoder V2，使模型能够以与人类相同的逻辑顺序“看见”图像。

不同于传统以固定网格（左上→右下）扫描图像的视觉大模型，DeepEncoder V2 先构建全局理解，然后学习类似人类的阅读顺序，知道先关注什么、接着关注什么，以此类推。这通过更好地遵循列顺序、将标签与数值关联、连贯地读取表格以及处理混合文本与结构，提升了复杂布局下的 OCR 效果。

您现在可以通过我们的 Unsloth 在 DeepSeek-OCR 2 上进行微调，方法为 [**免费微调笔记本**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Deepseek_OCR_\(3B\).ipynb)**.** 我们展示了 [88.6% 的提升](#fine-tuning-deepseek-ocr) 在语言理解方面。

<a href="#running-deepseek-ocr-2" class="button primary">运行 DeepSeek-OCR 2</a><a href="#fine-tuning-deepseek-ocr-2" class="button secondary">微调 DeepSeek-OCR 2</a>

## 🖥️ **运行 DeepSeek-OCR 2**

为了运行该模型，与第一个模型一样，DeepSeek-OCR 2 经过修改以支持在最新 transformers 上进行推理与训练（准确率无变化）。您可以在 [此处](https://huggingface.co/unsloth/DeepSeek-OCR-2).

要在以下平台运行该模型 [transformers](#transformers-run-deepseek-ocr-2-tutorial) 或 [Unsloth](#unsloth-run-deepseek-ocr-tutorial)，下面是推荐设置：

### :gear: 推荐设置

DeepSeek 推荐以下设置：

* <mark style="background-color:blue;">**Temperature = 0.0**</mark>
* `max_tokens = 8192`
* `ngram_size = 30`
* `window_size = 90`

**支持模式 - 动态分辨率：**

* 默认： (0-6)×768×768 + 1×1024×1024 — (0-6)×144 + 256 个视觉标记

**提示示例：**

```
# document: <image>\n<|grounding|>将文档转换为 markdown。
# other image: <image>\n<|grounding|>对该图像进行 OCR。
# without layouts: <image>\n自由 OCR。
# figures in document: <image>\n解析该图表。
# general: <image>\n详细描述此图像。
# rec: <image>\n在图像中定位 <|ref|>xxxx<|/ref|>。
```

<div align="center"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FGff3UBRgiu0aFXsbwll2%2Ffig1.png?alt=media&#x26;token=58864777-114a-450a-ab56-3cabc9219cf7" alt="" width="375"><figcaption><p>使用 Visual Causal Flow 将任何文档转换为 markdown。</p></figcaption></figure></div>

### 🦥 Unsloth：运行 DeepSeek-OCR 2 教程

1. 获取最新的 `unsloth` 通过 `pip install --upgrade unsloth` 。如果您已经安装了 Unsloth，请通过以下命令更新： `pip install --upgrade --force-reinstall --no-deps --no-cache-dir unsloth unsloth_zoo`
2. 然后使用以下代码运行 DeepSeek-OCR 2：

{% code overflow="wrap" %}

```python
from unsloth import FastVisionModel
import torch
from transformers import AutoModel
import os
os.environ["UNSLOTH_WARN_UNINITIALIZED"] = '0'

from huggingface_hub import snapshot_download
snapshot_download("unsloth/DeepSeek-OCR-2", local_dir = "deepseek_ocr")
model, tokenizer = FastVisionModel.from_pretrained(
    "./deepseek_ocr",
    load_in_4bit = False, # 使用 4bit 可降低内存占用。16bit LoRA 请设为 False。
    auto_model = AutoModel,
    trust_remote_code = True,
    unsloth_force_compile = True,
    use_gradient_checkpointing = "unsloth", # 对于长上下文使用 True 或 "unsloth"
)

prompt = "<image>\nFree OCR. "
image_file = 'your_image.jpg'
output_path = 'your/output/dir'
res = model.infer(tokenizer, prompt=prompt, image_file=image_file, output_path = output_path, base_size = 1024, image_size = 640, crop_mode=True, save_results = True, test_compress = False)
```

{% endcode %}

### 🤗 Transformers：运行 DeepSeek-OCR 2 教程

在 NVIDIA GPUs 上使用 Huggingface transformers 的推理。测试要求在 python 3.12.9 + CUDA11.8 环境下：

```bash
torch==2.6.0
transformers==4.46.3
tokenizers==0.20.3
einops
addict 
easydict
pip install flash-attn==2.7.3 --no-build-isolation
```

```python
from transformers import AutoModel, AutoTokenizer
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
model_name = 'unsloth/DeepSeek-OCR-2'

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(model_name, _attn_implementation='flash_attention_2', trust_remote_code=True, use_safetensors=True)
model = model.eval().cuda().to(torch.bfloat16)

# prompt = "<image>\nFree OCR. "
prompt = "<image>\n<|grounding|>将文档转换为 markdown。 "
image_file = 'your_image.jpg'
output_path = 'your/output/dir'

res = model.infer(tokenizer, prompt=prompt, image_file=image_file, output_path = output_path, base_size = 1024, image_size = 768, crop_mode=True, save_results = True)
```

## 🦥 **微调 DeepSeek-OCR 2**

Unsloth 现在支持对 DeepSeek-OCR 2 的微调。与第一个模型一样，您需要使用我们的 [自定义上传](https://huggingface.co/unsloth/DeepSeek-OCR-2) 以使其在 `transformers` 上工作（准确率无变化）。与第一个模型相同，Unsloth 在训练 DeepSeek-OCR-2 时速度提升 1.4 倍，显存减少 40%，上下文长度提高 5 倍且无准确率下降。\
\
您现在可以通过我们的免费 Colab 笔记本微调 DeepSeek-OCR 2。

* DeepSeek-OCR 2： [仅微调笔记本](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Deepseek_OCR_2_\(3B\).ipynb)

下面列出了在波斯语上的 CER（字符错误率）准确率改进：

#### 每样本 CER（10 个样本）

| 索引   | OCR1 之前 | OCR1 之后 | OCR2 之前 | OCR2 之后 |
| ---- | ------: | ------: | ------: | ------: |
| 1520 |  1.0000 |  0.8000 | 10.4000 |  1.0000 |
| 1521 |  0.0000 |  0.0000 |  2.6809 |  0.0213 |
| 1522 |  2.0833 |  0.5833 |  4.4167 |  1.0000 |
| 1523 |  0.2258 |  0.0645 |  0.8710 |  0.0968 |
| 1524 |  0.0882 |  0.1176 |  2.7647 |  0.0882 |
| 1525 |  0.1111 |  0.1111 |  0.9444 |  0.2222 |
| 1526 |  2.8571 |  0.8571 |  4.2857 |  0.7143 |
| 1527 |  3.5000 |  1.5000 | 13.2500 |  1.0000 |
| 1528 |  2.7500 |  1.5000 |  1.0000 |  1.0000 |
| 1529 |  2.2500 |  0.8750 |  1.2500 |  0.8750 |

#### 平均 CER（10 个样本）

* **OCR1：** 之前 **1.4866**，之后 **0.6409** (**-57%**)
* **OCR2：** 之前 **4.1863**，之后 **0.6018** (**-86%**)

## 📊 基准测试

DeepSeek-OCR 2 模型的基准来自官方研究论文。

**表 1：** 在 OmniDocBench v1.5 上对文档阅读的综合评估。V-token𝑚𝑎𝑥 表示本基准中每页使用的最大视觉标记数。R-order 表示阅读顺序。除 DeepSeek OCR 与 DeepSeek OCR 2 外，本表中的所有其他模型结果均来自 OmniDocBench 仓库。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F7CjBxsi10P3kqyF3utpq%2FScreenshot%202026-01-27%20at%201.14.02%E2%80%AFAM.png?alt=media&#x26;token=08fc9963-15d1-4d7a-9fb5-93749913928c" alt="" width="375"><figcaption></figcaption></figure>

**表 2：** OmniDocBench v1.5 中不同文档元素类别的编辑距离。\
V-token𝑚𝑎𝑥 表示最低的最大视觉标记数。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FcTVu52ugTwQAqLdnQLLz%2FScreenshot%202026-01-27%20at%201.14.15%E2%80%AFAM.png?alt=media&#x26;token=ec92a4a1-facb-4b63-90cd-73d23a41dcfb" alt="" width="563"><figcaption><p>在 OmniDocBench 上优于 Gemini-3 Pro</p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/mo-xing/tutorials/deepseek-ocr-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
