見出し画像

StableDiffusion WebUIをGoogleDriveにインストールする。

2023/04/18追記:ControlNetその他の大型アップデートにより本体その他の容量がGoogleDrive無料版の15GBでは厳しくなってきました。
無料版での使用モデルは1つに絞ってください。

前回記事を読んでいただいてありがとうございます。
今回は前回の続きとしてStableDiffusionのWebUIをGoogleDriveにインストールします。

StableDiffusionのWebUIをGoogleDriveに保存するメリットとデメリット

メリット

・WebUIの挙動がおかしくなった時にリロードが出来る。
・前回の設定を保存可能
・GoogleDriveに生成画像を保存可能

デメリット

・GoogleDrive(無料版)の容量をバカ食いする
 (入れれるモデルデータは2つが限界)
・GoogleColabの用量制限が緩和されるわけではない
・GoogleColab版よりそこまで速くない
導入が面倒

キツキツのパンパン

とまあ、あんまりGoogleDriveにWebUIを入れるメリットはないと言うのがDriveに入れる方式が流行らない原因なんだろうなと思います。
しかし、今後ローカル環境でWebUIを使う練習になると考えて、敢えて茨の道を進んでみましょう。

まずGoogleColab版WebUIを開く

GitHub - camenduru/stable-diffusion-webui-colab: stable diffusion webui colab

今回はSD2.1を使います

例のごとくcamenduruさんにお世話になります。

ドライブにコピー

ドライブにコピーまではいつも通りです。

GoogleDriveにデータを保存する前に

そんなもの全部GoogleDriveに「置換」で書き換えればいいじゃんと思ったあなた、私もそう思いましたが普通に失敗しました。
急がば回れ、GoogleColab版のコードを順番に見ていきましょう。

1.フォルダの移動

%cd /content

まずGoogleColabのcontentフォルダに移動しています。

2.エラーログの整理

%env TF_CPP_MIN_LOG_LEVEL=1

ここはログが出過ぎないように制限しています。あまり意味はないです。

3.パッケージのインストール

!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 torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchaudio==2.0.1+cu118 torchtext==0.15.1 torchdata==0.6.0 --extra-index-url https://download.pytorch.org/whl/cu118 -U
!pip install -q xformers==0.0.18 triton==2.0.0 -U

ここでWebUIを動かすために必要なランタイム、ダウンロードに必要なaria2コマンド、その他必要なパッケージのインストールが命令されています。

4.本体のダウンロード

!git clone -b v2.2 https://github.com/camenduru/stable-diffusion-webui
!git clone https://huggingface.co/embed/negative /content/stable-diffusion-webui/embeddings/negative
!git clone https://huggingface.co/embed/lora /content/stable-diffusion-webui/models/Lora/positive
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d /content/stable-diffusion-webui/models/ESRGAN -o 4x-UltraSharp.pth
!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 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/fkunn1326/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
!git clone https://github.com/catppuccin/stable-diffusion-webui /content/stable-diffusion-webui/extensions/stable-diffusion-webui-catppuccin
!git clone https://github.com/KohakuBlueleaf/a1111-sd-webui-locon /content/stable-diffusion-webui/extensions/a1111-sd-webui-locon
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg /content/stable-diffusion-webui/extensions/stable-diffusion-webui-rembg
!git clone https://github.com/ashen-sensored/stable-diffusion-webui-two-shot /content/stable-diffusion-webui/extensions/stable-diffusion-webui-two-shot
!git clone https://github.com/camenduru/sd_webui_stealth_pnginfo /content/stable-diffusion-webui/extensions/sd_webui_stealth_pnginfo
%cd /content/stable-diffusion-webui
!git reset --hard

!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/canny-sd21-safe.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o canny-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/depth-sd21-safe.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o depth-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/hed-sd21-safe.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o hed-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/openpose-sd21-safe.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o openpose-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/scribble-sd21-safe.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o scribble-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o canny-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o depth-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o hed-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o openpose-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o scribble-sd21-safe.yaml

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.ckpt -d /content/stable-diffusion-webui/models/Stable-diffusion -o v2-1_768-ema-pruned.ckpt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/stable-diffusion-2-1/raw/main/v2-inference-v.yaml -d /content/stable-diffusion-webui/models/Stable-diffusion -o v2-1_768-ema-pruned.yaml

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/joachimsallstrom/Double-Exposure-Embedding/resolve/main/dblx.pt -d /content/stable-diffusion-webui/embeddings -o dblx.pt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/Conflictx/AnimeScreencap/resolve/main/AnimeScreenCap.pt -d /content/stable-diffusion-webui/embeddings -o AnimeScreenCap.pt

この辺りは全部WebUIの本体、拡張機能、StableDiffusionのモデルをダウンロードするコードになっています。

5.shared.pyの書き換え

!sed -i -e 's/\"sd_model_checkpoint\"\,/\"sd_model_checkpoint\,sd_vae\,CLIP_stop_at_last_layers\"\,/g' /content/stable-diffusion-webui/modules/shared.py

ここは良く分からないけど、触らない方がよさそうです。

6.実行

!python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --multiple

launch.pyを実行して起動します。
これでこのGoogleColabノートの中身が
 1.フォルダの移動
 2.エラーログの整理
 3.パッケージのインストール
 4.本体のダウンロード
 5.shared.pyの書き換え
 6.実行
の6ステップから出来ていることが分かりました。

GoogleDriveに何をダウンロードするか

4.本体のダウンロードをGoogleDriveにして、実際に毎回実行するのは起動に必要な3.パッケージのインストール6.実行だけにしたいところです。
つまり、

のColabノートを
A.本体ダウンロード
 1.フォルダの移動
 4.本体のダウンロード
 5.shared.pyの書き換え

B.インストールと実行
 1.フォルダの移動
 3.パッケージのインストール
 6.実行
に分けることにします。

A.本体ダウンロード

本体ダウンロード用のコードは以下の通りになります。
左上のファイルから新規作成を行い、下記のコードをコピーしてください。

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/MyDrive

!apt -y install -qq aria2

!git clone -b v2.2 https://github.com/camenduru/stable-diffusion-webui /content/drive/MyDrive/stable-diffusion-webui
!git clone https://huggingface.co/embed/negative /content/drive/MyDrive/stable-diffusion-webui/embeddings/negative
!git clone https://huggingface.co/embed/lora /content/drive/MyDrive/stable-diffusion-webui/models/Lora/positive
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d /content/drive/MyDrive/stable-diffusion-webui/models/ESRGAN -o 4x-UltraSharp.pth
!wget https://raw.githubusercontent.com/camenduru/stable-diffusion-webui-scripts/main/run_n_times.py -O /content/drive/MyDrive/stable-diffusion-webui/scripts/run_n_times.py
!git clone https://github.com/deforum-art/deforum-for-automatic1111-webui /content/drive/MyDrive/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui
!git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser
!git clone https://github.com/camenduru/stable-diffusion-webui-huggingface /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface
!git clone https://github.com/camenduru/sd-civitai-browser /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-civitai-browser
!git clone https://github.com/kohya-ss/sd-webui-additional-networks /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-additional-networks
!git clone https://github.com/Mikubill/sd-webui-controlnet /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet
!git clone https://github.com/fkunn1326/openpose-editor /content/drive/MyDrive/stable-diffusion-webui/extensions/openpose-editor
!git clone https://github.com/jexom/sd-webui-depth-lib /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-depth-lib
!git clone https://github.com/hnmr293/posex /content/drive/MyDrive/stable-diffusion-webui/extensions/posex
!git clone https://github.com/camenduru/sd-webui-tunnels /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-tunnels
!git clone https://github.com/etherealxx/batchlinks-webui /content/drive/MyDrive/stable-diffusion-webui/extensions/batchlinks-webui
# !git clone https://github.com/catppuccin/stable-diffusion-webui /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-catppuccin
!git clone https://github.com/KohakuBlueleaf/a1111-sd-webui-locon /content/drive/MyDrive/stable-diffusion-webui/extensions/a1111-sd-webui-locon
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-rembg
!git clone https://github.com/ashen-sensored/stable-diffusion-webui-two-shot /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-two-shot
!git clone https://github.com/camenduru/sd_webui_stealth_pnginfo /content/drive/MyDrive/stable-diffusion-webui/extensions/sd_webui_stealth_pnginfo
#日本語化パッチの追加ダウンロード
!git clone https://github.com/Katsuyuki-Karasawa/stable-diffusion-webui-localization-ja_JP /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-localization-ja_JP

%cd /content/drive/MyDrive/stable-diffusion-webui
!git reset --hard

!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/canny-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o canny-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/depth-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o depth-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/hed-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o hed-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/openpose-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o openpose-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/scribble-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o scribble-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o canny-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o depth-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o hed-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o openpose-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o scribble-sd21-safe.yaml

#SD2.1
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.ckpt -d /content/drive/MyDrive/stable-diffusion-webui/models/Stable-diffusion -o v2-1_768-ema-pruned.ckpt
#SD2.1 VAE
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/vae/diffusion_pytorch_model.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/models/VAE -o v2-diffusion_pytorch_model.safetensors
#SD2.1 yaml
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/stable-diffusion-2-1/raw/main/v2-inference-v.yaml -d /content/drive/MyDrive/stable-diffusion-webui/models/Stable-diffusion -o v2-1_768-ema-pruned.yaml

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/joachimsallstrom/Double-Exposure-Embedding/resolve/main/dblx.pt -d /content/drive/MyDrive/stable-diffusion-webui/embeddings -o dblx.pt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/Conflictx/AnimeScreencap/resolve/main/AnimeScreenCap.pt -d /content/drive/MyDrive/stable-diffusion-webui/embeddings -o AnimeScreenCap.pt

!sed -i -e 's/\"sd_model_checkpoint\"\,/\"sd_model_checkpoint\,sd_vae\,CLIP_stop_at_last_layers\"\,/g' /content/drive/MyDrive/stable-diffusion-webui/modules/shared.py

 #日本語化 data = {"localization": "ja_JP"} 関西弁化 data = {"localization": "ks_JP"}
%cd /content/drive/MyDrive/stable-diffusion-webui/

import json

data = {"localization": "ja_JP"}

with open("config.json", "w") as f:
    json.dump(data, f)
    

このコードを丸ごとコピペして実行してください。

解説

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/MyDrive

元々のコードではColab内の一時ファイル/contentにファイルをダウンロードしていましたが、今回はGoogleDriveをマウントし、GoogleDrive内に移動します。

!apt -y install -qq aria2

wgetおよびgitコマンドはそのまま使えますが、大容量のファイルをダウンロードするaria2コマンドは先にパッケージのインストールが必要ですのでここだけ残しておきます。

!git clone -b v2.2 https://github.com/camenduru/stable-diffusion-webui /content/drive/MyDrive/stable-diffusion-webui
!git clone https://huggingface.co/embed/negative /content/drive/MyDrive/stable-diffusion-webui/embeddings/negative
!git clone https://huggingface.co/embed/lora /content/drive/MyDrive/stable-diffusion-webui/models/Lora/positive
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/embed/upscale/resolve/main/4x-UltraSharp.pth -d /content/drive/MyDrive/stable-diffusion-webui/models/ESRGAN -o 4x-UltraSharp.pth
!wget https://raw.githubusercontent.com/camenduru/stable-diffusion-webui-scripts/main/run_n_times.py -O /content/drive/MyDrive/stable-diffusion-webui/scripts/run_n_times.py
!git clone https://github.com/deforum-art/deforum-for-automatic1111-webui /content/drive/MyDrive/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui
!git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser
!git clone https://github.com/camenduru/stable-diffusion-webui-huggingface /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface
!git clone https://github.com/camenduru/sd-civitai-browser /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-civitai-browser
!git clone https://github.com/kohya-ss/sd-webui-additional-networks /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-additional-networks
!git clone https://github.com/Mikubill/sd-webui-controlnet /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet
!git clone https://github.com/fkunn1326/openpose-editor /content/drive/MyDrive/stable-diffusion-webui/extensions/openpose-editor
!git clone https://github.com/jexom/sd-webui-depth-lib /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-depth-lib
!git clone https://github.com/hnmr293/posex /content/drive/MyDrive/stable-diffusion-webui/extensions/posex
!git clone https://github.com/camenduru/sd-webui-tunnels /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-tunnels
!git clone https://github.com/etherealxx/batchlinks-webui /content/drive/MyDrive/stable-diffusion-webui/extensions/batchlinks-webui
# !git clone https://github.com/catppuccin/stable-diffusion-webui /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-catppuccin
!git clone https://github.com/KohakuBlueleaf/a1111-sd-webui-locon /content/drive/MyDrive/stable-diffusion-webui/extensions/a1111-sd-webui-locon
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-rembg
!git clone https://github.com/ashen-sensored/stable-diffusion-webui-two-shot /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-two-shot
!git clone https://github.com/camenduru/sd_webui_stealth_pnginfo /content/drive/MyDrive/stable-diffusion-webui/extensions/sd_webui_stealth_pnginfo
#日本語化パッチの追加ダウンロード
!git clone https://github.com/Katsuyuki-Karasawa/stable-diffusion-webui-localization-ja_JP /content/drive/MyDrive/stable-diffusion-webui/extensions/stable-diffusion-webui-localization-ja_JP

%cd /content/drive/MyDrive/stable-diffusion-webui
!git reset --hard

!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/canny-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o canny-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/depth-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o depth-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/hed-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o hed-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/openpose-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o openpose-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/resolve/main/scribble-sd21-safe.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o scribble-sd21-safe.safetensors
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o canny-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o depth-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o hed-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o openpose-sd21-safe.yaml
!aria2c --console-log-level=error -c -x 16 -k 1M -s 16 https://huggingface.co/ckpt/ControlNet/raw/main/cldm_v21.yaml -d /content/drive/MyDrive/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o scribble-sd21-safe.yaml

#SD2.1
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.ckpt -d /content/drive/MyDrive/stable-diffusion-webui/models/Stable-diffusion -o v2-1_768-ema-pruned.ckpt
#SD2.1 VAE
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/vae/diffusion_pytorch_model.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/models/VAE -o v2-diffusion_pytorch_model.safetensors
#SD2.1 yaml
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/stable-diffusion-2-1/raw/main/v2-inference-v.yaml -d /content/drive/MyDrive/stable-diffusion-webui/models/Stable-diffusion -o v2-1_768-ema-pruned.yaml

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/joachimsallstrom/Double-Exposure-Embedding/resolve/main/dblx.pt -d /content/drive/MyDrive/stable-diffusion-webui/embeddings -o dblx.pt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/Conflictx/AnimeScreencap/resolve/main/AnimeScreenCap.pt -d /content/drive/MyDrive/stable-diffusion-webui/embeddings -o AnimeScreenCap.pt

!sed -i -e 's/\"sd_model_checkpoint\"\,/\"sd_model_checkpoint\,sd_vae\,CLIP_stop_at_last_layers\"\,/g' /content/drive/MyDrive/stable-diffusion-webui/modules/shared.py

 #日本語化 data = {"localization": "ja_JP"} 関西弁化 data = {"localization": "ks_JP"}
%cd /content/drive/MyDrive/stable-diffusion-webui/

import json

data = {"localization": "ja_JP"}

with open("config.json", "w") as f:
    json.dump(data, f)

ここで本体と拡張機能、StableDiffusionのモデルをGoogleDriveに保存しています。ついでに日本語化パッチのダウンロードとパッチ有効化の設定ファイルも作成しておきます。
また、GoogleDriveの容量は本当にギリギリになりますが、二次元絵に強いUntitled_Replicantモデルを導入したい場合は、!aria2の並びの最後に

#untitled_replicantモデル
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/alfredplpl/untitled-replicant/resolve/main/untitled_replicant.safetensors -d /content/drive/MyDrive/stable-diffusion-webui/models/Stable-diffusion -o untitled_replicant.safetensors

を追加してください。

B.インストールと実行

Aのコードをコピーし終わったら、ボックスの下にマウスカーソルをもっていきましょう。

+コード
が表示されるのでここを押します。

新しいボックスが生成されるので、ここに以下のコードをコピーします。

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/MyDrive

!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

!pip install -q torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchaudio==2.0.1+cu118 torchtext==0.15.1 torchdata==0.6.0 --extra-index-url https://download.pytorch.org/whl/cu118 -U
!pip install -q xformers==0.0.18 triton==2.0.0 -U

%cd /content/drive/MyDrive/stable-diffusion-webui

!python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --multiple

解説

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/MyDrive

先ほどやりましたね。GoogleDriveに移動します。

!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

!pip install -q torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchaudio==2.0.1+cu118 torchtext==0.15.1 torchdata==0.6.0 --extra-index-url https://download.pytorch.org/whl/cu118 -U
!pip install -q xformers==0.0.18 triton==2.0.0 -U

ここでWebUIの起動に必要な各パッケージのインストールを行います。

%cd /content/drive/MyDrive/stable-diffusion-webui

!python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --multiple

最後にGoogleDrive内に保存してあるlanch.pyを実行します。

実際に動かしてみる

では実際にA.本体のダウンロード、B.インストールと実行の順に起動していきましょう。

GoogleDriveへのアクセス許可を求められるので、許可しておきます。
基本的にAは問題なく実行されるはずですが、Bを実行した際、おそらく下記のエラーが出るはずです。

File "/content/drive/MyDrive/stable-diffusion-webui/launch.py", line 97,
 in run     raise RuntimeError(message) 
RuntimeError: Couldn't checkout commit 5b3af030dd83e0297272d861c19477735d0317ec for K-diffusion.

あるいは

RuntimeError: Couldn't checkout commit c5b4593074ba6214284d6acd5f1719b6c5d739af for CodeFormer

のエラーが出ます。

K-diffusionとCodeFormerのエラー

k-diffusion (5b3af030dd83e0297272d861c19477735d0317ec)のエラーとCodeFormer (c5b4593074ba6214284d6acd5f1719b6c5d739af)のエラーが出るとドキッとしますよね。
これが起きる原因は単純で、2023年4月12日現在、WebUIが使えるバージョンのk-diffusionとCodeFormerが最新版ではないことに由来しています。
(今後、他の拡張機能に関しても同様の問題が生じてくると思います。)
さらに言えばエラーコードの中に

!git -C "/content/drive/MyDrive/stable-diffusion-webui/repositories/k-diffusion" checkout --force 5b3af030dd83e0297272d861c19477735d0317ec

を実行すれば直るよと書いてくれています。
pythonさん、めちゃくちゃ親切ですね。
つまり、A.本体のダウンロードのコードに

!git clone https://github.com/crowsonkb/k-diffusion /content/drive/MyDrive/stable-diffusion-webui/repositories/k-diffusion
!git clone https://github.com/sczhou/CodeFormer /content/drive/MyDrive/stable-diffusion-webui/repositories/CodeFormer

を追加して、k-diffusionとCodeFormerをインストールするようにしても、最新版がダウンロードされてしまうので直りません。
なので、pythonさんの言うことに従ってk-diffusionとCodeFormerの古いバージョンをダウンロードしなおします。

+コードをもう一度押し、追加に作成されたBOXに以下のコードを張り付けて実行してください。

from google.colab import drive
drive.mount('/content/drive')

%cd /content/drive/MyDrive/

#k-difffusionの差し替え
!rm -rf /content/drive/MyDrive/stable-diffusion-webui/repositories/k-diffusion

!git clone https://github.com/crowsonkb/k-diffusion /content/drive/MyDrive/stable-diffusion-webui/repositories/k-diffusion

!git -C "/content/drive/MyDrive/stable-diffusion-webui/repositories/k-diffusion" checkout --force 5b3af030dd83e0297272d861c19477735d0317ec

#CodeFormerの差し替え
!rm -rf /content/drive/MyDrive/stable-diffusion-webui/repositories/CodeFormer

!git clone https://github.com/sczhou/CodeFormer /content/drive/MyDrive/stable-diffusion-webui/repositories/CodeFormer

!git -C "/content/drive/MyDrive/stable-diffusion-webui/repositories/CodeFormer" checkout --force c5b4593074ba6214284d6acd5f1719b6c5d739af

これでk-diffusionとCodeFormerの古いバージョンがダウンロードされました。
もう一度、B.インストールと実行を起動し、以下の画面になれば成功です。

適当なリンクをクリックして画像生成を始めてください。

ここまでくれば後はご自由にと言うところです。お疲れ様でした。
生成された画像は全て、マイドライブ>stable-diffusion-webui>outputs
に保存されます。これで途中でWebUIやGoogleColabが止まってしまっても安心ですね。

実際のところ

モデルを1つだけ入れるならまだ余裕がありますが、SD2.1+もう一つとなると画像を見ていただいたようにカッツカツです。
GoogleDriveで大量に画像を生成したい場合、現実的にはメインで使いたいモデルを1本に絞らないといけないでしょう。
こまめにゴミ箱を空にしないといけませんし、拡張機能に見本で付いてくる.jpgファイルを全て消すなどの工夫が必要です。

650枚ほどjpgファイルがある。間違って余計なファイルを消してしまわないように注意。

しかし、生成した画像が残る、設定が保存できると言ったローカルに近い環境で操作できることも魅力です。
ローカルで画像生成をしたいけど、まだグラフィックボードやゲーミングPCを買う資金がない方は失敗や問題も含めて慣れておくと言う意味では挑戦してみる価値があると思います。

最後に

ほんの半年前なら、自分だけの力でここまで出来なかったと思います。
そこそこGoogleでの検索力とか、プログラミングは素人だけど、全く分からないと言う訳でもないと言うレベルで、今回色々試行錯誤する中で次々に出るエラーに心が折れていたと思います。
ただ、その中でエラーやコードの説明をしてくれたり、必要なコードの生成を補助してくれたchatGPTの存在は非常にありがたかったです。

消していいコードとダメなコードをいちいち教えてくれるchatGPT先生

このノートが何かの参考になればいいね下さい。

この記事が気に入ったらサポートをしてみませんか?