ControlNet
ControlNet adds spatial control, such as edges, depth maps or human poses, to a pretrained text-to-image diffusion model without retraining that model. The pretrained network is frozen. A trainable copy of its encoder reads the control image, and the copy feeds corrections into the frozen network through layers whose weights start at exactly zero. Zhang et al. propose the design and show it on Stable Diffusion with conditions including Canny edges, depth, segmentation and pose.
The problem it solves
A text prompt says what to draw, not where. Spatial structure can be added by concatenating a condition map to the model’s input channels and training, but that needs paired training data for each new condition type, and fine-tuning a large model on a small paired dataset tends to damage what it already knows. ControlNet instead keeps the original weights fixed and learns only the control pathway.
Structure
Take one block of the pretrained network, \(y=\mathcal F(x;\Theta)\). ControlNet freezes \(\Theta\), makes a trainable copy \(\Theta_c\) initialized from \(\Theta\), and joins the two with zero-initialized 1ร1 convolutions \(\mathcal Z\):
\[ y_c=\mathcal F(x;\Theta)+\mathcal Z\!\big(\mathcal F(x+\mathcal Z(c);\Theta_c)\big). \]
Here \(c\) is the control signal, already encoded to the latent grid. In the paper’s Stable Diffusion setup the copied part is the encoder half of the U-Net plus the middle block, and the outputs of the copy are added to the frozen decoder’s skip connections. The frozen model keeps its own path: if the branch outputs nothing, the network is exactly the original.
Why zero initialization is safe and still learns
At initialization \(\mathcal Z\) outputs 0 whatever its input, so \(y_c=\mathcal F(x;\Theta)\). The pretrained model is not disturbed by a randomly initialized branch at the first step, and the copy starts from sensible weights, not random ones. A layer that outputs zero might seem unable to learn. For a linear layer \(y=Zh\) the gradient with respect to the weights is the product of the layer input \(h\) and the incoming gradient, and neither is zero, so \(Z\) changes on the first update. The gradient flowing back through \(Z\) into the copy is zero at that step because it is multiplied by the zero weights, so the copy starts moving one step later.
A small linear toy shows both effects. A frozen layer \(W\) is paired with a copy \(W_c\) (initialized to \(W\)) and a zero matrix \(Z\); the target is the frozen output plus a correction computed from the control-conditioned features.
import numpy as np
rng = np.random.default_rng(0)
d, n = 6, 400
W = rng.normal(size=(d, d)) / np.sqrt(d)
x = rng.normal(size=(n, d)); c = rng.normal(size=(n, d))
B = rng.normal(size=(d, d)) / np.sqrt(d)
y = x @ W.T + ((x + c) @ W.T) @ B.T
Wc, Z = W.copy(), np.zeros((d, d))
print("output equals frozen model at init:", np.allclose(x @ W.T + ((x + c) @ Wc.T) @ Z.T, x @ W.T))
for step in range(401):
h = (x + c) @ Wc.T; out = x @ W.T + h @ Z.T; err = out - y
if step in (0, 1, 2, 5, 50, 400):
print(f"step {step:3d} loss {(err**2).mean():.4f} |Z|={np.abs(Z).max():.4f} |Wc-W|={np.abs(Wc-W).max():.4f}")
gZ = (2 / err.size) * err.T @ h
gWc = ((2 / err.size) * err @ Z).T @ (x + c)
Z -= 0.5 * gZ; Wc -= 0.5 * gWc
print("loss of the frozen layer alone: %.4f" % (((x @ W.T - y) ** 2).mean()))
output equals frozen model at init: True
step 0 loss 0.7703 |Z|=0.0000 |Wc-W|=0.0000
step 1 loss 0.3031 |Z|=0.2914 |Wc-W|=0.0000
step 2 loss 0.1598 |Z|=0.4223 |Wc-W|=0.0600
step 5 loss 0.0535 |Z|=0.4779 |Wc-W|=0.1351
step 50 loss 0.0003 |Z|=0.5777 |Wc-W|=0.2955
step 400 loss 0.0000 |Z|=0.5836 |Wc-W|=0.2912
loss of the frozen layer alone: 0.7703
The output at step 0 equals the frozen layer. \(Z\) leaves zero after the first update, while \(W_c\) is still unchanged after step 1 and moves from step 2, and the loss falls from 0.77, the loss of the frozen layer alone, to near zero. This toy is linear and checks the mechanism, not image quality.
What it gives and costs
- The base model’s quality and style are preserved, and one base model can host many ControlNets, each trained for one kind of condition. Zhang et al. report training with small and large datasets.
- The trainable copy adds a sizeable number of parameters (roughly the encoder half) and compute at inference, since both branches run.
- The control is soft. The model treats the map as strong evidence, not a constraint, so a conflicting prompt, a noisy map or a high guidance scale can produce outputs that deviate from it.
- The condition has to be extracted from the image the user supplies (an edge detector, a depth estimator, a pose estimator), so errors in that tool pass into the result.
Neighbors
Channel concatenation conditions through the input and needs the base model trained for it. Adapter approaches such as IP-Adapter add a separate cross-attention path for a reference image rather than a spatial copy, and LoRA changes the base weights with low-rank updates. Cross-attention handles token sequences such as text and can learn different token weights at different spatial queries. It does not guarantee the specified coordinates or edges of an aligned control map, which is the distinction from ControlNet.
References
- Zhang, Rao, Agrawala, Adding Conditional Control to Text-to-Image Diffusion Models โ https://arxiv.org/abs/2302.05543
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
