🐳DeepSeek-OCR 2: 実行とファインチューニングのガイド
DeepSeek-OCR-2 をローカルで実行・ファインチューニングする方法のガイド。
最終更新
役に立ちましたか?
役に立ちましたか?
# 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|> の位置を特定してください。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)torch==2.6.0
transformers==4.46.3
tokenizers==0.20.3
einops
addict
easydict
pip install flash-attn==2.7.3 --no-build-isolationfrom 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)