stable-diffusion icon indicating copy to clipboard operation
stable-diffusion copied to clipboard

cannot import name 'CLIPTextModelWithProjection' from 'transformers'

Open LeoStrange26 opened this issue 1 year ago • 14 comments

ImportError: cannot import name 'CLIPTextModelWithProjection' from 'transformers' (/root/miniconda3/envs/ldm/lib/python3.8/site-packages/transformers/init.py) what's the problem?

LeoStrange26 avatar Aug 01 '23 03:08 LeoStrange26

Traceback (most recent call last): File "txt2imghugxl.py", line 1, in from diffusers import StableDiffusionXLPipeline File "/export/home/fa077317/miniconda3/envs/ldm/lib/python3.8/site-packages/diffusers/init.py", line 38, in from .models import ( File "/export/home/fa077317/miniconda3/envs/ldm/lib/python3.8/site-packages/diffusers/models/init.py", line 20, in from .autoencoder_asym_kl import AsymmetricAutoencoderKL File "/export/home/fa077317/miniconda3/envs/ldm/lib/python3.8/site-packages/diffusers/models/autoencoder_asym_kl.py", line 21, in from .autoencoder_kl import AutoencoderKLOutput File "/export/home/fa077317/miniconda3/envs/ldm/lib/python3.8/site-packages/diffusers/models/autoencoder_kl.py", line 21, in from ..loaders import FromOriginalVAEMixin File "/export/home/fa077317/miniconda3/envs/ldm/lib/python3.8/site-packages/diffusers/loaders.py", line 46, in from transformers import CLIPTextModel, CLIPTextModelWithProjection, PreTrainedModel, PreTrainedTokenizer ImportError: cannot import name 'CLIPTextModelWithProjection' from 'transformers' (/export/home/fa077317/miniconda3/envs/ldm/lib/python3.8/site-packages/transformers/init.py)

Fazle045 avatar Aug 03 '23 21:08 Fazle045

Traceback (most recent call last): File "C:\Users\XXL\Documents\dev\Playgrounds\SDXL-Playground\SDXL\inpaint.py", line 1, in
from diffusers import StableDiffusionXLInpaintPipeline File "C:\Users\XXL\Documents\dev\Playgrounds\venv\lib\site-packages\diffusers_init_.py", line 38, in from .models import ( File "C:\Users\XXL\Documents\dev\Playgrounds\venv\lib\site-packages\diffusers\models_init_.py", line 20, in from .autoencoder_asym_kl import AsymmetricAutoencoderKL File "C:\Users\XXL\Documents\dev\Playgrounds\venv\lib\site-packages\diffusers\models\autoencoder_asym_kl.py", line 21, in from .autoencoder_kl import AutoencoderKLOutput File "C:\Users\XXL\Documents\dev\Playgrounds\venv\lib\site-packages\diffusers\models\autoencoder_kl.py", line 21, in from ..loaders import FromOriginalVAEMixin File "C:\Users\XXL\Documents\dev\Playgrounds\venv\lib\site-packages\diffusers\loaders.py", line 47, in from transformers import CLIPTextModel, CLIPTextModelWithProjection, PreTrainedModel, PreTrainedTokenizerImportError: cannot import name 'CLIPTextModelWithProjection' from 'transformers' (C:\Users\XXL\Documents\dev\Playgrounds\venv\lib\site-packages\transformers_init_.py)

rogersaloo avatar Aug 04 '23 00:08 rogersaloo

Delete the .cache file and upgrade transformers pip install --upgrade transfomers

rogersaloo avatar Aug 04 '23 16:08 rogersaloo

Updaging transformers from 4.19.0 to 4.31.0 has worked out for me. Thanks to @rogersaloo for the hint :)

nautics889 avatar Aug 05 '23 21:08 nautics889

Delete the .cache file and upgrade transformers pip install --upgrade transfomers

really work,thank u!

Far-sail avatar Aug 15 '23 02:08 Far-sail

What cache file?

Delete the .cache file and upgrade transformers pip install --upgrade transfomers

JohnnyNatrium avatar Aug 15 '23 06:08 JohnnyNatrium

I got the same error

python scripts/txt2img.py \
  --prompt "a red juicy apple floating in outer space, like a planet" \
  --n_samples 1 --n_iter 1 --plms
Traceback (most recent call last):
  File "/Users/user/stable-diffusion/scripts/txt2img.py", line 32, in <module>
    from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
  File "/Users/user/stable-diffusion/venv/lib/python3.10/site-packages/diffusers/__init__.py", line 38, in <module>
    from .models import (
  File "/Users/user/stable-diffusion/venv/lib/python3.10/site-packages/diffusers/models/__init__.py", line 20, in <module>
    from .autoencoder_asym_kl import AsymmetricAutoencoderKL
  File "/Users/user/stable-diffusion/venv/lib/python3.10/site-packages/diffusers/models/autoencoder_asym_kl.py", line 21, in <module>
    from .autoencoder_kl import AutoencoderKLOutput
  File "/Users/user/stable-diffusion/venv/lib/python3.10/site-packages/diffusers/models/autoencoder_kl.py", line 21, in <module>
    from ..loaders import FromOriginalVAEMixin
  File "/Users/user/stable-diffusion/venv/lib/python3.10/site-packages/diffusers/loaders.py", line 45, in <module>
    from transformers import CLIPTextModel, CLIPTextModelWithProjection, PreTrainedModel, PreTrainedTokenizer
ImportError: cannot import name 'CLIPTextModelWithProjection' from 'transformers' (/Users/user/stable-diffusion/venv/lib/python3.10/site-packages/transformers/__init__.py)

running first image creation so tried to upgrade the transformers but then got this error:

pip install --upgrade transfomers
ERROR: Could not find a version that satisfies the requirement transfomers (from versions: none)
ERROR: No matching distribution found for transfomers

I do have Python 3.10:

python -V
Python 3.10.13

P.S. Followed this tutorial https://replicate.com/blog/run-stable-diffusion-on-m1-mac

jasperf avatar Aug 31 '23 02:08 jasperf

I read https://huggingface.co/hakurei/waifu-diffusion/discussions/57 and decided to install the latest transformers package:

pip install git+https://github.com/huggingface/transformers
Collecting git+https://github.com/huggingface/transformers
  Cloning https://github.com/huggingface/transformers to /private/var/folders/l5/nh536b496w949rxkp_k9071m0000gn/T/pip-req-build-_j48m6bq
  Running command git clone --filter=blob:none --quiet https://github.com/huggingface/transformers /private/var/folders/l5/nh536b496w949rxkp_k9071m0000gn/T/pip-req-build-_j48m6bq
  Resolved https://github.com/huggingface/transformers to commit 716bb2e3910fd4872064c55b0d8bc3dad754d129
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: filelock in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (3.12.3)
Requirement already satisfied: huggingface-hub<1.0,>=0.15.1 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (0.16.4)
Requirement already satisfied: numpy>=1.17 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (1.23.1)
Requirement already satisfied: packaging>=20.0 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (23.1)
Requirement already satisfied: pyyaml>=5.1 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (6.0.1)
Requirement already satisfied: regex!=2019.12.17 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (2023.8.8)
Requirement already satisfied: requests in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (2.31.0)
Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (0.12.1)
Requirement already satisfied: safetensors>=0.3.1 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (0.3.3)
Requirement already satisfied: tqdm>=4.27 in ./venv/lib/python3.10/site-packages (from transformers==4.33.0.dev0) (4.66.1)
Requirement already satisfied: fsspec in ./venv/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.15.1->transformers==4.33.0.dev0) (2023.6.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in ./venv/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.15.1->transformers==4.33.0.dev0) (4.7.1)
Requirement already satisfied: charset-normalizer<4,>=2 in ./venv/lib/python3.10/site-packages (from requests->transformers==4.33.0.dev0) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.10/site-packages (from requests->transformers==4.33.0.dev0) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in ./venv/lib/python3.10/site-packages (from requests->transformers==4.33.0.dev0) (1.26.16)
Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.10/site-packages (from requests->transformers==4.33.0.dev0) (2023.7.22)
Building wheels for collected packages: transformers
  Building wheel for transformers (pyproject.toml) ... done
  Created wheel for transformers: filename=transformers-4.33.0.dev0-py3-none-any.whl size=7604306 sha256=c25078f256acf7993261a79f296d78b46e30675f651ac98b6156d0693190e6b8
  Stored in directory: /private/var/folders/l5/nh536b496w949rxkp_k9071m0000gn/T/pip-ephem-wheel-cache-eg4_aaho/wheels/c0/14/d6/6c9a5582d2ac191ec0a483be151a4495fe1eb2a6706ca49f1b
Successfully built transformers
Installing collected packages: transformers
  Attempting uninstall: transformers
    Found existing installation: transformers 4.19.2
    Uninstalling transformers-4.19.2:
      Successfully uninstalled transformers-4.19.2
Successfully installed transformers-4.33.0.dev0

and then it started working and even took care of cache

python3 scripts/txt2img.py \
  --prompt "a red juicy apple floating in outer space, like a planet" \
  --n_samples 1 --n_iter 1 --plms
The cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only operation. You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.
0it [00:00, ?it/s]
Downloading (…)rocessor_config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 342/342 [00:00<00:00, 728kB/s]
/Users/jasperfrumau/stable-diffusion/venv/lib/python3.10/site-packages/transformers/models/clip/feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.
  warnings.warn(
Downloading (…)lve/main/config.json: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.55k/4.55k [00:00<00:00, 7.70MB/s]
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["id2label"]` will be overriden.
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["bos_token_id"]` will be overriden.
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["eos_token_id"]` will be overriden.
Downloading pytorch_model.bin:  51%|████████████████████████████████████████████████████████
....

but there was one more error

FileNotFoundError: [Errno 2] No such file or directory: 'models/ldm/stable-diffusion-v1/model.ckpt'

So I copied sd-v1-4.ckpt and renamed it to model.ckpt and tried again:

python3 scripts/txt2img.py \
  --prompt "a red juicy apple floating in outer space, like a planet" \
  --n_samples 1 --n_iter 1 --plms
/Users/jasperfrumau/stable-diffusion/venv/lib/python3.10/site-packages/transformers/models/clip/feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.
  warnings.warn(
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["id2label"]` will be overriden.
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["bos_token_id"]` will be overriden.
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["eos_token_id"]` will be overriden.
Global seed set to 42
Loading model from models/ldm/stable-diffusion-v1/model.ckpt
Global Step: 470000
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
making attention of type 'vanilla' with 512 in_channels
Working with z of shape (1, 4, 32, 32) = 4096 dimensions.
making attention of type 'vanilla' with 512 in_channels
Downloading: "https://github.com/DagnyT/hardnet/raw/master/pretrained/train_liberty_with_aug/checkpoint_liberty_with_aug.pth" to /Users/jasperfrumau/.cache/torch/hub/checkpoints/checkpoint_liberty_with_aug.pth
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5.10M/5.10M [00:00<00:00, 9.50MB/s]
Downloading (…)olve/main/vocab.json: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 961k/961k [00:01<00:00, 883kB/s]
Downloading (…)olve/main/merges.txt: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 525k/525k [00:00<00:00, 669kB/s]
Downloading (…)cial_tokens_map.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 389/389 [00:00<00:00, 842kB/s]
Downloading (…)okenizer_config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 905/905 [00:00<00:00, 117kB/s]
Downloading (…)lve/main/config.json: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.52k/4.52k [00:00<00:00, 5.36MB/s]
Downloading pytorch_model.bin:  23%|██████████████████████████ 
...

jasperf avatar Aug 31 '23 03:08 jasperf

jasperf, followed what you did and that worked for me! cheers heaps, thank you! (on version 4.34.0 now).

MalcGee avatar Sep 08 '23 08:09 MalcGee

pip install -U transformers solved my problem.

ShoufaChen avatar Sep 26 '23 11:09 ShoufaChen

@ShoufaChen Thanks, pip install -U transformers also works for me.

Clear-3d avatar Nov 03 '23 11:11 Clear-3d

After upgrading the transformer, restart the kernel. That's what worked for me.

nishantguvvada avatar Nov 17 '23 14:11 nishantguvvada

Delete the .cache file and upgrade transformers pip install --upgrade transfomers

Where is the .cache file?

Torcelllo avatar Dec 20 '23 08:12 Torcelllo

pip install -U transformers

Thank you a lot! It works for me as well!

Maystern avatar Apr 29 '24 16:04 Maystern