[Model] Support Unlimited OCR (#46564)
Signed-off-by: Tianyu Guo <guoty9@mail2.sysu.edu.cn> Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Co-authored-by: Isotr0py <mozf@mail2.sysu.edu.cn> Co-authored-by: Roger Wang <hey@rogerw.io>
This commit is contained in:
co-authored by
Isotr0py
Roger Wang
parent
a65f93fb2e
commit
c6741b2ad4
@@ -161,10 +161,12 @@ class DeepseekOCRProcessor(ProcessorMixin):
|
||||
image_size: int = IMAGE_SIZE,
|
||||
base_size: int = BASE_SIZE,
|
||||
strategy: Literal["v1", "v2"] = "v1",
|
||||
max_crops: int = MAX_CROPS,
|
||||
**kwargs,
|
||||
):
|
||||
self.image_size = image_size
|
||||
self.base_size = base_size
|
||||
self.max_crops = max_crops
|
||||
|
||||
# image token calculation strategy for
|
||||
# Deepseek-OCR and Deepseek-OCR-2
|
||||
@@ -332,7 +334,7 @@ class DeepseekOCRProcessor(ProcessorMixin):
|
||||
crop_ratio = [1, 1]
|
||||
elif cropping:
|
||||
images_crop_raw, crop_ratio = dynamic_preprocess(
|
||||
image, image_size=self.image_size
|
||||
image, image_size=self.image_size, max_num=self.max_crops
|
||||
)
|
||||
else:
|
||||
crop_ratio = [1, 1]
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Image processor for Unlimited-OCR (baidu/Unlimited-OCR)."""
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.transformers_utils.processors.deepseek_ocr import DeepseekOCRProcessor
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class UnlimitedOCRProcessor(DeepseekOCRProcessor):
|
||||
"""DeepseekOCRProcessor variant for Unlimited-OCR.
|
||||
|
||||
The only behavioural difference from the base processor is a multi-image
|
||||
safeguard: when more than one image is present, crop ("gundam") mode is
|
||||
disabled.
|
||||
|
||||
Because the effective crop flag then depends on *how many* images are in the
|
||||
request, the per-item processing output is no longer invariant of sibling
|
||||
images. ``UnlimitedOCRMultiModalProcessor`` accounts for this by bypassing
|
||||
the multimodal processing cache for multi-image requests (see its
|
||||
``_cached_apply_hf_processor``), so the two paths stay consistent.
|
||||
|
||||
DeepSeek-OCR does *not* have this restriction because its ``max_crops=6`` is
|
||||
small enough to be safe for multi-image use.
|
||||
"""
|
||||
|
||||
def tokenize_with_images(
|
||||
self,
|
||||
conversation: str,
|
||||
images: list[Image.Image],
|
||||
bos: bool = True,
|
||||
eos: bool = True,
|
||||
cropping: bool = True,
|
||||
):
|
||||
if len(images) > 1 and cropping:
|
||||
logger.warning_once(
|
||||
"Unlimited-OCR: crop mode is not supported for multi-image "
|
||||
"input. Falling back to cropping=False."
|
||||
)
|
||||
cropping = False
|
||||
return super().tokenize_with_images(
|
||||
conversation, images, bos=bos, eos=eos, cropping=cropping
|
||||
)
|
||||
Reference in New Issue
Block a user