Search

Search papers and lessons

InstructPix2Pix: Learning to Follow Image Editing Instructions

Tim Brooks, Aleksander Holynski, Alexei A. Efros · 2022

InstructPix2Pix edits a photo from a written instruction such as make it winter, trained on more than 450,000 synthetic examples built by chaining a language model with a text-to-image model.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Architectures
  • Generative models
  • ReadReady to readNot yet

TL;DR

Editing a photo usually means a mask, a second example image, or a long description of both the before and after pictures. InstructPix2Pix takes one photo and one sentence, such as "make it winter", and returns the edited photo in a few seconds. The authors got training data without human editors: a fine-tuned language model wrote more than 450,000 edit instructions with captions, and a text-to-image model rendered before-and-after image pairs for them. A latent diffusion model trained on those triplets then follows new instructions on real photos.

Builds on: Latent Diffusion Model, Classifier-Free Guidance, Fine-Tuning, Language Model · Leads to: instruction-following image editors that were trained the same way, from synthetic pairs

Why it mattered

The paper showed that a model can learn a task that has almost no labelled data by chaining two large pretrained models, one for language and one for images, and using the pair to make the training set. The language model supplies the instructions and the image model supplies the matching pictures. The editor it trains works on real photos and on instructions no one wrote into the training set.

The approach is simple to copy. Later instruction-based image editors have used the same pattern of synthetic before-and-after data. The paper's ideas, especially a diffusion model with two separate guidance knobs, one for the image and one for the instruction, have been reused in other conditional editing systems. Editing a 512 by 512 image takes roughly 9 seconds on one NVIDIA A100 GPU, which made the method usable interactively in the setting the authors tested.

The problem

Before this work, text-based editing tools needed the model to know what the picture should look like, not what to do to it. Methods such as SDEdit need a full description of the output, and Text2Live needs a description of the input and an extra layer. Prompt-to-Prompt edits generated images but only for images it created itself. A real photo has no caption that describes the edited result, so a user who wants to change the season has to describe the whole scene.

Two things were missing. The first was a model that takes an image and a short instruction and performs the edit in one forward pass, without inverting the image into a latent code and fine-tuning on it. The second was training data: nobody had a large set of real photos paired with their edited versions and the instruction that links them. The paper's answer to both is the same move, building the data from generative models and then training a conditional diffusion model on it.

How it works

Take one tiny example through the whole pipeline. A caption is "photograph of a girl riding a horse". The pipeline has four stages.

  1. Write an instruction and a new caption. A language model fine-tuned on 700 human-written triplets sees the input caption and produces an instruction such as "have her ride a dragon" and an edited caption "photograph of a girl riding a dragon".
  2. Render a before image and an after image. A text-to-image diffusion model renders both captions. A method called Prompt-to-Prompt, which shares attention weights between the two generations, keeps the pair consistent so that the two pictures differ mainly by the edit. The authors render 100 candidate pairs for each caption pair, each with a random share of shared steps, and filter them with CLIP scores. They keep up to 4 that pass all three thresholds.
  3. Train the editor on triplets. Each training example is (before image, instruction, after image). The editor is a diffusion model that receives the before image and the instruction as conditions and learns to produce the after image from noise.
  4. Edit a real photo. Given a new real photo and a new instruction, the trained model denoises a random latent while attending to both conditions, then decodes the result. No extra training is done for that photo.

To see the guidance step concretely, suppose three scalar noise predictions at one denoising step are e(∅,∅)=0.0e(\varnothing, \varnothing) = 0.0, e(cI,∅)=1.0e(c_I, \varnothing) = 1.0 and e(cI,cT)=1.2e(c_I, c_T) = 1.2, with sI=1.5s_I = 1.5 and sT=7.5s_T = 7.5. The combined prediction is 0.0+1.5×(1.0−0.0)+7.5×(1.2−1.0)=1.5+1.5=3.00.0 + 1.5 \times (1.0 - 0.0) + 7.5 \times (1.2 - 1.0) = 1.5 + 1.5 = 3.0. The numbers are illustrative, not from the paper, but the shape is the paper's: each knob pushes the output harder toward one condition.

flowchart LR
  A["Caption"] --> B["Fine-tuned GPT-3 writes instruction and edited caption"]
  B --> C["Stable Diffusion with Prompt-to-Prompt renders before and after"]
  C --> D["CLIP filters the pairs"]
  D --> E["Triplets: image, instruction, edited image"]
  E --> F["Diffusion editor trained on triplets"]
  F --> G["Real photo plus new instruction gives edited photo"]

The math

Step 1 in symbols: the training objective

The paper uses the latent diffusion loss from latent diffusion. Each image xx is encoded to a latent z=E(x)z = \mathcal{E}(x) by a pretrained autoencoder (see Variational Autoencoder for the encoder idea). A noisy latent ztz_t is produced at a random step tt, and a network ϵθ\epsilon_\theta is trained to predict the noise that was added, given both conditions: L=E[ ∥ϵ−ϵθ(zt, t, E(cI), cT)∥22 ].L = \mathbb{E}\Big[\,\big\lVert \epsilon - \epsilon_\theta\big(z_t,\, t,\, \mathcal{E}(c_I),\, c_T\big) \big\rVert_2^2\,\Big]. Here cIc_I is the input image, cTc_T is the instruction, and the expectation is over images, noise, and step. The input image enters by concatenation: its latent is stacked onto ztz_t along the channel axis of the first U-Net layer, and the weights for those new channels start at zero, so training begins from the pretrained text-to-image model's behaviour.

Step 2 in symbols: the two conditions

The network sees two conditions. The image condition cIc_I and the instruction condition cTc_T are dropped at random during training, so the same network learns conditional and unconditional prediction. Following the paper, cIc_I alone is dropped for 5 percent of examples, cTc_T alone for 5 percent, and both for 5 percent.

Step 3 in symbols: guidance with two scales

Classifier-free guidance extrapolates from the unconditional prediction toward the conditional one. With two conditions and two scales sIs_I and sTs_T, the guided prediction is e~θ(zt,cI,cT)=eθ(zt,∅,∅)+sI(eθ(zt,cI,∅)−eθ(zt,∅,∅))+sT(eθ(zt,cI,cT)−eθ(zt,cI,∅)).\tilde{e}_\theta(z_t, c_I, c_T) = e_\theta(z_t, \varnothing, \varnothing) + s_I\big(e_\theta(z_t, c_I, \varnothing) - e_\theta(z_t, \varnothing, \varnothing)\big) + s_T\big(e_\theta(z_t, c_I, c_T) - e_\theta(z_t, c_I, \varnothing)\big). The first difference is the push toward keeping the input image, scaled by sIs_I. The second is the push toward the instruction, scaled by sTs_T. The worked numbers above use exactly this formula.

Step 4 in symbols: why the decomposition

Appendix B writes the posterior as P(z∣cT,cI)∝P(cT∣cI,z) P(cI∣z) P(z)P(z \mid c_T, c_I) \propto P(c_T \mid c_I, z)\, P(c_I \mid z)\, P(z). Taking the gradient of its logarithm gives one term for each factor, and each guidance term is an estimate of one of them.

Step 5 in symbols: the paired data filter

A generated pair is kept if all three CLIP checks pass: image-to-image similarity of at least 0.75, image-to-caption similarity of at least 0.2, and directional similarity of at least 0.2. Directional similarity compares the change between the two images in CLIP space with the change between the two captions, so it tests that the picture changed the way the text changed.

Results

The paper reports results mostly as images and trade-off plots rather than one headline score. The authors show edits to real photographs and artworks, including replacing objects, changing seasons and weather, changing the artistic medium, and changing the setting. They compare with SDEdit and Text2Live on the same inputs. SDEdit does well when the content stays the same and the style changes, but it struggles to keep the identity of objects and needs a full output description. Text2Live works for additive layer edits but is limited in which edits it can express.

The quantitative comparison with SDEdit (Figure 8) plots CLIP image similarity against directional similarity. At the same directional similarity, InstructPix2Pix keeps higher image consistency, which means it changes what the instruction asks and leaves the rest of the picture alone. The ablations (Figure 10) show that a smaller training set mostly loses the ability to make large edits, and that removing the CLIP filter lowers image consistency.

The limits are stated plainly. The model is limited by the quality of the synthetic images from Stable Diffusion, and by the human-written instructions, GPT-3, and Prompt-to-Prompt that produced the data. It struggles with counting numbers of objects and with spatial reasoning, such as "move it to the left of the image" or "swap their positions". Figure 13 also shows failures with viewpoint changes and with excessive edits. The authors also note that biases in the data and the pretrained models carry into the edits.

Reading guide

  • Section 1, Introduction and Figure 1: the task, in one picture. No prerequisites beyond the abstract.
  • Section 2, Prior work: the older editing methods. Read Diffusion Model first if the phrase "diffusion-based" is new.
  • Section 3.1, Generating a multi-modal training dataset: needs Language Model and Fine-Tuning for 3.1.1, and Latent Diffusion Model for the rendering in 3.1.2. The CLIP filter uses the scores from CLIP.
  • Section 3.2, InstructPix2Pix: needs Latent Diffusion Model and U-Net for the network, and Variational Autoencoder for the latent encoder.
  • Section 3.2.1, Classifier-free guidance for two conditionings: needs Classifier-Free Guidance. Figure 4 is the easiest place to see the two scales at work.
  • Section 4, Results and 4.1 comparisons: the trade-off plot uses Cosine Similarity between CLIP embeddings, so read that first.
  • Appendix A and B: implementation settings and the guidance derivation. Read after the body.

Check yourself

QuestionWhy does the paper generate training data with two pretrained models instead of collecting real before-and-after photos?Show answer

Real photo pairs that differ by exactly one described edit are very hard to find at scale. The authors use a language model to write instructions and edited captions, and a text-to-image model to render the matching images, so they get over 450,000 examples. Prompt-to-Prompt and the CLIP filter then make the pairs consistent enough to supervise an editor.

QuestionUsing the guidance numbers from How it works, but with s_I = 1.0 and s_T = 7.5, what is the combined prediction?Show answer

Start from e(∅,∅)=0.0e(\varnothing,\varnothing) = 0.0. The image term is 1.0×(1.0−0.0)=1.01.0 \times (1.0 - 0.0) = 1.0, and the instruction term is 7.5×(1.2−1.0)=1.57.5 \times (1.2 - 1.0) = 1.5. The total is 0.0+1.0+1.5=2.50.0 + 1.0 + 1.5 = 2.5.

QuestionThe paper drops each condition at random during training. Why does a single network then support guidance with both conditions?Show answer

Because the same network has seen conditional, image-only, instruction-only and fully unconditional inputs, it can estimate each of the needed predictions separately. At inference the differences between those estimates give the two guidance terms, so one trained model supports both scales.

QuestionA user asks the editor to 'put two cups on the table and one on the chair' and gets three cups on the table. Which limit in the paper explains this, and what would you change?Show answer

The paper says the model struggles with counting objects and with spatial reasoning, both inherited from the image model and the prompt-editing method. A fix would need better counting and spatial supervision in the training data, or a different conditioning mechanism; more guidance scale alone does not address counting.

Sources

Read the full paper

Tim Brooks, Aleksander Holynski, Alexei A. Efros, 2022. arXiv:2211.09800