conditional_image_generation.md 2.35 KB
Newer Older
Patrick von Platen's avatar
Patrick von Platen committed
1
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
Nathan Lambert's avatar
Nathan Lambert committed
2
3
4
5
6
7
8
9
10
11
12

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

13
14
15
16
17
# Conditional image generation

[[open-in-colab]]

Conditional image generation allows you to generate images from a text prompt. The text is converted into embeddings which are used to condition the model to generate an image from noise.
Patrick von Platen's avatar
Patrick von Platen committed
18

19
The [`DiffusionPipeline`] is the easiest way to use a pre-trained diffusion system for inference.
Patrick von Platen's avatar
Patrick von Platen committed
20

21
22
Start by creating an instance of [`DiffusionPipeline`] and specify which pipeline [checkpoint](https://huggingface.co/models?library=diffusers&sort=downloads) you would like to download.

Steven Liu's avatar
Steven Liu committed
23
In this guide, you'll use [`DiffusionPipeline`] for text-to-image generation with [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5):
24
25
26
27

```python
>>> from diffusers import DiffusionPipeline

Steven Liu's avatar
Steven Liu committed
28
>>> generator = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
Patrick von Platen's avatar
Patrick von Platen committed
29
```
30

31
The [`DiffusionPipeline`] downloads and caches all modeling, tokenization, and scheduling components. 
32
33
Because the model consists of roughly 1.4 billion parameters, we strongly recommend running it on a GPU.
You can move the generator object to a GPU, just like you would in PyTorch:
34
35
36

```python
>>> generator.to("cuda")
Patrick von Platen's avatar
Patrick von Platen committed
37
38
```

39
Now you can use the `generator` on your text prompt:
Patrick von Platen's avatar
Patrick von Platen committed
40

41
42
43
44
```python
>>> image = generator("An image of a squirrel in Picasso style").images[0]
```

45
The output is by default wrapped into a [`PIL.Image`](https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=image#the-image-class) object.
Patrick von Platen's avatar
Patrick von Platen committed
46

47
You can save the image by calling:
Patrick von Platen's avatar
Patrick von Platen committed
48

49
50
51
```python
>>> image.save("image_of_squirrel_painting.png")
```
52
53
54
55
56
57
58
59
60

Try out the Spaces below, and feel free to play around with the guidance scale parameter to see how it affects the image quality!

<iframe
	src="https://stabilityai-stable-diffusion.hf.space"
	frameborder="0"
	width="850"
	height="500"
></iframe>