doublemathew/DeepSeek-OCR
015
1---2base_model:3- deepseek-ai/DeepSeek-OCR4pipeline_tag: image-text-to-text5language:6- multilingual7tags:8- deepseek9- unsloth10- vision-language11- ocr12- custom_code13license: mit14---15# Read our Guide How to: [Run & Fine-tune DeepSeek-OCR!](https://docs.unsloth.ai/new/deepseek-ocr)16 17<div>18<p style="margin-top: 0;margin-bottom: 0;">19 <em>This DeepSeek-OCR upload was edited to enable inference & fine-tuning on the latest transformers (no accuracy change). <a href="https://docs.unsloth.ai/new/deepseek-ocr-run-and-fine-tune#fine-tuning-deepseek-ocr">Read more</a></em>20 </p>21 <div style="display: flex; gap: 5px; align-items: center; ">22 <a href="https://github.com/unslothai/unsloth/">23 <img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="133">24 </a>25 <a href="https://discord.gg/unsloth">26 <img src="https://github.com/unslothai/unsloth/raw/main/images/Discord%20button.png" width="173">27 </a>28 <a href="https://docs.unsloth.ai/new/deepseek-ocr">29 <img src="https://raw.githubusercontent.com/unslothai/unsloth/refs/heads/main/images/documentation%20green%20button.png" width="143">30 </a>31 </div>32<h1 style="margin-top: 0rem;">β¨ Read our DeepSeek-OCR Guide <a href="https://docs.unsloth.ai/new/deepseek-ocr">here</a>!</h1>33</div>34 35- Thank you to [Prithiv's](https://huggingface.co/prithivMLmods/DeepSeek-OCR-Latest-BF16.I64) model modifcations that enables DeepSeek-OCR fine-tuning.36- Fine-tune DeepSeek-OCR for free using our [Google Colab notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Deepseek_OCR_(3B).ipynb)37- View the rest of our notebooks in our [docs here](https://docs.unsloth.ai/get-started/unsloth-notebooks).38 39---40 41<div align="center">42 <img src="https://github.com/deepseek-ai/DeepSeek-V2/blob/main/figures/logo.svg?raw=true" width="60%" alt="DeepSeek AI" />43</div>44<hr>45<div align="center">46 <a href="https://www.deepseek.com/" target="_blank">47 <img alt="Homepage" src="https://github.com/deepseek-ai/DeepSeek-V2/blob/main/figures/badge.svg?raw=true" />48 </a>49 <a href="https://huggingface.co/deepseek-ai/DeepSeek-OCR" target="_blank">50 <img alt="Hugging Face" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-DeepSeek%20AI-ffc107?color=ffc107&logoColor=white" />51 </a>52 53</div>54 55<div align="center">56 57 <a href="https://discord.gg/Tc7c45Zzu5" target="_blank">58 <img alt="Discord" src="https://img.shields.io/badge/Discord-DeepSeek%20AI-7289da?logo=discord&logoColor=white&color=7289da" />59 </a>60 <a href="https://twitter.com/deepseek_ai" target="_blank">61 <img alt="Twitter Follow" src="https://img.shields.io/badge/Twitter-deepseek_ai-white?logo=x&logoColor=white" />62 </a>63 64</div>65 66 67 68<p align="center">69 <a href="https://github.com/deepseek-ai/DeepSeek-OCR"><b>π Github</b></a> |70 <a href="https://huggingface.co/deepseek-ai/DeepSeek-OCR"><b>π₯ Model Download</b></a> |71 <a href="https://github.com/deepseek-ai/DeepSeek-OCR/blob/main/DeepSeek_OCR_paper.pdf"><b>π Paper Link</b></a> |72 <a href="https://arxiv.org/abs/2510.18234"><b>π Arxiv Paper Link</b></a> |73</p>74<h2>75<p align="center">76 <a href="">DeepSeek-OCR: Contexts Optical Compression</a>77</p>78</h2>79<p align="center">80<img src="assets/fig1.png" style="width: 1000px" align=center>81</p>82<p align="center">83<a href="">Explore the boundaries of visual-text compression.</a> 84</p>85 86## Usage87Inference using Huggingface transformers on NVIDIA GPUs. Requirements tested on python 3.12.9 + CUDA11.8οΌ88 89```90torch==2.6.091transformers==4.46.392tokenizers==0.20.393einops94addict 95easydict96pip install flash-attn==2.7.3 --no-build-isolation97```98 99```python100from transformers import AutoModel, AutoTokenizer101import torch102import os103os.environ["CUDA_VISIBLE_DEVICES"] = '0'104model_name = 'deepseek-ai/DeepSeek-OCR'105 106tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)107model = AutoModel.from_pretrained(model_name, _attn_implementation='flash_attention_2', trust_remote_code=True, use_safetensors=True)108model = model.eval().cuda().to(torch.bfloat16)109 110# prompt = "<image>\nFree OCR. "111prompt = "<image>\n<|grounding|>Convert the document to markdown. "112image_file = 'your_image.jpg'113output_path = 'your/output/dir'114 115# infer(self, tokenizer, prompt='', image_file='', output_path = ' ', base_size = 1024, image_size = 640, crop_mode = True, test_compress = False, save_results = False):116 117# Tiny: base_size = 512, image_size = 512, crop_mode = False118# Small: base_size = 640, image_size = 640, crop_mode = False119# Base: base_size = 1024, image_size = 1024, crop_mode = False120# Large: base_size = 1280, image_size = 1280, crop_mode = False121 122# Gundam: base_size = 1024, image_size = 640, crop_mode = True123 124res = 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)125```126 127## vLLM128Refer to [πGitHub](https://github.com/deepseek-ai/DeepSeek-OCR/) for guidance on model inference acceleration and PDF processing, etc.<!-- -->129 130[2025/10/23] πππ DeepSeek-OCR is now officially supported in upstream [vLLM](https://docs.vllm.ai/projects/recipes/en/latest/DeepSeek/DeepSeek-OCR.html#installing-vllm).131```shell132uv venv133source .venv/bin/activate134# Until v0.11.1 release, you need to install vLLM from nightly build135uv pip install -U vllm --pre --extra-index-url https://wheels.vllm.ai/nightly136```137 138```python139from vllm import LLM, SamplingParams140from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor141from PIL import Image142 143# Create model instance144llm = LLM(145 model="deepseek-ai/DeepSeek-OCR",146 enable_prefix_caching=False,147 mm_processor_cache_gb=0,148 logits_processors=[NGramPerReqLogitsProcessor]149)150 151# Prepare batched input with your image file152image_1 = Image.open("path/to/your/image_1.png").convert("RGB")153image_2 = Image.open("path/to/your/image_2.png").convert("RGB")154prompt = "<image>\nFree OCR."155 156model_input = [157 {158 "prompt": prompt,159 "multi_modal_data": {"image": image_1}160 },161 {162 "prompt": prompt,163 "multi_modal_data": {"image": image_2}164 }165]166 167sampling_param = SamplingParams(168 temperature=0.0,169 max_tokens=8192,170 # ngram logit processor args171 extra_args=dict(172 ngram_size=30,173 window_size=90,174 whitelist_token_ids={128821, 128822}, # whitelist: <td>, </td>175 ),176 skip_special_tokens=False,177 )178# Generate output179model_outputs = llm.generate(model_input, sampling_param)180 181# Print output182for output in model_outputs:183 print(output.outputs[0].text)184```185 186 187## Visualizations188<table>189<tr>190<td><img src="assets/show1.jpg" style="width: 500px"></td>191<td><img src="assets/show2.jpg" style="width: 500px"></td>192</tr>193<tr>194<td><img src="assets/show3.jpg" style="width: 500px"></td>195<td><img src="assets/show4.jpg" style="width: 500px"></td>196</tr>197</table>198 199 200## Acknowledgement201 202We would like to thank [Vary](https://github.com/Ucas-HaoranWei/Vary/), [GOT-OCR2.0](https://github.com/Ucas-HaoranWei/GOT-OCR2.0/), [MinerU](https://github.com/opendatalab/MinerU), [PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR), [OneChart](https://github.com/LingyvKong/OneChart), [Slow Perception](https://github.com/Ucas-HaoranWei/Slow-Perception) for their valuable models and ideas.203 204We also appreciate the benchmarks: [Fox](https://github.com/ucaslcl/Fox), [OminiDocBench](https://github.com/opendatalab/OmniDocBench).205 206 207## Citation208```bibtex209@article{wei2025deepseek,210 title={DeepSeek-OCR: Contexts Optical Compression},211 author={Wei, Haoran and Sun, Yaofeng and Li, Yukun},212 journal={arXiv preprint arXiv:2510.18234},213 year={2025}214}