8000 add fast image processor nougat by NahieliV · Pull Request #37661 · huggingface/transformers · GitHub
[go: up one dir, main page]

Skip to content

add fast image processor nougat #37661

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
merged 15 commits into from
Jun 27, 2025

Conversation

NahieliV
Copy link
Contributor

What does this PR do?

Adds fast image processor for Nougat model.

#36978

Before submitting

Who can review?

@yonigozlan

@github-actions github-actions bot marked this pull request as draft April 21, 2025 20:03
Copy link
Contributor

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers and trigger CI.

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.

Hi @NahieliV , thanks for your contribution! Noted a few things to change :)

Comment on lines 112 to 122
def to_channels_first(image):
"""
Converts a NumPy image from channels-last (H, W, C) to channels-first (C, H, W)
if needed. Leaves PyTorch tensors and non-NumPy types unchanged.
"""
if isinstance(image, np.ndarray):
if image.ndim == 3 and image.shape[-1] in [1, 3]:
return torch.tensor(np.transpose(image, (2, 0, 1)))
return torch.tensor(image)
return image


Copy link
Member

Choose a reason for hiding this comment

The reason w 8000 ill be displayed to describe this comment to others. Learn more.

this shouldn't be needed if you make it so that all input images are channels_first

Copy link
Contributor Author
@NahieliV NahieliV Apr 26, 2025

Choose a reason for hiding this comment

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

I removed this for the tests of the method align_long_axis, which indeed returns the channels first if the input image is channels first.

Copy link
Contributor Author
@NahieliV NahieliV Apr 26, 2025

Choose a reason for hiding this comment

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

However, there is a small bug in the original implementation for the method crop_margin when the following condition is met.

 max_val = data.max()
 min_val = data.min()

Even when we set data_format = "channels_first", the image is returned chanels last. This is because the image input format is inferred at the beginning, and then when to_pil_image is called, it's transformed into channels last. Then, when calling to_channel_dimension_format, data_format and input_data_format are the same so the function just returns the image.

I can raise an issue and fix it if you agree.

Copy link
Member

Choose a reason for hiding this comment

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

I see, nice catch! If you could fix the issue in this PR that would be great. Thanks!

@NahieliV NahieliV force-pushed the adding_nougat_fast_processor branch from 86305b0 to 9aa40ea Compare April 27, 2025 17:28
@NahieliV NahieliV marked this pull request as ready for review April 27, 2025 17:56
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 iterating! Still a few changes left to make, but overall almost ready to go!

@NahieliV NahieliV force-pushed the adding_nougat_fast_processor branch 2 times, most recently from 41e8fab to ea91a91 Compare May 1, 2025 16:55
@NahieliV
Copy link
Contributor Author
NahieliV commented May 1, 2025

@yonigozlan , changes are done. There are small differences due to the difference in the implementation of interpolation.BICUBIC from PIL and PyTorch. For the test test_slow_fast_equivalence, there are 16 pixels with a difference over 1e-1. The max pixel difference is 0.18.

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.

Hi @NahieliV ! Sorry for the delay, but thanks for iterating! Looks ready to merge to me :). Very last thing to do is to add a comment explaining why we have a larger than usual difference with slow processor, and overwrite the equivalence tests with a higher threshold.


new_size = (height, width)

return F.resize(image, new_size, interpolation=F.InterpolationMode.BICUBIC)
Copy link
Member

Choose a reason for hiding this comment

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

Indeed the difference with the slow image processor probably comes from here, as in the slow processor, reducing_gap=2.0 is used. Could you add a comment just above this line explaining this issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be good now.

@NahieliV NahieliV force-pushed the adding_nougat_fast_processor branch from ea91a91 to b4dd0fe Compare May 21, 2025 19:59
< 8000 /span> 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 @NahieliV for iterating on this! I had to make some small changes mainly because of recent updates in Transformers, but LGTM! Waiting for the PR to be green then I'll merge

@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.

@yonigozlan yonigozlan enabled auto-merge (squash) June 27, 2025 14:27
@yonigozlan yonigozlan merged commit 4336ecd into huggingface:main Jun 27, 2025
20 checks passed
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.

3 participants
0