Skip to content

Fix incorrect width ratio calculation in Llama4 image processor #38842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Jingxiang-Zhang
Copy link
Contributor

In the Llama4ImageProcessorFast class (source), the image resizing ratios for height and width are incorrectly calculated using the image height for both dimensions:

ratio_h, ratio_w = (
    target_size[0] // size.height,
    target_size[1] // size.height,
)

Here, size is defined as:

size = {"height": 336, "width": 336}

This configuration is also present in the preprocessor_config.json:

"size": {
  "height": 336,
  "width": 336
}

Currently, the bug does not cause visible issues because the height and width are equal. However, this would lead to incorrect behavior if the dimensions were different.

Suggested Fix:
Update the width ratio calculation to use size.width instead of size.height:

ratio_h, ratio_w = (
    target_size[0] // size.height,
    target_size[1] // size.width,
)

This change ensures correct aspect ratio handling in cases where the input size is not square.

@Rocketknight1
Copy link
Member

cc @qubvel @zucchini-nlp

Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing @Jingxiang-Zhang, looks indeed like a bug 👍

cc @yonigozlan for fast image processors

Copy link
Member

@yonigozlan yonigozlan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing! Typo indeed

@zucchini-nlp zucchini-nlp enabled auto-merge (squash) June 17, 2025 07:20
@zucchini-nlp zucchini-nlp merged commit 9c878d2 into huggingface:main Jun 17, 2025
11 checks passed
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants