Waifu Diffusion導入とFloat32のエラー対処メモ

Stable Diffusion web UIの学習モデル「Waifu Diffusion 1.5 Beta2」を導入した時、Float32のエラーが発生しましたので対処メモを残しておきます。

目次

Waifu Diffusion 1.5 Beta2インストール

公式URL huggingface.co

学習モデル

「Files and version」の「checkpoints」からモデルファイルとyamlファイルをダウンロード(ファイルサイズが表示されている部分をクリック)。

モデルには「aesthetic有無」、「fp16/fp32」の4種類あります。

通常は aesthetic有バージョン を使用し、追学習する場合はaesthetic無バージョンを使用するとのことです。

fp16(Float16) / fp32(Float32)は、GPUメモリの使用量の違い(と思います)なので、通常は fp32(Float32) を使用するらしいです。

ダウンロードした2つのファイルを下記フォルダに移動。

stable-diffusion-webui\models\Stable-diffusion


VAE (Variational Auto-Encoder)

公式URLの説明に従って「kl-f8-anime2.ckpt」をダウンロード。

VAE

WD 1.5 uses the same VAE as WD 1.4, which can be found here https://huggingface.co/hakurei/waifu-diffusion-v1-4/blob/main/vae/kl-f8-anime2.ckpt


ダウンロードしたファイル名をモデル名に合わせて変更(赤字部分)、拡張子を「.vae.pt」に変更すると「Stable Diffusion Checkpoint」でモデルを変更した時、自動的にVAEを変更してくれます。

kl-f8-anime2.ckpt

↓↓↓

wd-1-5-beta2-aesthetic-fp32.vae.pt


VAEファイルを下記フォルダに移動。

stable-diffusion-webui\models\VAE


Stable Diffusion web UIを起動している場合、一旦終了(Ctrl+c)し再起動することで Waifu Diffusionが使用できるようになります。


Float32のエラー対処

「Generate」をクリックしても画像が生成されず、webui-user.batを実行しているコマンドプロンプトを見ると下記エラーが出力されていました。

modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.

「Try setting the "Upcast cross attention layer to float32"」と書かれてますので、「Settings」タブの「Stable Diffusion」項目の「Upcast cross attention layer to float32」のチェックを入れて、 「Apply settings」をクリック して対処完了です。

「Apply settings」をクリックしないと設定が保存されませんのでお忘れなく。。

画像生成

Example Images内のプロンプトを参考に画像生成してみました。

Example Images

https://cafeai.notion.site/WD-1-5-Beta-2-Aesthetic-Ver-c44a410fec06478fbf1a08a9890310ff

人物以外も作成。


あとがき

「VAEのファイル名をモデル名に合わせることでVAEが自動変更される」とありますが、私の環境では自動的に変更されず、手動で行わないとダメな時がありました。ですので、「なんか思ってたのと違う」画像が生成された時は設定を変更してみるといいかもしれませんね。