長すぎて読めない#
下のリンクをクリックして、すぐに Google Colab で Stable Diffusion Web UI Colab をデプロイします。
``https://colab.research.google.com/github/camenduru/stable-diffusion-webui-colab/blob/main/stable/chillout_mix_webui_colab.ipynb
1. Stable Diffusion Web UI Colab とは#
Stable Diffusion は Stability AI が提供する、深層学習技術に基づいたテキストから画像を生成する AI モデルです。
2. Stable Diffusion Web UI#
は、Stable Diffusion を操作して画像を生成するための強力で使いやすい Web アプリケーションです。一般的には GitHub アカウント内の A1111 という名前で呼ばれています。
Stable Diffusion を実行するには、コンピュータに強力な GPU と大きな実行メモリが必要です。しかし、そのようなハードウェアがない場合でも、少しお金を払って Google Colab で Stable Diffusion Web UI Colab を実行することができます。
Stable Diffusion Web UI Colab は、Google Colab 上で Stable Diffusion Web UI をデプロイするためのフォークプロジェクトです。GitHub リポジトリの README には、さまざまなモデルの .ipynb ファイルが用意されており、その中のいずれかの「Open in Colab」ボタンをクリックするだけで、Google Colab 上で一括デプロイできます。
3. Google Colab とは#
Colaboratory の略称である Google Colab は、Google Research チームが開発した製品です。Colab では、誰でもブラウザを通じて任意の Python コードを記述し、実行することができます。特に機械学習、データ分析、教育目的に適しています。技術的には、Colab はホスティングされた Jupyter ノートブックサービスです。その登場により、ユーザーはローカルに環境をインストールする必要がなく、ブラウザを開くだけで直接コードを記述できるようになり、非常に便利です。
Colab には無料版、従量課金版、プロ版があり、無料版で提供されるリソース(計算ユニット)は限られていますが、ほとんどの人には十分です。より多くのリソースが必要な場合は、プロ版の購読を検討し、A100 GPU を使って AIGC を楽しむことができます。
Colab チームの責任者の最新のツイートによると、Colab での画像生成を行うユーザーが非常に多いため、現在は有料ユーザーのみが Colab で Stable Diffusion を使用できるようになっています。
4. .ipynb とは#
Google Colab で実行されるファイルタイプは .ipynb です。.ipynb は計算が可能な Jupyter Notebook のノートファイル形式です。Colab でコードを記述したり、Colab で GitHub リポジトリ内の .ipynb ファイルを開いて直接実行したりすることができます。
5. 基本的な使い方#
Stable Diffusion Web UI Colab V2.0 は現在使用可能で、主分岐のバージョンと比べて ControlNet や LoRA などのプラグインをより良く使用できます。私は直接 2.0 から使用することをお勧めします。
Stable Diffusion Web UI Colab の GitHub 主リポジトリは、Google Colab で実行できる一連の .ipynb を提供しており、それぞれの .ipynb ファイルは一つのモデルに対応しています。README の中のいずれかの「Open in Colab」ボタンをクリックすると、Google Colab 上で一括デプロイできます。
GitHub 上の .ipynb ファイルを通じて Google Colab に入ったら、デプロイ完了まであと一歩です。コードブロック内の ▶️ 実行ボタンをクリックするだけで、Stable Diffusion Web UI Colab のデプロイを開始できます。
デプロイには数分かかる場合があり、主に選択したモデルのサイズによって異なります。
デプロイが完了すると、出力ログの最後に Gradio のリンク(例:https://xxxxxxxxxx.gradio.live)が表示され、クリックすると Stable Diffusion Web UI Colab のインターフェースが開きます。
私のカスタムノート
Stable Diffusion Web UI Colab が提供する .ipynb ノートはすでに非常に完成された一括スクリプトですが、私は自分の使用ニーズに応じて複数のモデルを読み込むために、いくつかの調整を行いました。
Google Colab では同時に一つのコードブロックしか実行できないため、追加のモデルやプラグインをインストールするためにノートを実行する必要がある場合、まず実行中のコードブロックを停止してから新しいコードブロックを実行する必要があります。そこで、私はコードブロックを「インストール」、「モデルのダウンロード」、「起動」の 3 つの部分に分け、さらに「成果物の保存」という部分を追加して、操作が完了した後に成果を保存できるようにしました。
自分で作成した新しいノートを実行する際に GPU が見つからない問題が発生した場合は、Runtime 設定で GPU タイプの実行環境に切り替えてください。
インストール
Stable Diffusion Web UI Colab は一度インストールすればよく、何度も停止や再起動が必要になるかもしれませんが、再起動時には再インストールやモデルのダウンロードは必要ありません。そこで、公式のスクリプトを参考にして、インストールとモデルのダウンロードのコードブロックをコメントアウトし、別のコードブロックにコピーしました。
インストールのコードブロックは一般的に一度実行すればよく、ノートブックの最下部に配置することをお勧めします。
import os
from google.colab.output import eval_js
os.environ['colab_url'] = eval_js("google.colab.kernel.proxyPort(7860, {'cache': false})")
!apt -y update -qq
!wget http://launchpadlibrarian.net/367274644/libgoogle-perftools-dev_2.5-2.2ubuntu3_amd64.deb
!wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/google-perftools_2.5-2.2ubuntu3_all.deb
!wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libtcmalloc-minimal4_2.5-2.2ubuntu3_amd64.deb
!wget https://launchpad.net/ubuntu/+source/google-perftools/2.5-2.2ubuntu3/+build/14795286/+files/libgoogle-perftools4_2.5-2.2ubuntu3_amd64.deb
!apt install -qq libunwind8-dev
!dpkg -i *.deb
%env LD_PRELOAD=libtcmalloc.so
!rm *.deb
!apt -y install -qq aria2
!pip install -q --pre xformers
!pip install -q --pre triton
!git clone -b v2.0 https://github.com/camenduru/stable-diffusion-webui
!wget https://raw.githubusercontent.com/camenduru/stable-diffusion-webui-scripts/main/run_n_times.py -O /content/stable-diffusion-webui/scripts/run_n_times.py
!git clone https://github.com/deforum-art/deforum-for-automatic1111-webui /content/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui
!git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser /content/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser
!git clone https://github.com/camenduru/stable-diffusion-webui-huggingface /content/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface
!git clone -b v2.0 https://github.com/camenduru/sd-civitai-browser /content/stable-diffusion-webui/extensions/sd-civitai-browser
!git clone https://github.com/kohya-ss/sd-webui-additional-networks /content/stable-diffusion-webui/extensions/sd-webui-additional-networks
!git clone https://github.com/Mikubill/sd-webui-controlnet /content/stable-diffusion-webui/extensions/sd-webui-controlnet
!git clone https://github.com/camenduru/openpose-editor /content/stable-diffusion-webui/extensions/openpose-editor
!git clone https://github.com/jexom/sd-webui-depth-lib /content/stable-diffusion-webui/extensions/sd-webui-depth-lib
!git clone https://github.com/hnmr293/posex /content/stable-diffusion-webui/extensions/posex
!git clone https://github.com/camenduru/sd-webui-tunnels /content/stable-diffusion-webui/extensions/sd-webui-tunnels
!git clone https://github.com/etherealxx/batchlinks-webui /content/stable-diffusion-webui/extensions/batchlinks-webui
%cd /content/stable-diffusion-webui
!git reset --hard
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_canny-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_canny-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_depth-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_depth-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_hed-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_hed-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_mlsd-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_mlsd-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_normal-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_normal-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_openpose-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_openpose-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_scribble-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_scribble-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/control_seg-fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_seg-fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/hand_pose_model.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/openpose -o hand_pose_model.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/body_pose_model.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/openpose -o body_pose_model.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/dpt_hybrid-midas-501f0c75.pt -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/midas -o dpt_hybrid-midas-501f0c75.pt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/mlsd_large_512_fp32.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/mlsd -o mlsd_large_512_fp32.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/mlsd_tiny_512_fp32.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/mlsd -o mlsd_tiny_512_fp32.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/network-bsds500.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/hed -o network-bsds500.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/upernet_global_small.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/annotator/uniformer -o upernet_global_small.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_style_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_style_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_sketch_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_sketch_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_seg_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_seg_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_openpose_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_openpose_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_keypose_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_keypose_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_depth_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_depth_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_color_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_color_sd14v1.pth
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet/resolve/main/t2iadapter_canny_sd14v1.pth -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o t2iadapter_canny_sd14v1.pth
# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/sd14/resolve/main/sd-v1-4.ckpt -d /content/stable-diffusion-webui/models/Stable-diffusion -o sd-v1-4.ckpt
!sed -i -e '''/ prepare_environment()/a\ os.system\(f\"""sed -i -e ''\"s/dict()))/dict())).cuda()/g\"'' /content/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/util.py""")''' /content/stable-diffusion-webui/launch.py
!mkdir /content/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/models
# !python launch.py --share --xformers --enable-insecure-extension-access --theme dark --gradio-queue --cloudflared
モデルのダウンロード
Stable Diffusion Web UI には Civitai 上のモデルをダウンロードするための組み込み拡張機能がありますが、wget や aria2 コマンドを使用してノートブック内で他のソースからモデルをダウンロードすることもできます。
ここでは、皆さんのためにアジア美女モデルコレクションを用意しましたので、一括で使用できます。
(1)主モデルのダウンロード
# 主モデルをダウンロードします。必要に応じてダウンロードしてください。
!wget https://civitai.com/api/download/models/11745 -O /content/stable-diffusion-webui/models/Stable-diffusion/Chilloutmix-Ni-pruned-fp32-fix.safetensors
# !wget https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned.safetensors -O /content/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned.safetensors
# !wget https://huggingface.co/DucHaiten/DucHaitenAIart/resolve/main/DucHaitenAIart_v2.0.safetensors -O /content/stable-diffusion-webui/models/DucHaitenAIart_v2.0.safetensors
(2)Lora モデルのダウンロード
# Lora モデルをダウンロードします。
!mkdir -p /content/stable-diffusion-webui/models/Lora
!wget https://civitai.com/api/download/models/12050 -O /content/stable-diffusion-webui/models/Lora/japaneseDollLikeness_v10.safetensors
!wget https://civitai.com/api/download/models/8750 -O /content/stable-diffusion-webui/models/Lora/koreanDollLikeness_v10.safetensors
(3)SD Web UI の起動
Web UI を起動または再起動する必要がある場合は、以下のコードを実行して起動できます。
# 起動
%cd /content/stable-diffusion-webui
!python launch.py --share --xformers --enable-insecure-extension-access --gradio-queue --theme dark --gradio-queue --cloudflared
# !python launch.py --share --xformers --enable-insecure-extension-access --theme dark --gradio-queue --cloudflared --vae-path "/content/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors"
6. 成果物の保存#
Colab にはファイルブラウザが付属していますが、そこからファイルを一括ダウンロードすることはできません。
そのため、私は zip を使用して成果物をパッケージ化し、Google Drive に保存することにしました。
Google Drive をマウントすると、認証ウィンドウが表示され、アカウントの認証を許可する必要があります。
# Google Drive をマウントします。認証モーダルが表示されます。
from google.colab import drive
drive.mount('/content/drive')
# 画像を zip して Google Drive に保存します。
%cd /content/stable-diffusion-webui/outputs
!zip -r /content/drive/MyDrive/images.zip txt2img-images
Stable Diffusion で画像生成を楽しんでください!