What ControlNet does
A text prompt says what should be in an image but not where. ControlNet (Zhang, Rao and Agrawala) adds spatial control to a pretrained text-to-image model: an edge map, a depth map, a human pose or a segmentation tells it the layout, and the prompt fills in the rest. It does so without touching the pretrained model. A trainable copy of the model's encoder reads the control image and adds its results into the frozen model's skip connections, through convolutions that start at zero, so training begins from the original model's exact behaviour.
This tab records lllyasviel/sd-controlnet-canny on Stable Diffusion 1.5: a photo of a beagle becomes an edge map, and the prompt a tiger cub in a garden, photo is drawn on it at 384 × 384 pixels (a 4 × 48 × 48 latent) with 20 DDIM steps, guidance 7.5 and seed 0. The ControlNet has 361 M parameters beside the frozen U-Net's 860 M.
Canny edges
The Canny edges panel runs Canny's detector as the model card does, cv2.Canny(photo, 100, 200), in four stages. First the gradient: 3 × 3 Sobel filters give and per colour channel, and each pixel keeps the channel with the largest . Then non-maximum suppression thins every edge to one pixel by keeping a pixel only if it beats both neighbours along its gradient direction. Then hysteresis with two thresholds: pixels above 200 are strong edges and always kept, pixels between 100 and 200 are kept only if a chain of such pixels reaches a strong one, and the rest are dropped.
Of the 147,456 pixels, 55,954 are above the low threshold, 27,799 of those are local maxima, 10,770 are strong, and 20,924 survive as edges, 14.2 % of the image. Canny designed the detector in 1986 to find edges reliably, locate them accurately and mark each once; his version also smooths the image with a Gaussian first, which OpenCV leaves to the caller.
Edge encoder
The control image is 384 × 384 pixels and the U-Net works on a 48 × 48 latent, so a small Edge encoder of 8 convolutions with SiLU brings it down: 3 → 16 → 32 → 96 → 256 → 320 channels, three of them with stride 2. It has 1.09 M parameters. Its last convolution is also initialised at zero (after training its weights have RMS 0.0043), and its 320 feature maps are added to the trainable copy's first convolution of the noisy latent, . The panel shows both maps and their sum, the copy's input.
Trainable copy and zero convolutions
Write a frozen block of the U-Net as . ControlNet makes a trainable copy of it and joins the copy to the frozen network through two zero convolutions , 1 × 1 convolutions whose weights and biases start at zero:
At the first training step both output zero, so : the model is SD 1.5 unchanged and no random noise disturbs its features. The zero convolutions still learn, because the gradient of with respect to is , which is not zero; after one step is no longer zero and the gradient flows on into the copy. Zhang et al. report a sudden convergence: the model goes from ignoring the condition to following it within a short stretch of training.
For Stable Diffusion the copy is the whole encoder and the middle block, started from SD 1.5's weights. The Trainable copy → frozen U-Net panel marks the 13 places where it adds in: the 12 saved encoder outputs that the decoder reads through skip connections, and the middle block. After training the copy's blocks are a median 7.9 % away from SD 1.5's weights, and its time embedding 30.2 %. Zero convolutions → residuals shows, for each place, the copy's feature, the residual after the zero convolution, the U-Net's own skip and their sum, with the residual's size relative to the skip.
Conditioning scale and guidance
The residuals can be scaled before they are added, . Conditioning scale shows the image at and . At nothing is added, and the recording confirms the image equals SD 1.5's own pipeline output exactly. At step 1 every run sees the same starting latent, so the residuals there are exactly in proportion to ; later the runs' latents differ and so do their residuals.
The Guidance panel shows classifier-free guidance at 7.5. Both halves, the empty prompt and the prompt, get the edges, so guidance strengthens the text and not the layout: . Each Sampling loop step therefore runs ControlNet and the U-Net twice, then takes a DDIM step. With and without control compares at each step with the control at 1, 0.5 and 0.
Where ControlNet came from
ControlNet (2023) appeared alongside other ways to add conditions to a frozen diffusion model; T2I-Adapter, published within days of it, trains a much smaller separate network whose features are added to the U-Net's encoder. ControlNet's copy is larger but starts from the pretrained encoder's knowledge. The same zero-initialisation idea appears in DiT's adaLN-Zero (the DiT tab) and in LoRA, whose matrix starts at zero (the LCM tab).
Papers
- Adding Conditional Control to Text-to-Image Diffusion Models ControlNet: the trainable copy, zero convolutions and the canny model this tab records.
- A Computational Approach to Edge Detection The Canny edge detector: gradient, non-maximum suppression and hysteresis thresholds.
- High-Resolution Image Synthesis with Latent Diffusion Models Latent diffusion, the Stable Diffusion U-Net that ControlNet copies and steers.
- T2I-Adapter: Learning Adapters to Dig out More Controllable Ability for Text-to-Image Diffusion Models The lighter alternative: a small adapter network whose features are added to the frozen U-Net's encoder.
- Denoising Diffusion Implicit Models DDIM, the sampler of the recorded run.
- Classifier-Free Diffusion Guidance The guidance mix, here with the edges in both halves.