frodos radames commited on
Commit
aa8e0d8
0 Parent(s):

Duplicate from radames/kandinsky-2-1-img2img

Browse files

Co-authored-by: Radamés Ajna <radames@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ prior:
4
+ - kandinsky-community/kandinsky-2-1-prior
5
+ tags:
6
+ - kandinsky
7
+ - image-to-image
8
+ duplicated_from: kandinsky-community/kandinsky-2-1
9
+ pipeline_tag: image-to-image
10
+ ---
11
+
12
+ # Kandinsky 2.1
13
+
14
+ Kandinsky 2.1 inherits best practices from Dall-E 2 and Latent diffusion while introducing some new ideas.
15
+
16
+ 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.
17
+
18
+ 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)
19
+
20
+ ## Usage
21
+
22
+ Kandinsky 2.1 is available in diffusers!
23
+
24
+ ```python
25
+ pip install diffusers transformers
26
+ ```
27
+ ### Text to image
28
+
29
+ ```python
30
+ from diffusers import KandinskyPipeline, KandinskyPriorPipeline
31
+ import torch
32
+
33
+
34
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16)
35
+ pipe_prior.to("cuda")
36
+
37
+ prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
38
+ negative_prompt = "low quality, bad quality"
39
+
40
+ image_emb = pipe_prior(
41
+ prompt, guidance_scale=1.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
42
+ ).images
43
+
44
+ zero_image_emb = pipe_prior(
45
+ negative_prompt, guidance_scale=1.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
46
+ ).images
47
+
48
+ pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
49
+ pipe.to("cuda")
50
+
51
+
52
+ images = pipe(
53
+ prompt,
54
+ image_embeds=image_emb,
55
+ negative_image_embeds=zero_image_emb,
56
+ num_images_per_prompt=2,
57
+ height=768,
58
+ width=768,
59
+ num_inference_steps=100,
60
+ guidance_scale=4.0,
61
+ generator=generator,
62
+ ).images[0]
63
+
64
+ image.save("./cheeseburger_monster.png")
65
+ ```
66
+
67
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/cheeseburger.png)
68
+
69
+
70
+ ### Text Guided Image-to-Image Generation
71
+
72
+ ```python
73
+ from diffusers import KandinskyImg2ImgPipeline, KandinskyPriorPipeline
74
+ import torch
75
+
76
+ from PIL import Image
77
+ import requests
78
+ from io import BytesIO
79
+
80
+ url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
81
+ response = requests.get(url)
82
+ original_image = Image.open(BytesIO(response.content)).convert("RGB")
83
+ original_image = original_image.resize((768, 512))
84
+
85
+ # create prior
86
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16)
87
+ pipe_prior.to("cuda")
88
+
89
+ # create img2img pipeline
90
+ pipe = KandinskyImg2ImgPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
91
+ pipe.to("cuda")
92
+
93
+ prompt = "A fantasy landscape, Cinematic lighting"
94
+ negative_prompt = "low quality, bad quality"
95
+
96
+ image_emb = pipe_prior(
97
+ prompt, guidance_scale=4.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
98
+ ).images
99
+
100
+ zero_image_emb = pipe_prior(
101
+ negative_prompt, guidance_scale=4.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
102
+ ).images
103
+
104
+ out = pipe(
105
+ prompt,
106
+ image=original_image,
107
+ image_embeds=image_emb,
108
+ negative_image_embeds=zero_image_emb,
109
+ height=768,
110
+ width=768,
111
+ num_inference_steps=500,
112
+ strength=0.3,
113
+ )
114
+
115
+ out.images[0].save("fantasy_land.png")
116
+ ```
117
+
118
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/img2img_fantasyland.png)
119
+
120
+
121
+ ### Interpolate
122
+
123
+ ```python
124
+ from diffusers import KandinskyPriorPipeline, KandinskyPipeline
125
+ from diffusers.utils import load_image
126
+ import PIL
127
+
128
+ import torch
129
+ from torchvision import transforms
130
+
131
+ pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16)
132
+ pipe_prior.to("cuda")
133
+
134
+ img1 = load_image(
135
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
136
+ )
137
+
138
+ img2 = load_image(
139
+ "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/starry_night.jpeg"
140
+ )
141
+
142
+ images_texts = ["a cat", img1, img2]
143
+ weights = [0.3, 0.3, 0.4]
144
+ image_emb, zero_image_emb = pipe_prior.interpolate(images_texts, weights)
145
+
146
+ pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
147
+ pipe.to("cuda")
148
+
149
+ image = pipe(
150
+ "", image_embeds=image_emb, negative_image_embeds=zero_image_emb, height=768, width=768, num_inference_steps=150
151
+ ).images[0]
152
+
153
+ image.save("starry_cat.png")
154
+ ```
155
+ ![img](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/kandinsky-docs/starry_cat.png)
156
+
157
+
158
+ ## Model Architecture
159
+
160
+ ### Overview
161
+ 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.
162
+
163
+ 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.
164
+
165
+ <p float="left">
166
+ <img src="https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/kandinsky21.png"/>
167
+ </p>
168
+
169
+ 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.
170
+
171
+
172
+ ### Details
173
+ 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).
174
+
175
+ 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.
176
+
177
+
178
+ ### Evaluation
179
+ We quantitatively measure the performance of Kandinsky 2.1 on the COCO_30k dataset, in zero-shot mode. The table below presents FID.
180
+
181
+ FID metric values ​​for generative models on COCO_30k
182
+ | | FID (30k)|
183
+ |:------|----:|
184
+ | eDiff-I (2022) | 6.95 |
185
+ | Image (2022) | 7.27 |
186
+ | Kandinsky 2.1 (2023) | 8.21|
187
+ | Stable Diffusion 2.1 (2022) | 8.59 |
188
+ | GigaGAN, 512x512 (2023) | 9.09 |
189
+ | DALL-E 2 (2022) | 10.39 |
190
+ | GLIDE (2022) | 12.24 |
191
+ | Kandinsky 1.0 (2022) | 15.40 |
192
+ | DALL-E (2021) | 17.89 |
193
+ | Kandinsky 2.0 (2022) | 20.00 |
194
+ | GLIGEN (2022) | 21.04 |
195
+
196
+ For more information, please refer to the upcoming technical report.
197
+
198
+ ## BibTex
199
+ If you find this repository useful in your research, please cite:
200
+ ```
201
+ @misc{kandinsky 2.1,
202
+ title = {kandinsky 2.1},
203
+ author = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
204
+ year = {2023},
205
+ howpublished = {},
206
+ }
207
+ ```
model_index.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "KandinskyPipeline",
3
+ "_diffusers_version": "0.17.0.dev0",
4
+ "text_encoder": [
5
+ "kandinsky",
6
+ "MultilingualCLIP"
7
+ ],
8
+ "tokenizer": [
9
+ "transformers",
10
+ "XLMRobertaTokenizerFast"
11
+ ],
12
+ "scheduler": [
13
+ "diffusers",
14
+ "DDIMScheduler"
15
+ ],
16
+ "unet": [
17
+ "diffusers",
18
+ "UNet2DConditionModel"
19
+ ],
20
+ "movq": [
21
+ "diffusers",
22
+ "VQModel"
23
+ ]
24
+ }
movq/config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "VQModel",
3
+ "_diffusers_version": "0.17.0.dev0",
4
+ "act_fn": "silu",
5
+ "block_out_channels": [
6
+ 128,
7
+ 256,
8
+ 256,
9
+ 512
10
+ ],
11
+ "down_block_types": [
12
+ "DownEncoderBlock2D",
13
+ "DownEncoderBlock2D",
14
+ "DownEncoderBlock2D",
15
+ "AttnDownEncoderBlock2D"
16
+ ],
17
+ "in_channels": 3,
18
+ "latent_channels": 4,
19
+ "layers_per_block": 2,
20
+ "norm_num_groups": 32,
21
+ "norm_type": "spatial",
22
+ "num_vq_embeddings": 16384,
23
+ "out_channels": 3,
24
+ "sample_size": 32,
25
+ "scaling_factor": 0.18215,
26
+ "up_block_types": [
27
+ "AttnUpDecoderBlock2D",
28
+ "UpDecoderBlock2D",
29
+ "UpDecoderBlock2D",
30
+ "UpDecoderBlock2D"
31
+ ],
32
+ "vq_embed_dim": 4
33
+ }
movq/diffusion_pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:772e09739d742ddee6807add2d3c2fd2a32db53896b5d07a92c729d8c879ce59
3
+ size 271492131
scheduler/scheduler_config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "DDIMScheduler",
3
+ "_diffusers_version": "0.17.0.dev0",
4
+ "num_train_timesteps": 1000,
5
+ "beta_schedule": "linear",
6
+ "beta_start": 0.00085,
7
+ "beta_end":0.012,
8
+ "clip_sample" : false,
9
+ "set_alpha_to_one" : false,
10
+ "steps_offset" : 1,
11
+ "prediction_type" : "epsilon",
12
+ "thresholding" : false
13
+ }
text_encoder/config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "MultilingualCLIP"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "eos_token_id": 2,
8
+ "hidden_act": "gelu",
9
+ "hidden_dropout_prob": 0.1,
10
+ "hidden_size": 1024,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 4096,
13
+ "layer_norm_eps": 1e-05,
14
+ "max_position_embeddings": 514,
15
+ "model_type": "xlm-roberta",
16
+ "num_attention_heads": 16,
17
+ "num_hidden_layers": 24,
18
+ "output_past": true,
19
+ "pad_token_id": 1,
20
+ "position_embedding_type": "absolute",
21
+ "transformers_version": "4.17.0.dev0",
22
+ "type_vocab_size": 1,
23
+ "use_cache": true,
24
+ "vocab_size": 250002,
25
+ "numDims": 768,
26
+ "transformerDimensions": 1024
27
+ }
text_encoder/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:152bab8bc5813ec882ec634c7301978a0ef29340d4c17b3ae77b6468e017dfbe
3
+ size 2242843757
tokenizer/sentencepiece.bpe.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cfc8146abe2a0488e9e2a0c56de7952f7c11ab059eca145a0a727afce0db2865
3
+ size 5069051
tokenizer/special_tokens_map.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<s>",
3
+ "cls_token": "<s>",
4
+ "eos_token": "</s>",
5
+ "mask_token": {
6
+ "content": "<mask>",
7
+ "lstrip": true,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false
11
+ },
12
+ "pad_token": "<pad>",
13
+ "sep_token": "</s>",
14
+ "unk_token": "<unk>"
15
+ }
tokenizer/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer/tokenizer_config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<s>",
3
+ "clean_up_tokenization_spaces": true,
4
+ "cls_token": "<s>",
5
+ "eos_token": "</s>",
6
+ "mask_token": {
7
+ "__type": "AddedToken",
8
+ "content": "<mask>",
9
+ "lstrip": true,
10
+ "normalized": true,
11
+ "rstrip": false,
12
+ "single_word": false
13
+ },
14
+ "model_max_length": 512,
15
+ "pad_token": "<pad>",
16
+ "sep_token": "</s>",
17
+ "tokenizer_class": "XLMRobertaTokenizer",
18
+ "unk_token": "<unk>"
19
+ }
unet/config.json ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "UNet2DConditionModel",
3
+ "_diffusers_version": "0.17.0.dev0",
4
+ "act_fn": "silu",
5
+ "addition_embed_type": "text_image",
6
+ "addition_embed_type_num_heads": 64,
7
+ "attention_head_dim": 64,
8
+ "block_out_channels": [
9
+ 384,
10
+ 768,
11
+ 1152,
12
+ 1536
13
+ ],
14
+ "center_input_sample": false,
15
+ "class_embed_type": null,
16
+ "class_embeddings_concat": false,
17
+ "conv_in_kernel": 3,
18
+ "conv_out_kernel": 3,
19
+ "cross_attention_dim": 768,
20
+ "cross_attention_norm": null,
21
+ "down_block_types": [
22
+ "ResnetDownsampleBlock2D",
23
+ "SimpleCrossAttnDownBlock2D",
24
+ "SimpleCrossAttnDownBlock2D",
25
+ "SimpleCrossAttnDownBlock2D"
26
+ ],
27
+ "downsample_padding": 1,
28
+ "dual_cross_attention": false,
29
+ "encoder_hid_dim": 1024,
30
+ "encoder_hid_dim_type": "text_image_proj",
31
+ "flip_sin_to_cos": true,
32
+ "freq_shift": 0,
33
+ "in_channels": 4,
34
+ "layers_per_block": 3,
35
+ "mid_block_only_cross_attention": null,
36
+ "mid_block_scale_factor": 1,
37
+ "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
38
+ "norm_eps": 1e-05,
39
+ "norm_num_groups": 32,
40
+ "num_class_embeds": null,
41
+ "only_cross_attention": false,
42
+ "out_channels": 8,
43
+ "projection_class_embeddings_input_dim": null,
44
+ "resnet_out_scale_factor": 1.0,
45
+ "resnet_skip_time_act": false,
46
+ "resnet_time_scale_shift": "scale_shift",
47
+ "sample_size": 64,
48
+ "time_cond_proj_dim": null,
49
+ "time_embedding_act_fn": null,
50
+ "time_embedding_dim": null,
51
+ "time_embedding_type": "positional",
52
+ "timestep_post_act": null,
53
+ "up_block_types": [
54
+ "SimpleCrossAttnUpBlock2D",
55
+ "SimpleCrossAttnUpBlock2D",
56
+ "SimpleCrossAttnUpBlock2D",
57
+ "ResnetUpsampleBlock2D"
58
+ ],
59
+ "upcast_attention": false,
60
+ "use_linear_projection": false
61
+ }
unet/diffusion_pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bf4c63c68860933264eaa290f7f9e308ce5d69a8a3012c26822de98aa056ab30
3
+ size 4914888985