# DeepSeek-OCR 2: 実行とファインチューニングガイド

**DeepSeek-OCR 2** は、DeepSeek が 2026年1月27日に公開した、SOTA のビジョンおよび文書理解向けの新しい 30億パラメータモデルです。このモデルは、単なるテキスト抽出ではなく、より強力な視覚推論を備えた画像からテキストへの変換に重点を置いています。

DeepSeek-OCR 2 は DeepEncoder V2 を導入し、画像を人間と同じ論理順で「見る」ことをモデルに可能にします。

固定グリッド（左上 → 右下）で画像を走査する従来の vision LLM とは異なり、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="/pages/f7d04e17a76834c8a0c837ec4aee9d3d64c1c9db#running-deepseek-ocr-2" class="button primary">DeepSeek-OCR 2 の実行</a><a href="/pages/f7d04e17a76834c8a0c837ec4aee9d3d64c1c9db#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="/files/585a882fe568523ec24c87cbfdac098bc8b7b1a6" 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>\n自由 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 GPU 上で 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>\n自由 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 を 40% 少ない VRAM で 1.4 倍高速に学習し、精度を損なうことなく 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 サンプル）

| idx  | 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="/files/ba7e14729cf62ed6f135dc64ef0ad0d83f479640" alt="" width="375"><figcaption></figcaption></figure>

**表 2:** OmniDocBench v1.5 における文書要素のカテゴリ別の編集距離。\
V-token𝑚𝑎𝑥 は、使用される視覚トークンの最大数の最小値を示します。

<figure><img src="/files/cd89d09b20e353ca04020a0d3ee7dbc69437eba3" 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/jp/moderu/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.
