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

ImageProcessor - check if input pixel values between 0-255 #25688

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/transformers/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ def is_batched(img):
return False


def _is_scaled_image(image: np.ndarray) -> bool:
rafaelpadilla marked this conversation as resolved.
Show resolved Hide resolved
"""
Checks to see whether the pixel values have already been rescaled to [0, 1].
"""
if image.dtype == np.uint8:
return False

# It's possible the image has pixel values in [0, 255] but is of floating type
return np.min(image) >= 0 and np.max(image) <= 1


def make_list_of_images(images, expected_ndims: int = 3) -> List[ImageInput]:
"""
Ensure that the input is a list of images. If the input is a single image, it is converted to a list of length 1.
Expand Down
9 changes: 8 additions & 1 deletion src/transformers/models/beit/image_processing_beit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -236,6 +237,11 @@ def _preprocess_image(
"""Preprocesses a single image."""
# All transformations expect numpy arrays.
image = to_numpy_array(image)
if _is_scaled_image(image) and do_rescale:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think it makes sense to also set do_rescale = False ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't want to do anything "magic" and surprise the user.

Checking whether the pixels are correct isn't a perfect test, and it'd be modifying the processor behaviour under an assumption.

logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)
if input_data_format is None:
input_data_format = infer_channel_dimension_format(image)
image = self._preprocess(
Expand Down Expand Up @@ -328,7 +334,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/models/bit/image_processing_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -184,7 +185,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -270,6 +272,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/models/blip/image_processing_blip.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -176,7 +177,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -253,6 +255,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
get_image_size,
infer_channel_dimension_format,
is_batched,
Expand Down Expand Up @@ -387,7 +388,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -469,6 +471,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if do_resize:
images = [
self.resize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -182,7 +183,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -268,6 +270,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/models/clip/image_processing_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -183,7 +184,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -269,6 +271,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doc of images could be updated for consistency with other changes !

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
get_image_size,
infer_channel_dimension_format,
make_list_of_images,
Expand Down Expand Up @@ -1259,6 +1260,12 @@ def preprocess(
# All transformations expect numpy arrays
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -203,7 +204,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -280,6 +282,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we can also update the doc here

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
get_image_size,
infer_channel_dimension_format,
make_list_of_images,
Expand Down Expand Up @@ -1257,6 +1258,12 @@ def preprocess(
# All transformations expect numpy arrays
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/models/deit/image_processing_deit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
infer_channel_dimension_format,
make_list_of_images,
to_numpy_array,
Expand Down Expand Up @@ -180,7 +181,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -258,6 +260,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/models/deta/image_processing_deta.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here I guess

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
get_image_size,
infer_channel_dimension_format,
is_batched,
Expand Down Expand Up @@ -921,6 +922,12 @@ def preprocess(
# All transformations expect numpy arrays
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/models/detr/image_processing_detr.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

here too!

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
get_image_size,
infer_channel_dimension_format,
make_list_of_images,
Expand Down Expand Up @@ -1229,6 +1230,12 @@ def preprocess(
# All transformations expect numpy arrays
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/models/donut/image_processing_donut.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ChannelDimension,
ImageInput,
PILImageResampling,
_is_scaled_image,
get_image_size,
infer_channel_dimension_format,
make_list_of_images,
Expand Down Expand Up @@ -319,7 +320,8 @@ def preprocess(

Args:
images (`ImageInput`):
Image to preprocess.
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
do_resize (`bool`, *optional*, defaults to `self.do_resize`):
Whether to resize the image.
size (`Dict[str, int]`, *optional*, defaults to `self.size`):
Expand Down Expand Up @@ -407,6 +409,12 @@ def preprocess(
# All transformations expect numpy arrays.
images = [to_numpy_array(image) for image in images]

if _is_scaled_image(images[0]) and do_rescale:
logger.warning_once(
"It looks like you are trying to rescale already rescaled images. If the input"
" images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
)

if input_data_format is None:
# We assume that all images have the same channel dimension format.
input_data_format = infer_channel_dimension_format(images[0])
Expand Down
Loading