Skip to content

Add ImageProcessorFast to Efficientnet processor - #37094

Open
Yann-CV wants to merge 14 commits into
huggingface:mainfrom
Yann-CV:fast_processor/efficientnet
Open

Add ImageProcessorFast to Efficientnet processor #37094
Yann-CV wants to merge 14 commits into
huggingface:mainfrom
Yann-CV:fast_processor/efficientnet

Conversation

@Yann-CV

@Yann-CV Yann-CV commented Mar 28, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

Following #36978:
This pull request introduces a new fast image processor for EfficientNet models and integrates it into the existing codebase. The changes include updates to documentation, initialization files, and test cases to support the new EfficientNetImageProcessorFast.

Integration of EfficientNetImageProcessorFast:

Implementation of EfficientNetImageProcessorFast:

Testing and Dummy Objects:

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Yann-CV
Yann-CV marked this pull request as ready for review March 28, 2025 22:39
@github-actions
github-actions Bot requested review from ydshieh and yonigozlan March 28, 2025 22:40
@Yann-CV

Yann-CV commented Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

I am spotting an inconsistency between slow (in efficientnet) and fast resize (base class):

    def test_resize(self):
        torch.manual_seed(0)
        data =  torch.randint(0, 256, (1, 3, 480, 640), dtype=torch.uint8)
        for image_processor_class in self.image_processor_list:
            image_processor = image_processor_class(**self.image_processor_dict)
            if image_processor_class == EfficientNetImageProcessorFast:
                image = data
                print(f"image: {image.shape} - {image.mean(dtype=torch.float32)}")
                resized_image = image_processor.resize(
                    image,
                    size=SizeDict(height=18, width=18),
                    interpolation=F.InterpolationMode.NEAREST,
                    antialias=False,
                )
                print(f"resized_image: {resized_image.shape} - {resized_image.mean(dtype=torch.float32)}")
            else:
                image = data.squeeze().permute(1, 2, 0).numpy()
                print(f"image: {image.shape} - {image.mean()}")
                resized_image = image_processor.resize(
                    image,
                    size={"height": 18, "width": 18},
                    resample=PILImageResampling.NEAREST
                )
                print(f"resized_image: {resized_image.shape} - {resized_image.mean()}")

is returning

image: (480, 640, 3) - 127.5092306857639
resized_image: (18, 18, 3) - 126.91769547325103
image: torch.Size([1, 3, 480, 640]) - 127.50922393798828
resized_image: torch.Size([1, 3, 18, 18]) - 121.97119140625

You can obtain quite a gap in mean value for the same interpolation mode. antialias does not impact the result

@Yann-CV

Yann-CV commented Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

All other method are outputing the same result (normalize and rescale) between slow and fast

@Yann-CV

Yann-CV commented Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

using bilinear resizing is making the test pass (too much difference between PIL and torchvision with nearest)

@Yann-CV

Yann-CV commented Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

@ydshieh @yonigozlan the test are failing on test non related to this pull request.
In addition of updating the resize interpolation in tests, I took the freedom to fix the seed of numpy in the tests.

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.

1 participant