视觉人工智能指南

用于多条件扩散控制的 T2I 适配器

T2I-Adapter 是一款轻量级扩散插件,可为文本到图像模型提供对边缘、深度、姿势和其他结构的多条件控制,而无需重新训练基础模型。

听听这个指南AI叙述 · ~8 min

在本页阅读时间 9 分钟
  1. 主要要点
  2. 深入探讨
  3. 将精明边缘图转换为受控 SDXL 图像
  4. 现实世界的实施
  5. 用于多条件扩散控制的 T2I 适配器的未来
  6. 付诸实践
  7. 资料来源与延伸阅读
  8. 不断探索
  9. 常见问题

主要要点

  1. T2I-Adapter 通过约 7700 万个参数的独立网络为冻结的文本到图像扩散模型添加了空间控制。
  2. 适配器将条件图转换为四个特征图,并添加到 UNet 编码器的四个尺度中。
  3. Hugging Face 在 7900 万个参数和 158 MB 上测量了 T2I-Adapter-SDXL,比 SDXL 的 ControlNet 小约 94%。
  4. 适配器在每个图像上运行一次,而不是在每个去噪步骤中运行一次,因此它们增加的推理成本很低。
  5. 多个适配器可以通过加权相加进行组合,无需重新训练,但权重必须手动调整。
  6. Stability AI 的稳定涂鸦草图到图像工具于 2023 年 7 月发布,基于 SDXL 和 T2I-Adapter 构建。

深入探讨

T2I-Adapter is a method for steering a pretrained text-to-image diffusion model with an extra picture-like input, called a condition map. A text prompt alone is vague about layout: it can say 'a cat on a sofa' but not where the cat sits or which way it faces. A condition map fills that gap. It can be a line sketch, a depth map (a grayscale image where brightness encodes distance from the camera), a semantic segmentation map (each pixel labeled with an object class), a human pose skeleton, a canny edge map, or a coarse grid of colors. The adapter reads the map and nudges the image generator so the output respects it, while the generator itself stays frozen.

The method was introduced in the paper 'T2I-Adapter: Learning Adapters to Dig out More Controllable Ability for Text-to-Image Diffusion Models' by Chong Mou, Xintao Wang and colleagues, posted to arXiv on 16 February 2023. The code appeared in Tencent ARC Lab's GitHub repository a day earlier. The paper's central argument is that a large model like Stable Diffusion has already learned how to draw structure and color, so a control method does not need to teach new drawing skills. It only needs to learn an alignment between the external signal and the model's internal features, which a small network can do cheaply.

Here is how it works step by step. Stable Diffusion generates images in a compressed latent space using a UNet, a neural network with an encoder that shrinks the feature map through several resolutions and a decoder that expands it back. Generation starts from random noise and removes noise over many steps. The adapter takes the condition map at 512 by 512 pixels and uses pixel unshuffle, an operation that folds spatial blocks into channels, to bring it down to 64 by 64, matching the latent size. It then passes the result through four feature extraction blocks, each a convolution plus two residual blocks, with downsampling between them. This yields four feature maps at 64, 32, 16 and 8 pixels square, exactly the sizes of the UNet encoder's four stages. Each adapter feature is simply added to the matching encoder feature. The text prompt still enters through cross-attention as usual.

Training touches only the adapter. Each example is a triple of an image, its condition map and a caption. The image is encoded to latents, noise is added at a random timestep, and the frozen UNet, now receiving the adapter features, must predict that noise. The standard diffusion loss flows back only into the adapter's weights. The paper trained on Stable Diffusion 1.4 for 10 epochs with batch size 8 on four 32 GB V100 GPUs, finishing within three days. Sketch maps came from 164,000 COCO images run through an edge detector, segmentation used COCO-Stuff, and keypose, color and depth adapters used 600,000 image-text pairs from LAION-Aesthetics with poses extracted by MMPose and depth by MiDaS.

The main alternative is ControlNet, posted to arXiv six days earlier by Lvmin Zhang, Anyi Rao and Maneesh Agrawala. ControlNet makes a trainable copy of the UNet encoder and joins it to the locked original through zero-initialized convolutions. That copy is large: Hugging Face's September 2023 comparison lists a ControlNet for SDXL at 1,251 million parameters and 2.5 GB in fp16, against 79 million parameters and 158 MB for T2I-Adapter-SDXL, a reduction of about 94 percent. The same post notes that the adapter runs once for the whole denoising process, while ControlNet runs at every step, so the adapter's added inference cost is small. ControlNet's larger trainable branch gives it more capacity, and both families remain supported side by side in the diffusers library.

On the paper's own benchmark, the 5,000-image COCO validation set, Stable Diffusion with text alone scored an FID of 24.68, while text plus a segmentation adapter scored 16.78 and text plus a sketch adapter scored 17.36. FID, Frechet Inception Distance, measures how close the statistics of generated images are to real ones, and lower is better. CLIP score, which measures text-image agreement, rose slightly from 0.2648 to 0.2666 with the sketch adapter. These are the authors' own numbers on one dataset, and that table did not include ControlNet, so it does not settle which method controls better.

Three properties made the approach popular. Adapters are composable: several can be summed with per-adapter weights, for example depth plus pose or sketch plus color, with no extra training. They are generalizable: an adapter trained on Stable Diffusion 1.4 also worked on 1.5 and on community fine-tunes derived from the same base, such as Anything-V4.0. They are small: ablations in the paper showed an 18-million-parameter 'small' and a 5-million-parameter 'tiny' version still controlling sketches well.

The limitations are concrete. When several adapters are combined, the weights must be tuned by hand, which the authors name as the method's main limitation. Because the adapter has no timestep input, the authors found that guidance given late in sampling had little effect, so they sampled training timesteps from a cubic distribution that favors early, noisy steps. An adapter is tied to the base model family it was trained for, which is why separate adapters were released for SDXL. Output quality also depends on the condition map: Stability AI warned that Stable Doodle results vary with the drawing, the description and scene complexity. In practice, the SDXL adapters were trained on 3 million LAION-Aesthetics V2 pairs for 20,000 to 35,000 steps, and the TencentARC repository lists about 15 GB of GPU memory for inference.

03工作示例

将精明边缘图转换为受控 SDXL 图像

  1. 在具有 CUDA GPU 的计算机上安装扩散器、变压器、加速器和 opencv-python 软件包。

  2. 加载一张照片并运行 OpenCV 的 Canny 检测器(阈值分别为 100 和 200)以获得黑白边缘图,然后将其堆叠到三个通道中。

  3. 使用 float16 中的 T2IAdapter.from_pretrained('TencentARC/t2i-adapter-canny-sdxl-1.0') 加载适配器。

  4. 从 stableai/stable-diffusion-xl-base-1.0 构建 StableDiffusionXLAdapterPipeline,传递适配器,并将其移动到 GPU。

  5. 使用文本提示调用管道,边缘图为图像,大约 30 个推理步骤,adapter_conditioning_scale 为 0.8。

  6. 将adapter_conditioning_scale设置为0.3和1.0,并将adapter_conditioning_factor设置为0.5,重复此操作,保持相同的种子。

  7. 将边缘图和每个输出并排放置,并比较轮廓的匹配程度。

它显示了什么

结果是一组 SDXL 图像,它们共享照片的轮廓,但遵循新的内容和风格提示。通过在每个输出上叠加边缘图来检查控制:较高的比例值应该更紧密地跟踪边缘,而较低的比例或因子值则为模型提供更多的自由度。

现实世界的实施

Stability AI 的 Stable Doodle 于 2023 年 7 月在 Clipdrop 上推出,将 SDXL 与 T2I-Adapter 配对,使粗略的鼠标草图变成 14 种风格之一的成品图像。

概念艺术家将 OpenPose 骨架输入到 openpose 适配器中,以便生成的角色保持精确的姿势,同时文本提示设置服装、灯光和设置。

室内和产品设计师将参考照片中的 MiDaS 或 Zoe 深度图传递到深度适配器中,以在重新设计材料和颜色的同时保持房间的几何形状。

团队将精明的边缘适配器和深度适配器与扩散器 MultiAdapter 类结合起来,每个适配器的权重约为 0.7,以在一代中锁定轮廓和场景深度。

插画师使用空间调色板适配器(一种包含 1700 万个参数的变体)来在模型绘制细节之前修复构图中暖色和冷色的位置。

图像编辑器擦除一个区域,在其上绘制一个新草图,然后使用草图适配器运行 Stable Diffusion 修复(如原始论文中所示),以更改头部方向或对象形状。

用于多条件扩散控制的 T2I 适配器的未来

The original paper lists adaptive fusion of several guidance signals as future work, because hand-tuning weights for multiple adapters is fiddly. The repository took a step in that direction with CoAdapter, a composable adapter released in March 2023, and SDXL adapters for sketch, canny, lineart, openpose and two depth estimators followed in September 2023 with the diffusers team. A broader research question is how lightweight conditioning carries over to newer backbones. Many recent image models replace the UNet with diffusion transformers, which lack the multi-scale convolutional encoder that T2I-Adapter was designed to add features into, so adapter designs need rethinking for those architectures. Another open area is evaluation. The paper reports FID and CLIP score on COCO, but neither metric directly measures whether an output follows the condition map, so condition-fidelity measures such as edge or depth agreement remain an active topic. Whether small adapters or larger control branches prove the better trade-off on future models is not settled by current published results.

付诸实践

为什么这很重要

  • 速度与规模. 视觉人工智能可以大规模自动化检查、检测和标记任务。
  • 构建选择. 创意团队可以通过更少的手动修改更快地构建概念原型。
  • 团队与工作流程. 操作可以使用以前难以处理的图像和视频信号。

留意

  • 如果出处不明,肖像权和同意可能会成为法律风险。
  • 模型性能可能因光照、人口统计和环境的不同而有所不同。
  • 除非监控置信阈值,否则误报可能会被忽视。

后续步骤

  1. 定义精确度、召回率和错误成本的接受标准。
  2. 使用符合实际生产条件的数据进行测试。
  3. 为低置信度或高影响力的预测添加人工审核。
  4. 跟踪模型漂移并在相机或数据集更改后重新验证。

资料来源与延伸阅读

  1. arXivT2I-Adapter:学习适配器为文本到图像扩散模型挖掘更多可控能力
  2. GitHub (Tencent ARC Lab)腾讯ARC/T2I-Adapter
  3. Hugging Face使用 T2I 适配器高效可控生成 SDXL
  4. arXiv向文本到图像扩散模型添加条件控制
  5. Stability AIClipdrop 推出稳定版 Doodle
  6. Hugging FaceT2I-Adapter(扩散器文档)

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

参加 T2I-Adapter 多条件扩散控制测验

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

什么是用于多条件扩散控制的 T2I 适配器?

T2I-Adapter 是一款轻量级扩散插件,可为文本到图像模型提供对边缘、深度、姿势和其他结构的多条件控制,而无需重新训练基础模型。

T2I-Adapter 相对于 ControlNet 的主要优点是什么?

T2I-Adapter使用小型适配器网络(大约77M参数)而不是复制完整模型,使其更轻、更便宜。

以下哪一项是 T2I-Adapter 的有效条件输入?

T2I-Adapter 接受边缘图、深度图、姿势骨架、分割蒙版和草图等结构条件。

为什么 T2I-Adapter 在推理时的计算效率很高?

条件特征被提取一次并添加到编码器中,而不是在每个去噪步骤中重新计算,从而节省了计算量。

添加 T2I 适配器后,基础扩散模型会发生什么情况?

基础模型保持冻结状态;仅训练较小的适配器权重,因此一种模型可以支持多种条件类型。

多个 T2I-Adapter 可以一起使用吗?

可以组合多个适配器,例如姿势加深度,以对构图进行分层控制。