はじめに

Stable Diffusionとは?

Wikipediaによると、Stable Diffusion拡散モデル(DM)の一形態です。2015年に導入された拡散モデルは、トレーニング画像にガウスノイズを連続的に適用することを除去する目的で訓練され、一連のデノイジングオートエンコーダーとして考えることができます。Stable Diffusionは「潜在拡散モデル」(LDM)として知られる変種を使用しています。

Stable Diffusionのインストール方法は?

Stable Diffusionを使用する方法は2つあります。

  • ローカルコンピュータにStable Diffusionをインストール
  • Colabやその他のオンラインサービスでStable Diffusionを使用

この記事では、ローカルコンピュータへのStable Diffusionのインストールに焦点を当てます


Apple Silicon Macユーザー向け

Stable Diffusionを使用するための主な方法が2つあります。

方法1:ターミナルでの通常の方法

必要条件:

  • M1M1 ProM1 MaxM1 ULTRAM2チップ搭載のMac
  • 16GB RAM以上
  • macOS 12.3以上

ステップ1

MacにPythonをインストールする必要があります

python3 -Vでバージョンを確認

1
2
python3 -V  
Python 3.10.0
1
2
which python3
/usr/bin/python3
1
2
pip3 --version
pip 22.X.X from XXX

Pythonをインストールする必要がある場合、最も簡単な方法はHomebrewを使用することです。詳細はhttps://brew.shをご覧ください

Homebrewを使用して最新バージョンのPythonをインストール:

1
2
brew update
brew install python3
ステップ2

リポジトリをクローンし、依存関係をインストールします。

gitをインストールしていない場合は、次のようにインストールできます:

1
2
brew update
brew install git
1
2
3
git clone -b apple-silicon-mps-support https://github.com/bfirsh/stable-diffusion.git
cd stable-diffusion
mkdir -p models/ldm/stable-diffusion-v1/
ステップ3

依存関係をインストールするための仮想環境をインストール:

1
2
python3 -m pip install virtualenv
python3 -m virtualenv venv

仮想環境を有効化:

1
source venv/bin/activate

仮想環境を無効化:

1
deactive

注意:すべてのステップはstable-diffusion-v1のパス内で実行する必要があります

ステップ4

必要なパッケージを再度インストール

1
brew install Cmake rust protobuf
ステップ5

リストの依存関係をインストール:

1
pip install -r requirements.txt
ステップ6

重みをダウンロード

Hugging Faceリポジトリにアクセスし、ライセンスを読んで理解した後、「Access repository」をクリックします。

そのページでsd-v1-4.ckpt(約4GB)をダウンロードし、上記で作成したディレクトリのmodels/ldm/stable-diffusion-v1/model.ckptとして保存します。

ステップ7

開始

1
2
3
python scripts/txt2img.py \
--prompt "a red juicy apple floating in outer space, like a planet" \
--n_samples 1 --n_iter 1 --plms

注意:ステップ6とステップ7はReplicateからの引用です


方法2:GUI

  • DiffusionbeeというGUIソフトウェア<—ここをクリックして開始

    必要条件:

    • M1M1 ProM1 MaxM1 ULTRAM2チップ搭載のMac
    • 16GB RAM以上
    • macOS 12.3以上

Linuxユーザー向け(DebianまたはUbuntu)

方法

ステップ1

Python、git、virtualenvをインストール

1
sudo apt install python3 git virtualenv
ステップ2

リポジトリをクローンし、依存関係をインストール。

1
2
git clone https://github.com/CompVis/stable-diffusion.git
cd stable-diffusion/

ライセンスを読んで同意する

ステップ3

仮想環境に移動

仮想環境を有効化

1
source venv/bin/activate

仮想環境を無効化

1
deactive
ステップ4

重みをダウンロード

Hugging Faceリポジトリにアクセスし、ライセンスを読んで理解した後、「Access repository」をクリックします。

1
curl https://www.googleapis.com/storage/v1/b/aai-blog-files/o/sd-v1-4.ckpt?alt=media > sd-v1-4.ckpt
ステップ5

実行

1
python scripts/txt2img.py --prompt "YOUR-PROMPT-HERE" --plms --ckpt sd-v1-4.ckpt --skip_grid --n_samples 1

参考文献

Run. Replicate. (n.d.). Retrieved October 11, 2022, from https://replicate.com/blog/run-stable-diffusion-on-m1-mac

Divamgupta. (n.d.). Divamgupta/diffusionbee-stable-diffusion-ui: Diffusion Bee is the easiest way to run stable diffusion locally on your M1 mac. comes with a one-click installer. no dependencies or technical knowledge needed. GitHub. Retrieved October 11, 2022, from https://github.com/divamgupta/diffusionbee-stable-diffusion-ui