Stable Diffusion WebUI 从安装到实战:原理、部署与问题全解

Stable Diffusion 原理、介绍及 WebUI 安装指南

一、Stable Diffusion 原理及介绍

Stable Diffusion 是一种基于潜在扩散模型(Latent Diffusion Model)的文本到图像生成人工智能模型,由 Stability AI 主导开发,于 2022 年发布。其核心原理是通过"扩散过程"的逆过程实现图像生成:

  1. 扩散过程:从一张清晰图像开始,逐步添加高斯噪声,最终将图像转化为完全随机的噪声
  2. 逆扩散过程:模型学习从纯噪声中逐步去除噪声,结合文本提示(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'

模型可通过以下方式获取:

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. 模型下载与安装

推荐从以下平台下载高质量模型:

下载后将模型文件(通常为 .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"

六、汉化配置

  1. 进入 WebUI 的 "Extensions" 选项卡
  2. 点击 "Install from URL",输入仓库地址:https://github.com/VinsonLaro/stable-diffusion-webui-chinese
  3. 点击 "Install",安装完成后重启界面
  4. 进入 "Settings" → "User interface",在 "Localization" 中选择 "Chinese-All" 或 "Chinese-English"
  5. 点击 "Apply settings" 并 "Reload UI" 完成汉化
相关推荐
Yeliang Wu3 小时前
ComfyUI 全流程指南:安装、配置、插件与模型选型
stable diffusion·文生图·图生图·comfyui
LCG米18 小时前
[OpenVINO实战] 在边缘设备上运行Stable Diffusion,实现离线文生图
人工智能·stable diffusion·openvino
水上冰石1 天前
rtx5060部署stable-diffusion1.10.1版本注意事项
stable diffusion
水上冰石1 天前
stable-diffusion-webui的v1.10.1版本汉化
stable diffusion
梯度下降不了班2 天前
【mmodel/xDit】Cross-Attention 深度解析:文生图/文生视频的核心桥梁
人工智能·深度学习·ai作画·stable diffusion·音视频·transformer
余蓝2 天前
快速部署 stable-diffusion-xl-base-1.0(SDXL)
ai作画·stable diffusion·dall·e 2
梯度下降不了班2 天前
【mmodel/xDiT】多模态^_^从入门到放弃的学习路径
人工智能·学习·stable diffusion
love530love3 天前
【ComfyUI/SD环境管理指南(二)】:如何避免插件安装导致的环境崩溃与“外科手术式”修复
人工智能·windows·python·stable diffusion·github·aigc·comfyui
小毅&Nora3 天前
【人工智能】【深度学习】④ Stable Diffusion核心算法解析:从DDPM到文本生成图像的飞跃
人工智能·深度学习·stable diffusion