Add EfficientNet Image PreProcessor#37055
Conversation
|
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 |
|
3 tests fail but can't figure out why. ======================================================================= short test summary info =======================================================================
FAILED tests/models/efficientnet/test_image_processing_efficientnet.py::EfficientNetImageProcessorTest::test_rescale - AssertionError: False is not true
FAILED tests/models/efficientnet/test_image_processing_efficientnet.py::EfficientNetImageProcessorTest::test_slow_fast_equivalence - AssertionError: False is not true
FAILED tests/models/efficientnet/test_image_processing_efficientnet.py::EfficientNetImageProcessorTest::test_slow_fast_equivalence_batched - AssertionError: False is not true
==================================================================== 3 failed, 17 passed in 3.93s ===================================================================== |
- reshape test passes when casted to float64 - equivalence test doesn't pass
|
|
- changes order of rescale, normalize acc to slow - rescale_offset defaults to False acc to slow - resample was causing difference in fast and slow. Changing test to bilinear resolves this difference
|
Thanks to #37094 (comment), changing the test resamping to bilinear passes the equivalence tests |
yonigozlan
left a comment
There was a problem hiding this comment.
Hi @zshn25 ! Thanks a lot for working on this, looks great! Only things to change is using F.InterpolationMode.NEAREST_EXACT and see if the equivalence tests pass this way.
| do_normalize=True, | ||
| image_mean=[0.5, 0.5, 0.5], | ||
| image_std=[0.5, 0.5, 0.5], | ||
| resample=PILImageResampling.BILINEAR, # NEAREST is too different between PIL and torchvision |
There was a problem hiding this comment.
Could you try with F.InterpolationMode.NEAREST_EXACT?
There was a problem hiding this comment.
Tried, both the equivalence tests fail with nearest but not with bilinear.
| BASE_IMAGE_PROCESSOR_FAST_DOCSTRING, | ||
| ) | ||
| class EfficientNetImageProcessorFast(BaseImageProcessorFast): | ||
| resample = PILImageResampling.NEAREST |
There was a problem hiding this comment.
Could you try with F.InterpolationMode.NEAREST_EXACT?
There was a problem hiding this comment.
Hi @yonigozlan, thanks for the review. Using F.InterpolationMode.NEAREST_EXACT, gives the TypeError: Object of type InterpolationMode is not JSON serializable error while dump the json pretrained file.
There was a problem hiding this comment.
I now use the pil_torch_interpolation_mapping to map PILImageResampling.NEAREST to InterpolationMode.NEAREST_EXACT instead of InterpolationMode.NEAREST
|
Hi @chewyuenrachael and @Yann-CV thank you for working on your PRs #37119 #37094 . I reviewed this one as it was the first to be posted, but it looks like your PR also helped here so thanks a lot! |
| device=images.device, | ||
| ) | ||
| # if/elif as we use fused rescale and normalize if both are set to True | ||
| if do_rescale: |
There was a problem hiding this comment.
I can be wrong but I guess here the offset will not be applied if the normalization and rescale are activated together (do_rescale becomes false from _fuse_mean_std_and_rescale_factor)
There was a problem hiding this comment.
Hi @Yann-CV, thank you for the review. Nice catch there. I pushed a fix. Thank you.
There was a problem hiding this comment.
won't there be a problem here if offset is true and do_normalize is True (because of the elif right after)?
There was a problem hiding this comment.
True. nice catch @yonigozlan. Replaced elif by if.
…erpolationMode is not JSON serializable
| rescaled_image = image_processor.rescale(image, scale=1 / 127.5) | ||
| expected_image = (image * (1 / 127.5)).astype(np.float32) - 1 | ||
| self.assertTrue(np.allclose(rescaled_image, expected_image)) | ||
| rescaled_image = image_processor.rescale(image, scale=1 / 127.5, dtype=np.float64) |
There was a problem hiding this comment.
if we compare the resize methods in both classes, the slow one is converting data to float64 inside itself. if the goal is to be fully equivalent, it probably needs to be done as well in the fast version.
Also the fast method is using torch tensors, in my opinion it is a better practice to test it torch.Tensor objects
There was a problem hiding this comment.
added tests with torch.tensor objects for rescale
- added tests for rescale + normalize
yonigozlan
left a comment
There was a problem hiding this comment.
Thanks for iterating and adding tests! some little things left to change/check
| BASE_IMAGE_PROCESSOR_FAST_DOCSTRING, | ||
| ) | ||
| class EfficientNetImageProcessorFast(BaseImageProcessorFast): | ||
| resample = PILImageResampling.NEAREST |
| device=images.device, | ||
| ) | ||
| # if/elif as we use fused rescale and normalize if both are set to True | ||
| if do_rescale: |
There was a problem hiding this comment.
won't there be a problem here if offset is true and do_normalize is True (because of the elif right after)?
|
All tests pass except one, which is unrelated to this PR |
yonigozlan
left a comment
There was a problem hiding this comment.
Thanks for iterating looks great now! Let's wait for @ArthurZucker final approval then LGTM
|
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. |
* added efficientnet image preprocessor but tests fail * ruff checks pass * ruff formatted * properly pass rescale_offset through the functions * - corrected indentation, ordering of methods - reshape test passes when casted to float64 - equivalence test doesn't pass * all tests now pass - changes order of rescale, normalize acc to slow - rescale_offset defaults to False acc to slow - resample was causing difference in fast and slow. Changing test to bilinear resolves this difference * ruff reformat * F.InterpolationMode.NEAREST_EXACT gives TypeError: Object of type InterpolationMode is not JSON serializable * fixes offset not being applied when do_rescale and do_normalization are both true * - using nearest_exact sampling - added tests for rescale + normalize * resolving reviews --------- Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
* added efficientnet image preprocessor but tests fail * ruff checks pass * ruff formatted * properly pass rescale_offset through the functions * - corrected indentation, ordering of methods - reshape test passes when casted to float64 - equivalence test doesn't pass * all tests now pass - changes order of rescale, normalize acc to slow - rescale_offset defaults to False acc to slow - resample was causing difference in fast and slow. Changing test to bilinear resolves this difference * ruff reformat * F.InterpolationMode.NEAREST_EXACT gives TypeError: Object of type InterpolationMode is not JSON serializable * fixes offset not being applied when do_rescale and do_normalization are both true * - using nearest_exact sampling - added tests for rescale + normalize * resolving reviews --------- Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
|
Hello! This change introduces a dependency of when running from transformers import PreTrainedModelThe underlying source only becomes apparent when importing with the full path: from transformers.modeling_utils import PreTrainedModelI'm fine with upgrading my
|
What does this PR do?
Add Fast Image Processor #36978 for EfficientNet
Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@yonigozlan
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.