conditional_image_generation.mdx 1.96 KB
Newer Older
Nathan Lambert's avatar
Nathan Lambert committed
1
2
3
4
5
6
7
8
9
10
11
12
<!--Copyright 2022 The HuggingFace Team. All rights reserved.

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.
-->

Patrick von Platen's avatar
Patrick von Platen committed
13
14


15
# Conditional Image Generation
Patrick von Platen's avatar
Patrick von Platen committed
16

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

19
20
21
22
23
24
25
26
Start by creating an instance of [`DiffusionPipeline`] and specify which pipeline checkpoint you would like to download.
You can use the [`DiffusionPipeline`] for any [Diffusers' checkpoint](https://huggingface.co/models?library=diffusers&sort=downloads).
In this guide though, you'll use [`DiffusionPipeline`] for text-to-image generation with [Latent Diffusion](https://huggingface.co/CompVis/ldm-text2im-large-256):

```python
>>> from diffusers import DiffusionPipeline

>>> generator = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256")
Patrick von Platen's avatar
Patrick von Platen committed
27
```
28
29
30
31
32
33
The [`DiffusionPipeline`] downloads and caches all modeling, tokenization, and scheduling components. 
Because the model consists of roughly 1.4 billion parameters, we strongly recommend running it on GPU.
You can move the generator object to GPU, just like you would in PyTorch.

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

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

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

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

44
You can save the image by simply calling:
Patrick von Platen's avatar
Patrick von Platen committed
45

46
47
48
```python
>>> image.save("image_of_squirrel_painting.png")
```
Patrick von Platen's avatar
Patrick von Platen committed
49
50