quicktour.mdx 7.58 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
# Quicktour
Nathan Lambert's avatar
Nathan Lambert committed
14

Patrick von Platen's avatar
Patrick von Platen committed
15
16
Get up and running with 🧨 Diffusers quickly!
Whether you're a developer or an everyday user, this quick tour will help you get started and show you how to use [`DiffusionPipeline`] for inference.
Nathan Lambert's avatar
Nathan Lambert committed
17

Patrick von Platen's avatar
Patrick von Platen committed
18
19
20
21
22
23
24
25
26
Before you begin, make sure you have all the necessary libraries installed:

```bash
pip install --upgrade diffusers
```

## DiffusionPipeline

The [`DiffusionPipeline`] is the easiest way to use a pre-trained diffusion system for inference. You can use the [`DiffusionPipeline`] out-of-the-box for many tasks across different modalities. Take a look at the table below for some supported tasks:
Nathan Lambert's avatar
Nathan Lambert committed
27

Patrick von Platen's avatar
Patrick von Platen committed
28
29
| **Task**                     | **Description**                                                                                              | **Pipeline**
|------------------------------|--------------------------------------------------------------------------------------------------------------|-----------------|
30
31
32
33
| Unconditional Image Generation          | generate an image from gaussian noise | [unconditional_image_generation](./using-diffusers/unconditional_image_generation`) |
| Text-Guided Image Generation | generate an image given a text prompt | [conditional_image_generation](./using-diffusers/conditional_image_generation) |
| Text-Guided Image-to-Image Translation     | generate an image given an original image and a text prompt | [img2img](./using-diffusers/img2img) |
| Text-Guided Image-Inpainting          | fill the masked part of an image given the image, the mask and a text prompt | [inpaint](./using-diffusers/inpaint) |
Nathan Lambert's avatar
Nathan Lambert committed
34

35
For more in-detail information on how diffusion pipelines function for the different tasks, please have a look at the [**Using Diffusers**](./using-diffusers/overview) section.
Patrick von Platen's avatar
Patrick von Platen committed
36

37
As an example, start by creating an instance of [`DiffusionPipeline`] and specify which pipeline checkpoint you would like to download.
Patrick von Platen's avatar
Patrick von Platen committed
38
39
40
41
42
43
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

44
>>> pipeline = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256")
Nathan Lambert's avatar
Nathan Lambert committed
45
```
Patrick von Platen's avatar
Patrick von Platen committed
46
47
48
49
50
51

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
52
>>> pipeline.to("cuda")
Patrick von Platen's avatar
Patrick von Platen committed
53
54
```

55
Now you can use the `pipeline` on your text prompt:
Patrick von Platen's avatar
Patrick von Platen committed
56
57

```python
58
>>> image = pipeline("An image of a squirrel in Picasso style").images[0]
Patrick von Platen's avatar
Patrick von Platen committed
59
60
61
62
63
64
65
66
67
68
69
70
```

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

You can save the image by simply calling:

```python
>>> image.save("image_of_squirrel_painting.png")
```

More advanced models, like [Stable Diffusion](https://huggingface.co/CompVis/stable-diffusion) require you to accept a [license](https://huggingface.co/spaces/CompVis/stable-diffusion-license) before running the model.
This is due to the improved image generation capabilities of the model and the potentially harmful content that could be produced with it.
apolinario's avatar
apolinario committed
71
Please, head over to your stable diffusion model of choice, *e.g.*  [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5), read the license carefully and tick the checkbox if you agree.
Patrick von Platen's avatar
Patrick von Platen committed
72
73
74
75
76
77
78
You have to be a registered user in 🤗 Hugging Face Hub, and you'll also need to use an access token for the code to work. For more information on access tokens, please refer to [this section of the documentation](https://huggingface.co/docs/hub/security-tokens).
Having "click-accepted" the license, you can save your token:

```python
AUTH_TOKEN = "<please-fill-with-your-token>"
```

apolinario's avatar
apolinario committed
79
You can then load [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5)
Patrick von Platen's avatar
Patrick von Platen committed
80
81
82
83
84
just like we did before only that now you need to pass your `AUTH_TOKEN`:

```python
>>> from diffusers import DiffusionPipeline

85
>>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_auth_token=AUTH_TOKEN)
Patrick von Platen's avatar
Patrick von Platen committed
86
87
```

88
89
If you do not pass your authentication token you will see that the diffusion system will not be correctly 
downloaded. Forcing the user to pass an authentication token ensures that it can be verified that the 
Patrick von Platen's avatar
Patrick von Platen committed
90
91
user has indeed read and accepted the license, which also means that an internet connection is required.

92
**Note**: If you do not want to be forced to pass an authentication token, you can also simply download 
Patrick von Platen's avatar
Patrick von Platen committed
93
94
95
96
the weights locally via:

```
git lfs install
apolinario's avatar
apolinario committed
97
git clone https://huggingface.co/runwayml/stable-diffusion-v1-5
Patrick von Platen's avatar
Patrick von Platen committed
98
99
```

100
and then load locally saved weights into the pipeline. This way, you do not need to pass an authentication
apolinario's avatar
apolinario committed
101
token. Assuming that `"./stable-diffusion-v1-5"` is the local path to the cloned stable-diffusion-v1-5 repo,
Patrick von Platen's avatar
Patrick von Platen committed
102
103
104
you can also load the pipeline as follows:

```python
105
>>> pipeline = DiffusionPipeline.from_pretrained("./stable-diffusion-v1-5")
Patrick von Platen's avatar
Patrick von Platen committed
106
107
108
109
110
111
112
113
114
115
```

Running the pipeline is then identical to the code above as it's the same model architecture.

```python
>>> generator.to("cuda")
>>> image = generator("An image of a squirrel in Picasso style").images[0]
>>> image.save("image_of_squirrel_painting.png")
```

116
Diffusion systems can be used with multiple different [schedulers](./api/schedulers) each with their
Patrick von Platen's avatar
Patrick von Platen committed
117
pros and cons. By default, Stable Diffusion runs with [`PNDMScheduler`], but it's very simple to 
118
use a different scheduler. *E.g.* if you would instead like to use the [`EulerDiscreteScheduler`] scheduler,
Patrick von Platen's avatar
Patrick von Platen committed
119
120
121
you could use it as follows:

```python
122
>>> from diffusers import EulerDiscreteScheduler
Patrick von Platen's avatar
Patrick von Platen committed
123

124
>>> pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_auth_token=AUTH_TOKEN)
Patrick von Platen's avatar
Patrick von Platen committed
125

126
127
>>> # change scheduler to Euler
>>> pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
Nathan Lambert's avatar
Nathan Lambert committed
128
129
```

130
131
For more in-detail information on how to change between schedulers, please refer to the [Using Schedulers](./using-diffusers/schedulers) guide.

Patrick von Platen's avatar
Patrick von Platen committed
132
133
[Stability AI's](https://stability.ai/) Stable Diffusion model is an impressive image generation model
and can do much more than just generating images from text. We have dedicated a whole documentation page,
134
just for Stable Diffusion [here](./conceptual/stable_diffusion).
Nathan Lambert's avatar
Nathan Lambert committed
135

Patrick von Platen's avatar
Patrick von Platen committed
136
137
If you want to know how to optimize Stable Diffusion to run on less memory, higher inference speeds, on specific hardware, such as Mac, or with [ONNX Runtime](https://onnxruntime.ai/), please have a look at our 
optimization pages:
Nathan Lambert's avatar
Nathan Lambert committed
138

139
140
141
142
- [Optimized PyTorch on GPU](./optimization/fp16)
- [Mac OS with PyTorch](./optimization/mps)
- [ONNX](./optimization/onnx)
- [OpenVINO](./optimization/open_vino)
Nathan Lambert's avatar
Nathan Lambert committed
143

144
If you want to fine-tune or train your diffusion model, please have a look at the [**training section**](./training/overview)
Nathan Lambert's avatar
Nathan Lambert committed
145

Patrick von Platen's avatar
Patrick von Platen committed
146
Finally, please be considerate when distributing generated images publicly 🤗.