↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What InstructPix2Pix does

Editing a photo with a text-to-image model usually means describing the whole result. InstructPix2Pix (Brooks, Holynski and Efros) edits by instruction instead: give it a photo and turn the dog into a tiger, and it draws the edited photo in one sampling run, with no inversion of the photo and no fine-tuning per photo. It is SD 1.5's U-Net fine-tuned to read the photo beside the noisy latent. The hard part was the data: no large collection of photos, instructions and edited photos existed, so Brooks et al. generated one with two other models.

This tab records timbrooks/instruct-pix2pix editing the beagle photo by turn the dog into a tiger at 256 × 256 pixels (a 4 × 32 × 32 latent), the resolution the model was trained at, with 20 Euler ancestral steps, text guidance 7.5, image guidance 1.5 and seed 0; the same seed then runs at 16 pairs of the two guidance scales.

A dataset made by two models

Brooks et al. fine-tuned GPT-3 on 700 human-written triplets: a caption from LAION-Aesthetics V2 6.5+, an edit instruction, and the caption after the edit. The fine-tuned model then wrote instructions and edited captions for many more captions. To turn each pair of captions into a pair of images they used Stable Diffusion with Prompt-to-Prompt (Hertz et al.), which generates both captions from the same noise and shares their cross-attention maps for a fraction of the denoising steps, so that the two images agree except where the captions differ. For each caption pair they sampled 100 image pairs with a random fraction of shared steps and kept those that passed CLIP-based filters, among them a directional similarity that asks whether the change between the two images in CLIP space matches the change between the captions. The paper reports 454,445 examples in the end.

So the model never saw a real photo being edited in training; Brooks et al. report that it still edits real photos by instructions people write, and works at 512 × 512 though trained at 256 × 256.

Eight input channels

The Photo → latent panel encodes the photo with the model's VAE encoder. The pipeline passes the mode of the latent distribution and does not multiply it by 0.18215, because the model was fine-tuned on latents prepared that way; the noisy latent beside it is in SD 1.5's usual scaled units. The photo's latent is read unchanged at every step; only the noisy half moves.

The U-Net's input stacks the two on channels, , 8 channels in all (the scaling of is the sampler's, below). As on the Inpainting tab, only conv_in changes shape, 8 → 320. Brooks et al. started from SD 1.5's weights with the weights on the four new channels at zero, so fine-tuning began from SD 1.5 exactly, ignoring the photo. The Eight input channels panel splits conv_in's output into what each half contributes at the chosen step, with bars for the size of the weights on each input channel.

The instruction goes in by cross-attention, as a prompt does in SD 1.5. The Instruction panel shows CLIP's text encoder output for it and for the empty prompt, and Where each word lands maps, for a chosen transformer block, each instruction token's cross-attention weight over the pixels, averaged over heads and scaled to its own maximum.

Text and image guidance

Brooks et al. drop the photo from 5 % of training examples, the instruction from 5 % and both from 5 %, so one network learns all three conditionings; in the pipeline a dropped photo is an all-zero latent. Sampling runs three passes, written here as : the instruction with the photo, the empty prompt with the photo, and the empty prompt with no photo. They are combined with two scales:

The paper derives it by writing and scaling the two conditional terms separately: sets how closely the edit keeps to the photo, how strongly it follows the instruction. At the first two terms add up to and the formula is ordinary classifier-free guidance with the photo in both passes, as on the Inpainting and ControlNet tabs. Each step runs the U-Net three times, U-Net ×3 in the Sampling loop.

The Text and image guidance panel shows the three predictions, the two differences and the guided result at the chosen step. Both scales is a grid of final edits from the same seed, across and down, and Same seed, three pairs of scales follows through every step for the defaults (7.5, 1.5), less image guidance (7.5, 1) and less text guidance (3, 1.5).

The grid bears the derivation out. At the dog becomes a whole tiger at every text scale, and its open mouth and tongue are gone. At and the beagle keeps its pose and its tongue and only takes a tiger's face and stripes. Raising along a row makes the change stronger and its colours harsher, until the chest turns green, a colour no tiger has.

Euler ancestral sampling

The model's published scheduler is Euler ancestral, from Katherine Crowson's k-diffusion library, in the noise-level form that Karras et al. made common: the noisy latent is , with falling from SD 1.5's 14.61 to 0 over the 20 steps, and the U-Net reads . Each step estimates , takes an Euler step of the probability-flow ODE past the next level down to a lower , then adds fresh noise of size to come back up to :

with standard normal noise. The last step goes to , where and the step lands on . Unlike DDIM with on the other U-Net tabs, the result depends on the noise drawn at every step as well as on the starting latent; seed 0 fixes them all. The Sampling loop shows each step's with its , the guided noise, , the step down to and the fresh noise added back.

Where InstructPix2Pix came from

Earlier ways to edit a real photo with a diffusion model changed it wholesale or needed work for every photo. SDEdit, the Inpainting tab's img2img and Brooks et al.'s baseline, noises the photo part of the way and denoises it with a caption of the result, one strength for the whole image. Prompt-to-Prompt edits by changing a caption while keeping its attention maps, but it works on images the model generated itself; a real photo must first be inverted into the model's noise. InstructPix2Pix turns that pipeline into training data and learns a single model that makes the edit in one sampling run.

It shares its input recipe with the Inpainting tab, which puts a mask and a masked photo in the extra channels, and with latent diffusion's own conditioning by concatenation. The IP-Adapter tab takes a picture in through cross-attention instead.

Papers

  1. InstructPix2Pix: Learning to Follow Image Editing Instructions Tim Brooks, Aleksander Holynski, Alexei A. Efros · CVPR 2023 InstructPix2Pix: the generated dataset, the 8-channel U-Net fine-tuned from SD 1.5 and the two guidance scales this tab records.
  2. Prompt-to-Prompt Image Editing with Cross Attention Control Amir Hertz et al. · ICLR 2023 Editing a generated image by changing its caption while sharing cross-attention maps, which made the before and after images of the training data.
  3. Language Models are Few-Shot Learners Tom B. Brown et al. · NeurIPS 2020 GPT-3, fine-tuned to write the edit instructions and the edited captions.
  4. High-Resolution Image Synthesis with Latent Diffusion Models Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Björn Ommer · CVPR 2022 Latent diffusion, the Stable Diffusion U-Net that InstructPix2Pix fine-tunes, and conditioning by concatenation to its input.
  5. Classifier-Free Diffusion Guidance Jonathan Ho, Tim Salimans · arXiv 2022 Guidance with one condition, which InstructPix2Pix splits into a text scale and an image scale.
  6. Elucidating the Design Space of Diffusion-Based Generative Models Tero Karras, Miika Aittala, Timo Aila, Samuli Laine · NeurIPS 2022 Sampling in σ-space with input scaling, the formulation of the Euler ancestral sampler.
  7. SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations Chenlin Meng et al. · ICLR 2022 Editing by noising a photo part of the way and denoising it with a new caption, the baseline InstructPix2Pix compares with.

See also