太長不讀#
點擊下面這個鏈接 馬上 進入 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 應用,你可以使用它來操作 Stable Diffusion 生成圖片。一般用它 GitHub 帳號中的 A1111 代稱。
要運行 Stable Diffusion,你的電腦需要有一塊強大的 GPU 和較大的運行內存。但如果你沒有這樣的硬件,你仍然可以充點小錢上 Google Colab 運行 Stable Diffusion Web UI Colab。
Stable Diffusion Web UI Colab 是一個可以在 Google Colab 上部署 Stable Diffusion Web UI 的 fork 項目。在它的 GitHub 倉庫的 README 中已經準備好了不同模型的.ipynb 文件,你只需要點擊其中一個 Open in Colab 按鈕,就可以在 Google Colab 上一鍵部署 Stable Diffusion Web UI Colab。
3. 什麼是 Google Colab#
Colaboratory 簡稱 Google Colab,是 Google Research 團隊開發的一款產品。在 Colab 中,任何人都可以通過瀏覽器編寫和執行任意 Python 代碼。它尤其適合機器學習、數據分析和教育目的。從技術上來說,Colab 是一種托管式 Jupyter 筆記本服務。它的出現讓用戶不需要在本地安裝任何環境,只需要打開瀏覽器就可以直接開始編寫代碼,非常方便。
Colab 提供免費版、按量付費版和專業版,免費版提供的資源(compute units)是有限的,但對於大多數人來說已經足夠了。如果你需要更多的資源,可以考慮訂閱專業版,快樂地使用 A100 GPU 來進行 AIGC。
根據 Colab 團隊主管最新的推文,由於使用 Colab 生圖的人實在是太多了,現在只允許付費用戶在 Colab 中使用 Stable Diffusion。
4. 什麼是 .ipynb#
Google Colab 上執行的文件類型是 .ipynb。 .ipynb 是一種可以進行計算的的 Jupyter Notebook 筆記文件格式。你可以在 Colab 中編寫代碼,也可以在 Colab 中打開 GitHub 倉庫中的.ipynb 文件,直接在 Colab 上運行。
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 上一鍵部署 Stable Diffusion Web UI 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 只能同時運行一個代碼塊,如果你需要通過執行筆記來安裝額外的模型或者插件,你需要先停止正在運行的代碼塊,才能執行新的代碼塊。所以我將代碼塊分為 安裝、下載模型、啟動 三個部分,另外還增加了一個 保存產物 的部分,以在完成操作以後保存成果。
如果你在運行自己創建的新筆記時遇到了無法找到 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 命令在 notebook 中下載其他來源的模型。
這裡我為各位中文用戶準備了亞洲美女模型合集,供你們一鍵打包使用。
(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 時,可以運行下面的代碼來啟動。
# Launch
%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 時會彈出授權窗口,然後允許授權帳號即可。
# Mount Google Drive, authorization modal will show up
from google.colab import drive
drive.mount('/content/drive')
# Zip images and save to Google Drive
%cd /content/stable-diffusion-webui/outputs
!zip -r /content/drive/MyDrive/images.zip txt2img-images
祝你在生成圖片時玩得開心!