Stable Diffusion高级教程 - Controlnet
前言
在 ControlNet 出现之前,AI 绘画更像开盲盒,在图像生成前,你永远都不知道它会是一张怎样的图。ControlNet 的出现,真正意义上让 AI 绘画上升到生产力级别。简单来说可以用 ControlNet 精准控制 AI 图像的生成效果。
ControlNet 就是在大型扩散生成模型的基础上,再加上一个结构,使得扩散生成模型能够接受一个新的 “图像输入”。并且对模型的输出起到控制作用,使其与输入图像类似。
在实际的 AI 绘图,用户要解决 2 个关键问题:
- 图像的精准控制。例如确定背景、结构等
 - 图像的风格控制。例如确定动作、表情等
 
无论你使用再准确的提示词再好的微调模型也是无法实现的。而通过 ControlNet 的应用模型,可以让这 2 个问题获得解决。所以不仅是常见的人物画像,在建筑、室内、产品图、平面设计、海报等等领域都可以提高生产力甚至做出设计师难以做出的效果。
安装
需要先安装扩展,点击「Extensions」Tab 下的「Install from URL」子 Tab,然后输入: https://github.com/Mikubill/sd-webui-controlnet,再点击「Install」,在提示安装完成后,点击「Apply and restart UI」按钮重启 UI。
接着需要下载应用模型。我使用了最新的 ControlNet-v1-1-nightly 的模型:
wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_ip2p.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_shuffle.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_tile.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11e_sd15_depth.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_inpaint.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_mlsd.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_normalbae.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_scribble.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_seg.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15_softedge.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15s2_lineart_anime.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_softedge.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15s2_lineart_anime.pth -P ~/workspace/stable-diffusion-webui/models/ControlNet  | 
这些模型文件都在 1.3G 左右,文件比较大,可以按需下载。
解决安装报错问题
我在安装这个插件时,发现报错了:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun  | 
需要安装 Mac 开发工具再重新安装:
➜ xcode-select --install
➜ cd ~/workspace/stable-diffusion-webui
➜ venv/bin/pip install svglib fvcore
 | 
初体验
我们通过一个李多惠的照片在 txt2img 模式下生成图片作为例子:
这个招聘的动作其实是复杂的,用提示词不可能表达出来,现在有了 ControlNet 就非常容易还原,下面是用 canny、openpose 这个两个不同的应用模型,加上对应大模型和提示词最终生成的图:
Counterfeit:
ReV Animated:
可以看到每个图都固定对了动作,衣服结构、头发等细节也都非常还原。
另外这次我们只讨论动作是不是正确,手部问题一直是 AI 绘画的难点 (SD 很难理解人应该有 5 个手指🤦🏻♀️),负面提示词也很难解决,我之后会专门写一篇文章介绍怎么修复。
接着了解一下各个参数及其意义:
- Single Image。上传单张图片。
 - Batch。处理多张图片。Input Directory 填写的是待处理的图片所在路径。
 - Enable。在生图时激活 ControlNet,必选。
 - Low VRAM。生图使用 ControlNet 会吃更多 VRAM,此选项可降低 VRAM 使用量。它是建议 8G 以下显存开启,我 16G 也会开启。
 - Pixel Perfect。由 ControlNet 自动决定 Preprocessor 分辨率。
 - Allow Preview。允许预览生图结果,会显示该模型侦测的线条或骨架。一会我们会展开
 - Preprocessor 预处理器。Preprocessor 是先将上传的图片处理过一轮,例如 Scribbles 会将彩色图片转成线稿。通常 Preprocessor 和下面...
 
剩余内容已隐藏