Commit
·
e52242a
1
Parent(s):
f91e691
Upload controlnet-cn.md (#54)
Browse files- Upload controlnet-cn.md (2ef1f2e77c852dce13b8bbcc7dbe056d332cd399)
Co-authored-by: SuSung-boy <[email protected]>
- controlnet-cn.md +422 -0
controlnet-cn.md
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: "ControlNet in 🧨 Diffusers"
|
| 3 |
+
thumbnail: /blog/assets/controlnet/thumbnail.png
|
| 4 |
+
authors:
|
| 5 |
+
- user: sayakpaul
|
| 6 |
+
- user: yiyixu
|
| 7 |
+
- user: patrickvonplaten
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# 使用 🧨 Diffusers 实现 ControlNet 高速推理
|
| 11 |
+
|
| 12 |
+
<!-- {blog_metadata} -->
|
| 13 |
+
<!-- {authors} -->
|
| 14 |
+
|
| 15 |
+
<script async defer src="https://unpkg.com/medium-zoom-element@0/dist/medium-zoom-element.min.js"></script>
|
| 16 |
+
|
| 17 |
+
<style>
|
| 18 |
+
.mx-auto {
|
| 19 |
+
margin-left: auto;
|
| 20 |
+
margin-right: auto;
|
| 21 |
+
}
|
| 22 |
+
</style>
|
| 23 |
+
|
| 24 |
+
<a target="_blank" href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/controlnet.ipynb">
|
| 25 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
|
| 26 |
+
</a>
|
| 27 |
+
|
| 28 |
+
自从 Stable Diffusion 风靡全球以来,人们一直在寻求如何更好地控制生成过程的方法。ControlNet 提供了一个简单的迁移学习方法,能够允许用户在很大程度上自定义生成过程。通过 [ControlNet](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/controlnet),用户可以轻松地使用多种空间语义条件信息(例如深度图、分割图、涂鸦图、关键点等)来控制生成过程。
|
| 29 |
+
|
| 30 |
+
具体来说,我们可以:
|
| 31 |
+
|
| 32 |
+
将卡通绘图转化为逼真的照片,同时保持极佳的布局连贯性。
|
| 33 |
+
|
| 34 |
+
<table>
|
| 35 |
+
<tr style="text-align: center;">
|
| 36 |
+
<th>Realistic Lofi Girl</th>
|
| 37 |
+
</tr>
|
| 38 |
+
<tr>
|
| 39 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/lofi.jpg" width=300 /></td>
|
| 40 |
+
</tr>
|
| 41 |
+
</table>
|
| 42 |
+
|
| 43 |
+
进行室内设计。
|
| 44 |
+
|
| 45 |
+
<table>
|
| 46 |
+
<tr style="text-align: center;">
|
| 47 |
+
<th>Before</th>
|
| 48 |
+
<th>After</th>
|
| 49 |
+
</tr>
|
| 50 |
+
<tr>
|
| 51 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/house_depth.png" width=300/></td>
|
| 52 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/house_after.jpeg" width=300/></td>
|
| 53 |
+
</tr>
|
| 54 |
+
</table>
|
| 55 |
+
|
| 56 |
+
将涂鸦草图变成艺术作品。
|
| 57 |
+
|
| 58 |
+
<table>
|
| 59 |
+
<tr style="text-align: center;">
|
| 60 |
+
<th>Before</th>
|
| 61 |
+
<th>After</th>
|
| 62 |
+
</tr>
|
| 63 |
+
<tr>
|
| 64 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/drawing_before.png" width=300/></td>
|
| 65 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/drawing_after.jpeg" width=300/></td>
|
| 66 |
+
</tr>
|
| 67 |
+
</table>
|
| 68 |
+
|
| 69 |
+
甚至拟人化著名的 logo 形象。
|
| 70 |
+
|
| 71 |
+
<table>
|
| 72 |
+
<tr style="text-align: center;">
|
| 73 |
+
<th>Before</th>
|
| 74 |
+
<th>After</th>
|
| 75 |
+
</tr>
|
| 76 |
+
<tr>
|
| 77 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/starbucks_logo.jpeg" width=300/></td>
|
| 78 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/starbucks_after.png" width=300/></td>
|
| 79 |
+
</tr>
|
| 80 |
+
</table>
|
| 81 |
+
|
| 82 |
+
ControlNet,使一切皆有可能 🌠
|
| 83 |
+
|
| 84 |
+
本文的主要内容:
|
| 85 |
+
|
| 86 |
+
1. 介绍 [`StableDiffusionControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/controlnet)
|
| 87 |
+
2. 展示多种控制条件样例
|
| 88 |
+
|
| 89 |
+
让我们开启控制之旅!
|
| 90 |
+
|
| 91 |
+
## ControlNet 简述
|
| 92 |
+
|
| 93 |
+
ControlNet 在 [Adding Conditional Control to Text-to-Image Diffusion Models](https://arxiv.org/abs/2302.05543) 一文中提被出,作者是 Lvmin Zhang 和 Maneesh Agrawala。它引入了一个框架,支持在扩散模型(如 Stable Diffusion)上附加额外的多种空间语义条件来控制生成过程。
|
| 94 |
+
|
| 95 |
+
训练 ControlNet 包括以下步骤:
|
| 96 |
+
|
| 97 |
+
1. 克隆扩散模型的预训练参数(文中称为 **可训练副本**, trainable copy。如 Stable Diffusion 的 latent UNet 部分),同时保留原本的预训练参数(文中称为 **锁定副本**, locked copy)。这样可以实现:a) 让锁定副本保留从大型数据集中学到的丰富知识;b) 让可训练副本学习特定任务的知识。
|
| 98 |
+
2. 可训练副本和锁定副本的参数通过 “零卷积” 层(详见 [此处](https://github.com/lllyasviel/ControlNet#controlnet))连接。“零卷积” 层是 ControlNet 框架的一部分,会在特定任务中优化参数。这是一种训练技巧,可以在新任务条件训练时保留已冻结模型已经学到的语义信息。
|
| 99 |
+
|
| 100 |
+
训练 ControlNet 的过程如图所示:
|
| 101 |
+
|
| 102 |
+
<p align="center">
|
| 103 |
+
<img src="https://github.com/lllyasviel/ControlNet/raw/main/github_page/sd.png" alt="controlnet-structure"><br>
|
| 104 |
+
<em>图表摘录于<a href=https://github.com/lllyasviel/ControlNet/blob/main/github_page/sd.png>此处</a></em>
|
| 105 |
+
</p>
|
| 106 |
+
|
| 107 |
+
ControlNet 训练集中的其中一种样例如下(额外的控制条件是 Canny 边缘图):
|
| 108 |
+
|
| 109 |
+
<table>
|
| 110 |
+
<tr style="text-align: center;">
|
| 111 |
+
<th>Prompt</th>
|
| 112 |
+
<th>Original Image</th>
|
| 113 |
+
<th>Conditioning</th>
|
| 114 |
+
</tr>
|
| 115 |
+
<tr style="text-align: center;">
|
| 116 |
+
<td style="vertical-align: middle">"bird"</td>
|
| 117 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/controlnet/original_bird.png" width=200/></td>
|
| 118 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/controlnet/canny_map.png" width=200/></td>
|
| 119 |
+
</tr>
|
| 120 |
+
</table>
|
| 121 |
+
|
| 122 |
+
同样地,如果我们使用的额外控制条件是语义分割图,那么 ControlNet 训练集的样例就是这样:
|
| 123 |
+
|
| 124 |
+
<table>
|
| 125 |
+
<tr style="text-align: center;">
|
| 126 |
+
<th>Prompt</th>
|
| 127 |
+
<th>Original Image</th>
|
| 128 |
+
<th>Conditioning</th>
|
| 129 |
+
</tr>
|
| 130 |
+
<tr style="text-align: center;">
|
| 131 |
+
<td style="vertical-align: middle">"big house"</td>
|
| 132 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/controlnet/original_house.png" width=300/></td>
|
| 133 |
+
<td><img class="mx-auto" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/controlnet/segmentation_map.png" width=300/></td>
|
| 134 |
+
</tr>
|
| 135 |
+
</table>
|
| 136 |
+
|
| 137 |
+
每对 ControlNet 施加一种额外的控制条件,都需要训练一份新的可训练副本参数。论文中提出了 8 种不同的控制条件,对应的控制模型在 [Diffusers](https://huggingface.co/lllyasviel?search=controlnet) 中均已支持!
|
| 138 |
+
|
| 139 |
+
推理阶段需要同时使用扩散模型的预训练权重以及训练过的 ControlNet 权重。如要使用 [Stable Diffusion v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5) 以及其 ControlNet 权重推理,其参数量要比仅使用 Stable Diffusion v1-5 多大约 7 亿个,因此推理 ControlNet 需要消耗更多的内存。
|
| 140 |
+
|
| 141 |
+
由于在训练过程中扩散模型预训练参数为锁定副本,因此在使用不同的控制条件训练时,只需要切换 ControlNet 可训练副本的参数即可。这样在一个应用程序中部署多个 ControlNet 权重就非常简单了,本文会在后面详细介绍。
|
| 142 |
+
|
| 143 |
+
## `StableDiffusionControlNetPipeline`
|
| 144 |
+
|
| 145 |
+
在开始之前,我们要向社区贡献者 [Takuma Mori](https://github.com/takuma104) 表示巨大的感谢。将 ControlNet 集成到 Diffusers 中,他功不可没 ❤️。
|
| 146 |
+
|
| 147 |
+
类似 Diffusers 中的 [其他 Pipeline](https://huggingface.co/docs/diffusers/api/pipelines/overview),Diffusers 同样为 ControlNet 提供了 [`StableDiffusionControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/controlnet) 供用户使用。 `StableDiffusionControlNetPipeline` 的核心是 `controlnet` 参数,它接收用户指定的训练过的 [`ControlNetModel`](https://huggingface.co/docs/diffusers/main/en/api/models#diffusers.ControlNetModel) 实例作为输入,同时保持扩散模型的预训练权重不变。
|
| 148 |
+
|
| 149 |
+
本文将介绍 `StableDiffusionControlNetPipeline` 的多个不同用例。首先要介绍的第一个 ControlNet 模型是 [Canny 模型](https://huggingface.co/runwayml/stable-diffusion-v1-5),这是目前最流行的 ControlNet 模型之一,您可能已经在网上见识过一些它生成的精美图片。在阅读到各个部分的代码时,也欢迎您使用此 [Colab 笔记本](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/controlnet.ipynb) 运行相关代码片段。
|
| 150 |
+
|
| 151 |
+
运行代码之前,首先确保我们已经安装好所有必要的库:
|
| 152 |
+
|
| 153 |
+
```bash
|
| 154 |
+
pip install diffusers==0.14.0 transformers xformers git+https://github.com/huggingface/accelerate.git
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
为处理不同 ControlNet 对应的多种控制条件,还需要安装一些额外的依赖项:
|
| 158 |
+
|
| 159 |
+
- [OpenCV](https://opencv.org/)
|
| 160 |
+
- [controlnet-aux](https://github.com/patrickvonplaten/controlnet_aux#controlnet-auxiliary-models) - ControlNet 预处理模型库
|
| 161 |
+
|
| 162 |
+
```bash
|
| 163 |
+
pip install opencv-contrib-python
|
| 164 |
+
pip install controlnet_aux
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
我们将以著名的油画作品[《戴珍珠耳环的少女》](https://en.wikipedia.org/wiki/Girl_with_a_Pearl_Earring)为例,首先让我们下载这张图像并查看一下:
|
| 168 |
+
|
| 169 |
+
```python
|
| 170 |
+
from diffusers.utils import load_image
|
| 171 |
+
|
| 172 |
+
image = load_image(
|
| 173 |
+
"https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png"
|
| 174 |
+
)
|
| 175 |
+
image
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
<p align="center">
|
| 179 |
+
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_6_output_0.jpeg" width=600/>
|
| 180 |
+
</p>
|
| 181 |
+
|
| 182 |
+
然后将图像输入给 Canny 预处理器:
|
| 183 |
+
|
| 184 |
+
```python
|
| 185 |
+
import cv2
|
| 186 |
+
from PIL import Image
|
| 187 |
+
import numpy as np
|
| 188 |
+
|
| 189 |
+
image = np.array(image)
|
| 190 |
+
|
| 191 |
+
low_threshold = 100
|
| 192 |
+
high_threshold = 200
|
| 193 |
+
|
| 194 |
+
image = cv2.Canny(image, low_threshold, high_threshold)
|
| 195 |
+
image = image[:, :, None]
|
| 196 |
+
image = np.concatenate([image, image, image], axis=2)
|
| 197 |
+
canny_image = Image.fromarray(image)
|
| 198 |
+
canny_image
|
| 199 |
+
```
|
| 200 |
+
|
| 201 |
+
如图可见,Canny 本质上是边缘检测器:
|
| 202 |
+
|
| 203 |
+
<p align="center">
|
| 204 |
+
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_10_output_0.jpeg" width=600/>
|
| 205 |
+
</p>
|
| 206 |
+
|
| 207 |
+
接下来,我们加载 [runwaylml/stable-diffusion-v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5) 和 [Canny 边缘 ControlNet](https://huggingface.co/lllyasviel/sd-controlnet-canny) 模型。设置参数 `torch.dtype=torch.float16` 可以指定模型以半精度模式加载,可实现内存高效和快速的推理。
|
| 208 |
+
|
| 209 |
+
```python
|
| 210 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 211 |
+
import torch
|
| 212 |
+
|
| 213 |
+
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
|
| 214 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 215 |
+
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
|
| 216 |
+
)
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
这里我们不使用 Stable Diffusion 默认的 [PNDMScheduler](https://huggingface.co/docs/diffusers/main/en/api/schedulers/pndm) 调度器,而使用改进的 [UniPCMultistepScheduler](https://huggingface.co/docs/diffusers/main/en/api/schedulers/unipc)(目前最快的扩散模型调度器之一),可以极大地加快推理速度。经测试,在保证生成图像质量的同时,我们能将推理阶段的采样步数从 50 降到 20。更多关于调度器的信息可以点击 [此处](https://huggingface.co/docs/diffusers/main/en/using-diffusers/schedulers) 查看。
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
```python
|
| 223 |
+
from diffusers import UniPCMultistepScheduler
|
| 224 |
+
|
| 225 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 226 |
+
```
|
| 227 |
+
|
| 228 |
+
我们通过调用 [`enable_model_cpu_offload` function](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/controlnet#diffusers.StableDiffusionControlNetPipeline.enable_model_cpu_offload) 函数来启用智能 CPU 卸载,而不是直接将 pipeline 加载到 GPU 上。
|
| 229 |
+
|
| 230 |
+
智能 CPU 卸载是一种降低显存占用的方法。扩散模型(如 Stable Diffusion)的推理并不是运行一个单独的模型,而是多个模型组件的串行推理。如在推理 ControlNet Stable Diffusion 时,需要首先运行 CLIP 文本编码器,其次推理扩散模型 UNet 和 ControlNet,然后运行 VAE 解码器,最后运行 safety checker(安全检查器,主要用于审核过滤违规图像)。而在扩散过程中大多数组件仅运行一次,因此不需要一直占用 GPU 内存。通过启用智能模型卸载,可以确保每个组件在不需要参与 GPU 计算时卸载到 CPU 上,从而显著降低显存占用,并且不会显著增加推理时间(仅增加了模型在 GPU-CPU 之间的转移时间)。
|
| 231 |
+
|
| 232 |
+
**注意**:启用 `enable_model_cpu_offload` 后,pipeline 会自动进行 GPU 内存管理,因此请不要再使用 `.to("cuda")` 手动将 pipeline 转移到 GPU。
|
| 233 |
+
|
| 234 |
+
```py
|
| 235 |
+
pipe.enable_model_cpu_offload()
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
最后,我们要充分利用 [FlashAttention/xformers](https://github.com/facebookresearch/xformers) 进行注意力层加速。运行下列代码以实现加速,如果该代码没有起作用,那么您可能没有正确安装 `xformers` 库,此时您可以跳过该代码。
|
| 239 |
+
|
| 240 |
+
```py
|
| 241 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 242 |
+
```
|
| 243 |
+
|
| 244 |
+
基本条件准备就绪,现在来运行 ControlNet pipeline!
|
| 245 |
+
|
| 246 |
+
跟运行 Stable Diffusion image-to-image pipeline 相同的是,我们也使用了文本提示语来引导图像生成过程。不过有一些不同的是,ControlNet 允许施加更多种类的控制条件来控制图像生成过程,比如使用刚才我们创建的 Canny 边缘图就能更精确的控制生成图像的构图。
|
| 247 |
+
|
| 248 |
+
让我们来看一些有趣的,将 17 世纪的名作《戴珍珠耳环的少女》中的少女一角换为现代的名人会是什么样?使用 ControlNet 就能轻松做到,只需要在提示语中写上他们的名字即可!
|
| 249 |
+
|
| 250 |
+
首先创建一个非常简单的帮助函数来实现生成图像的网格可视化。
|
| 251 |
+
|
| 252 |
+
```python
|
| 253 |
+
def image_grid(imgs, rows, cols):
|
| 254 |
+
assert len(imgs) == rows * cols
|
| 255 |
+
|
| 256 |
+
w, h = imgs[0].size
|
| 257 |
+
grid = Image.new("RGB", size=(cols * w, rows * h))
|
| 258 |
+
grid_w, grid_h = grid.size
|
| 259 |
+
|
| 260 |
+
for i, img in enumerate(imgs):
|
| 261 |
+
grid.paste(img, box=(i % cols * w, i // cols * h))
|
| 262 |
+
return grid
|
| 263 |
+
```
|
| 264 |
+
|
| 265 |
+
然后输入名字提示语,并设置随机种子以便复现。
|
| 266 |
+
|
| 267 |
+
```py
|
| 268 |
+
prompt = ", best quality, extremely detailed"
|
| 269 |
+
prompt = [t + prompt for t in ["Sandra Oh", "Kim Kardashian", "rihanna", "taylor swift"]] # 分别为:吴珊卓、金·卡戴珊、蕾哈娜、泰勒·斯威夫特
|
| 270 |
+
generator = [torch.Generator(device="cpu").manual_seed(2) for i in range(len(prompt))]
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
最后运行 pipeline,并可视化生成的图像!
|
| 274 |
+
|
| 275 |
+
```py
|
| 276 |
+
output = pipe(
|
| 277 |
+
prompt,
|
| 278 |
+
canny_image,
|
| 279 |
+
negative_prompt=["monochrome, lowres, bad anatomy, worst quality, low quality"] * 4,
|
| 280 |
+
num_inference_steps=20,
|
| 281 |
+
generator=generator,
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
image_grid(output.images, 2, 2)
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
<p align="center">
|
| 288 |
+
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_16_output_1.jpeg" width=600/>
|
| 289 |
+
</p>
|
| 290 |
+
|
| 291 |
+
我们还能轻松地将 ControlNet 与微调结合使用!例如使用 [DreamBooth](https://huggingface.co/docs/diffusers/main/en/training/dreambooth) 对模型进行微调,然后使用 ControlNet 增加控制信息,将其渲染到不同的场景中。
|
| 292 |
+
|
| 293 |
+
本文将以我们最爱的土豆先生为例,来介绍怎样结合使用 ControlNet 和 DreamBooth。
|
| 294 |
+
|
| 295 |
+
相较于上文,pipeline 中使用的 ControlNet 部分保持不变,但是不使用 Stable Diffusion 1.5,而��重新加载一个 [土豆先生](https://huggingface.co/sd-dreambooth-library/mr-potato-head) 模型(使用 Dreambooth 微调的 Stable Diffusion 模型)🥔。
|
| 296 |
+
|
| 297 |
+
虽然 ControlNet 没变,但仍然需要重新加载 pipeline。
|
| 298 |
+
|
| 299 |
+
```python
|
| 300 |
+
model_id = "sd-dreambooth-library/mr-potato-head"
|
| 301 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 302 |
+
model_id,
|
| 303 |
+
controlnet=controlnet,
|
| 304 |
+
torch_dtype=torch.float16,
|
| 305 |
+
)
|
| 306 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 307 |
+
pipe.enable_model_cpu_offload()
|
| 308 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 309 |
+
```
|
| 310 |
+
|
| 311 |
+
现在来让土豆先生摆一个《戴珍珠耳环的少女》的姿势吧!
|
| 312 |
+
|
| 313 |
+
```python
|
| 314 |
+
generator = torch.manual_seed(2)
|
| 315 |
+
prompt = "a photo of sks mr potato head, best quality, extremely detailed"
|
| 316 |
+
output = pipe(
|
| 317 |
+
prompt,
|
| 318 |
+
canny_image,
|
| 319 |
+
negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
|
| 320 |
+
num_inference_steps=20,
|
| 321 |
+
generator=generator,
|
| 322 |
+
)
|
| 323 |
+
output.images[0]
|
| 324 |
+
```
|
| 325 |
+
|
| 326 |
+
看得出来土豆先生尽力了,这场景着实不太适合他,不过他仍然抓住了精髓🍟。
|
| 327 |
+
|
| 328 |
+
<p align="center">
|
| 329 |
+
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_22_output_0.jpeg" width=600/>
|
| 330 |
+
</p>
|
| 331 |
+
|
| 332 |
+
ControlNet 还有另一个独特应用:从图像提取人体姿态,用姿态信息控制生成具有相同姿态的新图像。因此在下一个示例中,我们将使用 [Open Pose ControlNet](https://huggingface.co/lllyasviel/sd-controlnet-openpose) 来教超级英雄如何做瑜伽!
|
| 333 |
+
|
| 334 |
+
首先,我们需要收集一些瑜伽动作图像集:
|
| 335 |
+
|
| 336 |
+
```python
|
| 337 |
+
urls = "yoga1.jpeg", "yoga2.jpeg", "yoga3.jpeg", "yoga4.jpeg"
|
| 338 |
+
imgs = [
|
| 339 |
+
load_image("https://huggingface.co/datasets/YiYiXu/controlnet-testing/resolve/main/" + url)
|
| 340 |
+
for url in urls
|
| 341 |
+
]
|
| 342 |
+
|
| 343 |
+
image_grid(imgs, 2, 2)
|
| 344 |
+
```
|
| 345 |
+
|
| 346 |
+
<p align="center">
|
| 347 |
+
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_25_output_0.jpeg" width=600/>
|
| 348 |
+
</p>
|
| 349 |
+
|
| 350 |
+
通过 `controlnet_aux` 提供的 OpenPose 预处理器,我们可以很方便地提取瑜伽姿态。
|
| 351 |
+
|
| 352 |
+
```python
|
| 353 |
+
from controlnet_aux import OpenposeDetector
|
| 354 |
+
|
| 355 |
+
model = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 356 |
+
|
| 357 |
+
poses = [model(img) for img in imgs]
|
| 358 |
+
image_grid(poses, 2, 2)
|
| 359 |
+
```
|
| 360 |
+
|
| 361 |
+
<p align="center">
|
| 362 |
+
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_28_output_0.jpeg" width=600/>
|
| 363 |
+
</p>
|
| 364 |
+
|
| 365 |
+
瑜伽姿态提取完成后,我们接着创建一个 [Open Pose ControlNet](https://huggingface.co/lllyasviel/sd-controlnet-openpose) pipeline 来生成一些相同姿态的超级英雄图像。Let's go 🚀
|
| 366 |
+
|
| 367 |
+
```python
|
| 368 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 369 |
+
"fusing/stable-diffusion-v1-5-controlnet-openpose", torch_dtype=torch.float16
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 373 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 374 |
+
model_id,
|
| 375 |
+
controlnet=controlnet,
|
| 376 |
+
torch_dtype=torch.float16,
|
| 377 |
+
)
|
| 378 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 379 |
+
pipe.enable_model_cpu_offload()
|
| 380 |
+
```
|
| 381 |
+
|
| 382 |
+
超级英雄的瑜伽时间!
|
| 383 |
+
|
| 384 |
+
```python
|
| 385 |
+
generator = [torch.Generator(device="cpu").manual_seed(2) for i in range(4)]
|
| 386 |
+
prompt = "super-hero character, best quality, extremely detailed"
|
| 387 |
+
output = pipe(
|
| 388 |
+
[prompt] * 4,
|
| 389 |
+
poses,
|
| 390 |
+
negative_prompt=["monochrome, lowres, bad anatomy, worst quality, low quality"] * 4,
|
| 391 |
+
generator=generator,
|
| 392 |
+
num_inference_steps=20,
|
| 393 |
+
)
|
| 394 |
+
image_grid(output.images, 2, 2)
|
| 395 |
+
```
|
| 396 |
+
|
| 397 |
+
<p align="center">
|
| 398 |
+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/controlnet/anime_do_yoga.png" width=600/>
|
| 399 |
+
</p>
|
| 400 |
+
|
| 401 |
+
通过以上示例,我们对 [`StableDiffusionControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/controlnet) 的多种用法有了直观的认识,也学会了如何使用 Diffusers 玩转 ControlNet。不过,还有一些 ControlNet 支持的其他类型的控制条件示例,由于篇幅原因本文不再展开,如想了解更多信息,可以点击以下链接查看相应的模型文档页面:
|
| 402 |
+
|
| 403 |
+
* [lllyasviel/sd-controlnet-depth](https://huggingface.co/lllyasviel/sd-controlnet-depth)
|
| 404 |
+
* [lllyasviel/sd-controlnet-hed](https://huggingface.co/lllyasviel/sd-controlnet-hed)
|
| 405 |
+
* [lllyasviel/sd-controlnet-normal](https://huggingface.co/lllyasviel/sd-controlnet-normal)
|
| 406 |
+
* [lllyasviel/sd-controlnet-scribble](https://huggingface.co/lllyasviel/sd-controlnet-scribble)
|
| 407 |
+
* [lllyasviel/sd-controlnet-seg](https://huggingface.co/lllyasviel/sd-controlnet-scribble)
|
| 408 |
+
* [lllyasviel/sd-controlnet-openpose](https://huggingface.co/lllyasviel/sd-controlnet-openpose)
|
| 409 |
+
* [lllyasviel/sd-controlnet-mlsd](https://huggingface.co/lllyasviel/sd-controlnet-mlsd)
|
| 410 |
+
* [lllyasviel/sd-controlnet-mlsd](https://huggingface.co/lllyasviel/sd-controlnet-canny)
|
| 411 |
+
|
| 412 |
+
我们非常欢迎您尝试组合不同的控制组件来生成精美的图像,并在 twitter 上与 [@diffuserslib](https://twitter.com/diffuserslib) 分享您的作品。如果您还没有运行上述代码段,这里再次建议您查看此 [Colab 笔记本](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/controlnet.ipynb),亲自运行代码体验示例的效果!
|
| 413 |
+
|
| 414 |
+
在上文中,我们介绍了加速生成过程、减少显存占用的一些技巧,它们包括:快速调度器、智能模型卸载、`xformers`。如果结合使用这些技巧,单张图像的生成过程仅需要:V100 GPU 上约 3 秒的推理时间以及约 4 GB 的 VRAM 占用;免费 GPU 服务(如 Google Colab 的 T4)上约 5 秒的推理时间。如果没有实现这些技巧,同样的生成过程可达 17 秒!现已集成至 Diffusers 工具箱,来使用 Diffusers 吧,它真的非常强力!💪
|
| 415 |
+
|
| 416 |
+
## 结语
|
| 417 |
+
|
| 418 |
+
本文介绍了 [`StableDiffusionControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/controlnet) 的多个用例,非常有趣!我们也非常期待看到社区在此 pipeline 的基础上能构建出什么好玩的应用。如果您想了解更多 Diffusers 支持的关于控制模型的其他 pipeline 和技术细节,请查看我们的 [官方文档](https://huggingface.co/docs/diffusers/main/en/using-diffusers/controlling_generation)。
|
| 419 |
+
|
| 420 |
+
如果您想直接尝试 ControlNet 的控制效果,我们也能满足!只需点击以下 HuggingFace Spaces 即可尝试控制生成图像:
|
| 421 |
+
- [](https://huggingface.co/spaces/diffusers/controlnet-canny)
|
| 422 |
+
- [](https://huggingface.co/spaces/diffusers/controlnet-openpose)
|