patrickvonplaten commited on
Commit
27b588e
1 Parent(s): eb22179

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +238 -1
README.md CHANGED
@@ -2,5 +2,242 @@
2
  license: apache-2.0
3
  tags:
4
  - kandinsky
5
- duplicated_from: ai-forever/kandinsky-2-1-prior
6
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: apache-2.0
3
  tags:
4
  - kandinsky
 
5
  ---
6
+
7
+ # Kandinsky 2.1
8
+
9
+ Kandinsky 2.1 inherits best practices from Dall-E 2 and Latent diffusion while introducing some new ideas.
10
+
11
+ It uses the CLIP model as a text and image encoder, and diffusion image prior (mapping) between latent spaces of CLIP modalities. This approach increases the visual performance of the model and unveils new horizons in blending images and text-guided image manipulation.
12
+
13
+ The Kandinsky model is created by [Arseniy Shakhmatov](https://github.com/cene555), [Anton Razzhigaev](https://github.com/razzant), [Aleksandr Nikolich](https://github.com/AlexWortega), [Igor Pavlov](https://github.com/boomb0om), [Andrey Kuznetsov](https://github.com/kuznetsoffandrey) and [Denis Dimitrov](https://github.com/denndimitrov)
14
+
15
+ ## Usage
16
+
17
+ Kandinsky 2.1 is available in diffusers!
18
+
19
+ ```python
20
+ pip install diffusers transformers
21
+ ```
22
+ ### Text to image
23
+
24
+ ```python
25
+ from diffusers import KandinskyPipeline, KandinskyPriorPipeline
26
+ import torch
27
+
28
+
29
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("YiYiXu/Kandinsky-prior", torch_dtype=torch.float16)
30
+ pipe_prior.to("cuda")
31
+
32
+ prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
33
+ negative_prompt = "low quality, bad quality"
34
+
35
+ image_emb = pipe_prior(
36
+ prompt, guidance_scale=1.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
37
+ ).images
38
+
39
+ zero_image_emb = pipe_prior(
40
+ negative_prompt, guidance_scale=1.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
41
+ ).images
42
+
43
+ pipe = KandinskyPipeline.from_pretrained("YiYiXu/Kandinsky", torch_dtype=torch.float16)
44
+ pipe.to("cuda")
45
+
46
+
47
+ images = pipe(
48
+ prompt,
49
+ image_embeds=image_emb,
50
+ negative_image_embeds=zero_image_emb,
51
+ num_images_per_prompt=2,
52
+ height=768,
53
+ width=768,
54
+ num_inference_steps=100,
55
+ guidance_scale=4.0,
56
+ generator=generator,
57
+ ).images[0]
58
+
59
+ image.save("./cheeseburger_monster.png")
60
+ ```
61
+
62
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/cheeseburger.png)
63
+
64
+
65
+ ### Text Guided Image-to-Image Generation
66
+
67
+ ```python
68
+ from diffusers import KandinskyImg2ImgPipeline, KandinskyPriorPipeline
69
+ import torch
70
+
71
+ from PIL import Image
72
+ import requests
73
+ from io import BytesIO
74
+
75
+ url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
76
+ response = requests.get(url)
77
+ original_image = Image.open(BytesIO(response.content)).convert("RGB")
78
+ original_image = original_image.resize((768, 512))
79
+
80
+ # create prior
81
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("YiYiXu/Kandinsky-prior", torch_dtype=torch.float16)
82
+ pipe_prior.to("cuda")
83
+
84
+ # create img2img pipeline
85
+ pipe = KandinskyImg2ImgPipeline.from_pretrained("YiYiXu/Kandinsky", torch_dtype=torch.float16)
86
+ pipe.to("cuda")
87
+
88
+ prompt = "A fantasy landscape, Cinematic lighting"
89
+ negative_prompt = "low quality, bad quality"
90
+
91
+ image_emb = pipe_prior(
92
+ prompt, guidance_scale=4.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
93
+ ).images
94
+
95
+ zero_image_emb = pipe_prior(
96
+ negative_prompt, guidance_scale=4.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
97
+ ).images
98
+
99
+ out = pipe(
100
+ prompt,
101
+ image=original_image,
102
+ image_embeds=image_emb,
103
+ negative_image_embeds=zero_image_emb,
104
+ height=768,
105
+ width=768,
106
+ num_inference_steps=500,
107
+ strength=0.3,
108
+ )
109
+
110
+ out.images[0].save("fantasy_land.png")
111
+ ```
112
+
113
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/img2img_fantasyland.png)
114
+
115
+
116
+ ### Text Guided Inpainting Generation
117
+
118
+ ```python
119
+ from diffusers import KandinskyInpaintPipeline, KandinskyPriorPipeline
120
+ from diffusers.utils import load_image
121
+ import torch
122
+ import numpy as np
123
+
124
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("YiYiXu/Kandinsky-prior", torch_dtype=torch.float16)
125
+ pipe_prior.to("cuda")
126
+
127
+ prompt = "a hat"
128
+ image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)
129
+
130
+ pipe = KandinskyInpaintPipeline.from_pretrained("YiYiXu/Kandinsky-inpaint", torch_dtype=torch.float16)
131
+ pipe.to("cuda")
132
+
133
+ init_image = load_image(
134
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
135
+ )
136
+
137
+ mask = np.ones((768, 768), dtype=np.float32)
138
+ mask[:250, 250:-250] = 0
139
+
140
+ out = pipe(
141
+ prompt,
142
+ image=init_image,
143
+ mask_image=mask,
144
+ image_embeds=image_emb,
145
+ negative_image_embeds=zero_image_emb,
146
+ height=768,
147
+ width=768,
148
+ num_inference_steps=150,
149
+ )
150
+
151
+ image = out.images[0]
152
+ image.save("cat_with_hat.png")
153
+ ```
154
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/inpaint_cat_hat.png)
155
+
156
+
157
+ ### Interpolate
158
+
159
+ ```python
160
+ from diffusers import KandinskyPriorPipeline, KandinskyPipeline
161
+ from diffusers.utils import load_image
162
+ import PIL
163
+
164
+ import torch
165
+ from torchvision import transforms
166
+
167
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("YiYiXu/Kandinsky-prior", torch_dtype=torch.float16)
168
+ pipe_prior.to("cuda")
169
+
170
+ img1 = load_image(
171
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
172
+ )
173
+
174
+ img2 = load_image(
175
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/starry_night.jpeg"
176
+ )
177
+
178
+ images_texts = ["a cat", img1, img2]
179
+ weights = [0.3, 0.3, 0.4]
180
+ image_emb, zero_image_emb = pipe_prior.interpolate(images_texts, weights)
181
+
182
+ pipe = KandinskyPipeline.from_pretrained("YiYiXu/Kandinsky", torch_dtype=torch.float16)
183
+ pipe.to("cuda")
184
+
185
+ image = pipe(
186
+ "", image_embeds=image_emb, negative_image_embeds=zero_image_emb, height=768, width=768, num_inference_steps=150
187
+ ).images[0]
188
+
189
+ image.save("starry_cat.png")
190
+ ```
191
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/starry_cat.png)
192
+
193
+
194
+ ## Model Architecture
195
+
196
+ ### Overview
197
+ Kandinsky 2.1 is a text-conditional diffusion model based on unCLIP and latent diffusion, composed of a transformer-based image prior model, a unet diffusion model, and a decoder.
198
+
199
+ The model architectures are illustrated in the figure below - the chart on the left describes the process to train the image prior model, the figure in the center is the text-to-image generation process, and the figure on the right is image interpolation.
200
+
201
+ <p float="left">
202
+ <img src="https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/kandinsky21.png"/>
203
+ </p>
204
+
205
+ Specifically, the image prior model was trained on CLIP text and image embeddings generated with a pre-trained [mCLIP model](https://huggingface.co/M-CLIP/XLM-Roberta-Large-Vit-L-14). The trained image prior model is then used to generate mCLIP image embeddings for input text prompts. Both the input text prompts and its mCLIP image embeddings are used in the diffusion process. A [MoVQGAN](https://openreview.net/forum?id=Qb-AoSw4Jnm) model acts as the final block of the model, which decodes the latent representation into an actual image.
206
+
207
+
208
+ ### Details
209
+ The image prior training of the model was performed on the [LAION Improved Aesthetics dataset](https://huggingface.co/datasets/bhargavsdesai/laion_improved_aesthetics_6.5plus_with_images), and then fine-tuning was performed on the [LAION HighRes data](https://huggingface.co/datasets/laion/laion-high-resolution).
210
+
211
+ The main Text2Image diffusion model was trained on the basis of 170M text-image pairs from the [LAION HighRes dataset](https://huggingface.co/datasets/laion/laion-high-resolution) (an important condition was the presence of images with a resolution of at least 768x768). The use of 170M pairs is due to the fact that we kept the UNet diffusion block from Kandinsky 2.0, which allowed us not to train it from scratch. Further, at the stage of fine-tuning, a dataset of 2M very high-quality high-resolution images with descriptions (COYO, anime, landmarks_russia, and a number of others) was used separately collected from open sources.
212
+
213
+
214
+ ### Evaluation
215
+ We quantitatively measure the performance of Kandinsky 2.1 on the COCO_30k dataset, in zero-shot mode. The table below presents FID.
216
+
217
+ FID metric values ​​for generative models on COCO_30k
218
+ | | FID (30k)|
219
+ |:------|----:|
220
+ | eDiff-I (2022) | 6.95 |
221
+ | Image (2022) | 7.27 |
222
+ | Kandinsky 2.1 (2023) | 8.21|
223
+ | Stable Diffusion 2.1 (2022) | 8.59 |
224
+ | GigaGAN, 512x512 (2023) | 9.09 |
225
+ | DALL-E 2 (2022) | 10.39 |
226
+ | GLIDE (2022) | 12.24 |
227
+ | Kandinsky 1.0 (2022) | 15.40 |
228
+ | DALL-E (2021) | 17.89 |
229
+ | Kandinsky 2.0 (2022) | 20.00 |
230
+ | GLIGEN (2022) | 21.04 |
231
+
232
+ For more information, please refer to the upcoming technical report.
233
+
234
+ ## BibTex
235
+ If you find this repository useful in your research, please cite:
236
+ ```
237
+ @misc{kandinsky 2.1,
238
+ title = {kandinsky 2.1},
239
+ author = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
240
+ year = {2023},
241
+ howpublished = {},
242
+ }
243
+ ```