wuerstchen.md 6.18 KB
Newer Older
Kashif Rasul's avatar
Kashif Rasul committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Würstchen

<img src="https://github.com/dome272/Wuerstchen/assets/61938694/0617c863-165a-43ee-9303-2a17299a0cf9">

[Würstchen: Efficient Pretraining of Text-to-Image Models](https://huggingface.co/papers/2306.00637) is by Pablo Pernias, Dominic Rampas, and Marc Aubreville.

The abstract from the paper is:

*We introduce Würstchen, a novel technique for text-to-image synthesis that unites competitive performance with unprecedented cost-effectiveness and ease of training on constrained hardware. Building on recent advancements in machine learning, our approach, which utilizes latent diffusion strategies at strong latent image compression rates, significantly reduces the computational burden, typically associated with state-of-the-art models, while preserving, if not enhancing, the quality of generated images. Wuerstchen achieves notable speed improvements at inference time, thereby rendering real-time applications more viable. One of the key advantages of our method lies in its modest training requirements of only 9,200 GPU hours, slashing the usual costs significantly without compromising the end performance. In a comparison against the state-of-the-art, we found the approach to yield strong competitiveness. This paper opens the door to a new line of research that prioritizes both performance and computational accessibility, hence democratizing the use of sophisticated AI technologies. Through Wuerstchen, we demonstrate a compelling stride forward in the realm of text-to-image synthesis, offering an innovative path to explore in future research.*

## Würstchen v2 comes to Diffusers

After the initial paper release, we have improved numerous things in the architecture, training and sampling, making Würstchen competetive to current state-of-the-art models in many ways. We are excited to release this new version together with Diffusers. Here is a list of the improvements.

- Higher resolution (1024x1024 up to 2048x2048)
- Faster inference
- Multi Aspect Resolution Sampling
- Better quality

Kashif Rasul's avatar
Kashif Rasul committed
20

Kashif Rasul's avatar
Kashif Rasul committed
21
We are releasing 3 checkpoints for the text-conditional image generation model (Stage C). Those are: 
22

Kashif Rasul's avatar
Kashif Rasul committed
23
24
25
26
27
- v2-base
- v2-aesthetic
- v2-interpolated (50% interpolation between v2-base and v2-aesthetic)

We recommend to use v2-interpolated, as it has a nice touch of both photorealism and aesthetic. Use v2-base for finetunings as it does not have a style bias and use v2-aesthetic for very artistic generations.
Kashif Rasul's avatar
Kashif Rasul committed
28
A comparison can be seen here:
Kashif Rasul's avatar
Kashif Rasul committed
29
30
31
32
33
34
35
36
37
38

<img src="https://github.com/dome272/Wuerstchen/assets/61938694/2914830f-cbd3-461c-be64-d50734f4b49d" width=500>

## Text-to-Image Generation

For the sake of usability Würstchen can be used with a single pipeline. This pipeline is called `WuerstchenCombinedPipeline` and can be used as follows:

```python
import torch
from diffusers import AutoPipelineForText2Image
Kashif Rasul's avatar
Kashif Rasul committed
39
from diffusers.pipelines.wuerstchen import DEFAULT_STAGE_C_TIMESTEPS
Kashif Rasul's avatar
Kashif Rasul committed
40

Kashif Rasul's avatar
Kashif Rasul committed
41
pipe = AutoPipelineForText2Image.from_pretrained("warp-ai/wuerstchen", torch_dtype=torch.float16).to("cuda")
Kashif Rasul's avatar
Kashif Rasul committed
42
43

caption = "Anthropomorphic cat dressed as a fire fighter"
Kashif Rasul's avatar
Kashif Rasul committed
44
45
images = pipe(
    caption, 
Kashif Rasul's avatar
Kashif Rasul committed
46
    width=1024,
Kashif Rasul's avatar
Kashif Rasul committed
47
48
    height=1536,
    prior_timesteps=DEFAULT_STAGE_C_TIMESTEPS,
Kashif Rasul's avatar
Kashif Rasul committed
49
    prior_guidance_scale=4.0,
Kashif Rasul's avatar
Kashif Rasul committed
50
    num_images_per_prompt=2,
Kashif Rasul's avatar
Kashif Rasul committed
51
52
53
).images
```

54
For explanation purposes, we can also initialize the two main pipelines of Würstchen individually. Würstchen consists of 3 stages: Stage C, Stage B, Stage A. They all have different jobs and work only together. When generating text-conditional images, Stage C will first generate the latents in a very compressed latent space. This is what happens in the `prior_pipeline`. Afterwards, the generated latents will be passed to Stage B, which decompresses the latents into a bigger latent space of a VQGAN. These latents can then be decoded by Stage A, which is a VQGAN, into the pixel-space. Stage B & Stage A are both encapsulated in the `decoder_pipeline`. For more details, take a look at the [paper](https://huggingface.co/papers/2306.00637).
Kashif Rasul's avatar
Kashif Rasul committed
55
56
57
58

```python
import torch
from diffusers import WuerstchenDecoderPipeline, WuerstchenPriorPipeline
Kashif Rasul's avatar
Kashif Rasul committed
59
from diffusers.pipelines.wuerstchen import DEFAULT_STAGE_C_TIMESTEPS
Kashif Rasul's avatar
Kashif Rasul committed
60
61
62
63
64
65

device = "cuda"
dtype = torch.float16
num_images_per_prompt = 2

prior_pipeline = WuerstchenPriorPipeline.from_pretrained(
Kashif Rasul's avatar
Kashif Rasul committed
66
    "warp-ai/wuerstchen-prior", torch_dtype=dtype
Kashif Rasul's avatar
Kashif Rasul committed
67
68
).to(device)
decoder_pipeline = WuerstchenDecoderPipeline.from_pretrained(
Kashif Rasul's avatar
Kashif Rasul committed
69
    "warp-ai/wuerstchen", torch_dtype=dtype
Kashif Rasul's avatar
Kashif Rasul committed
70
71
).to(device)

Kashif Rasul's avatar
Kashif Rasul committed
72
caption = "Anthropomorphic cat dressed as a fire fighter"
Kashif Rasul's avatar
Kashif Rasul committed
73
74
75
76
77
negative_prompt = ""

prior_output = prior_pipeline(
    prompt=caption,
    height=1024,
Kashif Rasul's avatar
Kashif Rasul committed
78
79
    width=1536,
    timesteps=DEFAULT_STAGE_C_TIMESTEPS,
Kashif Rasul's avatar
Kashif Rasul committed
80
    negative_prompt=negative_prompt,
Kashif Rasul's avatar
Kashif Rasul committed
81
    guidance_scale=4.0,
Kashif Rasul's avatar
Kashif Rasul committed
82
83
84
85
86
87
88
89
90
91
92
93
94
    num_images_per_prompt=num_images_per_prompt,
)
decoder_output = decoder_pipeline(
    image_embeddings=prior_output.image_embeddings,
    prompt=caption,
    negative_prompt=negative_prompt,
    num_images_per_prompt=num_images_per_prompt,
    guidance_scale=0.0,
    output_type="pil",
).images
```

## Speed-Up Inference
95
You can make use of `torch.compile` function and gain a speed-up of about 2-3x:
Kashif Rasul's avatar
Kashif Rasul committed
96
97
98
99
100
101
102

```python
pipeline.prior = torch.compile(pipeline.prior, mode="reduce-overhead", fullgraph=True)
pipeline.decoder = torch.compile(pipeline.decoder, mode="reduce-overhead", fullgraph=True)
```

## Limitations
103

Kashif Rasul's avatar
Kashif Rasul committed
104
105
- Due to the high compression employed by Würstchen, generations can lack a good amount
of detail. To our human eye, this is especially noticeable in faces, hands etc.
Kashif Rasul's avatar
Kashif Rasul committed
106
- **Images can only be generated in 128-pixel steps**, e.g. the next higher resolution
Kashif Rasul's avatar
Kashif Rasul committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
after 1024x1024 is 1152x1152
- The model lacks the ability to render correct text in images
- The model often does not achieve photorealism
- Difficult compositional prompts are hard for the model

The original codebase, as well as experimental ideas, can be found at [dome272/Wuerstchen](https://github.com/dome272/Wuerstchen).

## WuerschenPipeline

[[autodoc]] WuerstchenCombinedPipeline
	- all
	- __call__

## WuerstchenPriorPipeline

[[autodoc]] WuerstchenDecoderPipeline

	- all
	- __call__

## WuerstchenPriorPipelineOutput

[[autodoc]] pipelines.wuerstchen.pipeline_wuerstchen_prior.WuerstchenPriorPipelineOutput

## WuerstchenDecoderPipeline

[[autodoc]] WuerstchenDecoderPipeline
	- all
	- __call__