MiniCPM-OCR is a document-understanding model from the MiniCPM-V family developed by OpenBMB. It keeps the MiniCPM-V 4.6 architecture -- a SigLIP vision encoder with a…
MiniCPM-OCR
This model was published in HF papers on 2025-09-16 and contributed to Hugging Face Transformers on 2026-10-26.
MiniCPM-OCR
MiniCPM-OCR is a document-understanding model from the MiniCPM-V family developed by OpenBMB. It keeps the MiniCPM-V 4.6 architecture -- a SigLIP vision encoder with a window-attention merger in front of a Qwen3.5 language model -- and specializes it for reading text-dense images such as scanned pages, screenshots, receipts and tables.
This model was contributed by OpenBMB. The original code can be found here.
Usage example
Inference with Pipeline
from transformers import pipeline
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/ocr_receipt.jpeg",
},
{"type": "text", "text": "Transcribe all the text in this document."},
],
},
]
pipe = pipeline("image-text-to-text", model="openbmb/MiniCPM-OCR-1.0")
outputs = pipe(text=messages, max_new_tokens=256, return_full_text=False)
print(outputs[0]["generated_text"])
#
#
# Document No : TD01167104
# Date : 25/12/2018 8:13:39 PM
# Cashier : MANIS
# Member :
# CASH BILL
# CODE/DESC PRICE Disc AMOUNT
# QTY RM RM
# 9556939040118 KF MODELLING CLAY KIDDY FISH
# 1 PC * 9.00 0.00 9.00
# Total : 9.00
# Rounding Adjustment : 0.00
# Round-d Total (RM) : 9.00
Inference on a single image
from transformers import AutoModelForImageTextToText, AutoProcessor
model_id = "openbmb/MiniCPM-OCR-1.0"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id, device_map="auto")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/ocr_suggestion_form.jpg"},
{"type": "text", "text": "Convert this page to markdown."},
],
}
]
inputs = processor.apply_chat_template(
messages, add_generation_prompt=True, tokenize=True,
return_dict=True, return_tensors="pt",
).to(model.device, dtype=model.dtype)
output = model.generate(**inputs, max_new_tokens=512)
decoded_output = processor.decode(output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
print(decoded_output)
# ```markdown
# R&D QUALITY IMPROVEMENT
# SUGGESTION/SOLUTION FORM
#
# Name/Phone Ext.: M. Hamann, P. Harper, P. Martinez Date: 9/3/92
#
# Supervisor/Manager: J. S. Wigand R&D Group: Licensee
# ...
# ```
The released checkpoint's chat template disables thinking by default. Pass enable_thinking=True
to apply_chat_template to enable it explicitly.
Downsampling mode
MiniCPM-OCR supports both visual downsampling modes of the MiniCPM-V family:
- 4x (default): Less downsampling, more visual tokens, which is what document and screenshot transcription needs.
- 16x: More aggressive downsampling, fewer visual tokens, faster inference on natural images.
The mode has to be passed to the processor and to model.generate together, because the processor counts the visual placeholders the vision tower is expected to produce:
from transformers import AutoModelForImageTextToText, AutoProcessor
model_id = "openbmb/MiniCPM-OCR-1.0"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id, device_map="auto")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/ocr_suggestion_form.jpg"},
{"type": "text", "text": "Convert this page to markdown."},
],
}
]
inputs = processor.apply_chat_template(
messages, add_generation_prompt=True, tokenize=True,
return_dict=True, return_tensors="pt",
processor_kwargs={"downsample_mode": "4x"},
).to(model.device, dtype=model.dtype)
output = model.generate(**inputs, max_new_tokens=512, downsample_mode="4x")
print((inputs["input_ids"] == model.config.image_token_id).sum().item())
# 1260 visual tokens; the same page in `"16x"` mode is 315 instead
A per-call downsample_mode applies to that call only and leaves the processor unchanged, so the
same processor can serve both modes concurrently. When calling the processor directly instead of
going through apply_chat_template, pass it as a plain keyword argument:
inputs = processor(images=[image], text=[text], return_tensors="pt", downsample_mode="4x")
To make a mode the default instead, set it when loading the processor:
processor = AutoProcessor.from_pretrained("openbmb/MiniCPM-OCR-1.0", downsample_mode="4x")
Small image resizing
Images too small to be sliced are upscaled to scale_resolution by default, which is the setting
the released checkpoint was trained with. Screenshots and crops are often better kept at their
native resolution, where upscaling only blurs the glyphs, so small_image_upsize selects between
the two:
"always"(default): upscale them toscale_resolution."never": keep them at their native resolution, aligned to the patch grid only."adaptive": decide per image, and per slice, from glyph size and density.
from transformers import AutoProcessor
from transformers.image_utils import load_image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/ocr_receipt.jpeg"
image = load_image(url).resize((224, 224))
processor = AutoProcessor.from_pretrained("openbmb/MiniCPM-OCR-1.0")
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "Read the text."}]}]
text = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
print(processor(images=[image], text=[text], return_tensors="pt")["target_sizes"].tolist())
# [[32, 32]] -- upscaled to `scale_resolution`
processor = AutoProcessor.from_pretrained("openbmb/MiniCPM-OCR-1.0", small_image_upsize="never")
print(processor(images=[image], text=[text], return_tensors="pt")["target_sizes"].tolist())
# [[16, 16]] -- 224 / 14 patches per side, no upscaling
"adaptive" upscales small dense text to scale_resolution, images below 6272 pixels to 84, and
keeps the rest native; on a slice it never spends more tokens than the baseline tile would. Glyph
height and density are measured with an Otsu threshold followed by connected component analysis, so
it requires OpenCV and raises without it, rather than
silently falling back to a different rule.
processor = AutoProcessor.from_pretrained("openbmb/MiniCPM-OCR-1.0", small_image_upsize="adaptive")
print(processor(images=[image], text=[text], return_tensors="pt")["target_sizes"].tolist())
# [[32, 32]] -- dense text detected, so this one is upscaled anyway
Like downsample_mode, it can be overridden per call as well, which keeps the 224x224 receipt at
its native resolution without reloading the processor:
inputs = processor(images=[image], text=[text], return_tensors="pt", small_image_upsize="never")
print(inputs["target_sizes"].tolist())
# [[16, 16]] -- 224 / 14 patches per side, no upscaling
Batched inference
代码(47 行)
from transformers import AutoModelForImageTextToText, AutoProcessor
model_id = "openbmb/MiniCPM-OCR-1.0"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id, device_map="auto")
messages = [
[
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/ocr_receipt.jpeg"},
{"type": "text", "text": "Transcribe all the text in this document."},
],
}
],
[
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/ocr_suggestion_form.jpg"},
{"type": "text", "text": "Transcribe all the text in this document."},
],
}
],
]
inputs = processor.apply_chat_template(
messages, add_generation_prompt=True, tokenize=True,
return_dict=True, return_tensors="pt",
processor_kwargs={"padding": True},
).to(model.device, dtype=model.dtype)
output = model.generate(**inputs, max_new_tokens=256)
decoded_outputs = processor.batch_decode(output[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
print(decoded_outputs[0])
# Document No : TD01167104
# Date : 25/12/2018 8:13:39 PM
# Cashier : MANIS
# ...
print(decoded_outputs[1])
# R&D QUALITY IMPROVEMENT
# SUGGESTION/SOLUTION FORM
#
# Name/Phone Ext.: M. Hamann P. Harper P. Martinez Date: 9/3/92
# Supervisor/Manager: J. S. Wigand R&D Group: Licensee
# ...The two pages are resized independently, so their prompts have different lengths. padding has to
be requested through processor_kwargs for apply_chat_template to forward it to the processor,
and the processor pads on the left so that generate keeps producing from the end of each prompt.
MiniCPMOcrConfig
[[autodoc]] MiniCPMOcrConfig
MiniCPMOcrVisionConfig
[[autodoc]] MiniCPMOcrVisionConfig
MiniCPMOcrVisionPreTrainedModel
[[autodoc]] MiniCPMOcrVisionPreTrainedModel - forward
MiniCPMOcrVisionModel
[[autodoc]] MiniCPMOcrVisionModel - forward
MiniCPMOcrModel
[[autodoc]] MiniCPMOcrModel - forward - get_image_features - get_video_features
MiniCPMOcrForConditionalGeneration
[[autodoc]] MiniCPMOcrForConditionalGeneration - forward - get_image_features - get_video_features
MiniCPMOcrProcessor
[[autodoc]] MiniCPMOcrProcessor - call
MiniCPMOcrImageProcessor
[[autodoc]] MiniCPMOcrImageProcessor - preprocess
MiniCPMOcrVideoProcessor
[[autodoc]] MiniCPMOcrVideoProcessor - preprocess
Source and license
Source: Transformers documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.