Search

Search papers and lessons

Adding Conditional Control to Text-to-Image Diffusion Models (ControlNet)

Lvmin Zhang, Anyi Rao, Maneesh Agrawala · 2023

ControlNet adds spatial controls such as edges, poses or depth to a frozen text-to-image diffusion model by training a copy of its encoder, linked through zero-initialised convolutions.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Generative models
  • Architectures
  • Linear algebra
  • Neural network basics
  • Sign in to see your progress

TL;DR

ControlNet lets a frozen text-to-image diffusion model take a spatial control such as edges, a pose or a depth map. It trains a copy of the model's encoder, and joins the copy to the frozen model with convolutions that start at zero, so the model begins exactly as it was and learns the control without damaging its general image quality.

Where you'll see it: in image generation tools that let a user fix the composition with an edge map, a pose skeleton or a segmentation map while keeping a text prompt for style and content.

The problem

Text-to-image diffusion models such as Stable Diffusion generate striking images from a prompt, but a prompt is a weak way to say where things go. "A person waving" does not fix the pose, and a long prompt cannot reliably pin down a layout. Users want to give the model an image that fixes the composition: a Canny edge map, a human pose skeleton, a segmentation map, a depth map, or a rough sketch.

Training such a control from scratch is hard for two reasons. The paper notes that the data for a specific condition, such as sketches or poses paired with images, is far smaller than the general text-image data the base model was trained on. Training end to end on the small set risks damaging the image quality and the general knowledge the large model already has. The paper's related work covers the earlier routes: training conditional models from scratch, and steering a pretrained model at sampling time. Its comparisons test against several of these.

Key idea

ControlNet keeps the pretrained diffusion model frozen and adds a trainable copy of its encoder that receives the condition image. The copy and the frozen model are joined by convolutions whose weights start at zero. At the first training step, the joined output is exactly the frozen model's output, so the pretrained behaviour is preserved, and the copy learns to add the control signal gradually.

The idea reduces to two choices. First, reuse the large pretrained encoder as a trainable copy, which gives a deep and strong feature extractor for the condition. Second, connect it through zero-initialised 1×11 \times 1 convolutions, which add no noise to the frozen network at the start of training. The paper reports that this lets the model learn from small datasets without collapse.

flowchart LR
  C[Condition image c] --> Z1[zero conv]
  Z1 --> TC[Trainable copy of encoder block]
  X[Input feature x from frozen block] --> F[Frozen block F]
  X --> TC
  TC --> Z2[zero conv]
  F --> ADD((+))
  Z2 --> ADD
  ADD --> Y[Output y_c]

The method

One block

Take any neural block F(⋅;Θ)\mathcal{F}(\cdot; \Theta) with input feature map xx and output y=F(x;Θ)y = \mathcal{F}(x; \Theta). ControlNet locks Θ\Theta, so it is frozen, and makes a trainable copy with parameters Θc\Theta_c. Two zero convolutions Z(⋅;⋅)\mathcal{Z}(\cdot; \cdot), with parameters Θz1\Theta_{z1} and Θz2\Theta_{z2}, connect the pieces. The block's output with the control applied is

yc=F(x;Θ)+Z(F(x+Z(c;Θz1);Θc);Θz2).y_c = \mathcal{F}(x; \Theta) + \mathcal{Z}\Big( \mathcal{F}\big( x + \mathcal{Z}(c; \Theta_{z1}); \Theta_c \big); \Theta_{z2} \Big).

Here cc is the condition image after its own encoding. A zero convolution is a 1×11 \times 1 convolution whose weights and bias are all zero. At the first step, both Z\mathcal{Z} terms are zero, so yc=yy_c = y. The frozen model's behaviour is unchanged. Zero weights do not stop learning. The gradient on the weights of the output zero convolution (Θz2\Theta_{z2}) is nonzero at the first step, because its input is a nonzero feature map, so it moves away from zero immediately. Once it is nonzero, gradient reaches the input zero convolution and the trainable copy as well.

The paper also argues that the frozen copy saves compute. The locked encoder needs no gradients, and on a single A100 40GB the paper reports that training Stable Diffusion with ControlNet needs about 23 percent more GPU memory and 34 percent more time per iteration than training Stable Diffusion alone.

Applying it to Stable Diffusion

Stable Diffusion is a U-Net with an encoder, a middle block and a skip-connected decoder. ControlNet makes a trainable copy of the 12 encoding blocks and the middle block. The encoding blocks sit at four resolutions, from 64 by 64 down to 8 by 8. The control outputs are added to the 12 skip connections and to the middle block of the U-Net, so the locked decoder receives the control through its skip paths.

The training objective is the standard diffusion loss. Given a noisy latent ztz_t, time step tt, text prompt ctc_t and the task condition cfc_f, the network ϵθ\epsilon_\theta predicts the added noise, and the loss is

L=Ez0,t,ct,cf,ϵ∼N(0,1)[∥ϵ−ϵθ(zt,t,ct,cf)∥22].\mathcal{L} = \mathbb{E}_{z_0, t, c_t, c_f, \epsilon \sim \mathcal{N}(0, 1)} \Big[ \big\lVert \epsilon - \epsilon_\theta(z_t, t, c_t, c_f) \big\rVert_2^2 \Big].

This is the same loss that trains latent diffusion models. The only new input is cfc_f, which goes through the ControlNet branch.

To help the model read the condition without relying on the prompt, the paper replaces 50 percent of the text prompts with empty strings during training.

Sudden convergence

The paper reports a training behaviour it calls sudden convergence. Because the zero convolutions add nothing at the start, the model's outputs stay high quality throughout training. The model does not slowly learn to follow the condition. It switches to following the condition abruptly, usually in less than 10,000 optimisation steps, as in the example the paper plots at around 6,133 steps.

Guidance

Stable Diffusion relies on Classifier-Free Guidance, which combines an unconditional and a conditional output with a weight βcfg\beta_{\text{cfg}}. The paper notes that when the condition image is added to both the unconditional and conditional branches, guidance is completely removed in the no-prompt case, and when it is added only to the conditional branch, guidance becomes very strong. Its fix adds the condition only to the conditional branch and multiplies each ControlNet connection by a weight wi=64/hiw_i = 64 / h_i, where hih_i is the resolution of the block. The paper calls this CFG resolution weighting. It reduces guidance strength where the control matters, and it matters most when there is no prompt.

Results

The paper evaluates with qualitative examples, a user study, segmentation-conditioned metrics, and an ablation.

Ablation. The authors replace the zero convolutions with ordinary convolutions initialised from a Gaussian, and they replace the trainable copy with a single convolution (ControlNet-lite). Replacing the zero convolutions makes performance drop to about the level of ControlNet-lite, which the authors read as the pretrained backbone of the copy being damaged during fine-tuning. ControlNet-lite fails in the no-prompt and insufficient-prompt settings, while the full model succeeds in all four prompt settings the paper tests: no prompt, insufficient prompt, conflicting prompt, and perfect prompt.

User study. Twelve users ranked outputs for 20 hand-drawn sketches on a 1 to 5 scale. On result quality, ControlNet scored 4.22 plus or minus 0.43, against 3.93 for ControlNet-lite, 3.21 for Sketch-Guided Diffusion with edge scale 1.6, and 1.10 for PITI's sketch model. On condition fidelity, ControlNet scored 4.28 plus or minus 0.45.

Segmentation. On ADE20K, the segmentation IoU of the reconstructed labels is 0.35 for ControlNet, against 0.31 for LDM and 0.21 for VQGAN as baselines, with the ground truth at 0.58. For image quality, ControlNet's FID is 15.27 against 6.09 for Stable Diffusion alone, and its CLIP-aesthetic score is 6.31. The paper uses this table to show that ControlNet keeps image quality while following the segmentation far better than the models trained from scratch, but its FID of 15.27 is worse than the base model's 6.09, so the control costs some image quality on this measure.

Data scale. The paper reports that training does not collapse with 1,000 images and still yields a recognisable lion. Performance grows with more data.

Comparison with an industrial model. The paper compares a ControlNet trained on Stable Diffusion 2 with depth conditioning against the official Stable Diffusion 2 depth model. The official model used a large A100 cluster, thousands of GPU hours, and more than 12 million images. The ControlNet used 200,000 samples, one RTX 3090 Ti, and five days of training. In a blind test, users could not reliably tell the two apart, with an average precision of 0.52 plus or minus 0.17.

The results are on the paper's own benchmarks and user study. They are not a head-to-head on a standard benchmark for every condition, and the user study is small. A reader should treat the numbers as evidence for the design, not a full ranking.

Why it mattered

The paper's contribution is a way to add a control to a frozen, large diffusion model without retraining it. The design is simple to state: a trainable copy, two zero convolutions, and the standard denoising loss. It shows that a small amount of paired data can produce a usable control, and that several controls can be combined by adding the outputs of their ControlNet branches. The evidence for the design is the ablation and the comparisons in the results above.

Reading guide

Read the paper in this order, with the prerequisites each part leans on.

  1. Introduction and Section 2 (related work). Needs Fine-Tuning for the idea of adapting a pretrained network, and Transfer Learning for why a pretrained backbone helps with small datasets. The related-work section on fine-tuning includes HyperNetworks, which is the paper's reference point for changing a network's weights from outside.
  2. Section 3.1 (the ControlNet block). Needs Zero Convolution and Convolution. The zero initialisation and the two zero convolutions are the core of the method, so read this section slowly.
  3. Section 3.2 (on Stable Diffusion). Needs Latent Diffusion Model and U-Net. The attachment points are in the encoder and middle block of a U-Net.
  4. Section 3.3 (training). Needs Diffusion Model for the loss and the 50 percent prompt dropout.
  5. Section 3.4 (inference and guidance). Needs Classifier-Free Guidance and the latent diffusion paper (High-Resolution Image Synthesis with Latent Diffusion Models) for the base model's sampling.
  6. Section 4 (experiments). Needs no new prerequisites. Read the ablation first, then the user study.

Check yourself

QuestionRecall: what is a zero convolution, and why is it initialised to zero?Show answer

A zero convolution is a 1×11 \times 1 convolution with all weights and biases set to zero. At the start of training, the control branch contributes nothing, so the combined model reproduces the frozen model's output exactly. This keeps the pretrained behaviour intact while the weights begin to learn.

QuestionComputation: a ControlNet block has frozen output y=2.0y = 2.0. At the first training step, what is ycy_c, and what is the value of Z(c;Θz1)\mathcal{Z}(c; \Theta_{z1})?Show answer

The zero convolution output is Z(c;Θz1)=0\mathcal{Z}(c; \Theta_{z1}) = 0 for any input cc, because its weights and bias are zero. Both zero-convolution terms in the paper's formula are zero at the first step, so yc=y+0=2.0y_c = y + 0 = 2.0. The control branch adds nothing until the zero convolution weights move away from zero.

QuestionWhy does the paper replace 50 percent of prompts with empty strings during training?Show answer

If the model always received a prompt, it could learn to read the semantics from the text and ignore the condition image. Dropping half the prompts forces the control branch to interpret the condition on its own. That is what makes the model work in the no-prompt setting, and it is why the paper's ControlNet succeeds where ControlNet-lite fails with no prompt.

QuestionWhy does adding the condition image to both the unconditional and conditional branches of classifier-free guidance cause a problem?Show answer

Classifier-free guidance works by taking the difference between the conditional and unconditional outputs and amplifying it. If the condition is in both branches, the difference that carries the prompt signal shrinks, and in the no-prompt case the guidance effect is removed. Adding the condition only to the conditional branch causes the opposite issue, with guidance becoming too strong. The paper's resolution weighting handles this trade-off.

QuestionApplied: you want to add a depth control to a diffusion model that you can train on only 5,000 image pairs. Which parts of the design would you keep, and what would you check first?Show answer

Keep the frozen base model, the trainable encoder copy, and the zero convolutions, since they protect the base model when data is small. Check that the condition is learned without prompts by training with empty prompts for part of the run, as the paper does. Verify with an ablation that replacing the zero convolutions hurts the output quality, since that is the signal the paper uses to show the backbone is preserved.

Sources

Read the full paper

Lvmin Zhang, Anyi Rao, Maneesh Agrawala, 2023. arXiv:2302.05543