Repository navigation
Fix center_crop odd-padding placement to match the torchvision backend - #50592
Closed
InesZenkri wants to merge 1 commit into
Closed
InesZenkri wants to merge 1 commit into
InesZenkri wants to merge 1 commit into
Conversation
When a crop pads an undersized image by an odd number of pixels, image_transforms.center_crop placed the extra pixel on the top/left (ceil), while TorchvisionBackend.center_crop places it on the bottom/right. Pad with floor division and compute the crop offsets relative to the padded canvas, so both backends produce identical output. Adds a parity test against torchvision for both channel layouts. Fixes huggingface#50584
Contributor
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it. |
2 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #50584.
Here is my suggested fix: align
image_transforms.center_crop(the numpy/PIL backend) with the torchvision convention when a crop has to pad an undersized image - the extra pixel of an odd difference now goes on the bottom/right instead of the top/left.Two changes in
center_crop:ceil, which placed the extra pixel top/left);ceilcompensated).Note that the direction is my proposal - changing the numpy/PIL backend rather than the torchvision one - since the backend split aims for torchvision parity. Happy to flip it if you'd rather keep the numpy convention. Only inputs smaller than the crop size by an odd number of pixels change; even padding and plain crops are untouched.
Tests
test_center_crop_odd_padding_matches_torchvision: exact array equality with torchvision for an odd padding difference, both channel layouts.pytest tests/test_image_transforms.py: 28 passed locally (Python 3.12, torch 2.14.1+cpu).