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: dtype might change during resize #36089

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LarsHaalck
Copy link

What does this PR do?

I noticed a small bug, where an np.ndarray changes dtype when calling resize:

import numpy as np
from transformers.image_transforms import resize

x = np.zeros((3, 100, 100), dtype=np.float32)
print(x.dtype) # prints np.float32
x = resize(x, size=(50, 50))
print(x.dtype) # prints np.uint8`

When using natural numbers in a float array, the image is not rescaled and converted into a PIL.Image as-is but when returning back to a numpy array, the type stays uint8.

This PR, stores the original dtype and casts the array back to this type before returning. I also added tests to check for this behavior in the future.

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?

I think @amyeroberts might be best suited since she wrote lots of surrounding code.

@marthos1
Copy link

marthos1 commented Feb 7, 2025

안녕하세요, 모두 감사합니다.

@marthos1
Copy link

marthos1 commented Feb 7, 2025

4D 환경을 얘기할때 그날 저는 머리에 두통을 너무나 느꼈고, 귀신얼굴이 보였습니다. 너무 무서웠어요.

@Rocketknight1
Copy link
Member

I'm not sure if the changed dtype is the intended behaviour - cc @qubvel @zucchini-nlp

Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @LarsHaalck, thanks for opening a PR. I left a comment re the implementation

@@ -365,6 +365,7 @@ def resize(
# To maintain backwards compatibility with the resizing done in previous image feature extractors, we use
# the pillow library to resize the image and then convert back to numpy
do_rescale = False
original_type = image.dtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going to raise the error in case the input image is Pillow Image

>>> Image.new("RGB", (100, 100)).dtype
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Image' object has no attribute 'dtype'

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.

4 participants