"vscode:/vscode.git/clone" did not exist on "cd6d113874119c2bde7262c27421268b86f2e9de"
unconditional_image_generation.md 2.23 KB
Newer Older
Aryan's avatar
Aryan committed
1
<!--Copyright 2025 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

Steven Liu's avatar
Steven Liu committed
17
Unconditional image generation generates images that look like a random sample from the training data the model was trained on because the denoising process is not guided by any additional context like text or image.
Patrick von Platen's avatar
Patrick von Platen committed
18

Steven Liu's avatar
Steven Liu committed
19
To get started, use the [`DiffusionPipeline`] to load the [anton-l/ddpm-butterflies-128](https://huggingface.co/anton-l/ddpm-butterflies-128) checkpoint to generate images of butterflies. The [`DiffusionPipeline`] downloads and caches all the model components required to generate an image.
Patrick von Platen's avatar
Patrick von Platen committed
20

Steven Liu's avatar
Steven Liu committed
21
22
23
24
25
26
27
```py
from diffusers import DiffusionPipeline

generator = DiffusionPipeline.from_pretrained("anton-l/ddpm-butterflies-128").to("cuda")
image = generator().images[0]
image
```
28
29
30

<Tip>

Steven Liu's avatar
Steven Liu committed
31
Want to generate images of something else? Take a look at the training [guide](../training/unconditional_training) to learn how to train a model to generate your own images.
32
33
34

</Tip>

Steven Liu's avatar
Steven Liu committed
35
The output image is a [`PIL.Image`](https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=image#the-image-class) object that can be saved:
36

Steven Liu's avatar
Steven Liu committed
37
38
```py
image.save("generated_image.png")
Patrick von Platen's avatar
Patrick von Platen committed
39
```
40

Steven Liu's avatar
Steven Liu committed
41
You can also try experimenting with the `num_inference_steps` parameter, which controls the number of denoising steps. More denoising steps typically produce higher quality images, but it'll take longer to generate. Feel free to play around with this parameter to see how it affects the image quality.
42

Steven Liu's avatar
Steven Liu committed
43
44
```py
image = generator(num_inference_steps=100).images[0]
45
image
Patrick von Platen's avatar
Patrick von Platen committed
46
47
```

Steven Liu's avatar
Steven Liu committed
48
Try out the Space below to generate an image of a butterfly!
Patrick von Platen's avatar
Patrick von Platen committed
49

50
<iframe
Steven Liu's avatar
Steven Liu committed
51
	src="https://stevhliu-unconditional-image-generation.hf.space"
52
53
54
55
	frameborder="0"
	width="850"
	height="500"
></iframe>