👏

StableDiffusion3 - transformersエラー

2024/06/14に公開

昨日、StableDiffusion3 Mediumがリリースされ、さっそく使用している方も多くいらっしゃると思います。

私もさっそく使用してみようと、huggingfaceで公開されているコードを実行しようと試みましたが、何やらエラーが出て実行できませんでした。

C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\diffusers\models\transformers\transformer_2d.py:34:
FutureWarning: Transformer2DModelOutput is deprecated and will be removed in version 1.0.0. Importing Transformer2DModelOutput from diffusers.models.transformer_2d is deprecated and this will be removed in a future version. Please use from diffusers.models.modeling_outputs import Transformer2DModelOutput

何やらdiffusersのtransformersで推奨されていないクラスが利用されているようです。

ググってみるとtransformersをアップデートするといいらしいですが

pip install -U transformers

私はこれで解決しませんでした。

そこで、試行錯誤した結果以下の方法で解決しました。

解決方法

まずtransformer_2d.pyを開きます。

venvを構築している場合は

.venv\Lib\site-packages\diffusers\models\transformers\transformer_2d.py

にあるはずです。

次に24行目のインポート分を

from ..modeling_outputs import Transformer2DModelOutput

以下に置き換えます

from diffusers.models.modeling_outputs import Transformer2DModelOutput

次に32行目から34行目の以下のクラスをまるごと消します

class Transformer2DModelOutput(Transformer2DModelOutput):
    deprecation_message = "Importing `Transformer2DModelOutput` from `diffusers.models.transformer_2d` is deprecated and this will be removed in a future version. Please use `from diffusers.models.modeling_outputs import Transformer2DModelOutput`, instead."
    deprecate("Transformer2DModelOutput", "1.0.0", deprecation_message)

401 Client Errorが発生した場合

Couldn't connect to the Hub: 401 Client Error. (Request ID: Root=1-666c41a6-16b04d6407b26bd65ab9b4c8;bbe79591-84ea-428a-9d4c-deff8aace19d)

Cannot access gated repo for url https://huggingface.co/api/models/stabilityai/stable-diffusion-3-medium-diffusers.
Access to model stabilityai/stable-diffusion-3-medium-diffusers is restricted. You must be authenticated to access it..
Will try to load from local cache.
(略)
OSError: Cannot load model stabilityai/stable-diffusion-3-medium-diffusers: model is not cached locally and an error occurred while trying to fetch metadata from the Hub. Please check out the root cause in the stacktrace above.

現在(2024/06/14時点)、Stable Diffusion 3は悪用されないよう、HuggingFaceでアカウント登録をしなければいけません。

まずココでトークンを作成します。
Nameは適当に"StableDiffusion"など、TypeはReadに設定します

次にコンソールで

huggingface-cli login

を実行

Enter your token (input will not be visible):

と出てきたら右クリック(ペースト)をします

Add token as git credential? (Y/n)

はどっちでもいいです。私はnを選択しました。

これで

python main.py

をすれば実行できるはずです。

GoodLuck!

Discussion