YiYiXu commited on
Commit
7ce3500
1 Parent(s): 67a4915

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +187 -0
README.md ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
138
+ ## Model Architecture
139
+
140
+ ### Overview
141
+ 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.
142
+
143
+ 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.
144
+
145
+ <p float="left">
146
+ <img src="https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/kandinsky21.png"/>
147
+ </p>
148
+
149
+ 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.
150
+
151
+
152
+ ### Details
153
+ 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).
154
+
155
+ 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.
156
+
157
+
158
+ ### Evaluation
159
+ We quantitatively measure the performance of Kandinsky 2.1 on the COCO_30k dataset, in zero-shot mode. The table below presents FID.
160
+
161
+ FID metric values ​​for generative models on COCO_30k
162
+ | | FID (30k)|
163
+ |:------|----:|
164
+ | eDiff-I (2022) | 6.95 |
165
+ | Image (2022) | 7.27 |
166
+ | Kandinsky 2.1 (2023) | 8.21|
167
+ | Stable Diffusion 2.1 (2022) | 8.59 |
168
+ | GigaGAN, 512x512 (2023) | 9.09 |
169
+ | DALL-E 2 (2022) | 10.39 |
170
+ | GLIDE (2022) | 12.24 |
171
+ | Kandinsky 1.0 (2022) | 15.40 |
172
+ | DALL-E (2021) | 17.89 |
173
+ | Kandinsky 2.0 (2022) | 20.00 |
174
+ | GLIGEN (2022) | 21.04 |
175
+
176
+ For more information, please refer to the upcoming technical report.
177
+
178
+ ## BibTex
179
+ If you find this repository useful in your research, please cite:
180
+ ```
181
+ @misc{kandinsky 2.1,
182
+ title = {kandinsky 2.1},
183
+ author = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
184
+ year = {2023},
185
+ howpublished = {},
186
+ }
187
+ ```