0%

Stable Diffusion 和 ComfyUI

原理

Stable Diffusion is a latent text-to-image diffusion model. Thanks to a generous compute donation from Stability AI and support from LAION, we were able to train a Latent Diffusion Model on 512x512 images from a subset of the LAION-5B database. Similar to Google’s Imagen, this model uses a frozen CLIP ViT-L/14 text encoder to condition the model on text prompts. With its 860M UNet and 123M text encoder, the model is relatively lightweight and runs on a GPU with at least 10GB VRAM.

QQs按:Stable Diffusion并非特定程序,而是一种文生图扩散模型,所谓扩散模型大致是将语意或原图,人为加入随机种子并采样,从局部要素发散以匹配学习过的素材特征,从而生成完整图像。Stability AI是推出Stable Diffusion模型的创业公司,LAION(Large-scale Artificial Intelligence Open Network)是一家非营利组织,成员来自世界各地,旨在向公众提供大规模机器学习模型、数据集和相关代码。这里需要指出的是,由于训练素材大多是512x512的小尺寸图像,使用文生图不宜创建太大尺寸,否则会被算法认为是多图拼接,欲生成大尺寸图像应由计算结果通过高清插值插件扩充。

通俗理解扩散模型

大白话讲解扩散模型

vae和潜空间

webui

Stable Diffusion Webui 为方便调用模型接口制作了图形化的交互界面

env requirement:

  • Python3.10

models

  • GFPGAN 腾讯开源的人像修复算法
  • VAE (Variational Auto-Encoder 变分自编码器) 修复图片中的某些部分,例如修复人脸图片中的眼睛,或者可以理解为一种滤镜
  • LoRA (Low-Rank Adaptation of Large Language Models) 自然语言处理大模型的低秩(Low-Rank)适应性版本(Adaptation)
  • DeepBooru

LoRA

什么是LoRA模型
自然语言大模型参数庞大(如GPT参数量超过千亿),训练成本太高,因此LoRA采用了一个办法,仅训练低秩矩阵(low rank matrics),使用时将LoRA模型的参数注入(inject)SD模型,从而改变SD模型的生成风格,或者为SD模型添加新的人物/IP。

CLIP 和 VAE

prompt通过CLIP等文本编码器转换为文本向量 像素分布和特征图通过VAE生成映射关系

tags

korean doll

nagetive prompt

paintings, sketchers, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, glans

troubleshooting

>

not enough GPU memory

1
2
3
4
5
6
7
8
@echo off

set PYTHON=
set GIT=
set VENV_DIR=
set COMMANDLINE_ARGS= --lowvram --no-half --precision full --no-half-vae --opt-sub-quad-attention --opt-split-attention-v1 --autolaunch

call webui.bat

No python at “D:\Program Files\Anaconda3\env\py310\Python.exe”
删除项目env下除Libs外的所有文件 重新执行webui.bat

资源

Stable Diffusion 提示词入门指南
基础模型库:C站 Huggingface 出自魔法学院文章

ComfyUI

recommended Python3.12!
对Stable Diffusion相关工具链的进一步封装,提供工作流编辑。

GitHub

1
git clone https://github.com/comfyanonymous/ComfyUI.git

编辑extra_model_paths.yaml文件以访问Stable Diffusion Webui项目目录下的模型

1
2
3
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu126
pip install -r requirements.txt
python main.py --listen=127.0.0.1 --port=8188

其中torch 2.5GB下载较久 源地址 https://download.pytorch.org/whl/cu126/torch-2.6.0%2Bcu126-cp312-cp312-win_amd64.whl 下载后本地安装命令如

1
pip install D:\Download\torch-2.6.0+cu126-cp312-cp312-win_amd64.whl

修改haggingface.io使用国内镜像:D:\Software\Anaconda3\envs\py312\Lib\site-packages\huggingfacehub_init.py脚本末尾添加

1
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'

ComfyUI中文手册

常见节点

  • Checkpoint加载器
  • CLIP文本加载器 正反prompt输入
  • 空latent 潜空间设置(长、宽、批次)
  • K采样器
  • VAE编码器
  • 保存

常用快捷键

  • 双击屏幕搜索并添加新节点

SD3 SD3.5 Flux.1

checkpoints

可见载入我们的大模型的label写的是checkpoints, 即所谓大模型放在model/checkpoints目录, checkpoints常见于RPG游戏被认为是存档点,对于训练复杂的模型,也需要记录阶段性的实验结果,以方便之后的运算基于此进行。
Keras Docs对checkpoints的解释 from《理解checkpoints》

  • The architecture of the model, allowing you to re-create the model
  • The weights of the model
  • The training configuration (loss, optimizer, epochs, and other meta-information)
  • The state of the optimizer, allowing to resume training exactly where you left off.

checkpoints是基础模型的参数定制

latent

从基本的文生图到图生图,删除空latent节点,因为要基于已有图像生成而不是随机空白画布

双击添加节点,搜索图像加载器,添加vae编码器,输入图像输出latent

另外 Ksampler中 设置随机种0 运行后操作randomize 采样器dpmpp_2m 调度器karras 步数20-30 cfg 7-8 降噪0.75-1

从基本图生图到局部重绘 也是对初始latent的修改 相比图生图的输入,局部重回需要加入一个遮罩层,需要把vae编码器换成vae内补编码器(VAE Encode (for Inpaint))可见该编码器相比前者多了一个“遮罩”输入 对应的是图像加载器的遮罩输出 绘制遮罩只需要在加载图像节点右键菜单选择在遮罩编辑器中打开即可绘制

K Sampler

设置对潜空间去噪过程,生成起始噪声的随机数种子、去噪步数…

并输出去噪后(按prompt生成后)的潜空间

controlnet

自定义节点

1
2
3
cd custom_nodes
# 以manager插件为例
git clone https://github.com/ltdrdata/ComfyUI-Manager

others

清华大学第六弹:AIGC发展研究3.0

人物替换

局部重绘

Kontext 万物迁移

应用

carvton
lovart