unconditional_image_generation.md 2.6 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
# Unconditional image generation
Patrick von Platen's avatar
Patrick von Platen committed
14

15
[[open-in-colab]]
Patrick von Platen's avatar
Patrick von Platen committed
16

17
Unconditional image generation is a relatively straightforward task. The model only generates images - without any additional context like text or an image - resembling the training data it was trained on.
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
Start by creating an instance of [`DiffusionPipeline`] and specify which pipeline checkpoint you would like to download.
22
23
24
25
You can use any of the 🧨 Diffusers [checkpoints](https://huggingface.co/models?library=diffusers&sort=downloads) from the Hub (the checkpoint you'll use generates images of butterflies).

<Tip>

26
💡 Want to train your own unconditional image generation model? Take a look at the training [guide](../training/unconditional_training) to learn how to generate your own images.
27
28
29
30

</Tip>

In this guide, you'll use [`DiffusionPipeline`] for unconditional image generation with [DDPM](https://arxiv.org/abs/2006.11239):
31
32

```python
33
from diffusers import DiffusionPipeline
34

35
generator = DiffusionPipeline.from_pretrained("anton-l/ddpm-butterflies-128", use_safetensors=True)
36
```
37

38
The [`DiffusionPipeline`] downloads and caches all modeling, tokenization, and scheduling components. 
39
40
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:
41
42

```python
43
generator.to("cuda")
Patrick von Platen's avatar
Patrick von Platen committed
44
```
45

46
Now you can use the `generator` to generate an image:
47
48

```python
49
50
image = generator().images[0]
image
Patrick von Platen's avatar
Patrick von Platen committed
51
52
```

53
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
54

55
You can save the image by calling:
56
57

```python
58
image.save("generated_image.png")
59
```
Patrick von Platen's avatar
Patrick von Platen committed
60

61
Try out the Spaces below, and feel free to play around with the inference steps parameter to see how it affects the image quality!
Patrick von Platen's avatar
Patrick von Platen committed
62

63
64
65
66
67
68
<iframe
	src="https://stevhliu-ddpm-butterflies-128.hf.space"
	frameborder="0"
	width="850"
	height="500"
></iframe>