添加微信号:AIGC_Tech,公众号小助手会拉你进群!点击下方名片关注AIGC Studio公众号!
点击下方名片关注AIGC Studio公众号!获取最新AI前沿应用/AIGC实践教程!
DeepSeek 构建了一个OCR系统,可以将长文本压缩成视觉标记,真正做到将段落转化为像素。模型 DeepSeek-OCR 在 10 倍压缩下可达到 97% 的解码精度,即使在 20 倍压缩下也能保持 60% 的准确率。这意味着一张图像可以用远低于 LLM 所需的标记数量来表示整个文档。比 GOT-OCR2.0 和 MinerU2.0 的性能更胜一筹,同时使用的标记数量最多可减少 60 倍,并且单台 A100 芯片每天可以处理超过 20 万页的内容。这有望解决人工智能面临的最大难题之一:长上下文处理效率低下。模型或许很快就能"看到"文本而不是"读"文本,而无需为更长的文本序列支付更多费用。上下文压缩的未来可能根本不是文本压缩,而是光学压缩。
相关链接
论文:https://github.com/deepseek-ai/DeepSeek-OCR/blob/main/DeepSeek_OCR_paper.pdf 模型:https://huggingface.co/deepseek-ai/DeepSeek-OCR 代码:https://github.com/deepseek-ai/DeepSeek-OCR 官网:https://www.deepseek.com
当前开源模型支持以下几种模式:
原始分辨率: 微型:512×512(64 个vision token) 小号:640×640(100个vision token) 底图:1024×1024(256个vision token) 大尺寸:1280×1280(400个vision token) 动态分辨率 高达:n×640×640 + 1×1024×1024
效果展示
使用教程
克隆此存储库并导航至 DeepSeek-OCR 文件夹
git clone https://github.com/deepseek-ai/DeepSeek-OCR.git
创建conda环境
conda create -n deepseek-ocr python=3.12.9 -y conda activate deepseek-ocr
下载安装包
下载链接:https://github.com/vllm-project/vllm/releases/tag/v0.8.5
pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu118
pip install vllm-0.8.5+cu118-cp38-abi3-manylinux1_x86_64.whl
pip install -r requirements.txt
pip install flash-attn==2.7.3 --no-build-isolation
vLLM推理
修改 DeepSeek-OCR-master/DeepSeek-OCR-vllm/config.py 文件中的 INPUT_PATH/OUTPUT_PATH 和其他设置。
cd DeepSeek-OCR-master/DeepSeek-OCR-vllm
图像:流输出 python run_dpsk_ocr_image.pypdf:并发量约为 2500tokens/s(A100-40G) python run_dpsk_ocr_pdf.py基准测试的批量评估 python run_dpsk_ocr_eval_batch.py
uv venv
source .venv/bin/activate
# Until v0.11.1 release, you need to install vLLM from nightly build
uv pip install -U vllm --pre --extra-index-url https://wheels.vllm.ai/nightly
from vllm import LLM, SamplingParams
from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
from PIL import Image
# Create model instance
llm = LLM(
model="deepseek-ai/DeepSeek-OCR",
enable_prefix_caching=False,
mm_processor_cache_gb=0,
logits_processors=[NGramPerReqLogitsProcessor]
)
# Prepare batched input with your image file
image_1 = Image.open("path/to/your/image_1.png").convert("RGB")
image_2 = Image.open("path/to/your/image_2.png").convert("RGB")
prompt = "<image>\nFree OCR."
model_input = [
{
"prompt": prompt,
"multi_modal_data": {"image": image_1}
},
{
"prompt": prompt,
"multi_modal_data": {"image": image_2}
}
]
sampling_param = SamplingParams(
temperature=0.0,
max_tokens=8192,
# ngram logit processor args
extra_args=dict(
ngram_size=30,
window_size=90,
whitelist_token_ids={128821, 128822}, # whitelist: <td>, </td>
),
skip_special_tokens=False,
)
# Generate output
model_outputs = llm.generate(model_input, sampling_param)
# Print output
for output in model_outputs:
print(output.outputs[0].text)
Transformer推理
在NVIDIA GPU上使用Huggingface Transformer进行推理。测试环境:Python 3.12.9 + CUDA 11.8。
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
from transformers import AutoModel, AutoTokenizer
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
model_name = 'deepseek-ai/DeepSeek-OCR'
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|>Convert the document to markdown. "
image_file = 'your_image.jpg'
output_path = 'your/output/dir'
# infer(self, tokenizer, prompt='', image_file='', output_path = ' ', base_size = 1024, image_size = 640, crop_mode = True, test_compress = False, save_results = False):
# Tiny: base_size = 512, image_size = 512, crop_mode = False
# Small: base_size = 640, image_size = 640, crop_mode = False
# Base: base_size = 1024, image_size = 1024, crop_mode = False
# Large: base_size = 1280, image_size = 1280, crop_mode = False
# Gundam: base_size = 1024, image_size = 640, crop_mode = True
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 = True)感谢你看到这里,添加小助手 AIGC_Tech 加入官方 AIGC读者交流群,下方扫码加入 AIGC Studio 星球,获取前沿AI应用、AIGC实践教程、大厂面试经验、AI学习路线以及IT类入门到精通学习资料等,欢迎一起交流学习💗~
没有评论:
发表评论