Skip to content

When Pillow is not installed, importing from transformers.image_transforms raises an unclear NameError #20627

@convoliution

Description

@convoliution

System Info

  • transformers version: 4.25.1
  • Platform: macOS-11.6.8-x86_64-i386-64bit
  • Python version: 3.10.8
  • Huggingface_hub version: 0.11.1
  • PyTorch version (GPU?): not installed (NA)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: no
  • Using distributed or parallel set-up in script?: no

Who can help?

@amyeroberts @NielsRogge

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Steps to reproduce the behavior:

  1. Create and activate clean Python environment
    python3 -m venv venv
    source venv/bin/activate
  2. Install transformers and its direct dependencies
    pip install transformers
  3. Attempt to import transformers.image_transforms or one of its publicly-documented members
    python -c 'from transformers.image_transforms import center_crop'
  4. Encounter a NameError
    None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/miliu/venv/lib/python3.10/site-packages/transformers/image_transforms.py", line 51, in <module>
        def to_channel_dimension_format(image: np.ndarray, channel_dim: Union[ChannelDimension, str]) -> np.ndarray:
    NameError: name 'ChannelDimension' is not defined
    

Expected behavior

Rather than a NameError on import (caused by to_channel_dimension_format()'s signature type annotation containing ChannelDimension, which is conditionally imported only when PIL is available), I would've expected something like transformers.rescale's user experience, where it helpfully recommends installing Pillow when one attempts to use it:

>>> from transformers import rescale
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
>>> rescale()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/miliu/venv/lib/python3.10/site-packages/transformers/utils/dummy_vision_objects.py", line 14, in rescale
    requires_backends(rescale, ["vision"])
  File "/Users/miliu/venv/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 997, in requires_backends
    raise ImportError("".join(failed))
ImportError: 
rescale requires the PIL library but it was not found in your environment. You can install it with pip:
`pip install pillow`. Please note that you may need to restart your runtime after installation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions