# InstructPix2Pix: Learning to Follow Image Editing Instructions

Edits images from written instructions by training on generated before-and-after pairs

Tim Brooks, Aleksander Holynski, Alexei A. Efros · arXiv · 2022-11-17

- **Problem** Instruction-based image editing needs before-and-after image pairs with matching instructions. Such pairs are hard to collect at scale, so earlier methods lacked training data.
- **Method** InstructPix2Pix learns edits from generated image pairs made by GPT-3 and Stable Diffusion.
- **Results** The model edits real photographs from human-written instructions. At matched directional CLIP similarity, it keeps higher CLIP image similarity than SDEdit in the reported sweep.

## Why paired editing data is scarce

Training a model to edit an image from a written instruction calls for examples of the same scene before and after the change, and such triples are difficult to obtain at scale. The authors therefore treat the task as supervised learning: they first build a paired dataset of instructions and image pairs, and then train an image editing diffusion model on that dataset.

The trained model is expected to act on real photographs and on instructions written by people, even though every training example was generated.

The data strategy depends on complementarity: a language model is strong at describing what changes in words, and a text-to-image model turns descriptions into images. Combining the two gives paired examples for a task that spans both modalities, which neither model supplies alone.

## Writing instructions and captions with GPT-3

The language model is GPT-3, fine-tuned on editing triplets made of an input caption, an edit instruction and an output caption. The seed set is 700 input captions sampled from LAION-Aesthetics V2 6.5+, for which the authors wrote instructions and output captions by hand. The GPT-3 Davinci model was fine-tuned for a single epoch on this seed set, and the fine-tuned model then writes new instructions and captions for real LAION captions, with duplicate captions and duplicate image URLs excluded.

LAION was chosen for its size, for the range of its content, including proper nouns and popular culture, and for its mix of photographs, paintings and digital artwork. Its captions are noisy and sometimes meaningless, and the authors argue that the noise is reduced by dataset filtering and by classifier-free guidance at training and sampling.

The final corpus of generated instructions and captions contains 454, 445 examples.

## Rendering caption pairs into image pairs

A text-to-image model gives no guarantee that two close prompts yield related images, so a small wording change can alter the whole picture. Prompt-to-Prompt reduces this by sharing cross-attention weights between the two generations over part of the denoising process, which pushes the two images to stay similar.

The fraction p of denoising steps with shared attention controls how similar the pair is, and the right amount depends on the edit: changes to large-scale structure need less similarity. Because the best p is hard to infer from the captions and the edit text, the method generates 100 sample pairs per caption pair, each with p drawn from \\(U(0.1, 0.9)\\).

The samples are then scored with the directional CLIP similarity of Gal et al., which checks whether the change between the two images agrees with the change between the two captions. This filter keeps the pairs diverse and sound even when Prompt-to-Prompt or Stable Diffusion fails on a particular pair. The method description places the resulting training set at over 450,000 examples.

## The conditional diffusion model

InstructPix2Pix is built on Stable Diffusion, a large latent text-to-image diffusion model. Latent diffusion works in the latent space of a pretrained variational autoencoder with encoder \\(E\\) and decoder \\(D\\), so the noising acts on the latent \\(z = E(x)\\) rather than on pixels.

The network \\(\\epsilon\_\\theta\\) predicts the noise added to the noisy latent \\(z\_t\\), given the encoded input image \\(E(c\_I)\\) and the text instruction \\(c\_T\\) as conditions.

To accept the input image, the model adds input channels to the first convolutional layer and concatenates \\(z\_t\\) with \\(E(c\_I)\\); the weights acting on these new channels start at zero. Other weights start from a pretrained Stable Diffusion checkpoint, and the instruction goes through the text conditioning path that Stable Diffusion already uses for captions.

This starting point follows Wang et al., who report that fine-tuning a large image diffusion model beats training from scratch for image translation when paired data is limited.

## Two guidance scales at inference

Classifier-free guidance trains one network for both conditional and unconditional denoising, and at sampling time it pushes the conditional estimate away from the unconditional one. With guidance scale \\(s \\ge 1\\), the adjusted estimate is \\\[ \\tilde{e}\_\\theta(z\_t,c) = e\_\\theta(z\_t,\\varnothing) + s\\,(e\_\\theta(z\_t,c)-e\_\\theta(z\_t,\\varnothing)) \\\]

The score network here has two conditions, so during training the image condition is replaced by the null value for 5% of examples, the instruction is replaced for another 5%, and both are replaced for a further 5%. With two conditions, the adjusted estimate becomes \\\[ \\tilde{e}\_\\theta(z\_t,c\_I,c\_T) = e\_\\theta(z\_t,\\varnothing,\\varnothing) + s\_I\\,(e\_\\theta(z\_t,c\_I,\\varnothing)-e\_\\theta(z\_t,\\varnothing,\\varnothing)) + s\_T\\,(e\_\\theta(z\_t,c\_I,c\_T)-e\_\\theta(z\_t,c\_I,\\varnothing)) \\\]

The first term of the correction moves the estimate from the fully unconditional case toward the image-only case with \\(s\_I\\), and the second moves it toward the case with the instruction added with \\(s\_T\\). Raising \\(s\_T\\) makes the edit stronger, and raising \\(s\_I\\) helps keep the spatial structure of the input image. The authors report that values of \\(s\_T\\) in the range 5-10 and of \\(s\_I\\) in the range 1-1.5 typically give the best results, and they adjust both for each example.

## Qualitative results on real photographs and artwork

The results cover real photographs and artwork and include object replacement, changes of season and weather, background replacement, changes to material appearance and changes of artistic medium. The instructions vary in wording, and they cover style, setting and medium changes as well as more open-ended requests.

SDEdit and Text2Live expect a description of the image or of the edit layer rather than an instruction, so the authors supply them with the after-edit caption. In the side-by-side comparison, SDEdit appears in one configuration conditioned on the output caption and in another conditioned on the edit string.

The authors observe that SDEdit works reasonably when content stays roughly constant and only the style changes, but it struggles to keep identity and individual objects when larger changes are wanted, and it needs a full output description. Text2Live gives convincing results for edits that can be expressed as additive layers, but its formulation limits the categories of edits it can handle.

## Trade-off against SDEdit

The quantitative comparison covers SDEdit only, and Text2Live is compared qualitatively. The sweep fixes text guidance at 7.5, varies \\(s\_I\\) over \[1.0, 2.2\], and varies SDEdit's strength between \[0.3, 0.9\].

The two metrics are CLIP image similarity, which measures agreement with the input image, and the directional CLIP similarity, which measures agreement with the edit. These metrics compete, because a stronger edit moves the output away from the input image.

At the same directional CLIP similarity, the model's CLIP image similarity is clearly higher than SDEdit's, so under this sweep the model keeps more of the input image for a given edit.

## Dataset size and CLIP filtering

The ablation trains variants on the full dataset, on 10% and on 1% of the data, and on the unfiltered data, with \\(s\_T\\) fixed and \\(s\_I\\) swept over \[1.0, 2.2\].

Smaller datasets lose the ability to make larger edits and mostly give subtle or stylistic changes, which keeps CLIP image similarity high while directional CLIP similarity is low.

Removing the CLIP filter lowers the overall consistency of the edited images with the input image. The authors conclude that the proposed configuration performs best, but the results are shown as a sweep plot and the exact gaps are not given in the text.

## Limitations

Generalization to new edits is limited by the human-written instructions used to fine-tune GPT-3, by GPT-3's ability to write instructions and modify captions, and by what Prompt-to-Prompt can modify in generated images.

The model struggles with counting objects and with spatial reasoning, such as moving something to the left of the image or swapping positions, in the same way as Stable Diffusion and Prompt-to-Prompt do. The failure cases include viewpoint changes, edits that are too strong, failure to isolate the named object, and difficulty reorganizing or swapping objects.

The edited images may inherit biases from the data and pretrained models, such as correlations between profession and gender.

## Paper info

- **Authors**: Tim Brooks, Aleksander Holynski, Alexei A. Efros
- **Venue**: arXiv · Preprint
- **First published**: 2022-11-17
- **Added**: 2026-11-12
- **Updated**: 2023-1-18
- **Version**: v2
- **arXiv**: 2211.09800
- **Category**: cs.CV · cs.AI · cs.CL · cs.GR · cs.LG
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2211.09800v2) · [PDF ↗](https://arxiv.org/pdf/2211.09800v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Tim Brooks, Aleksander Holynski, Alexei A. Efros (2022). InstructPix2Pix: Learning to Follow Image Editing Instructions. arXiv:2211.09800v2. https://arxiv.org/abs/2211.09800v2

**BibTeX**

```
@misc{brooks2022instructpix2pix,
  title = {{InstructPix2Pix: Learning to Follow Image Editing Instructions}},
  author = {Tim Brooks and Aleksander Holynski and Alexei A. Efros},
  year = {2022},
  eprint = {2211.09800},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2211.09800v2}
}
```

## Other paper commentary

- [MemNet: A Persistent Memory Network for Image Restoration](https://refivu.com/en/papers/80fb0022-b1b0-497f-8285-0c27e8c156fc-memnet-a-persistent-memory-network-for-image-restoration.md)
- [Unsupervised Learning of Video Representations using LSTMs](https://refivu.com/en/papers/a715f01b-843f-4405-90d9-dbb255bc47c5-unsupervised-learning-of-video-representations-using-lstms.md)
- [Expressive Body Capture: 3D Hands, Face, and Body from a Single Image](https://refivu.com/en/papers/13b094cd-136a-43d9-a552-fafa9aa19691-expressive-body-capture-3d-hands-face-and-body.md)
- [WIDER FACE: A Face Detection Benchmark](https://refivu.com/en/papers/f25dbb8a-9814-427e-8059-2cad94728cee-wider-face-a-face-detection-benchmark.md)
- [End-to-end Recovery of Human Shape and Pose](https://refivu.com/en/papers/da029864-5c93-4c98-a8b5-99aa97d36118-end-to-end-recovery-of-human-shape-and.md)
- [Vision Mamba: Efficient Visual Representation Learning with Bidirectional State Space Model](https://refivu.com/en/papers/178fce5e-d4f5-44ae-8265-df3fb75ee87b-vision-mamba-efficient-visual-representation-learning-with.md)
- [Curiosity-driven Exploration by Self-supervised Prediction](https://refivu.com/en/papers/93936438-d2c7-4a12-b814-3073185546ca-curiosity-driven-exploration-by-self-supervised-prediction.md)
- [FFA-Net: Feature Fusion Attention Network for Single Image Dehazing](https://refivu.com/en/papers/05326df3-34c3-4503-97d2-d46822e1fbfa-ffa-net-feature-fusion-attention-network-for-single.md)
- [Adversarial Examples: Attacks and Defenses for Deep Learning](https://refivu.com/en/papers/757e3533-c849-4349-8fbc-e8ea84c0b042-adversarial-examples-attacks-and-defenses-for-deep-learning.md)
- [SpanBERT: Improving Pre-training by Representing and Predicting Spans](https://refivu.com/en/papers/11f04ed9-a517-4d5d-a835-58a3e13f7118-spanbert-improving-pre-training-by-representing-and.md)

## Terms in this paper

- [classifier-free guidance](https://refivu.com/en/terms/7f72cec70f8e-classifier-free-guidance)
- [GPT-3](https://refivu.com/en/terms/d95e818ac9ce-gpt-3)
- [Stable Diffusion](https://refivu.com/en/terms/14d619b23a70-stable-diffusion)
- [Latent diffusion](https://refivu.com/en/terms/777db0c80b01-latent-diffusion)
- [SDEdit](https://refivu.com/en/terms/708d7c473fd5-sdedit)
- [Prompt-to-Prompt](https://refivu.com/en/terms/f6e66b1809fb-prompt-to-prompt)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/b87997ee-e5f2-4a2e-9b66-e15df3a71be7-instructpix2pix-learning-to-follow-image-editing)
