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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/transformers/image_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,12 @@ 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 = None
if not isinstance(image, PIL.Image.Image):
original_type = image.dtype
do_rescale = _rescale_for_pil_conversion(image)
image = to_pil_image(image, do_rescale=do_rescale, input_data_format=input_data_format)

height, width = size
# PIL images are in the format (width, height)
resized_image = image.resize((width, height), resample=resample, reducing_gap=reducing_gap)
Expand All @@ -385,6 +388,9 @@ def resize(
# If an image was rescaled to be in the range [0, 255] before converting to a PIL image, then we need to
# rescale it back to the original range.
resized_image = rescale(resized_image, 1 / 255) if do_rescale else resized_image
# convert back to original type if original image was np.ndarray
if original_type is not None:
resized_image = resized_image.astype(original_type)
return resized_image


Expand Down
5 changes: 5 additions & 0 deletions tests/test_image_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ def test_resize(self):
self.assertIsInstance(resized_image, np.ndarray)
self.assertEqual(resized_image.shape, (4, 30, 40))

# check that resize keeps dtype
image = np.zeros((1, 128, 128), dtype=np.float32)
resized_image = resize(image, size=(64, 64))
self.assertEqual(image.dtype, resized_image.dtype)

def test_normalize(self):
image = np.random.randint(0, 256, (224, 224, 3)) / 255

Expand Down