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

[复制链接]
查看1102 | 回复0 | 2023-8-15 11:05:58 | 显示全部楼层 |阅读模式
一、材料准备



  • 一台带GPU的电脑,系统Ubuntu20.2
  • Xshell和Xftp,用于连接云主机,传输文件
我用的是阿里云ECS,型号ecs.gn6i-c4g1.xlarge(4 vCPU 15 GiB 1 * NVIDIA T4)。推荐挂一个NAS盘方便保存文件。
二、安装步骤


  • (可选)安装显卡驱动
  1. # T4显卡的驱动是460,在这找https://www.nvidia.com/Download/index.aspx
  2. apt-get install nvidia-driver-460
  3. # 试试可不可以nvidia-smi检测显卡状态,如果可以显示则成功了
  4. nvidia-smi
复制代码

  • 安装Python虚拟环境
  1. # 安装python环境
  2. sudo apt install wget git python3 python3-venv
复制代码
  如果git报错443,可以多试几次,或者在/etc/hosts文件最后加一行:
140.82.113.4 github.com
↑这个IP可以从这里找到
  

  • 创建个非root用户
  1. # --home 用户目录,我的NAS挂载在/mnt,所以用户目录就建在mnt下了
  2. adduser --home /mnt/sd2 sd2
  3. su sd2
  4. cd /mnt/sd2
复制代码

  • 正式安装webui,主要遵循官方[1]的方法
  1. # 这里运行官方的自动化安装脚本webui.sh
  2. wget https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh
  3. 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
  三、下载模型


  • 下载模型的checkpoint:https://huggingface.co/stabilityai/stable-diffusion-2
   官方给的示例有:
768-v-ema.ckpt 模型下载+yaml下载
512-base-ema.ckpt 模型下载+yaml下载
  

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

把参数带上,让远端可以访问。参数列表可以在/modules/shared.py里面找到
  1. # 进到文件夹内,用虚拟环境的python运行
  2. cd /stable-diffusion-webui
  3. venv/bin/python3 launch.py --port 9965 --listen --no-half --enable-insecure-extension-access
复制代码
第一次运行会下载一些模型,成功后会提示:
  1. Launching Web UI with arguments: --port 9965 --listen --no-half --enable-insecure-extension-access
  2. No module 'xformers'. Proceeding without it.
  3. ...
  4. LatentDiffusion: Running in v-prediction mode
  5. DiffusionWrapper has 865.91 M params
  6. Loading weights [0b0d9f28] from ...
  7. Model loaded.
  8. Loaded a total of 0 textual inversion embeddings.
  9. Embeddings:
  10. Running on local URL:  http://0.0.0.0:9965 # 这里是监听地址
复制代码
电脑访问http://云主机ip:9965就可以玩了~
五、其他



  • 如果你像我一样是用云主机,为方便下一次运行,可以生成一个镜像,以后直接加载镜像就不需要重新安装了
  • 以后重新进入记得切用户su 你的用户名
  • 启动的完整参数看这里
  1. 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]
  2.                  [--max-batch-count MAX_BATCH_COUNT] [--embeddings-dir EMBEDDINGS_DIR] [--hypernetwork-dir HYPERNETWORK_DIR] [--localizations-dir LOCALIZATIONS_DIR] [--allow-code]
  3.                  [--medvram] [--lowvram] [--lowram] [--always-batch-cond-uncond] [--unload-gfpgan] [--precision {full,autocast}] [--share] [--ngrok NGROK] [--ngrok-region NGROK_REGION]
  4.                  [--enable-insecure-extension-access] [--codeformer-models-path CODEFORMER_MODELS_PATH] [--gfpgan-models-path GFPGAN_MODELS_PATH]
  5.                  [--esrgan-models-path ESRGAN_MODELS_PATH] [--bsrgan-models-path BSRGAN_MODELS_PATH] [--realesrgan-models-path REALESRGAN_MODELS_PATH]
  6.                  [--clip-models-path CLIP_MODELS_PATH] [--xformers] [--force-enable-xformers] [--deepdanbooru] [--opt-split-attention] [--opt-split-attention-invokeai]
  7.                  [--opt-split-attention-v1] [--disable-opt-split-attention] [--use-cpu USE_CPU [USE_CPU ...]] [--listen] [--port PORT] [--show-negative-prompt]
  8.                  [--ui-config-file UI_CONFIG_FILE] [--hide-ui-dir-config] [--freeze-settings] [--ui-settings-file UI_SETTINGS_FILE] [--gradio-debug] [--gradio-auth GRADIO_AUTH]
  9.                  [--gradio-img2img-tool {color-sketch,editor}] [--gradio-inpaint-tool {sketch,color-sketch}] [--opt-channelslast] [--styles-file STYLES_FILE] [--autolaunch]
  10.                  [--theme THEME] [--use-textbox-seed] [--disable-console-progressbars] [--enable-console-prompts] [--vae-path VAE_PATH] [--disable-safe-unpickle] [--api]
  11.                  [--api-auth API_AUTH] [--nowebui] [--ui-debug-mode] [--device-id DEVICE_ID] [--administrator] [--cors-allow-origins CORS_ALLOW_ORIGINS]
  12.                  [--cors-allow-origins-regex CORS_ALLOW_ORIGINS_REGEX] [--tls-keyfile TLS_KEYFILE] [--tls-certfile TLS_CERTFILE] [--server-name SERVER_NAME]
  13.                  [--ldsr-models-path LDSR_MODELS_PATH] [--scunet-models-path SCUNET_MODELS_PATH] [--swinir-models-path SWINIR_MODELS_PATH]
复制代码
5.1 XFormer

如果不想用–no-half,可以安装XFormer
  1. # 先进入stable-diffusion-webui目录下
  2. source ./venv/bin/activate
  3. cd repositories
  4. git clone https://github.com/facebookresearch/xformers.git
  5. cd xformers
  6. git submodule update --init --recursive
  7. ../../venv/bin/python3 -m pip install -r requirements.txt
  8. ../../venv/bin/python3 -m pip install -e .
复制代码
  我经常卡在git submodule update --init --recursiv这一步,国内还是老老实实用–no-half吧。。
  成功后,把启动参数改成–xformers
  1. venv/bin/python3 launch.py --port 9965 --listen --enable-insecure-extension-access --xformers
复制代码
5.2 自定义脚本

可以安装一些实用的自定义脚本
  1. # Tag输入智能提示,能在输入Tag时提供booru风格(如Danbooru)的TAG自动补全
  2. git clone "https://github.com/DominikDoom/a1111-sd-webui-tagcomplete.git" extensions/tag-autocomplete
  3. # Prompt测试,这个插件可以根据用户输入的提示语句,自动生成相应数量的图像,以测试提示语句中每个单词的影响。
  4. git clone "https://github.com/Extraltodeus/test_my_prompt.git" extensions/test-my-prompt
复制代码
来源:https://blog.csdn.net/weixin_39955411/article/details/128435879
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则