WSL2でStable Diffusion 2.0を使用する

公開日:2022年11月26日
最終更新日:2022年12月3日

はじめに

別記事にWSL2を使わない方法も書いていますので良かったらそちらも読んで下さい。
touch-sp.hatenablog.com

PC環境

Ubuntu 20.04 on WSL2
RTX 3080(VRAM 12GB)
CUDA 11.6.2



使用したPythonはUbuntu 20.04に最初からインストールされている3.8.10をそのまま使いました。

Python 3.8.10

環境構築

CUDA 11.6.2のインストールはNVIDA公式通りにやると問題なくできると思います。

cuDNNのインストールは不要です。

簡単にするために一部「requirements4wsl2.txt」にまとめました。自分のGitHubに公開しているのでだれでも利用可能です。

それを使うと以下の4行で環境構築が終了しました。

pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
pip install -U setuptools
pip install ninja
pip install -r https://raw.githubusercontent.com/dai-ichiro/env4stable-diffusion/main/v2/requirements4wsl2.txt

実行方法

リポジトリのクローン

git clone https://github.com/Stability-AI/stablediffusion.git
cd stablediffusion

学習済みパラメーター「768-v-ema.ckpt」のダウンロード

こちらのサイトから「768-v-ema.ckpt」をダウンロードします。
保存先は「stablediffusion」フォルダ直下としました。

いよいよ実行

python scripts/txt2img.py --prompt "a professional photograph of an astronaut riding a horse" --ckpt 768-v-ema.ckpt --config configs/stable-diffusion/v2-inference-v.yaml --H 768 --W 768 --n_samples 2