在Colab上部署Stable Diffusion WebUI完整指南
2025-07-06 01:08:53作者:尤峻淳Whitney
前言
Stable Diffusion作为当前最流行的AI图像生成模型之一,其WebUI版本为用户提供了友好的图形界面。本文将详细介绍如何在Colab环境中完整部署Stable Diffusion WebUI,包括环境配置、扩展安装和模型加载等关键步骤。
环境准备
基础环境配置
首先需要设置Colab的工作目录并配置TensorFlow日志级别,避免不必要的输出干扰:
%cd /content
%env TF_CPP_MIN_LOG_LEVEL=1
性能优化组件
安装高性能内存分配库libtcmalloc
可以显著提升运行效率:
!apt -y update -qq
!wget https://github.com/camenduru/gperftools/releases/download/v1.0/libtcmalloc_minimal.so.4 -O /content/libtcmalloc_minimal.so.4
%env LD_PRELOAD=/content/libtcmalloc_minimal.so.4
依赖安装
安装必要的系统依赖和Python包:
!apt -y install -qq aria2 libcairo2-dev pkg-config python3-dev
!pip install -q torch==2.0.1+cu118 torchvision==0.15.2+cu118 torchaudio==2.0.2+cu118 torchtext==0.15.2 torchdata==0.6.1 --extra-index-url https://download.pytorch.org/whl/cu118 -U
!pip install -q xformers==0.0.20 triton==2.0.0 gradio_client==0.2.7 -U
核心组件安装
Stable Diffusion WebUI
克隆特定版本的WebUI仓库:
!git clone -b v2.4 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
扩展功能安装
常用扩展
安装一系列实用的扩展功能:
!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/camenduru/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
ControlNet扩展
ControlNet是控制图像生成的重要扩展,安装其模型和配置文件:
!git clone https://github.com/Mikubill/sd-webui-controlnet /content/stable-diffusion-webui/extensions/sd-webui-controlnet
下载ControlNet的各种预训练模型(部分示例):
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_v11e_sd15_ip2p_fp16.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle_fp16.safetensors -d /content/stable-diffusion-webui/extensions/sd-webui-controlnet/models -o control_v11e_sd15_shuffle_fp16.safetensors
其他实用扩展
安装更多增强功能的扩展:
!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
模型加载与配置
加载特定模型
下载并加载ACertainModel模型:
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/ACertainModel/resolve/main/ACertainModel.ckpt -d /content/stable-diffusion-webui/models/Stable-diffusion -o ACertainModel.ckpt
环境配置调整
修改启动脚本以优化CUDA兼容性:
!sed -i -e '''/from modules import launch_utils/a\\import os''' /content/stable-diffusion-webui/launch.py
!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
!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
启动WebUI
使用以下命令启动WebUI服务:
!python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --gradio-queue --multiple
启动参数说明:
--listen
: 允许外部访问--xformers
: 启用xformers优化--enable-insecure-extension-access
: 允许扩展安装--theme dark
: 使用暗色主题--gradio-queue
: 启用队列系统--multiple
: 允许多用户同时访问
结语
通过上述步骤,我们成功在Colab环境中部署了功能完整的Stable Diffusion WebUI,包含了ControlNet等众多实用扩展。这种部署方式特别适合需要临时使用高性能GPU进行AI图像生成的用户,无需本地配置复杂环境即可体验Stable Diffusion的强大功能。