【生成模型之三】ControlNet & Latent Diffusion Models论文详解

论文:《Adding Conditional Control to Text-to-Image Diffusion Models》

我们提出了ControlNet,这是一种神经网络架构,可以将空间条件控制添加到大型预训练的文本到图像扩散模型中。ControlNet 冻结了生产就绪的大型扩散模型,并重用其经过数十亿张图像预训练的深度和鲁棒编码层作为学习各种条件控制的强大骨干。神经网络结构与“零卷积”(零初始化卷积层)相连接,该卷积层从零开始逐步增加参数,并确保没有有害噪声会影响微调。

在这里插入图片描述

1.问题挑战

以端到端的方式学习大型文本到图像融合模型的条件控制具有挑战性。特定条件下的训练数据量可能远小于一般文本到图像训练的可用数据量。对数据有限的大型预训练模型进行直接fine-tune或继续训练可能会导致过拟合和灾难性遗忘[31,75]。研究人员已经证明,通过限制可训练参数的数量,可以缓解这种遗忘。

本文介绍了ControlNet,这是一种端到端的神经网络架构,用于学习大型预训练文本到图像扩散模型的条件控制(在我们的实现中为稳定扩散)。ControlNet通过freeze其参数并制作其编码层的可训练副本来保留大型模型的质量和功能。

在这里插入图片描述

  • zero convolution零卷积层是一个1×1的卷积层,其权重和偏置都初始化为零。零卷积通过在初始训练步骤中消除随机噪声作为梯度来保护backbone。
  • 稳定扩散本质上是一个U-Net[73],它有一个编码器、一个中间块和一个跳跃连接的解码器。编码器和解码器都包含12个块,完整模型包含25个块,包括中间块。
  • 文本提示使用CLIP文本编码器[66],扩散时间步长使用位置编码的时间编码器进行编码。

在训练过程中,由于零卷积不会给网络增加噪声,因此模型应该始终能够预测高质量的图像。我们观察到,该模型不会逐渐学习控制条件,而是突然成功地跟随输入的调节图像;通常在不到10K的优化步骤中。如图4所示,我们称之为“突然收敛现象”。

在这里插入图片描述

在这里插入图片描述


High-Resolution Image Synthesis with Latent Diffusion Models

一文读懂Stable Diffusion 论文原理+代码超详细解读
MindSpore中U-Net代码详解
Stability-AI代码库—stablediffusion
https://github.com/CompVis/stable-diffusion
https://github.com/CompVis/latent-diffusion

应用

unconditional image synthesis, inpainting, stochastic super-resolution, image editing

解决问题

1.DDPM在pixel-spcae中运行,需要巨大的计算资源。
2.为了在有限的计算资源上实现DM训练,同时保持其质量和灵活性,我们将它们应用于强大的预训练自编码器的latent space中。
3.通过在模型架构中引入cross-attention,我们将扩散模型转化为强大而灵活的生成器,用于文本或边界框等一般条件输入,并以卷积方式实现高分辨率合成。
4.我们的潜在扩散模型(LDM)在image inpainting和class-conditional image synthesis方面取得了最新的最先进成绩,在各种任务上具有很强的竞争力,包括文本到图像合成、无条件图像生成和超分辨率,同时与基于像素的DM相比,显著降低了计算要求。

实现方式

首先,我们训练一个自动编码器,它提供了一个低维(从而高效)的表示空间,在感知上与数据空间等效。降低的复杂性还提供了通过单个网络通道从潜在空间高效生成图像的方法。我们将得到的模型称为潜在扩散模型(LDMs)。这种方法的一个显著优点是,我们只需要训练一次通用自动编码阶段,因此可以将其重新用于多个DM训练或探索可能完全不同的任务。这使得能够有效地探索各种图像到图像和文本到图像任务的大量扩散模型。最后,我们设计了一种架构,将变压器连接到DM的UNet骨干网[71],并支持任意类型的基于token的调节机制

优势

1.获得了计算效率更高的DM,因为采样是在低维空间上进行的。
2.利用了DM从其UNet架构中继承的归纳偏差,这使得它们对具有空间结构的数据特别有效,从而减轻了对先前方法所需的激进、降低质量的压缩级别的需求。
3.最后,我们得到了通用的压缩模型,其潜在空间可用于训练多个生成模型,也可用于其他下游应用,如单图像CLIP引导合成。

在这里插入图片描述
在这里插入图片描述

Conditioning Mechanisms

我们通过用交叉注意力机制增强其底层UNet骨干,将DM转化为更灵活的条件图像生成器[97],这对于学习各种输入模态的基于注意力的模型是有效的。
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

重要实验

对于text-to-images和layout-to-images实验(第4.3.1节),我们将调节器τθ实现为一个unmask transformer,它处理输入y的tokenized版本并产生输出ζ:=τθ(y),其中ζ∈RM×dτ。 更具体地说,该变换器由N个变换器块实现,这些变换器块由global self-attention layers, layer-normalization and position-wise MLPs,如下2:

请注意,第4.1节中描述的class-conditional model也是通过交叉注意实现的,其中τθ是一个维度为512的 a single learnable embedding layer,将类y映射到ζ∈R1×512。

在ζ可用的情况下,条件通过交叉注意力机制映射到UNet中,如图3所示。我们修改了“ablated-UNet”架构[15],并用一个由T块组成的浅(无掩模)变压器替换了self-attention,(i) self-attention, (ii) a position-wise MLP and (iii) a cross-attention layer;

虽然可以通过额外调节时间步长t来提高τθ的表征能力,但我们不追求这种选择,因为它降低了推理速度。我们将对这一修改的更详细分析留给未来的工作。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

### Stable Diffusion ControlNet Model Usage and Implementation #### Overview of ControlNet Integration with Stable Diffusion ControlNet is a plugin designed to enhance the capabilities of generative models like Stable Diffusion by providing additional guidance during image generation. This allows for more controlled outcomes, such as preserving specific structures or styles from input images while generating new content[^2]. #### Installation Requirements To use ControlNet alongside Stable Diffusion, ensure that all necessary dependencies are installed. The environment setup typically involves installing Python packages related to deep learning frameworks (e.g., PyTorch), along with libraries specifically required for handling image data. For instance, one can set up an environment using pip commands similar to those found in Hugging Face's diffusers repository: ```bash pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117 pip install transformers accelerate safetensors datasets ``` Additionally, clone the relevant repositories containing both `stable-diffusion` and `controlnet` implementations: ```bash git clone https://github.com/huggingface/diffusers.git cd diffusers/examples/community/ git clone https://github.com/Mikubill/sd-webui-controlnet.git ``` #### Basic Workflow Using ControlNet The workflow generally includes preparing inputs suitable for conditioning purposes within the diffusion process. For example, when working on edge detection tasks, preprocess your source material into formats compatible with what ControlNet expects – often grayscale images representing edges extracted via Canny filters or other methods. Here’s how you might implement this step programmatically: ```python from PIL import Image import numpy as np import cv2 def prepare_canny_edges(image_path): img = cv2.imread(image_path) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 100, 200) # Convert back to RGB format expected by some pipelines edged_img = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) return Image.fromarray(edged_img.astype('uint8'), 'RGB') ``` Afterwards, integrate these processed inputs directly into the pipeline configuration provided by either custom scripts derived from community contributions or official examples available through platforms like GitHub. #### Advanced Customization Options Beyond basic integration, users may explore advanced customization options offered by developers who have extended functionalities beyond initial designs. These enhancements could involve modifying architectures slightly differently than originally proposed or incorporating novel techniques aimed at improving performance metrics across various benchmarks. One notable advancement comes from research efforts focused on depth estimation problems where researchers introduced Depth-Anything—a robust single-view depth prediction framework capable of producing high-quality results under diverse conditions without requiring extensive retraining processes per dataset encountered[^3]. Such advancements indirectly benefit projects involving conditional GANs since better quality auxiliary information leads to improved final outputs. --related questions-- 1. How does integrating multiple types of conditioners affect the output diversity in generated images? 2. What preprocessing steps should be taken before feeding real-world photographs into ControlNet-enhanced models? 3. Can pre-trained weights from different domains improve cross-domain adaptation performances significantly? 4. Are there any limitations associated with current versions of ControlNet regarding supported modalities?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeremy_lf

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值