Skip to content
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

fix the get_size_with_aspect_ratio in max_size situation #30902

Merged
merged 20 commits into from
Jun 3, 2024

Conversation

SangbumChoi
Copy link
Contributor

@SangbumChoi SangbumChoi commented May 20, 2024

What does this PR do?

This fix the issue as the following example. AKA round issue.
This is important when the size should be divisible by specific value such as 32, 16.
@amyeroberts

from transformers import DetrImageProcessor
from PIL import Image
import requests
import torch
import io
import numpy as np


def rgb_to_id(color):
    if isinstance(color, np.ndarray) and len(color.shape) == 3:
        if color.dtype == np.uint8:
            color = color.astype(np.int32)
        return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2]
    return int(color[0] + 256 * color[1] + 256 * 256 * color[2])
    
height, width = 653, 958
image = Image.new('RGB', (width, height), 'white')
image_processor = DetrImageProcessor.from_pretrained('facebook/detr-resnet-50')
image_processor.size = \
{
    "longest_edge": 640,
    "shortest_edge": 640
}

Before commit

# prepare inputs for the model
inputs = image_processor(images=image, return_tensors="pt")
print(inputs['pixel_values'].shape)
torch.Size([1, 3, 436, 639])

After commit

# prepare inputs for the model
inputs = image_processor(images=image, return_tensors="pt")
print(inputs['pixel_values'].shape)
torch.Size([1, 3, 436, 640])

Fixes # (issue)

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.

@SangbumChoi
Copy link
Contributor Author

SangbumChoi commented May 20, 2024

Additional script

height, width = 653, 958

for i in range(640,660):
    image = Image.new('RGB', (width, i), 'white')
    
    print(image.size)
    
    image_processor = DetrImageProcessor.from_pretrained('facebook/detr-resnet-50')
    image_processor.size = \
    {
        "longest_edge": 640,
        "shortest_edge": 640
    }
    
    # prepare inputs for the model
    inputs = image_processor(images=image, return_tensors="pt")
    print(inputs['pixel_values'].shape)
(958, 640)
torch.Size([1, 3, 428, 640])
...
(958, 659)
torch.Size([1, 3, 440, 640])

@SangbumChoi
Copy link
Contributor Author

def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]:

We can consider merging both all-in-one

Copy link
Collaborator

@amyeroberts amyeroberts 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 fixing!

Could you add a test, checking both the upper and lower bounds, which fail on main and pass here?

cc @qubvel

@qubvel
Copy link
Member

qubvel commented May 20, 2024

@SangbumChoi Thanks for fixing, a great catch!

We can consider merging both all-in-one

will be great to have them consistent, but take into consideration that yolos function "round" height and width to the lowest divisible value

@SangbumChoi
Copy link
Contributor Author

I will update the yolos and the test function and ping again.

@SangbumChoi
Copy link
Contributor Author

@qubvel @amyeroberts I added the screenshot which fails at the main branch and success in here.

For the record, I tried to make yolos consistent to all the other function. However, it turns out it needs to remove #Copied from line which I would like to maintain. As a result I just changed it with appropriate format.

I just added test on the detr but if it seems good I could add it to all the related models.

Screenshot 2024-05-23 at 10 18 59 PM

Copy link
Collaborator

@amyeroberts amyeroberts 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 working on this! Overall looks good to me. It would be good to add some tests to make sure the behaviour is as expected for specific cases eg. when max_size is set or not; height < width; width < height; height == width; height == size etc.

src/transformers/models/yolos/image_processing_yolos.py Outdated Show resolved Hide resolved
SangbumChoi and others added 2 commits May 24, 2024 09:18
Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
@SangbumChoi
Copy link
Contributor Author

I have added the 3 more cases which is total 5 cases. Note that max_size should be always set in the situation of get_size_with_aspect_ratio! Again I just added test on the detr but if it seems good I could add it to all the related models :)

@SangbumChoi
Copy link
Contributor Author

@amyeroberts Hi amy, since recent merge has deprecated DETA. I also removed in here.

@SangbumChoi
Copy link
Contributor Author

@amyeroberts Could you rerun the CI?

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

Copy link
Collaborator

@amyeroberts amyeroberts 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 fixing!

@amyeroberts amyeroberts merged commit 874ac12 into huggingface:main Jun 3, 2024
21 checks passed
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request Jun 11, 2024
…#30902)

* fix the get_size_with_aspect_ratio in max_size situation

* make fix-up

* add more general solution

* consider when max_size is not defined

* fix typo

* fix typo

* simple fix

* fix error

* fix if else error

* fix error of size overwrite

* fix yolos image processing

* fix detr image processing

* make

* add longest related test script

* Update src/transformers/models/yolos/image_processing_yolos.py

Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>

* add more test

* add test script about longest size

* remove deprecated

---------

Co-authored-by: amyeroberts <22614925+amyeroberts@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.

None yet

4 participants