YiYiXu commited on
Commit
ff6faae
1 Parent(s): ef016e0

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +208 -0
README.md ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ prior:
4
+ - kandinsky-community/kandinsky-2-2-prior
5
+ tags:
6
+ - text-to-image
7
+ - kandinsky
8
+ ---
9
+
10
+ # Kandinsky 2.2
11
+
12
+ Kandinsky inherits best practices from Dall-E 2 and Latent diffusion while introducing some new ideas.
13
+
14
+ 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.
15
+
16
+ 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)
17
+
18
+ ## Usage
19
+
20
+ Kandinsky 2.2 is available in diffusers!
21
+
22
+ ```python
23
+ pip install diffusers transformers accelerate
24
+ ```
25
+
26
+ ### Text-to-Image Generation with ControlNet Conditioning
27
+
28
+
29
+ ```python
30
+ import torch
31
+ import numpy as np
32
+
33
+ from transformers import pipeline
34
+ from diffusers.utils import load_image
35
+
36
+ from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline
37
+
38
+ # let's take an image and extract its depth map.
39
+ def make_hint(image, depth_estimator):
40
+ image = depth_estimator(image)["depth"]
41
+ image = np.array(image)
42
+ image = image[:, :, None]
43
+ image = np.concatenate([image, image, image], axis=2)
44
+ detected_map = torch.from_numpy(image).float() / 255.0
45
+ hint = detected_map.permute(2, 0, 1)
46
+ return hint
47
+
48
+ img = load_image(
49
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/cat.png"
50
+ ).resize((768, 768))
51
+
52
+ # We can use the `depth-estimation` pipeline from transformers to process the image and retrieve its depth map.
53
+ depth_estimator = pipeline("depth-estimation")
54
+ hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")
55
+
56
+ # Now, we load the prior pipeline and the text-to-image controlnet pipeline
57
+ pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
58
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
59
+ )
60
+ pipe_prior = pipe_prior.to("cuda")
61
+
62
+ pipe = KandinskyV22ControlnetPipeline.from_pretrained(
63
+ "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16
64
+ )
65
+ pipe = pipe.to("cuda")
66
+
67
+ # We pass the prompt and negative prompt through the prior to generate image embeddings
68
+ prompt = "A robot, 4k photo"
69
+ 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"
70
+
71
+ generator = torch.Generator(device="cuda").manual_seed(43)
72
+ image_emb, zero_image_emb = pipe_prior(
73
+ prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator
74
+ ).to_tuple()
75
+
76
+ # 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.
77
+ images = pipe(
78
+ image_embeds=image_emb,
79
+ negative_image_embeds=zero_image_emb,
80
+ hint=hint,
81
+ num_inference_steps=50,
82
+ generator=generator,
83
+ height=768,
84
+ width=768,
85
+ ).images
86
+ images[0].save("robot_cat.png")
87
+ ```
88
+
89
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/cat.png)
90
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/robot_cat_text2img.png)
91
+
92
+ ### Image-to-Image Generation with ControlNet Conditioning
93
+
94
+ ```python
95
+ import torch
96
+ import numpy as np
97
+
98
+ from diffusers import KandinskyV22PriorEmb2EmbPipeline, KandinskyV22ControlnetImg2ImgPipeline
99
+ from diffusers.utils import load_image
100
+ from transformers import pipeline
101
+
102
+ img = load_image(
103
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/cat.png"
104
+ ).resize((768, 768))
105
+
106
+ def make_hint(image, depth_estimator):
107
+ image = depth_estimator(image)["depth"]
108
+ image = np.array(image)
109
+ image = image[:, :, None]
110
+ image = np.concatenate([image, image, image], axis=2)
111
+ detected_map = torch.from_numpy(image).float() / 255.0
112
+ hint = detected_map.permute(2, 0, 1)
113
+ return hint
114
+
115
+ depth_estimator = pipeline("depth-estimation")
116
+ hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")
117
+
118
+ pipe_prior = KandinskyV22PriorEmb2EmbPipeline.from_pretrained(
119
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
120
+ )
121
+ pipe_prior = pipe_prior.to("cuda")
122
+
123
+ pipe = KandinskyV22ControlnetImg2ImgPipeline.from_pretrained(
124
+ "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16
125
+ )
126
+ pipe = pipe.to("cuda")
127
+
128
+ prompt = "A robot, 4k photo"
129
+ 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"
130
+
131
+ generator = torch.Generator(device="cuda").manual_seed(43)
132
+
133
+ # run prior pipeline
134
+
135
+ img_emb = pipe_prior(prompt=prompt, image=img, strength=0.85, generator=generator)
136
+ negative_emb = pipe_prior(prompt=negative_prior_prompt, image=img, strength=1, generator=generator)
137
+
138
+ # run controlnet img2img pipeline
139
+ images = pipe(
140
+ image=img,
141
+ strength=0.5,
142
+ image_embeds=img_emb.image_embeds,
143
+ negative_image_embeds=negative_emb.image_embeds,
144
+ hint=hint,
145
+ num_inference_steps=50,
146
+ generator=generator,
147
+ height=768,
148
+ width=768,
149
+ ).images
150
+
151
+ images[0].save("robot_cat.png")
152
+ ```
153
+
154
+ 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.
155
+
156
+ ![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/robot_cat.png)
157
+
158
+
159
+ ## Model Architecture
160
+
161
+ ### Overview
162
+ 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.
163
+
164
+ 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.
165
+
166
+ <p float="left">
167
+ <img src="https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/kandinsky21.png"/>
168
+ </p>
169
+
170
+ 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.
171
+
172
+
173
+ ### Details
174
+ 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).
175
+
176
+ 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.
177
+
178
+
179
+ ### Evaluation
180
+ We quantitatively measure the performance of Kandinsky 2.1 on the COCO_30k dataset, in zero-shot mode. The table below presents FID.
181
+
182
+ FID metric values ​​for generative models on COCO_30k
183
+ | | FID (30k)|
184
+ |:------|----:|
185
+ | eDiff-I (2022) | 6.95 |
186
+ | Image (2022) | 7.27 |
187
+ | Kandinsky 2.1 (2023) | 8.21|
188
+ | Stable Diffusion 2.1 (2022) | 8.59 |
189
+ | GigaGAN, 512x512 (2023) | 9.09 |
190
+ | DALL-E 2 (2022) | 10.39 |
191
+ | GLIDE (2022) | 12.24 |
192
+ | Kandinsky 1.0 (2022) | 15.40 |
193
+ | DALL-E (2021) | 17.89 |
194
+ | Kandinsky 2.0 (2022) | 20.00 |
195
+ | GLIGEN (2022) | 21.04 |
196
+
197
+ For more information, please refer to the upcoming technical report.
198
+
199
+ ## BibTex
200
+ If you find this repository useful in your research, please cite:
201
+ ```
202
+ @misc{kandinsky 2.2,
203
+ title = {kandinsky 2.2},
204
+ author = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
205
+ year = {2023},
206
+ howpublished = {},
207
+ }
208
+ ```