Skip to content

Commit

Permalink
[QoL fix] [Image processing] Add warning on assumption of channel dim…
Browse files Browse the repository at this point in the history
… and avoid infering when inputs are PIL.Image (#31364)

* Add warning on assumption of channel dim
Use PIL info whenever possible to decide channel axis

* Fix ruff format

* Remove type checking
Improve warning message

* Update src/transformers/models/siglip/image_processing_siglip.py

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

* Update src/transformers/image_utils.py

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

* Update src/transformers/image_utils.py

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

---------

Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
  • Loading branch information
2 people authored and itazap committed Jun 18, 2024
1 parent 58b7e4c commit 4c3d517
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/transformers/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ def infer_channel_dimension_format(
else:
raise ValueError(f"Unsupported number of image dimensions: {image.ndim}")

if image.shape[first_dim] in num_channels:
if image.shape[first_dim] in num_channels and image.shape[last_dim] in num_channels:
logger.warning(
f"The channel dimension is ambiguous. Got image shape {image.shape}. Assuming channels are the first dimension."
)
return ChannelDimension.FIRST
elif image.shape[first_dim] in num_channels:
return ChannelDimension.FIRST
elif image.shape[last_dim] in num_channels:
return ChannelDimension.LAST
Expand Down

0 comments on commit 4c3d517

Please sign in to comment.