【踩坑笔记】从零开始部署安装Stable Diffusion 2 WebUI

一、材料准备

我用的是阿里云ECS,型号ecs.gn6i-c4g1.xlarge(4 vCPU 15 GiB 1 * NVIDIA T4)。推荐挂一个NAS盘方便保存文件。

二、安装步骤

  1. (可选)安装显卡驱动
# T4显卡的驱动是460,在这找https://www.nvidia.com/Download/index.aspx
apt-get install nvidia-driver-460

# 试试可不可以nvidia-smi检测显卡状态,如果可以显示则成功了
nvidia-smi
  1. 安装Python虚拟环境
# 安装python环境
sudo apt install wget git python3 python3-venv

如果git报错443,可以多试几次,或者在/etc/hosts文件最后加一行:
140.82.113.4 github.com
↑这个IP可以从
这里找到

  1. 创建个非root用户
# --home 用户目录,我的NAS挂载在/mnt,所以用户目录就建在mnt下了
adduser --home /mnt/sd2 sd2
su sd2
cd /mnt/sd2
  1. 正式安装webui,主要遵循官方[1]的方法
# 这里运行官方的自动化安装脚本webui.sh
wget https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh
bash webui.sh

到这里其实已经结束了,但是如果主机是在国内,那么一般来说都会报错的,下面总结几种我遇到的错误解决方法:

ERROR: Can’t cd to /mnt/sd2/stable-diffusion-webui/, aborting…
原因:没有成功git clone
解决方式:重试几次一般就可以了,或者手动下载 https://github.com/AUTOMATIC1111/stable-diffusion-webui,然后放在stable-diffusion-webui文件夹下

重试的时候要运行bash webui.sh,切勿python3 launch.py,因为是使用虚拟环境的,比如我的python3路径是/mnt/sd2/stable-diffusion-webui/venv/bin/python3

Torch is not able to use GPU; add –skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check
原因:可能是驱动版本不对,也可能是你在docker里
解决:检查一下GPU驱动的版本对不对,在这里查找https://www.nvidia.com/Download/index.aspx
如果是docker,搜一下csdn一大片解决方式

RuntimeError: Couldn’t install torch. (或者无法install其他的pip库)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out.
原因:网络问题,pip安装torch超时失败
解决:使用清华pip源
1.编辑/stable-diffusion-webui/launch.py文件,找到这段代码:
run(f'"{python}" -m {torch_command}
整行改成:
run(f'"{python}" -m {torch_command} -i https://pypi.tuna.tsinghua.edu.cn/simple', "Installing torch and torchvision", "Couldn't install torch")
2.然后再找到这段代码:
return run(f'"{python}" -m pip {args}
整行改成:
return run(f'"{python}" -m pip {args} -i https://pypi.tuna.tsinghua.edu.cn/simple --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}")
3.然后重试,一般就好了。原理就是加了清华的pip镜像 -i https://pypi.tuna.tsinghua.edu.cn/simple

fatal: unable to access ‘https://github.com/sczhou/CodeFormer.git/’ (或者其他的库): GnuTLS recv error (-110): The TLS connection was non-properly terminated.
原因:还是github网络问题
解决:这个和git clone失败的解决方式一样,重试几次,或者改hosts

三、下载模型

  1. 下载模型的checkpoint:https://huggingface.co/stabilityai/stable-diffusion-2

官方给的示例有:
768-v-ema.ckpt 模型下载+yaml下载
512-base-ema.ckpt 模型下载+yaml下载

  1. 把.ckpt放到models/Stable-Diffusion/目录下(如果你下载的模型带有.yaml文件,那么也一起放进去,.yaml的文件命名需要和你的.ckpt前面一样)
  2. 启动后在右上角选择你的模型就可以了

四、启动

把参数带上,让远端可以访问。参数列表可以在/modules/shared.py里面找到

# 进到文件夹内,用虚拟环境的python运行
cd /stable-diffusion-webui
venv/bin/python3 launch.py --port 9965 --listen --no-half

第一次运行会下载一些模型,成功后会提示:

Launching Web UI with arguments: --port 9965 --listen --no-half
No module 'xformers'. Proceeding without it.
...
LatentDiffusion: Running in v-prediction mode
DiffusionWrapper has 865.91 M params
Loading weights [0b0d9f28] from ...
Model loaded.
Loaded a total of 0 textual inversion embeddings.
Embeddings: 
Running on local URL:  http://0.0.0.0:9965 # 这里是监听地址

电脑访问http://云主机ip:9965就可以玩了~

五、其他

usage: launch.py [-h] [--config CONFIG] [--ckpt CKPT] [--ckpt-dir CKPT_DIR] [--gfpgan-dir GFPGAN_DIR] [--gfpgan-model GFPGAN_MODEL] [--no-half] [--no-half-vae] [--no-progressbar-hiding]
                 [--max-batch-count MAX_BATCH_COUNT] [--embeddings-dir EMBEDDINGS_DIR] [--hypernetwork-dir HYPERNETWORK_DIR] [--localizations-dir LOCALIZATIONS_DIR] [--allow-code]
                 [--medvram] [--lowvram] [--lowram] [--always-batch-cond-uncond] [--unload-gfpgan] [--precision {full,autocast}] [--share] [--ngrok NGROK] [--ngrok-region NGROK_REGION]
                 [--enable-insecure-extension-access] [--codeformer-models-path CODEFORMER_MODELS_PATH] [--gfpgan-models-path GFPGAN_MODELS_PATH]
                 [--esrgan-models-path ESRGAN_MODELS_PATH] [--bsrgan-models-path BSRGAN_MODELS_PATH] [--realesrgan-models-path REALESRGAN_MODELS_PATH]
                 [--clip-models-path CLIP_MODELS_PATH] [--xformers] [--force-enable-xformers] [--deepdanbooru] [--opt-split-attention] [--opt-split-attention-invokeai]
                 [--opt-split-attention-v1] [--disable-opt-split-attention] [--use-cpu USE_CPU [USE_CPU ...]] [--listen] [--port PORT] [--show-negative-prompt]
                 [--ui-config-file UI_CONFIG_FILE] [--hide-ui-dir-config] [--freeze-settings] [--ui-settings-file UI_SETTINGS_FILE] [--gradio-debug] [--gradio-auth GRADIO_AUTH]
                 [--gradio-img2img-tool {color-sketch,editor}] [--gradio-inpaint-tool {sketch,color-sketch}] [--opt-channelslast] [--styles-file STYLES_FILE] [--autolaunch]
                 [--theme THEME] [--use-textbox-seed] [--disable-console-progressbars] [--enable-console-prompts] [--vae-path VAE_PATH] [--disable-safe-unpickle] [--api]
                 [--api-auth API_AUTH] [--nowebui] [--ui-debug-mode] [--device-id DEVICE_ID] [--administrator] [--cors-allow-origins CORS_ALLOW_ORIGINS]
                 [--cors-allow-origins-regex CORS_ALLOW_ORIGINS_REGEX] [--tls-keyfile TLS_KEYFILE] [--tls-certfile TLS_CERTFILE] [--server-name SERVER_NAME]
                 [--ldsr-models-path LDSR_MODELS_PATH] [--scunet-models-path SCUNET_MODELS_PATH] [--swinir-models-path SWINIR_MODELS_PATH]

5.1 XFormer

如果不想用–no-half,可以安装XFormer


# 先进入stable-diffusion-webui目录下
source ./venv/bin/activate
cd repositories
git clone https://github.com/facebookresearch/xformers.git
cd xformers
git submodule update --init --recursive
../../venv/bin/python3 -m pip install -r requirements.txt
../../venv/bin/python3 -m pip install -e .

我经常卡在git submodule update --init --recursiv这一步,国内还是老老实实用–no-half吧。。

成功后,把启动参数改成–xformers

venv/bin/python3 launch.py --port 9965 --listen --xformers

5.2 自定义脚本

可以安装一些实用的自定义脚本

# Tag输入智能提示,能在输入Tag时提供booru风格(如Danbooru)的TAG自动补全
git clone "https://github.com/DominikDoom/a1111-sd-webui-tagcomplete.git" extensions/tag-autocomplete
# Prompt测试,这个插件可以根据用户输入的提示语句,自动生成相应数量的图像,以测试提示语句中每个单词的影响。
git clone "https://github.com/Extraltodeus/test_my_prompt.git" extensions/test-my-prompt

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年3月18日 上午11:01
下一篇 2023年3月18日 上午11:02

相关推荐