部署环境
- 操作系统:Ubuntu Server 22.04(无图形界面)
- GPU:A100 显卡(40GB 显存)
准备部署完成后通过其他电脑的浏览器访问服务器的 webui 服务。
一、安装Anaconda3
a、linux下安装启动
版本:anaconda2023.03(2023.3)
debian系和rhat系通用
A100环境下的显卡环境已存在就省略了A100的显卡驱动安装
1.下载
进入目录
cd /usr/local
如果下载过慢,建议用下载软件下载后上传
wget https://repo.anaconda.com/archive/Anaconda3-2023.03-Linux-x86_64.sh
或者从镜像下载
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.03-Linux-x86_64.sh
2.安装
授予权限
chmod +x Anaconda3-2023.03-Linux-x86_64.sh
安装
sh ./Anaconda3-2023.03-Linux-x86_64.sh
会提示一堆信息,大意就是用户须知,操作如下:
(见到Enter就按Enter,见到yes/no就是yes)
Enter,下拉,yes,
Enter,yes
3.配置
刷新配置
cd /home/test
source ~/.bashrc
验证
conda -V # 安装python环境 3.10以上 conda create --name sd_env_310 python=3.10
# 查看有多少个环境 conda info --envs # 激活环境 conda activate sd_env_310
设置不自动激活conda base 环境
conda config --set auto_activate_base false
全自动安装
官方给出的 Linux 安装指导非常简单:下载 webui.sh,运行它,然后一切都会被全自动地下载、安装完成。
但是我是不情愿让别人对我的电脑做手脚而自己却不知情的,所以决定研究一下 webui.sh. 在查看其内容之后,我发现它无非是干这么几件事:
- 下载 webui 仓库;
- 在 ./venv 下创建并激活 python 虚拟环境;
- 调用 launch.py 安装需要的包及下载需要的仓库;
- 启动 webui.
虽然 webui.sh 帮我们全自动地完成了这些工作,但这里面有几个问题:
- 它使用 venv 管理环境,而我更倾向于使用 conda 管理环境;
- 由于网络原因,希望把 pip install 换到国内镜像源;
- 由于网络原因,git clone 有时无法访问导致失败。
对于第二个问题,可以全局设置镜像,或者在运行 webui.sh 时设置环境变量 INDEX_URL;对于第三个问题,可以直接把代码里出现的 http://github.com 改成镜像站。但第一个问题无法简单地解决,因此我还是决定手动分解步骤安装,见下一节。
分解步骤安装
- 下载 stable-diffusion-webui 仓库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
- 下载其他仓库
webui 仅仅是一个外层界面包装,核心功能是通过其他仓库(或以仓库的形式、或以安装包的形式)支撑的。其他仓库需放在 ./repositories 文件夹下:
mkdir repositories
然后依次下载以下仓库,并切换到合适的版本以避免可能的版本适配问题:
stablediffusion
git clone https://github.com/Stability-AI/stablediffusion.git ./repositories/stable-diffusion-stability-ai
git -C ./repositories/stable-diffusion-stability-ai checkout cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf
taming-transformers
git clone https://github.com/CompVis/taming-transformers.git ./repositories/taming-transformers
git -C ./repositories/taming-transformers checkout 24268930bf1dce879235a7fddd0b2355b84d7ea6
k-diffusion
git clone https://github.com/crowsonkb/k-diffusion.git ./repositories/k-diffusion
git -C ./repositories/k-diffusion checkout c9fe758757e022f05ca5a53fa8fac28889e4f1cf
CodeFormer
git clone https://github.com/sczhou/CodeFormer.git ./repositories/CodeFormer
git -C ./repositories/CodeFormer checkout c5b4593074ba6214284d6acd5f1719b6c5d739af
BLIP
git clone https://github.com/salesforce/BLIP.git ./repositories/BLIP
git -C ./repositories/BLIP checkout 48211a1594f1321b00f14c9f7a5b4813144b2fb9
- 建立并激活新 conda 环境
conda create -n sd-webui python=3.10
conda activate sd-webui
- 安装 requirements.txt 中的依赖包
pip install torch torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tb-nightly
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
说明:
- 必须先安装 torch:因为 basicsr 的安装程序会 import torch.
- 安装 tb-nightly:清华源缺少这个包,但安装 basicsr 时需要它,所以先不换源把它安装上。
- 安装其他依赖包
pip install ftfy regex tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install git+https://github.com/openai/CLIP.git
pip install open_clip_torch xformers -i https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip install -r "requirements.txt" --prefer-binary -i https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip install -r "requirements_versions.txt" --prefer-binary -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/CodeFormer/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/BLIP/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/k-diffusion/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/stable-diffusion-stability-ai/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
- 下载基础模型
从链接下载基础模型(Stable Diffusion v2.1,safetensors 格式,5.21 GB),放在 ./models/Stable-diffusion/ 下。当然也可以下载其他基础模型。
- 启动 webui
python webui.py
服务默认运行在 127.0.0.1:7860 上。
如果部署在远程服务器上,想通过其他电脑的浏览器访问服务,则需要添加 --listen 参数,这样服务运行在服务器的 0.0.0.0:7860 上,我们在浏览器输入 <server ip>:7860 才能访问。推荐添加 --xformers 参数,使用 xformers 优化时间和空间。添加 --port xxxx 参数更改端口。
--device-id DEVICE_ID 指定设备号(可通过nvidia-smi查看 显卡设备号)
--enable-insecure-extension-access 解除禁止插件下载;临时允许后台下载插件的启动后面加入 --enable-insecure-extension-access (没有特殊下载要求,不建议开启,设涉及安全问题)
python webui.py --listen --port 8000 --xformers --device-id 0
添加账号密码
python webui.py --listen --port 8000 --xformers --device-id 0 --gradio-auth admin:888888
备注:为了不同用户不互相影响webui上的模型参数混淆调用
另外复制一个stable-diffusion-webui 目录并指定不同的端口和设备号使用,如有必要env环境也可以重新复制一个
复制环境:conda create -n <new_env_name> --clone <origin_env_name>
cp -rp stable-diffusion-webui stable-diffusion-webui_8001
cd stable-diffusion-webui_8001
python webui.py --listen --port 8001 --xformers --device-id 1 --gradio-auth admin01:888888
有需要放前台运行 使用screen 来做前台启动 webui
一、screen命令简介
Screen是一个全屏窗口管理器,它在多个进程(通常是交互式shell)之间多路传输物理终端。每个虚拟终端提供DEC VT100终端的功能,以及ANSI X3的几个控制功能。64(ISO 6429)和ISO 2022标准(例如,插入/删除行和支持多个字符集)。每个虚拟终端都有一个回滚历史缓冲区和一个复制粘贴机制,允许用户在窗口之间移动文本区域。当调用screen时,它会创建一个包含shell(或指定命令)的窗口,然后避开您的方式,以便您可以正常使用该程序。然后,您可以随时创建包含其他程序(包括更多shell)的新(全屏)窗口、关闭当前窗口、查看活动窗口列表、打开和关闭输出日志、在窗口之间复制文本、查看滚动历史记录、在窗口之间切换,等等。所有窗口都完全独立运行其程序。当窗口当前不可见时,甚至当整个屏幕会话与用户终端分离时,程序仍继续运行。
二、使用示例
1、安装screen命令
[root@tmp]~# yum install -y screen
Installed:
screen.x86_64 0:4.1.0-0.27.20120314git3c2946.el7_9
Complete!
2、列出当前所有的session
[root@s145]~# screen -ls
No Sockets found in /var/run/screen/S-root.
3、创建一个新的窗口
[root@tmp]~# screen -S test
…
[root@tmp]~# screen -ls
There is a screen on:
2877.test (Attached)
1 Socket in /var/run/screen/S-root
4、退出当前窗口
可以使用ctrl+a,然后输入d,退出当前窗口,也可以使用screen -d退出当前窗口。如果要结束只窗口使用exit。
5、重新连接窗口
使用screen -r id或者名称重新连接会话,重新连接会话前要求会话的状态为Detached。
6、清除dead状态窗口
如果会话窗口被kill,状态转为dead无法连接,可以使用screen -wipe命令清除会话窗口。
7、会话锁定与解锁
输入ctl+a s锁定会话,锁定之后输入任何内容屏幕都无反应,输入ctl+a q之后解锁,虽然屏幕锁定的时候无反应但是会接受输入的命令,解锁后会全部执行,切勿输入危险命令,所以建议使用ctl+a x锁定。
输入ctl+a x锁定会话,需要输入用户密码后才可以解锁。
8、会话共享(当会话Attached状态)
screen -x
9、给screen窗口发送命令
[root@tmp]~# date && screen -S test -X screen ls > /tmp/1.txt
Tue Jan 11 17:12:24 CST 2023
退出当前窗口
可以使用ctrl+a,然后输入d,退出当前窗口,也可以使用screen -d退出当前窗口。如果要结束只窗口使用exit。
错误处理参考:
git clone https://github.com/Stability-AI/stablediffusion.git ./repositories/stable-diffusion-stability-ai
fatal: unable to access 'https://github.com/Stability-AI/stablediffusion.git/': GnuTLS recv error (-110): The TLS connection was non-properly terminated.
解决方法:
原因:权限不足
解决方法1:
git clone https://github.com/Stability-AI/stablediffusion.git ./repositories/stable-diffusion-stability-ai解决办法2:
git config --global --unset http.proxy git config --global --unset https.proxy
No module 'xformers'. Proceeding without it.
解决方法:./webui.sh --xformers
解决 Stable Diffusion 中的 AssertionError: extension access disabled because of commandline flags 错误
如果你在使用 Stable Diffusion 时遇到了 AssertionError: extension access disabled because of commandline flags
这个错误通常是由于命令行标志禁用了扩展访问所导致的。以下是两种解决此问题的方法:
方法一:使用命令行参数
添加 -enable-insecure-extension-access
下面是一个启动脚本的例子,它将添加 --enable-insecure-extension-access
#!/bin/bash
/path/to/stable_diffusion --enable-insecure-extension-access
请注意,您需要将 /path/to/stable_diffusion
方法二:删除 CMD 变量
如果您在 CMD 变量中使用了 -listen
或 -share
,则删除它们也可以解决此问题。
我们建议您在使用 Stable Diffusion 时遵守最佳实践,不要禁用扩展访问。如果您必须禁用它,请确保您知道自己在做什么,并且知道为什么这样做。
希望这篇文章对您有所帮助
stable diffusion webUi 拥有丰富的插件,这里说一下新手本地刚部署后如何汉化
如下图
1、选择 extensions
cd /home/test/stable-diffusion-webui/extensions
2、下载到服务器上extensions上
git clone https://github.com/dtlnor/stable-diffusion-webui-localization-zh_CN
3、勾选插件
4、勾选语言zh_ch|Apply setting| Reload UI
汉化完成
Webui 界面顶部开启VAE、CLIP 功能选择项
1、点击setting, 点击左侧的后面的Show all pages(显示所有页面)--Quictsettings list(主界面额外选项),选择 sd_model_checkpoint,sd_vae,CLIP_stop_at_last_layers