Search

Search papers and lessons

Micro-Conditioning

Level 4 · 8 min read

Micro-conditioning gives a diffusion model the original image size and the crop offset as extra inputs, so it can keep all its training images and stop learning resizing and cropping artifacts.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Generative models
  • Discrete math
  • Inference
  • Sign in to see your progress

TL;DR

Training images come in many sizes, and the usual fixes both cost something. Micro-conditioning hands the model two facts it already knows during training: the original height and width of each image, and the offset of the crop that was taken from it. The model learns to use these as inputs. At sampling time you can set them yourself, and the defaults push the model toward whole, object-centred pictures.

Where you'll see it: in SDXL, where the original size and crop offset are extra inputs alongside the prompt.

Intuition

A photo that was trimmed

Imagine learning to paint from a stack of photos, where some have been trimmed to fit a frame. A painter who never knows which photos were trimmed will start to paint the trimmed look as normal, with a head cut off at the edge. A painter who is told "this photo was 600 by 400, and we kept the square starting 90 pixels in from the left" can tell the trim apart from the subject.

Micro-conditioning does the same for a diffusion model. The trimming happens anyway, in the data pipeline. The model is simply told about it.

Why the idea exists

A latent diffusion model needs every training image to be at least a minimum size, because of how it is built. The paper that introduced micro-conditioning, SDXL, lists two standard answers to small images, and both hurt:

  • Drop every image below the training resolution. For SDXL's pretraining data, discarding everything below 256 pixels would throw away 39 percent of the samples.
  • Upscale small images. This adds upscaling artifacts that can leak into the outputs, for example as blur.

Micro-conditioning avoids both. Every image is resized and cropped to the training size, and the model is given what the original size was and where the crop came from.

flowchart LR
  A["Original image (h, w)"] --> B["Resize shorter side to target"]
  B --> C["Random crop to target size"]
  A --> D["c_size = (h, w)"]
  C --> E["c_crop = (c_top, c_left)"]
  D --> F["UNet conditioning"]
  E --> F

The math

Definition: Micro-conditioning

Let an image have original height hh and width ww before any rescaling. Let s=(htgt,wtgt)s = (h_{\text{tgt}}, w_{\text{tgt}}) be the training size, for example 256×256256 \times 256. Micro-conditioning adds two vectors to the model's inputs: the size csize=(h,w)\mathbf{c}_{\text{size}} = (h, w) and the crop offset ccrop=(ctop,cleft)\mathbf{c}_{\text{crop}} = (c_{\text{top}}, c_{\text{left}}), where ctopc_{\text{top}} and cleftc_{\text{left}} count the pixels cut from the top and the left edge.

Each number is turned into a vector with a Fourier feature encoding. The encodings of the components are concatenated, and the result is added to the timestep embedding τ(t)\tau(t) that tells the UNet how much noise is present. For size and crop together, the paper concatenates the two embeddings along the channel dimension before the addition. This lesson does not state the feature frequencies or widths.

The sampling rule

During training, the crop is random, so the model sees the crop offset it actually got. The rule is in Algorithm 1 of the paper. After resizing, the shorter side equals the target side, so the longer side has some extra length ℓ\ell. The crop offset along that longer side is drawn uniformly from the integers in the range

{0,1,…,ℓ},so there are ℓ+1 possible offsets.\{0, 1, \dots, \ell\}, \qquad \text{so there are } \ell + 1 \text{ possible offsets.}

The offset along the shorter side is zero. If h≤wh \le w, then ctop=0c_{\text{top}} = 0 and cleftc_{\text{left}} is drawn from that range. If h>wh \gt w, then cleft=0c_{\text{left}} = 0 and ctopc_{\text{top}} is drawn from it.

Inference

At sampling time the user chooses csize\mathbf{c}_{\text{size}}, which sets the apparent resolution of the picture. The paper sets ccrop=(0,0)\mathbf{c}_{\text{crop}} = (0, 0) by default. The reason it gives is that large scale datasets are, on average, object-centric, so offset zero gives whole objects rather than cut-off ones.

Worked examples

Example
A 600 by 400 photo cropped to 256

Take an image 600 pixels wide and 400 pixels tall, and a training size of 256×256256 \times 256.

  1. The height h=400h = 400 is less than the width w=600w = 600, so the shorter side is the height.
  2. Scale so the height becomes 256: the scale factor is 256/400=0.64256 / 400 = 0.64.
  3. The width becomes 600×0.64=384600 \times 0.64 = 384. The resized image is 384×256384 \times 256.
  4. The extra length is ℓ=384−256=128\ell = 384 - 256 = 128, so cleftc_{\text{left}} is drawn from {0,…,128}\{0, \dots, 128\}, which has 129 values. Here ctop=0c_{\text{top}} = 0.
  5. The chance that the crop starts at the left edge is 1/129≈0.00781 / 129 \approx 0.0078.
  6. The model receives csize=(400,600)\mathbf{c}_{\text{size}} = (400, 600) and ccrop=(0,cleft)\mathbf{c}_{\text{crop}} = (0, c_{\text{left}}).

Interpretation: the model learns that a 256-pixel square from this photo can start at many horizontal offsets, and it is told which one was used. The numbers are worked out by hand from the sampling rule above, not taken from the paper or measured.

Example
What the data savings are worth

The paper reports that discarding images below 256 pixels would remove 39 percent of its pretraining data. Suppose a set of one million images with that spread.

  1. Discarding: 1,000,000×(1−0.39)=610,0001{,}000{,}000 \times (1 - 0.39) = 610{,}000 images remain.
  2. Micro-conditioning: all 1,000,0001{,}000{,}000 images are used, each with its size and crop given to the model.

Interpretation: the conditioning does not add images. It keeps the 390,000 images that the simple filter would have thrown away, at no cost in preprocessing.

Example
The ImageNet ablation, read as numbers

The paper trains class-conditional latent diffusion models on ImageNet at 5122512^2 and reports FID (lower is better) and IS (higher is better). Its Table 2 has three rows.

ModelFIDIS
Only images at least 512 px (70k images)43.84110.64
All images, no size conditioning39.76211.50
All images, with size conditioning36.53215.34
  1. Size conditioning against no conditioning: the FID falls by 39.76−36.53=3.2339.76 - 36.53 = 3.23.
  2. Size conditioning against keeping only large images: the FID falls by 43.84−36.53=7.3143.84 - 36.53 = 7.31, which is 7.31/43.84≈16.77.31 / 43.84 \approx 16.7 percent of the baseline.
  3. The IS rises from 211.50 to 215.34, a difference of 3.84.

Interpretation: throwing away data hurts more than the blur that comes from keeping small images, and conditioning on size helps further on top of keeping all the data. The paper also notes that these classical scores are not reliable for text-to-image models, so the table supports the claim only on ImageNet.

Tradeoffs and failure modes

SituationProblemBetter choice
Drop images below the training sizeLarge loss of data, here 39 percentMicro-conditioning on size
Upscale small imagesUpscaling artifacts, such as blur, reach the outputsMicro-conditioning on size
Random crop with no record of itCut-off objects are learned as normal, the failure seen in older Stable Diffusion modelsRecord the crop offset as a condition
Sample with offset other than zeroCut-off or off-centre framing, which is useful for simulating a cropKeep the default (0,0)(0, 0) for object-centred output
Set a size the model rarely saw in trainingOutput may be unpredictable, since the model has little data for that rangeStay near the sizes in the data

Alternatives include data bucketing, which groups images by aspect ratio so batches share a shape. The paper says bucketing also solves the size problem, but it gives up the crop augmentation that micro-conditioning keeps, and it needs extra preprocessing. Micro-conditioning is applied online during training.

Related ideas are conditioning augmentation, where a model is conditioned on the amount of augmentation it saw, used in autoregressive and diffusion models. The paper cites this link.

History

Micro-conditioning was introduced in the SDXL paper by Dustin Podell, Robin Rombach and co-authors, posted to arXiv in July 2023. The problem was practical. The Stable Diffusion 1.x models discarded every image under 512 pixels, and their random crops produced cut-off subjects. SDXL was pretrained at 256 pixels, and the authors used the conditioning to keep the full dataset and avoid cut-off objects.

Check yourself

QuestionWhat two pieces of information does micro-conditioning give the model, and where are they added?Show answer

The original height and width of the image, csize=(h,w)\mathbf{c}_{\text{size}} = (h, w), and the crop offset ccrop=(ctop,cleft)\mathbf{c}_{\text{crop}} = (c_{\text{top}}, c_{\text{left}}). Each number is Fourier-encoded, and the encodings are added to the timestep embedding.

QuestionAn image is 1024 pixels wide and 512 pixels tall, and the training size is 256 by 256. The image is resized and then randomly cropped along the longer axis. What is the probability that the crop starts at the far right edge?Show answer

Resize by 256/512=0.5256 / 512 = 0.5, so the image becomes 512×256512 \times 256. The extra length is 512−256=256512 - 256 = 256, so cleftc_{\text{left}} takes one of {0,…,256}\{0, \dots, 256\}, which is 257 values. The chance of the far right, cleft=256c_{\text{left}} = 256, is 1/257≈0.00391 / 257 \approx 0.0039.

QuestionWhy not just discard all images below the training size?Show answer

Because it throws away data. For SDXL's pretraining set, discarding images below 256 pixels would remove 39 percent of the samples. Upscaling the small images instead adds artifacts that can appear in the outputs. Micro-conditioning keeps every image and records how it was resized and cropped.

QuestionA user generates a cat in SDXL and its head is cut off at the top of the frame. Using the paper's defaults, what should they set the crop offset to, and why?Show answer

They should set (ctop,cleft)=(0,0)(c_{\text{top}}, c_{\text{left}}) = (0, 0). The paper sets this as the default at inference because large datasets are mostly object-centric, so zero offset gives whole objects. The cut-off head comes from the crop that older models learned from training data, and the zero offset avoids it.

Sources