Skip to content

add fast image processor nougat#37661

Merged
yonigozlan merged 15 commits intohuggingface:mainfrom
NahieliV:adding_nougat_fast_processor
Jun 27, 2025
Merged

add fast image processor nougat#37661
yonigozlan merged 15 commits intohuggingface:mainfrom
NahieliV:adding_nougat_fast_processor

Conversation

@NahieliV
Copy link
Copy Markdown
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
@github-actions
Copy link
Copy Markdown
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
Copy Markdown
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 thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!

Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py Outdated
Comment thread src/transformers/models/nougat/image_processing_nougat_fast.py
@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy link
Copy Markdown
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
Copy link
Copy Markdown

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
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* add fast image processor nougat

* test fixes

* docstring white space

* last fixes

* docstring_type

* tolerance unit test

* fix tolerance

* fix rtol

* remove traling white space

* remove white space

* note for tolerance unit test

* fix tests

* remove print

---------

Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co>
Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
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