What IP-Adapter does
Some things are easier to show than to say: a particular dog, a face, a painter's style. IP-Adapter (Ye et al.) lets a picture act as a prompt for a pretrained text-to-image model, next to the text, without changing that model. A frozen CLIP image encoder sums the picture up in one embedding, a small projection turns the embedding into a few tokens, and every cross-attention of the U-Net gets a second pair of key and value projections that read those tokens beside the text. Ye et al. train only these new parts, about 22 M parameters, on about 10 M image–text pairs from LAION-2B and COYO-700M.
This tab records h94/IP-Adapter's ip-adapter_sd15 on Stable Diffusion 1.5. The photo of a beagle is the image prompt and a dog in the snow, photo the text, drawn at 384 × 384 pixels (a 4 × 48 × 48 latent) with 20 DDIM steps, guidance 7.5 and seed 0. One image-prompt scale, , is recorded in depth; the same seed then runs at five scales from 0 to 1.
The image prompt
The Image prompt panel runs the photo through the image half of OpenCLIP ViT-H/14, frozen. CLIP's processor resizes the photo's shorter side to 224 pixels and keeps the centre 224 × 224, which the encoder cuts into 16 × 16 patches of 14 pixels. A learned class token joins the 256 patch tokens, and 32 transformer layers, 1280 wide, let every token attend to every other. The class token's final state, normalised and projected, is the 1024-number image embedding: the vector that CLIP's contrastive training pulls towards the embedding of the image's caption. The overlay shows where the class token attends in the last layer, averaged over heads.
Ye et al. chose this global embedding because it is aligned with captions and carries the image's content and style. It is a summary, though, not a map: it says much more about what is in the picture than about where.
Four image tokens
The U-Net's cross-attentions read tokens 768 wide, the width of SD 1.5's text encoder. The Image tokens panel shows how one 1024-number embedding becomes four of them: one linear layer to numbers, cut into four rows, each LayerNormed:
These four tokens are all the U-Net ever sees of the photo. The panel's second button shows the tokens of guidance's empty pass, which gets an all-zero embedding. A zero embedding does not give zero tokens: , so the four tokens are the LayerNorm of the projection's bias, a fixed set of tokens that the model learned to read as no image.
Decoupled cross-attention
In SD 1.5 each cross-attention lets the latent's pixels, as queries , read the 77 text tokens through keys and values (see the SD 1.5 tab). The obvious way to add an image would be to append its tokens to the text's, through the same projections and one softmax. Ye et al. found that insufficient, and give the image its own projections and its own softmax, with the same queries:
, and stay frozen; and (to_k_ip and to_v_ip) are the adapter's only weights inside the block, and Ye et al. start them from copies of and to speed up training. Because the image's softmax runs over just four tokens, each pixel's four image weights always sum to 1: attention decides which image token a pixel reads, not whether it reads the image at all. How much the image changes the block's output is set by and by the size of .
The Decoupled cross-attention panel opens one block, up.1.transformer.0 (12 × 12 pixels, 8 heads), in the prompt pass: pick a pixel and a head to see its weights over the text tokens and over the four image tokens, and each branch's output and their sum as maps. Image tokens in every cross-attention marks SD 1.5's 16 cross-attentions (6 down, 1 in the middle, 9 up), each with its own and , all reading the same four tokens; each block's badge is the size (RMS) of its image branch times as a share of its text branch. Where each image token lands draws, for a chosen block, each image token's weight over the pixels, averaged over heads.
Guidance and the image-prompt scale
For classifier-free guidance Ye et al. drop the text, the image or both during training, each with probability 0.05, and a dropped image is a zero CLIP embedding. At sampling the empty pass gets the empty prompt and the zero embedding, the other pass the prompt and the photo's embedding :
So guidance pushes towards the text and the image together. The Guidance panel shows both predictions and the result; each Sampling loop step runs the U-Net twice, then takes SD 1.5's DDIM step.
weighs the image branch in every cross-attention at once. At the branch vanishes and the model is SD 1.5 exactly: the recorder checks that scale 0 reproduces SD 1.5's own image. Ye et al. use for an image prompt alone and lower it to balance an image prompt against a text prompt. The Image-prompt scale λ panel shows the final image at and with the same prompt and seed, and Same seed, three image-prompt scales sets at every step side by side for , and .
In this run, SD 1.5 alone () draws a dark dog in a red hat in the snow. From the dog takes the beagle's tan and white, and at most of the snow gives way to green like the photo's garden and the dog's mouth opens as the beagle's does: the image prompt starts to override the text. The badges show that the image branch is no small correction: even at its contribution is larger than the text branch's in 13 of the 16 blocks at and in 11 at and .
Where IP-Adapter came from
Generating from a CLIP image embedding goes back to DALL·E 2 (unCLIP, Ramesh et al.), whose decoder turns an image embedding into images. Later image prompts either fine-tuned Stable Diffusion itself on image embeddings or, like T2I-Adapter's style mode, added an image condition to a frozen model. Because IP-Adapter leaves the base model untouched, Ye et al. note that one trained adapter can be reused on models fine-tuned from the same base and combined with ControlNet: the ControlNet tab adds layout through the U-Net's skip connections, IP-Adapter adds content through its cross-attentions. The Inpainting and Pix2Pix tabs go the other way: to edit a given photo rather than borrow from it, they hand the U-Net the photo's latent as extra input channels.
Papers
- IP-Adapter: Text Compatible Image Prompt Adapter for Text-to-Image Diffusion Models IP-Adapter: the projection to image tokens, decoupled cross-attention and the image-prompt scale this tab records.
- Learning Transferable Visual Models From Natural Language Supervision CLIP: an image encoder and a text encoder trained into one embedding space, the source of the image embedding here and of SD 1.5's text encoder.
- Reproducible scaling laws for contrastive language-image learning OpenCLIP trained on public LAION data, which produced the ViT-H/14 image encoder that IP-Adapter reads.
- Hierarchical Text-Conditional Image Generation with CLIP Latents unCLIP (DALL·E 2): a decoder that generates images from CLIP image embeddings, the earlier way to prompt with a picture.
- High-Resolution Image Synthesis with Latent Diffusion Models Latent diffusion and the U-Net's cross-attention to text, which IP-Adapter gives a second branch.
- Classifier-Free Diffusion Guidance The guidance mix, here with an empty prompt and a zero image embedding in the unconditional pass.
- Denoising Diffusion Implicit Models DDIM, the sampler of the recorded run.