GOT-OCR2 produces incorrect bounding-box coordinates for NumPy images
System Info
transformersversion: 5.21.0.dev0- Platform: Windows-10-10.0.22631-SP0
- Python version: 3.11.17
- Huggingface_hub version: 1.33.0
- Safetensors version: 0.8.0
- Accelerate version: 1.15.0
- PyTorch version (accelerator?): 2.14.1+cpu (NA)
Who can help?
No response
Reproduction
Steps to reproduce
The issue can be reproduced without downloading a model or using a GPU.
import numpy as np
from PIL import Image
from transformers.image_utils import get_image_size
from transformers.models.got_ocr2.processing_got_ocr2 import preprocess_box_annotation
image = Image.new("RGB", (320, 120))
box = [32, 12, 160, 60]
for name, img in [
("PIL", image),
("NumPy", np.asarray(image)),
]:
size = get_image_size(img)
normalized = preprocess_box_annotation(box.copy(), size)
print(f"{name}: size={size}, normalized_box={normalized}")
Actual output
PIL: size=(320, 120), normalized_box=[100, 100, 500, 500]
NumPy: size=(120, 320), normalized_box=[266, 37, 1333, 187]
The same image and bounding box produce different normalized coordinates depending on the image representation.
Root cause
get_image_size() returns (width, height) for PIL images but (height, width) for NumPy arrays.
However, preprocess_box_annotation() interprets its image_size argument as (width, height).
In GotOcr2Processor.prepare_inputs_layout(), both the single-image and multi-page paths pass get_image_size() results directly to the bounding-box normalization function.
Consequently, NumPy inputs can produce incorrect coordinates, including values outside the expected [0, 1000] range.
Impact
This also affects actual processor output. A regression test comparing GotOcr2Processor input IDs for identical PIL and NumPy images with the same bounding box failed with 14 mismatched token IDs.
Expected behavior
Identical PIL and NumPy representations of the same image should produce identical normalized bounding-box coordinates and equivalent GOT-OCR2 input IDs.
For the reproduction above, both should produce:
normalized_box=[100, 100, 500, 500]
Bounding-box coordinates should be normalized consistently regardless of image representation.
Hi @ArthurZucker @qubvel! I'd like to work on this issue.
I've reproduced the bug: get_image_size() returns (width, height) for PIL images via image.size, but (height, width) for NumPy arrays. When passed into GotOcr2Processor.prepare_inputs_layout(), preprocess_box_annotation() unpacks width, height = image_size, leading to swapped dimensions and invalid coordinates for NumPy inputs.
I'm opening a PR with the fix and regression tests shortly.
Thanks @aarushdubey .I’ve already opened #49904 for this issue with the fix and regression coverage for the PIL/NumPy mismatch. Feel free to take a look there and share any suggestions.
Thanks @BlazeSR10hawk!
I noticed in #49904 the fix introduces a local helper (_get_box_image_size) that branches on is_pil_image. In #49957, I addressed the root cause directly in image_utils.get_image_size() so PIL images adhere to the documented (height, width) contract just like NumPy arrays, along with regression tests for both image_utils and GotOcr2Processor.
Happy to hear the maintainers' preference on whether they'd like the root fix in image_utils.py or the processor-local helper!
cc @molbap @guarin for vision models