YiYiXu commited on
Commit
1e1e7d5
1 Parent(s): 44e469c

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +361 -0
README.md ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ prior: kandinsky-community/kandinsky-2-2-prior
4
+ tags:
5
+ - text-to-image
6
+ - kandinsky
7
+ ---
8
+
9
+ # Kandinsky 2.2
10
+
11
+ Kandinsky inherits best practices from Dall-E 2 and Latent diffusion while introducing some new ideas.
12
+
13
+ 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.
14
+
15
+ 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)
16
+
17
+ ## Usage
18
+
19
+ Kandinsky 2.2 is available in diffusers!
20
+
21
+ ```python
22
+ pip install diffusers transformers accelerate
23
+ ```
24
+ ### Text to image
25
+
26
+ ```python
27
+ from diffusers import DiffusionPipeline
28
+ import torch
29
+
30
+ pipe_prior = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16)
31
+ pipe_prior.to("cuda")
32
+
33
+ t2i_pipe = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
34
+ t2i_pipe.to("cuda")
35
+
36
+ prompt = "portrait of a young women, blue eyes, cinematic"
37
+ negative_prompt = "low quality, bad quality"
38
+
39
+ image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt, guidance_scale=1.0).to_tuple()
40
+
41
+ image = t2i_pipe(image_embeds=image_embeds, negative_image_embeds=negative_image_embeds, height=768, width=768).images[0]
42
+ image.save("portrait.png")
43
+ ```
44
+
45
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/%20blue%20eyes.png)
46
+
47
+
48
+ ### Text Guided Image-to-Image Generation
49
+
50
+ ```python
51
+ from PIL import Image
52
+ import requests
53
+ from io import BytesIO
54
+
55
+ url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
56
+ response = requests.get(url)
57
+ original_image = Image.open(BytesIO(response.content)).convert("RGB")
58
+ original_image = original_image.resize((768, 512))
59
+ ```
60
+ ![img](https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg)
61
+
62
+ ```python
63
+ from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline
64
+ import torch
65
+
66
+ # create prior
67
+ pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
68
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
69
+ )
70
+ pipe_prior.to("cuda")
71
+
72
+ # create img2img pipeline
73
+ pipe = KandinskyV22Img2ImgPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
74
+ pipe.to("cuda")
75
+
76
+ prompt = "A fantasy landscape, Cinematic lighting"
77
+ negative_prompt = "low quality, bad quality"
78
+
79
+ image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt).to_tuple()
80
+
81
+ out = pipe(
82
+ image=original_image,
83
+ image_embeds=image_embeds,
84
+ negative_image_embeds=negative_image_embeds,
85
+ height=768,
86
+ width=768,
87
+ strength=0.3,
88
+ )
89
+
90
+ out.images[0].save("fantasy_land.png")
91
+ ```
92
+
93
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/fantasy_land.png)
94
+
95
+
96
+ ### Interpolate
97
+
98
+ ```python
99
+ from diffusers import KandinskyV22PriorPipeline, KandinskyV22Pipeline
100
+ from diffusers.utils import load_image
101
+ import PIL
102
+
103
+ import torch
104
+
105
+ pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
106
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
107
+ )
108
+ pipe_prior.to("cuda")
109
+
110
+ img1 = load_image(
111
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
112
+ )
113
+
114
+ img2 = load_image(
115
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/starry_night.jpeg"
116
+ )
117
+
118
+ # add all the conditions we want to interpolate, can be either text or image
119
+ images_texts = ["a cat", img1, img2]
120
+
121
+ # specify the weights for each condition in images_texts
122
+ weights = [0.3, 0.3, 0.4]
123
+
124
+ # We can leave the prompt empty
125
+ prompt = ""
126
+ prior_out = pipe_prior.interpolate(images_texts, weights)
127
+
128
+ pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
129
+ pipe.to("cuda")
130
+
131
+ image = pipe(**prior_out, height=768, width=768).images[0]
132
+
133
+ image.save("starry_cat.png")
134
+ ```
135
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/starry_cat2.2.png)
136
+
137
+ ### Text Guided Inpainting Generation
138
+
139
+ ```python
140
+ from diffusers import KandinskyV22InpaintPipeline, KandinskyV22PriorPipeline
141
+ from diffusers.utils import load_image
142
+ import torch
143
+ import numpy as np
144
+
145
+ pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
146
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
147
+ )
148
+ pipe_prior.to("cuda")
149
+
150
+ prompt = "a hat"
151
+ prior_output = pipe_prior(prompt)
152
+
153
+ pipe = KandinskyV22InpaintPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-inpaint", torch_dtype=torch.float16)
154
+ pipe.to("cuda")
155
+
156
+ init_image = load_image(
157
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
158
+ )
159
+
160
+ mask = np.ones((768, 768), dtype=np.float32)
161
+ # Let's mask out an area above the cat's head
162
+ mask[:250, 250:-250] = 0
163
+
164
+ out = pipe(
165
+ image=init_image,
166
+ mask_image=mask,
167
+ **prior_output,
168
+ height=768,
169
+ width=768,
170
+ num_inference_steps=150,
171
+ )
172
+
173
+ image = out.images[0]
174
+ image.save("cat_with_hat.png")
175
+ ```
176
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/cat_with_hat.png)
177
+
178
+
179
+ ### Text-to-Image Generation with ControlNet Conditioning
180
+
181
+
182
+ ```python
183
+ import torch
184
+ import numpy as np
185
+
186
+ from transformers import pipeline
187
+ from diffusers.utils import load_image
188
+
189
+ from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline
190
+
191
+ # let's take an image and extract its depth map.
192
+ def make_hint(image, depth_estimator):
193
+ image = depth_estimator(image)["depth"]
194
+ image = np.array(image)
195
+ image = image[:, :, None]
196
+ image = np.concatenate([image, image, image], axis=2)
197
+ detected_map = torch.from_numpy(image).float() / 255.0
198
+ hint = detected_map.permute(2, 0, 1)
199
+ return hint
200
+
201
+ img = load_image(
202
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/cat.png"
203
+ ).resize((768, 768))
204
+
205
+ # We can use the `depth-estimation` pipeline from transformers to process the image and retrieve its depth map.
206
+ depth_estimator = pipeline("depth-estimation")
207
+ hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")
208
+
209
+ # Now, we load the prior pipeline and the text-to-image controlnet pipeline
210
+ pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
211
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
212
+ )
213
+ pipe_prior = pipe_prior.to("cuda")
214
+
215
+ pipe = KandinskyV22ControlnetPipeline.from_pretrained(
216
+ "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16
217
+ )
218
+ pipe = pipe.to("cuda")
219
+
220
+ # We pass the prompt and negative prompt through the prior to generate image embeddings
221
+ prompt = "A robot, 4k photo"
222
+ negative_prior_prompt = "lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"
223
+
224
+ generator = torch.Generator(device="cuda").manual_seed(43)
225
+ image_emb, zero_image_emb = pipe_prior(
226
+ prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator
227
+ ).to_tuple()
228
+
229
+ # Now we can pass the image embeddings and the depth image we extracted to the controlnet pipeline. With Kandinsky 2.2, only prior pipelines accept `prompt` input. You do not need to pass the prompt to the controlnet pipeline.
230
+ images = pipe(
231
+ image_embeds=image_emb,
232
+ negative_image_embeds=zero_image_emb,
233
+ hint=hint,
234
+ num_inference_steps=50,
235
+ generator=generator,
236
+ height=768,
237
+ width=768,
238
+ ).images
239
+ images[0].save("robot_cat.png")
240
+ ```
241
+
242
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/cat.png)
243
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/robot_cat_text2img.png)
244
+
245
+ ### Image-to-Image Generation with ControlNet Conditioning
246
+
247
+ ```python
248
+ import torch
249
+ import numpy as np
250
+
251
+ from diffusers import KandinskyV22PriorEmb2EmbPipeline, KandinskyV22ControlnetImg2ImgPipeline
252
+ from diffusers.utils import load_image
253
+ from transformers import pipeline
254
+
255
+ img = load_image(
256
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/cat.png"
257
+ ).resize((768, 768))
258
+
259
+ def make_hint(image, depth_estimator):
260
+ image = depth_estimator(image)["depth"]
261
+ image = np.array(image)
262
+ image = image[:, :, None]
263
+ image = np.concatenate([image, image, image], axis=2)
264
+ detected_map = torch.from_numpy(image).float() / 255.0
265
+ hint = detected_map.permute(2, 0, 1)
266
+ return hint
267
+
268
+ depth_estimator = pipeline("depth-estimation")
269
+ hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")
270
+
271
+ pipe_prior = KandinskyV22PriorEmb2EmbPipeline.from_pretrained(
272
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
273
+ )
274
+ pipe_prior = pipe_prior.to("cuda")
275
+
276
+ pipe = KandinskyV22ControlnetImg2ImgPipeline.from_pretrained(
277
+ "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16
278
+ )
279
+ pipe = pipe.to("cuda")
280
+
281
+ prompt = "A robot, 4k photo"
282
+ negative_prior_prompt = "lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"
283
+
284
+ generator = torch.Generator(device="cuda").manual_seed(43)
285
+
286
+ # run prior pipeline
287
+
288
+ img_emb = pipe_prior(prompt=prompt, image=img, strength=0.85, generator=generator)
289
+ negative_emb = pipe_prior(prompt=negative_prior_prompt, image=img, strength=1, generator=generator)
290
+
291
+ # run controlnet img2img pipeline
292
+ images = pipe(
293
+ image=img,
294
+ strength=0.5,
295
+ image_embeds=img_emb.image_embeds,
296
+ negative_image_embeds=negative_emb.image_embeds,
297
+ hint=hint,
298
+ num_inference_steps=50,
299
+ generator=generator,
300
+ height=768,
301
+ width=768,
302
+ ).images
303
+
304
+ images[0].save("robot_cat.png")
305
+ ```
306
+
307
+ Here is the output. Compared with the output from our text-to-image controlnet example, it kept a lot more cat facial details from the original image and worked into the robot style we asked for.
308
+
309
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/robot_cat.png)
310
+
311
+
312
+ ## Model Architecture
313
+
314
+ ### Overview
315
+ 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.
316
+
317
+ 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.
318
+
319
+ <p float="left">
320
+ <img src="https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/kandinsky21.png"/>
321
+ </p>
322
+
323
+ 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.
324
+
325
+
326
+ ### Details
327
+ 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).
328
+
329
+ 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.
330
+
331
+
332
+ ### Evaluation
333
+ We quantitatively measure the performance of Kandinsky 2.1 on the COCO_30k dataset, in zero-shot mode. The table below presents FID.
334
+
335
+ FID metric values ​​for generative models on COCO_30k
336
+ | | FID (30k)|
337
+ |:------|----:|
338
+ | eDiff-I (2022) | 6.95 |
339
+ | Image (2022) | 7.27 |
340
+ | Kandinsky 2.1 (2023) | 8.21|
341
+ | Stable Diffusion 2.1 (2022) | 8.59 |
342
+ | GigaGAN, 512x512 (2023) | 9.09 |
343
+ | DALL-E 2 (2022) | 10.39 |
344
+ | GLIDE (2022) | 12.24 |
345
+ | Kandinsky 1.0 (2022) | 15.40 |
346
+ | DALL-E (2021) | 17.89 |
347
+ | Kandinsky 2.0 (2022) | 20.00 |
348
+ | GLIGEN (2022) | 21.04 |
349
+
350
+ For more information, please refer to the upcoming technical report.
351
+
352
+ ## BibTex
353
+ If you find this repository useful in your research, please cite:
354
+ ```
355
+ @misc{kandinsky 2.2,
356
+ title = {kandinsky 2.2},
357
+ author = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
358
+ year = {2023},
359
+ howpublished = {},
360
+ }
361
+ ```