Stable Diffusion 原理、介绍及 WebUI 安装指南
一、Stable Diffusion 原理及介绍
Stable Diffusion 是一种基于潜在扩散模型(Latent Diffusion Model)的文本到图像生成人工智能模型,由 Stability AI 主导开发,于 2022 年发布。其核心原理是通过"扩散过程"的逆过程实现图像生成:
- 扩散过程:从一张清晰图像开始,逐步添加高斯噪声,最终将图像转化为完全随机的噪声
- 逆扩散过程:模型学习从纯噪声中逐步去除噪声,结合文本提示(Prompt)的语义信息,最终生成符合描述的清晰图像
该模型的优势在于:
- 开源可访问性:允许研究者和开发者自由使用和修改
- 高质量生成:能生成具有细节和艺术感的图像
- 灵活性:支持文本生成图像、图像修复、风格迁移等多种任务
- 资源友好性:相比早期扩散模型,通过潜在空间(Latent Space)计算大幅降低了硬件需求
Stable Diffusion WebUI 是基于 Stable Diffusion 模型的可视化操作界面(以 AUTOMATIC1111 版本最为流行),提供了直观的参数调整、模型管理和图像生成功能,让非专业用户也能便捷使用该模型。
具体可以参见这篇文章,原理介绍的比较清晰:https://zhuanlan.zhihu.com/p/628714183
二、Stable Diffusion WebUI 安装步骤
1. Conda 设置清华源
bash
conda config --show channels
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
2. 创建并激活 conda 环境
bash
conda create -n sdw python=3.10
conda activate sdw
3. pip 设置清华源
bash
pip config list
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
4. 下载源码并切换分支
bash
# 国内源克隆(推荐)
git clone https://mirror.ghproxy.com/https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
# 进入目录并切换版本
cd stable-diffusion-webui/
git checkout v1.10.1
5. 配置模型下载路径
修改 ./modules/sd_models.py 文件,指定模型下载地址(解决网络限制问题):
python
161 else:
162 model_url = f"http://192.168.1.10/data/model/SD/v1-5-pruned-emaonly.safetensors"
163 expected_sha256 = '6ce0161689b3853acaa03779ec93eafe75a02f4ced659bee03f50797806fa2fa'
模型可通过以下方式获取:
- 官方地址:https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors
- 局域网地址:http://192.168.1.10/data/model/SD/v1-5-pruned-emaonly.safetensors
6. 安装 CLIP 模型
bash
# 创建目录
mkdir -p ./openai/
cd ./openai/
# 下载 CLIP 模型(局域网方式)
wget http://192.168.1.10/data/model/SD/clip-vit-large-patch14.tar.gz
# 解压
tar -zxvf clip-vit-large-patch14.tar.gz
# 返回主目录
cd ..
7. 安装 Python 依赖
bash
pip install -r requirements_versions.txt
8. 允许 root 用户执行
修改 webui.sh 第 74 行:
bash
73 # this script cannot be run as root by default
74 can_run_as_root=1
9. 解决 CLIP 安装超时问题
由于网络限制,直接安装 CLIP 可能失败,可通过以下方式解决:
bash
# 方法1:手动下载并安装
wget http://192.168.1.10/data/soft/CLIP-d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip
pip install CLIP-d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip
# 方法2:修改源码中的下载地址
vi ./modules/launch_utils.py
# 将第345-346行修改为:
# clip_package = os.environ.get('CLIP_PACKAGE', "http://192.168.1.10/data/soft/CLIP-d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip")
10. 解决 TCMalloc 依赖问题
bash
# 安装依赖
apt-get update
apt-get install google-perftools -y
# 配置环境变量
echo "export LD_PRELOAD=\"/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4.5.3\"" >> /etc/profile
source /etc/profile
11. 执行安装
bash
bash webui.sh
成功安装后会显示类似以下信息:
Loading weights [6ce0161689] from /root/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors
Running on local URL: http://127.0.0.1:7860
Creating model from config: /root/stable-diffusion-webui/configs/v1-inference.yaml
Applying attention optimization: Doggettx... done.
Model loaded in 3.5s (load weights from disk: 0.8s, create model: 0.8s, apply weights to model: 1.5s, calculate empty prompt: 0.2s).
三、服务配置与访问
1. 设置开机启动
创建服务文件 /etc/systemd/system/sdw.service:
ini
[Unit]
Description=Stable Diffusion WebUI Service
After=network.target
[Service]
Type=simple
User=root
StandardOutput=file:/var/log/sdw.log
StandardError=file:/var/log/sdw.log
WorkingDirectory=/root/stable-diffusion-webui/
ExecStart=/bin/bash -c 'source /root/miniconda3/bin/activate && conda activate sdw && bash /root/stable-diffusion-webui/webui.sh'
Restart=on-failure
[Install]
WantedBy=multi-user.target
启用服务:
bash
systemctl daemon-reload
systemctl restart sdw
systemctl enable sdw
2. 端口转发配置
方法1:SSH 端口转发
bash
ssh -L 192.168.1.63:7860:127.0.0.1:7860 root@192.168.1.63
方法2:HAProxy 转发
bash
# 安装 HAProxy
apt install haproxy -y
修改配置文件 /etc/haproxy/haproxy.cfg,添加以下内容:
listen haihe_proxy_gateway_https
mode tcp
bind 192.168.1.64:80 # 替换为实际IP
balance roundrobin
option tcpka
option tcplog
server hostname 127.0.0.1:7860 check inter 2000 rise 2 fall 5
启动服务:
bash
systemctl enable haproxy
systemctl start haproxy
通过 http://192.168.1.64/ 访问(替换为实际IP)
四、模型管理与使用
1. 模型下载与安装
推荐从以下平台下载高质量模型:
- Hugging Face:https://huggingface.co/models
- Civitai:https://civitai.com/models
下载后将模型文件(通常为 .safetensors 或 .ckpt 格式)放入目录:
/root/stable-diffusion-webui/models/Stable-diffusion/
在 WebUI 界面中刷新模型列表即可选择使用。
2. 基础使用示例
- 正向提示词(Prompts) :
1girl, face, white background - 反向提示词(Negative Prompts) :
(worst quality:2), (low quality:2), lowres, watermark
反向提示词用于排除不想要的特征(如低质量、水印等)。
五、常见问题解决
1. 缺少 xdg-open 错误
bash
apt install xdg-utils desktop-file-utils -y
2. 本地访问代理问题
bash
export no_proxy="localhost, 127.0.0.1, ::1"
六、汉化配置
- 进入 WebUI 的 "Extensions" 选项卡
- 点击 "Install from URL",输入仓库地址:
https://github.com/VinsonLaro/stable-diffusion-webui-chinese - 点击 "Install",安装完成后重启界面
- 进入 "Settings" → "User interface",在 "Localization" 中选择 "Chinese-All" 或 "Chinese-English"
- 点击 "Apply settings" 并 "Reload UI" 完成汉化