ubuntu2204 部署 stable-diffusion-webui

显卡:(一个实例仅能用一张卡)
顶配:rtx 6000 ada 48g,a100 40g,a100 80g ,a100 96g,a800 80g,h100,h200
高端:rtx4090 24g,rtx4090D 24g,rtx a6000 48g,rtx a5000 24g,rtx 5000 ada 32g
魔改:rtx2080ti 22g,rtx3080 20g
性价比:rtx4060ti 16g,rtx2060 12g,rtx3060 12g,rtx3090 24g,rtx titan 24g

其它硬件及系统环境
内存32g以上,固态300g以上,rtx之后8G以上显卡
ubuntu2204+命令行+N卡+conda

---------------------------------------------------------------------------------------------------------------
代理网站:
https://mirror.ghproxy.com/

常见名词:

# 预训练模型(pre model):也称基座模型。
是指在大量数据集上面提前训练好的模型,其本质是一堆参数集,未经过迁移训练,
这个时候的预训练模型并不具备特定的专业能力。
通常需要大量算力才能训练pre-model,一般大公司或者机构才玩得起。

# finetune model: 在预训练模型的基础上通过迁移训练,用以完成特定任务的模型。
一般分为全量参数微调和部分参数微调,微调相对于于预训练所消耗的算力资源要小得多,所需要的数据集也要小得多。
但是经过微调以后,模型的能力却能得到质变。
finetune后的model使用方式和基座模型一样,放到models/Stable-diffusion。

# Loro model:也是一种微调模型。
不同的是Loro通常仅微调特定的层,如Linner层(全连接层),非常灵活,开销很小。
要想获得不同的lora,可以是到网络上C站或国内的AI图站下载。
下载后的lora文件直接放到Stable Diffusion安装目录的models的lora目录里。

# VAE美化模型:VAE,全名Variational autoenconder,中文叫变分自编码器。
作用是:滤镜+微调。等于美颜相机。vaemodel通常放到models/VAE

# Embeddings:数据嵌入。
简单点说就是给模型外挂了一个记忆盘,把一些你需要教它的知识存储在这里面,
这个记忆盘实质是一个向量数据库,将你需要嵌入的信息转化为高维向量,然后通过在高维比较相似度来查询数据。
模型每次干活前从这里查询相关数据,但严格来说embedding不应该被定义为微调,因为它并没有修改模型本身的参数。
在SD中创建Embeddings需要安装cond_stage_model。

# DreamBooth:可用于训练预调模型用的。
是使用指定主题的图像进行演算,训练后可以让模型产生更精细和个性化的输出图像。

# LyCORIS模型:此类模型也可以归为Lora模型,也是属于微调模型的一种。
一般文件大小在340M左右。不同的是训练方式与常见的lora不同,但效果似乎会更好不少。
使用此类模型,需要安装插件​ https://github.com/KohakuBlueleaf/a1111-sd-webui-locon

---------------------------------------------------------------------------------------------------------------

检查cuda
# ll /sys/bus/pci/devices/
# sudo apt install nvidia-driver-530

nvcc -V
dpkg -l |grep cudnn
watch -n 0.5 nvidia-smi

https://stablediffusionweb.com
https://github.com/Stability-AI/stablediffusion
https://github.com/AUTOMATIC1111/stable-diffusion-webui


安装需要的包
sudo apt install bc libgoogle-perftools-dev
ldconfig -p | grep tcmalloc

sudo vim /etc/profile
export LD_PRELOAD=/lib/x86_64-linux-gnu/libtcmalloc.so
source /etc/profile

sudo apt install libgl1 libgl1-mesa-glx libgl1-mesa-dev
ldconfig -p | grep libGL.so.1


搭建python虚拟环境
conda update conda
conda config --add channels conda-forge
conda info
python -m pip install --upgrade pip
conda create -n sdwebui python=3.10.6
conda activate sdwebui

安装 Stable Diffusion web UI
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
pip install -r requirements_versions.txt
pip install -r requirements.txt

wget https://hf-mirror.com/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors   -N -P  models/Stable-diffusion

mkdir openai && cd openai
#https://huggingface.co/openai/clip-vit-large-patch14
git clone https://hf-mirror.com/openai/clip-vit-large-patch14.git

cd ..

vim webui.sh
can_run_as_root=1

vim modules/paths_internal.py
commandline_args = os.environ.get('COMMANDLINE_ARGS', "--xformers --share --theme dark --gradio-auth tuser:pwd")

-----------------------------------------------------------------------------------------------------
命令行参数
https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Command-Line-Arguments-and-Settings

--share online运行,也就是public address
--listen 使服务器侦听网络连接。这将允许本地网络上的计算机访问UI。
--port 更改端口,默认为端口7860。
--xformers 使用xformers库。极大地改善了内存消耗和速度。Windows 版本安装由C43H66N12O12S2 维护的二进制文件
--force-enable-xformers 无论程序是否认为您可以运行它,都启用 xformers。不要报告你运行它的错误。
--opt-split-attention Cross attention layer optimization 优化显着减少了内存使用。黑魔法。默认情况下torch.cuda,包括 NVidia 和 AMD 卡。
--disable-opt-split-attention 禁用上面的优化
--opt-split-attention-v1 使用上述优化的旧版本,它不会占用大量内存(它将使用更少的 VRAM,但会限制您可以制作的最大图片大小)。
--medvram 通过将稳定扩散模型分为三部分,使其消耗更少的VRAM,即cond(用于将文本转换为数字表示)、first_stage(用于将图片转换为潜在空间并返回)和unet(用于潜在空间的实际去噪),并使其始终只有一个在VRAM中,将其他部分发送到CPU RAM。降低性能,但只会降低一点-除非启用实时预览。
--lowvram 对上面更彻底的优化,将 unet 拆分成多个模块,VRAM 中只保留一个模块,破坏性能
*do-not-batch-cond-uncond 防止在采样过程中对正面和负面提示进行批处理,可以以 0.5 批量大小运行,从而节省大量内存。降低性能。
不是命令行选项,而是使用–medvramor 隐式启用的优化–lowvram。
--always-batch-cond-uncond 禁用上述优化。只有与–medvram或–lowvram一起使用才有意义
--opt-channelslast 更改 torch 内存类型,以稳定扩散到最后一个通道。
--theme dark 主题
--enable-insecure-extension-access 允许安装插件
--gradio-queue

------------------------------------------------------------------------------------------------------

界面方式启动
./webui.sh
http://127.0.0.1:7860/

API方式启动
./webui.sh --nowebui
http://127.0.0.1:7861/docs

### 相关接口
https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API
https://github.com/Mikubill/sd-webui-controlnet/wiki/API


### 启动脚本
vim start_sdwebui.sh

#!/bin/bash
source ~/anaconda3/etc/profile.d/conda.sh
conda activate sdwebui
cd /home/ai/stable-diffusion-webui
export LD_PRELOAD=/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4
./webui.sh

chmod +x start_sdwebui.sh
./start_sdwebui.sh


# systemctl
sudo vim /etc/systemd/system/sdwebui.service

[Unit]
Description=SDWebUI Service
After=network-online.target

[Service]
User=ai
Group=ai
ExecStart=/home/ai/stable-diffusion-webui/start_sdwebui.sh

[Install]
WantedBy=default.target

sudo systemctl start sdwebui && sudo systemctl status sdwebui
sudo systemctl enable sdwebui


### 安装中文插件
git clone https://github.com/VinsonLaro/stable-diffusion-webui-chinese
ll stable-diffusion-webui-chinese/localizations/
cp stable-diffusion-webui-chinese/localizations/* localizations

Settings->User interface->Localization(requires restart) 选择Chinese-ALL 或者 Chinese-English
Apply setting Reload UI

### 网站上,打开显示VAE模型、CLIP终止层数
settings->User Interface->Quicksettings list
添加 sd_vae 和 CLIP_stop_at_last_layers


### 其它SD扩展地址

https://gitcode.net/ranting8323/a1111-sd-webui-tagcomplete
https://gitcode.net/ranting8323/adetailer
https://gitcode.net/ranting8323/multidiffusion-upscaler-for-automatic1111
https://gitcode.net/ranting8323/sd-dynamic-thresholding
https://gitcode.net/ranting8323/sd-webui-controlnet

https://gitcode.net/ranting8323/sd-webui-infinite-image-browsing
https://gitcode.net/ranting8323/sd-webui-lora-block-weight
https://gitcode.net/ranting8323/sd-webui-model-converter
https://gitcode.net/ranting8323/sd-webui-openpose-editor
https://gitcode.net/ranting8323/sd-webui-prompt-all-in-one
https://gitcode.net/ranting8323/sd-webui-segment-anything

https://gitcode.net/ranting8323/sd-webui-supermerger
https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans
https://gitcode.net/ranting8323/stable-diffusion-webui-model-toolkit
https://gitcode.net/ranting8323/stable-diffusion-webui-wd14-tagger
https://gitcode.net/ranting8323/ultimate-upscale-for-automatic1111

 

################################################################

API 调用

https://blog.csdn.net/qq_40600379/article/details/131147790
https://blog.csdn.net/m0_54105644/article/details/135263814
https://blog.csdn.net/ll837448792/article/details/132226117
https://blog.csdn.net/Python_anning/article/details/135269356

LoRA模型的训练

https://zhuanlan.zhihu.com/p/671353062

################################################################

其它注意

### 替换镜像
./modules/launch_utils.py (./launch.py)
全局搜索https://github.com,然后在前面加上https://mirror.ghproxy.com/前缀

### 一直卡着clip安装
### 修改stable-diffusion-webui/modules目录下的launch_utils.py文件,修改https://github.com为其它镜像

### 缺少open_clip依赖
python -m pip install open_clip_torch
可以直接去github仓库将open_clip的代码clone到本地,然后进入open_clip项目根目录下执行相应指令去安装
open_clip>python setup.py build install

### openai/clip-vit-large-patch14
cat /repositories/generative-models/sgm/modules/encoders/modules.py
#350 version='openai/clip-vit-large-patch14",

### Max retries exceeded with url: /openai/clip-vit-large-patch14/resolve/main/vocab.json (Caused by ConnectTimeoutError
下载clip-vit-large-patch14 文件到本地 https://gitee.com/husky2008/clip-vit-large-patch14/repository/archive/main.zip
解压后到stable diffusion 根目录 openai/clip-vit-large-patch14/**

### 优化cpu和内存 Cannot locate TCMalloc (improves CPU memory usage)
apt-get install libgoogle-perftools4 libtcmalloc-minimal4

### 启动时提示Cannot activate python venv
python3 -c 'import venv'
python3 -m venv venv/

### 半精度问题 Try adding --no-half-vae commandline argument to fix this
commandline_args = os.environ.get('COMMANDLINE_ARGS', "--no-half-vae")

### websocket问题 Error: Connection errored out
commandline_args = os.environ.get('COMMANDLINE_ARGS', "--no-gradio-queue")

### git提示https没法访问
git config --global https.sslVerify false


### RuntimeError: GET was unable to find an engine to execute this computation
orch torchvision cuda以及python版本都是有兼容关系


################################################################

 

更新 sd-webui-controlnet,将 controlnet 插件升级至最新版本,即

cd extensions/sd-webui-controlnet
git remote -v
# https://ghproxy.com/ 代理已经失效,需要重新设置
git remote set-url origin https://github.com/Mikubill/sd-webui-controlnet.git
git pull origin main


更新 a1111-sd-webui-tagcomplete 插件:
cd workspace/stable_diffusion_webui/extensions/a1111-sd-webui-tagcomplete
git remote -v
# https://ghproxy.com/ 代理已经失效,需要重新设置
# git remote set-url origin https://github.com/DominikDoom/a1111-sd-webui-tagcomplete.git
git remote set-url origin git@github.com:DominikDoom/a1111-sd-webui-tagcomplete.git
git pull origin main


提示信息,不需考虑,参考 GitHub - Issues:
"Tag Autocomplete: Could not locate model-keyword extension, Lora trigger word completion will be limited to those added through the extra networks menu."
标签自动完成:无法定位模型关键字扩展,Lora 触发词补全,将仅限于通过额外网络菜单添加的内容。


更新 sd-webui-roop 插件:
cd workspace/stable_diffusion_webui/extensions/sd-webui-roop
git remote -v
# https://ghproxy.com/ 代理已经失效,需要重新设置
git remote set-url origin git@github.com:s0md3v/sd-webui-roop.git
git pull origin main


Warning 信息,修复参考 Deprecation Warning,替换文件 faceswap.py:
workspace/stable_diffusion_webui/extensions/sd-webui-roop/scripts/faceswap.py:38: GradioDeprecationWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components
img = gr.inputs.Image(type="pil")
workspace/stable_diffusion_webui/modules/gradio_extensons.py:25: GradioDeprecationWarning: `optional` parameter is deprecated, and it has no effect
res = original_IOComponent_init(self, *args, **kwargs)
workspace/stable_diffusion_webui/extensions/sd-webui-roop/scripts/faceswap.py:55: GradioDeprecationWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components
upscaler_name = gr.inputs.Dropdown(
workspace/stable_diffusion_webui/extensions/sd-webui-roop/scripts/faceswap.py:74: GradioDeprecationWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components
model = gr.inputs.Dropdown(


更新 sdweb-easy-prompt-selector 插件:
cd workspace/stable_diffusion_webui/extensions
git clone git@github.com:blue-pen5805/sdweb-easy-prompt-selector.git

posted @ 2024-04-08 11:41  vicowong  阅读(742)  评论(0)    收藏  举报