r/MachineLearning • u/Illustrious_Row_9971 • Sep 05 '21
Project [P] CLIP Guided Diffusion: Generates images from text prompts Web Demo
15
7
4
u/best_damn_milkshake Sep 06 '21
Can someone explain to me what’s going on here in simple terms
6
Sep 06 '21 edited Sep 06 '21
text goes in, image comes out
in this case the text was "coral reef city by artstation artists"
CLIP by itself simply takes an image/text pair and scores how much the image fits the text.
But CLIP is differentiable, and so are image generation models. So you can set the text and optimize for the image that best fits the text. This way CLIP can be used to guide a myriad of image models, or even optimize pixels directly.
"artstation" is used here to make the output cooler, because CLIP has associated Artstation with cool art.
9
u/dogs_like_me Sep 06 '21 edited Sep 06 '21
CLIP is a model that you can give an image or text as input, and it will represent them in the same space. Concretely, by "same space" I mean if the image contents and text contents are similar, their respective CLIP representations should be near each other.
This is a really useful property that has a lot of powerful consequences. You can use this as a "zero-shot" model: just come up with a set of phrases describing the possible classes an image could fall into, and you classify the image based on which phrase has the "closest" representation.
If we convert that distance to a "similarity" in the range [0,1], we can pretend we're working with probabilities. A classifier like this that returns a probability can be interpreted as calculating the pdf of the distribution: P(Image|Phrase), read as "probability of 'Image' given 'Phrase'." In the zero-shot case above, we have multiple phrases and are trying to see which phrase maximizes that likelihood with respect to our image.
If we have a mechanism for proposing "candidate" samples, we can sample from any probability distribution. This includes a distribution over images. We can then use this sampling procedure to try to estimate the modes of the distribution, i.e. where the PDF is highest. If you can picture a bell curve, the "mode" is just a fancy way for saying the peak of a distribution: for a normal distribution, it's the top of that bell. It's the area of the distribution where if I show you a sample from it, you are the least surprised to learn that it came from the distribution.
Tying everything together: if we can sample from the distribution P(Image|Phrase), then that means if we focus on a single phrase, we can find images that are "close" to it in the same kind of "ok, not surprised to see that image paired with that phrase" kind of way.
A "diffusion" model is a type of generative model that has been increasingly gaining popularity over the last few years. If you're familiar with Generative Adversarial Networks (GANs), diffusion models can be used in a similar way to generate images. We can pair this procedure with CLIP: if we feed CLIP a phrase, we can use it to push the generative model towards P(Image|OurPhraseThanksCLIP).
In OPs video, we're seeing images produced by a generative model, OP's "diffusion" model. The model is initialized randomly and starts out giving us nonsense. OP gave CLIP a phrase, and is using it to "guide" the diffusion model towards an image near an associated mode.
Or more succinctly: CLIP-Guided diffusion generates images from text prompts.
1
u/Coffee_Cute_ Sep 06 '21
A software program has alot of pictures saved and labeled. When you enter a phrase like "blue bannana", it will search the words and try to make an image. It will take 'blue' and find images that have that label, and will do the same for "banana". It takes samples out of a couple photos, then fills in the rest of the image using random photos. Its like putting a dab of paint on a portrait then finishing the painting. The pixels fading in and out are just to make it look cool.
1
u/Ehiltz333 Apr 20 '22
I know this is an 8-month old thread, but I'd like to make sure nobody gets the wrong idea from this comment: this is not how CLIP-guided Diffusion works. This comment makes it seem like all the program does is interpolate between images, which is absolutely not how it works.
CLIP-guided Diffusion uses starting noise, paired with a diffusion model which is used to increase the sharpness of an image. CLIP, an image classification AI, is used to score each step of the process based on how likely it is to be classified under the prompt. At the beginning, the noise has a very low score, because it is nowhere close to the "LSD Coral Reef" prompt the OP gave. By the end, you can see that it is much closer to matching the prompt.
Every step, CLIP guides the diffusion model towards an image that gives a higher score (hence, CLIP-guided Diffusion). This is kind of like sculpting a figure out of marble. The starting noise is the uncarved marble, and CLIP tells the diffusion model where to carve to get it closest to a figure at each step.
The parent comment implies that the noise is all for show, and that it's just trickery that blurs between a few images and calls it a day. This is so far from the truth, and shows that the parent commenter has never used CLIP-guided Diffusion, and may even be actively against it. The noise that they claim is "just to make it look cool" is actually a vital part of the process, and their explanation shows they know nothing about it.
1
u/number77s Aug 22 '22
Thanks for the great explanation. Much cooler than how I originally thought it was done.
2
Sep 05 '21
Can it make people ? Or faces ?
3
Sep 06 '21
From my experience CLIP can make weird face-like and humanoid figures, but nothing too precise, unless your pair it with a GAN trained on faces of course.
1
1
1
1
1
1
1
u/bogoramondo Oct 09 '21
What settings did you use? How many batches, batch size etc? Could you share?
1
39
Sep 05 '21
[removed] — view removed comment
13
u/Illustrious_Row_9971 Sep 05 '21
same as the example in the huggingface gradio web demo "coral reef city by artistation artists"
105
21
u/Illustrious_Row_9971 Sep 05 '21 edited Sep 06 '21
colab: https://colab.research.google.com/drive/12a_Wrfi2_gwwAuN3VvMTwVMz9TfqctNj
huggingface Gradio Demo: https://huggingface.co/spaces/akhaliq/clip-guided-diffusion
guided diffusion github: https://github.com/crowsonkb/guided-diffusion
clip github: https://github.com/openai/CLIP
gradio github: https://github.com/gradio-app/gradio
huggingface spaces: https://huggingface.co/spaces