r/StableDiffusion Sep 10 '22

Prompt-to-Prompt Image Editing with Cross Attention Control in Stable Diffusion


View all comments

Show parent comments


u/bloc97 Sep 10 '22

First question: Yes, right now the control mechanisms are really basic, you have a initial prompt (that you can generate to see what the image looks like), then a second prompt that is an edit of the first. The algorithm will generate your second prompt so that it looks as "close" as possible to the first (with the concept of closeness being encoded inside of the network). You can also tweak the weights of each token, such that you can reduce or increase its contribution on the final image (e.g you want less clouds, more trees). Note that tweaking the weights in attention space gives much better results than editing the prompt embeddings, as the prompt embeddings are highly nonlinear and often editing them will break the image.

Second question: Yes, but not right now. What everyone is using as "img2img" is actually a crude approximation of the correct "inverse" process for the network (not to be confused with textual inversion). What we actually want for prompt editing is not to add random noise to an image but find which noise will reconstruct our intended image and use that to modify our prompt or generate variations. I was hoping someone would have already implemented it but I guess I can give it a try when I have more time.

Also, because stable diffusion is slightly different to what I guess was Imagen used in the paper, we have a second self-cross-attention layer, which can be controlled by using an additional mask (that is not yet implemented right now), that means that if image inversion is implemented correctly, we could actually "inpaint" using the cross-attention layers themselves and modify the prompt, this should give us much better results than simply masking out the image and adding random noise...

Exciting times ahead!


u/Zertofy Sep 10 '22

Cool! Also, is it take the same time to generate as usual image? Probably yes, but just to be sure. Some time before I see post here about video editing, and one of the problems was the lack of consistency between frames. I proposed use of the same seed, but it give only partial result. May this technology be the missing element for this?

Anyway, it's really exciting to see how people explore and upgrade SD in real time. Wish you success i quess


u/bloc97 Sep 10 '22

It is slightly slower, because instead of 2 u-net calls, we need 3 for the edited prompt. For video, I'm not sure if this can achieve temporal consistency, as the latent space is way too nonlinear, even with cross-attention control you don't always get exactly the same results (eg. backgrounds, trees, rocks might change shape when you are editing the sky). I think hybrid methods (that are not purely end-to-end) will be the way forward for video generation. (eg. augmenting stablediffusion with depth prediction and motion vector generation)


u/TiagoTiagoT Sep 10 '22

Would it be possible to somehow freeze stuff that is not identified by the AI as being what is being changed, sorta like masking but at a deeper level and done automatically?