Skip to content
BIASED

Point-based editing: DragGAN and DragDiffusion

Dragging points on an image by optimizing a latent code — in StyleGAN (DragGAN) and in a noisy Stable Diffusion latent (DragDiffusion), with inversion of real photos.

Grab a point and drag

The topic Latent diffusion ended with a working text-to-image model: a frozen autoencoder, DDPM in its latent space, a prompt read through cross-attention, and DDIM for speed. A prompt is good at saying what is in the picture. It is a blunt tool for saying where. "Turn the head a little to the left" or "move the paw 40 pixels down" rarely comes out right.

Point-based editing gives a direct handle instead. The user clicks a few handle points pip_i on the image and, for each one, a target point gig_i where that bit of content should end up. Optionally they paint a mask MM: the region that may change, with M=1M = 1 inside and M=0M = 0 outside. A good edit is

  • precise: the content under each pip_i ends up at gig_i;
  • general: it works for faces, cars, cats and landscapes, without a separate tool for each;
  • faithful: everything else stays as it was, and whatever the moved object uncovers is filled in plausibly.

Simply warping the pixels fails the last test. Rotate a head, and the ear that was hidden behind it has to come from somewhere. A generative model knows what such regions usually contain. So both methods in this topic move content in the latent space of a generator and let the generator redraw the image. DragGAN (Pan et al., 2023) does this with a GAN. DragDiffusion (Shi et al., 2024) carries the same idea over to latent diffusion.

A generator with a layout knob

DragGAN needs a generator whose code controls layout separately from appearance. StyleGAN2 (Karras et al., 2020) is such a generator. The extension StyleGAN: a style-based generator builds it in detail; here we need only three facts about it.

First, the noise z∼N(0,I)z \sim \mathcal N(0, I) does not enter the image-making network directly. A mapping network turns it into a style code w∈R512w \in \mathbb R^{512}. The synthesis network then grows the image from 4×44 \times 4 to full resolution and reads ww again at every layer. At 512×512512 \times 512 there are 16 such style inputs.

Second, nothing forces those 16 inputs to be equal. Giving each layer its own code yields the extended latent space W+\mathcal W^+:

w=(w1,…,w16),wk∈R512.w = (w_1, \dots, w_{16}), \qquad w_k \in \mathbb R^{512}.

The early, low-resolution layers decide pose and shape. The late layers decide colour and texture. DragGAN optimizes only w1,…,w6w_1, \dots, w_6 and keeps the other codes fixed.

Third, the activations inside the generator are informative. Take the feature map after the sixth synthesis block and resize it bilinearly to the size of the image. Call it F(w)F(w); its feature vector at pixel qq is Fq(w)F_q(w). Features at this depth tell an eye corner from a cheek, and their resolution is still fine enough to locate a point to a pixel. Both steps of DragGAN work on FF.

Motion supervision: a step toward the target

Each DragGAN iteration has two steps. The first one nudges the content around every handle a small step toward its target. Its direction is the unit vector

di=gi−pi∥gi−pi∥2.d_i = \frac{g_i - p_i}{\lVert g_i - p_i \rVert_2}.

Let Ω1(pi,r1)\Omega_1(p_i, r_1) be the pixels within distance r1r_1 of the handle. The motion supervision loss asks the features one step ahead, at q+diq + d_i, to look like the current features at qq:

L(w)=∑i=1n∑q∈Ω1(pi,r1)∥Fq+di(w)−sg[Fq(w)]∥1+λ∥(F(w)−F0)⊙(1−M)∥1.\mathcal L(w) = \sum_{i=1}^{n} \sum_{q \in \Omega_1(p_i, r_1)} \big\lVert F_{q + d_i}(w) - \mathrm{sg}\big[F_q(w)\big] \big\rVert_1 + \lambda \big\lVert \big(F(w) - F^0\big) \odot (1 - M) \big\rVert_1 .

Positions q+diq + d_i usually fall between pixels, so FF is read there by bilinear interpolation.

sg[⋅]\mathrm{sg}[\cdot] is the stop-gradient. In the forward pass it is the identity; in the backward pass its gradient is zero. So the reference FqF_q counts as a constant, and ww is changed only so that the patch at q+diq + d_i takes on the look the patch at qq has now. Without it the loss could also fall by making FqF_q look like Fq+diF_{q+d_i}. In a small one-dimensional example that backward pull even wins, and the gradient points the wrong way.

The second term compares with the features F0F^0 of the image before the edit. Outside the mask it penalizes any change, with weight λ\lambda. At 512×512512 \times 512 the paper uses r1=3r_1 = 3 and λ=20\lambda = 20. One Adam step on w1,…,w6w_1, \dots, w_6 ends this half of the iteration.

Where did the handle go?

After the step the content has moved, but by how much and exactly where? The loss does not say. The image shifts by roughly a pixel, rarely by exactly did_i. If the handle pip_i stayed where it was, the next step would push whatever now sits there instead of the content the user grabbed.

So the second step re-finds each handle. Its fingerprint is its feature in the very first image, fi=Fpi0(w0)f_i = F_{p_i^0}(w_0). In the new image, with features F(w′)F(w'), the method searches a window Ω2(pi,r2)\Omega_2(p_i, r_2) around the old position for the closest feature:

pi←arg⁡min⁡q∈Ω2(pi,r2)∥Fq(w′)−fi∥1.p_i \leftarrow \arg\min_{q \in \Omega_2(p_i, r_2)} \big\lVert F_q(w') - f_i \big\rVert_1 .

This is a plain nearest-neighbour search in the generator's own feature space. No separate optical-flow network is needed.

The two steps alternate. Motion supervision moves the content a little, tracking finds where it went, and motion supervision starts again from the new handle positions. The loop stops when every handle is within 1 pixel of its target, or 2 pixels when there are more than five handles. At 512×512512 \times 512 the paper uses r2=12r_2 = 12. A typical edit takes 30–200 iterations, a few seconds on one GPU.

A real photo

So far we have edited images the GAN generated itself, where ww is known. A user's photo xx comes with no ww. It must first be inverted: we look for a code whose image matches the photo,

w∗=arg⁡min⁡w  ∥G(w)−x∥2+Lperc(G(w),x),w^\ast = \arg\min_{w}\; \big\lVert G(w) - x \big\rVert^2 + \mathcal L_{\text{perc}}\big(G(w), x\big),

where GG is the StyleGAN generator and Lperc\mathcal L_{\text{perc}} is a perceptual loss of the kind the autoencoder in Latent diffusion is trained with.

GAN inversion rarely lands exactly on the photo. The generator draws only what it has learned, and a particular face with its lighting and background is usually a little outside that range. The closest G(w∗)G(w^\ast) looks like a relative rather than the person. Pivotal tuning inversion (PTI; Roich et al., 2022) closes the gap in two stages. First it inverts as above and keeps the result as a pivot wpw_p. Then it freezes wpw_p and fine-tunes the generator weights θ\theta until Gθ(wp)G_\theta(w_p) reproduces the photo; a regularizer keeps the generator's behaviour away from the pivot unchanged. The drag then runs on wpw_p in the tuned generator.

Inversion cannot fix the deepest limit: a GAN cannot leave its training domain. A StyleGAN trained on faces has no code for a cat or a busy street, and a photo far from its data reconstructs poorly however carefully it is inverted. Diffusion models trained on billions of varied images do not have this problem, and that is the motivation for DragDiffusion.

The same loop in a diffusion latent

DragDiffusion (Shi et al., 2024) keeps the DragGAN loop and swaps the generator for Stable Diffusion 1.5 from Latent diffusion. What should play the role of ww?

Not the clean latent z0z_0: there the model has nothing left to redraw, and dragging it works little better than a warp. Not the starting noise zTz_T either: it has no layout left to drag. DragDiffusion edits a single intermediate noisy latent ztz_t. In a 50-step DDIM schedule it takes step t=35t = 35: noisy enough for the model to redraw the layout, clean enough to keep the content. That is 4⋅64⋅64=16 3844 \cdot 64 \cdot 64 = 16\,384 numbers.

The features FF come from the U-Net: the output of its third upsampling block, resized to the image. Motion supervision and point tracking are DragGAN's, with r1=1r_1 = 1, r2=3r_2 = 3 and at most 80 Adam steps. Only the mask term changes: it compares the latent after one denoising step with that of the original.

For a real photo we need its z35z_{35}, and DDIM provides it. Latent diffusion used DDIM to step from noisier to cleaner latents: estimate the clean latent z^0\hat z_0 from ztz_t and εθ(zt,t)\varepsilon_\theta(z_t, t), then jump to another step t′t'. Nothing in that update requires t′<tt' < t. Encode the photo, z0=E(x)z_0 = \mathcal E(x), and run the same update with t′>tt' > t for 35 steps. This is DDIM inversion. It is approximate: each step uses the noise predicted at the current latent, not at the noisier one it is about to reach.

Keeping the photo itself

DDIM inversion is only approximate, and 35 steps of it, followed by 35 steps back, tend to lose some of the photo: a face drifts toward someone similar, a texture changes. DragDiffusion adds two safeguards, one on each side of the edit.

Identity-preserving fine-tuning. This is PTI's job in the diffusion world. Each attention projection matrix WW of the U-Net gets a trainable low-rank correction, LoRA (low-rank adaptation):

W+BA,B∈Rm×r,  A∈Rr×n,  r=16.W + BA, \qquad B \in \mathbb R^{m \times r},\; A \in \mathbb R^{r \times n},\; r = 16 .

Only AA and BB are trained, for about 80 steps, on the input photo alone and with the ordinary noise-prediction loss. A model that has just been fitted to this one image inverts and redraws it far more faithfully.

Reference-latent control. After the latent is optimized, the edited ztz_t is denoised to z0′z_0' and decoded. In parallel, the original ztz_t is denoised too. At every step the U-Net's self-attention on the edited branch takes its keys and values from the original branch. Each position of the edited image can then copy appearance — skin, fur, fabric — from wherever it sits in the original, while the layout is the new one.

DragGAN DragDiffusion
code w1,…,w6w_1, \dots, w_6 noisy latent z35z_{35}
GAN inversion + PTI DDIM inversion + LoRA
features after synthesis block 6 features of the U-Net's third upsampling block
one forward pass to the image denoising with reference-latent control, then D\mathcal D

Measuring it and where it breaks

How do we score a drag? DragBench (Shi et al., 2024) collects 205 images with 349 handle–target pairs in ten categories and reports two numbers. Image fidelity is 1−LPIPS1 - \mathrm{LPIPS} between the original and the edited image; LPIPS is a learned perceptual distance, built later in the course in LPIPS: a perceptual distance. Mean distance is the average distance in pixels between where each handle's content actually ended up and its target.

The two pull against each other. An edit that does nothing has perfect fidelity and a poor mean distance. A heavy-handed edit can reach every target and wreck the picture. Either number alone can be gamed.

The methods also share their failure modes.

  • Tracking needs distinctive features. On a repetitive texture, such as a brick wall or a striped shirt, the nearest neighbour can jump to the next brick or stripe. On a texture-less region, such as a plain white wall, there is nothing to match.
  • Sparse handles are ambiguous. Dragging a chin point down may open the mouth, stretch the face or tilt the head. The mask and extra handles narrow the choice.
  • GAN versus diffusion. A GAN cannot leave its training domain. Diffusion is general but slower: LoRA fine-tuning and inversion are paid for on every photo.
  • Misuse. Both make it easy to change a real person's pose or expression in a photo. That is a reason to label edited images.

Step back once more. Both methods edit through a code that describes the whole image at once, StyleGAN's ww or the noisy latent ztz_t, and every pixel is redrawn together. Latent diffusion on the main line generates that way as well: each step denoises the whole latent in one go. The main line continues with Autoregressive models, the oldest family with an exact likelihood. It builds an image one piece at a time, each piece conditioned on the ones before it.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.