5
6

More than 1 year has passed since last update.

お題は不問!Qiita Engineer Festa 2023で記事投稿!

Stable Diffusion web UIをMacBook(M2)にインストールして画像の生成をしてみた記録

Posted at

Supershipの名畑です。話題のウルトラマンブレーザー、キャラ設定や台詞回しやSEに至るまでかなり練られていて話題になって当然という感じですが、個人的には馴染みのある池袋サンシャイン周辺推しな第1話にめちゃくちゃ強く心奪われ「叙庵だー」とテンション上がりました。

はじめに

これまで「Stable Diffusionでの画像生成をPythonとWeb APIで実装してみた記録」などのいくつかの記事でStable DiffusionのWeb API経由での呼び出しを取り上げてきました。

今回はローカルでのStable Diffusion web UIの環境構築について記録に残します。

流れとしては、公式情報であるInstallation on Apple Silicon通りに構築を進め、発生したエラーコードに対処した上でstability.aiによる公式モデルを用いて画像生成まで行いました。
その上でstability.aiによる配布ではないモデルの適用と画像生成もしてみました。

すでに似たような記事は世の中に大量にございますが、GPUの関係でどうしてもMac以外の情報が多いかと思うので、同一環境の方の指針になれば幸いです。

環境

MacBook Pro(Apple M2 Proチップ)です。OSはmacOS 13 Venturaです。

stable-diffusion-webuiの公式に

Currently most functionality in the web UI works correctly on macOS, with the most notable exceptions being CLIP interrogator and training. Although training does seem to work, it is incredibly slow and consumes an excessive amount of memory. CLIP interrogator can be used but it doesn't work correctly with the GPU acceleration macOS uses so the default configuration will run it entirely via CPU (which is slow).

とありますので、AppleシリコンのMacでもCLIP interrogatorトレーニング以外の基本機能は無事に動くと期待します。
ちなみにCLIP interrogatorは画像からプロンプトを生成するというものです。

Homebrewのインストール

パッケージ管理ソフトであるHomebrewが入っていない方はインストールします。

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

パスも張っておきます。
パスの張り方はインストール完了時に表示されます。「Run these two commands in your terminal to add Homebrew to your PATH:」以下の2行をコピペしてコマンド実行します。ユーザー名のところは環境に合わせて変わります。

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/ユーザー名/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"

その他ソフトウェアのインストール

マニュアル通りですと下記です。未インストールのものだけで大丈夫です。

$ brew install cmake protobuf rust python@3.10 git wget

Stable Diffusion web UIのダウンロード

Githubのstable-diffusion-webuiから必要なコード諸々を持ってきます。ダウンロード先は自環境のお好きなところにどうぞ。

$ git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui

この記事執筆時点でのバージョンはv1.4.0でした。

モデルのダウンロード

使用するモデルはHugging Faceというサイトからダウンロード可能です。もう一つ有名なサイトがあるのですが、それは後述するとし、ここでは公式通りの手順で進めます。

Stable Diffusion 2.1のモデルであるv2-1_768-ema-pruned.ckptをダウンロードします。

ダウンロードしたモデルは、先ほどダウンロード済みのstable-diffusion-webui配下のmodels/Stable-diffusionに配置します。

Stable Diffusion web UIの起動

stable-diffusion-webuiに移動します。/path/to/のところは配置場所に合わせて変更してください。

$ cd /path/to/stable-diffusion-webui

webui.shを叩きます。初回は必要な諸々の設定等で少し時間がかかります。

$ ./webui.sh

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################

略

pruned.ckpt: preload_extensions_git_metadata for 7 extensions took 0.00s
Running on local URL:  http://127.0.0.1:7860

略

Model loaded in 16.5s (calculate hash: 2.8s, load weights from disk: 2.0s, find config: 3.3s, create model: 0.1s, apply weights to model: 2.9s, apply half(): 2.2s, move model to device: 0.6s, load textual inversion embeddings: 0.1s, calculate empty prompt: 2.3s).

ブラウザで http://127.0.0.1:7860 を開きます。

image01.png

無事に起動ができました。

エラー対応

試しとしてPrompt

28-year-old Japanese pretty woman with black bobbed hair

Negative prompt

text, letter, belongings

を入れてGenerateを押してみました。
するとターミナルにエラーが表示されました。

*** Error completing request

略

    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.

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.

という指示通りに、ブラウザ上でSettingsStable Diffusionの最下部にあるUpcast cross attention layer to float32のチェックを入れてみます。

image02.png

その上で再度Generateを押すと、今度は無事、画像が生成されました。
生成先はstable-diffusion-webui/outputs配下です。

image03.png

28-year-old Japanese pretty woman with black bobbed hair
Negative prompt: text, letter, belongings
Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 458200453, Size: 512x512, Model hash: ad2a33c361, Model: v2-1_768-ema-pruned, Version: v1.4.0

Time taken: 11.29s

ということでデフォルトの設定で11秒程度でした。
(個人的には)ストレスになる待ち時間ではないです。素晴らしい。

Civitaiから別モデルのダウンロード

せっかくなので他のモデルも使用してみます。

先述したHugging Faceと並ぶ有名なサイトとしてCivitaiがございます。
Stable Diffusion特化のサイトで、サンプル画像も一覧で見られて非常に使い勝手が良いです。

HIGHEST RATEDの先頭にあったDreamShaperをダウンロードしてみました。

ダウンロードしたファイルdreamshaper_7.safetensorsをまたmodels/Stable-diffusionに配置します。

リロードするとStable Diffusion checkpointの一覧にdreamshaper_7.safetensorsが追加されていることがわかります。

image04.png

dreamshaper_7.safetensorsを選んでGenerateをします。

image05.png

28-year-old Japanese pretty woman with black bobbed hair
Negative prompt: text, letter, belongings
Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1766013823, Size: 512x512, Model hash: ed989d673d, Model: dreamshaper_7, Version: v1.4.0

Time taken: 14.87s

15秒弱で無事に生成されました。

終了

ブラウザを閉じてもプロセスは生き続けますので、Ctrl + Cなどで停止してください。

アップデート

stable-diffusion-webui配下でgit pullしてください。

$ git pull
remote: Enumerating objects: 511, done.
remote: Counting objects: 100% (460/460), done.

略

Updating 394ffa7b..f865d3e1
Fast-forward
 CHANGELOG.md       | 5 +++++
 modules/api/api.py | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)

stable-diffusion-webui配下のCHANGELOG.mdを見れば更新履歴がわかります。

## 1.4.1

### Bug Fixes:
 * add queue lock for refresh-checkpoints

以下略

最後に

CLIP interrogatorトレーニング以外にもMacだと厳しいこともあるのかもしれませんが、今回取り上げた範疇においては想像よりスムーズに快適に動きました。

次回はインストールしたStable Diffusionブラウザ経由ではなくPythonからAPI経由で呼び出してみようと思います。

宣伝

SupershipのQiita Organizationを合わせてご覧いただけますと嬉しいです。他のメンバーの記事も多数あります。

Supershipではプロダクト開発やサービス開発に関わる方を絶賛募集しております。
興味がある方はSupership株式会社 採用サイトよりご確認ください。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6