Views
No views yet
pip install diffusers transformers accelerate1from diffusers import AutoPipelineForText2Image
2import torch
3
4pipe = AutoPipelineForText2Image.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
5pipe = pipe.to("cuda")
6
7prompt = "portrait of a young women, blue eyes, cinematic"
8negative_prompt = "low quality, bad quality"
9
10image = pipe(prompt=prompt, negative_prompt=negative_prompt, prior_guidance_scale =1.0, height=768, width=768).images[0]
11image.save("portrait.png")
1from PIL import Image
2import requests
3from io import BytesIO
4
5url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
6response = requests.get(url)
7original_image = Image.open(BytesIO(response.content)).convert("RGB")
8original_image = original_image.resize((768, 512))
1from diffusers import AutoPipelineForImage2Image
2import torch
3
4pipe = AutoPipelineForImage2Image.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
5pipe.enable_model_cpu_offload()
6
7prompt = "A fantasy landscape, Cinematic lighting"
8negative_prompt = "low quality, bad quality"
9
10image = pipe(prompt=prompt, image=original_image, strength=0.3, height=768, width=768).images[0]
11
12out.images[0].save("fantasy_land.png")
1from diffusers import KandinskyV22PriorPipeline, KandinskyV22Pipeline
2from diffusers.utils import load_image
3import PIL
4
5import torch
6
7pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
8 "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
9)
10pipe_prior.to("cuda")
11
12img1 = load_image(
13 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
14)
15
16img2 = load_image(
17 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/starry_night.jpeg"
18)
19
20# add all the conditions we want to interpolate, can be either text or image
21images_texts = ["a cat", img1, img2]
22
23# specify the weights for each condition in images_texts
24weights = [0.3, 0.3, 0.4]
25
26# We can leave the prompt empty
27prompt = ""
28prior_out = pipe_prior.interpolate(images_texts, weights)
29
30pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
31pipe.to("cuda")
32
33image = pipe(**prior_out, height=768, width=768).images[0]
34
35image.save("starry_cat.png")
1from diffusers import AutoPipelineForInpainting
2from diffusers.utils import load_image
3import torch
4import numpy as np
5
6pipe = AutoPipelineForInpainting.from_pretrained("kandinsky-community/kandinsky-2-2-decoder-inpaint", torch_dtype=torch.float16)
7pipe.enable_model_cpu_offload()
8
9prompt = "a hat"
10
11init_image = load_image(
12 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
13)
14
15mask = np.zeros((768, 768), dtype=np.float32)
16# Let's mask out an area above the cat's head
17mask[:250, 250:-250] = 1
18
19
20out = pipe(
21 prompt=prompt,
22 image=init_image,
23 mask_image=mask,
24 height=768,
25 width=768,
26 num_inference_steps=150,
27)
28
29image = out.images[0]
30image.save("cat_with_hat.png")
1# For PIL input
2import PIL.ImageOps
3mask = PIL.ImageOps.invert(mask)
4
5# For PyTorch and Numpy input
6mask = 1 - mask1import torch
2import numpy as np
3
4from transformers import pipeline
5from diffusers.utils import load_image
6
7from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline
8
9# let's take an image and extract its depth map.
10def make_hint(image, depth_estimator):
11 image = depth_estimator(image)["depth"]
12 image = np.array(image)
13 image = image[:, :, None]
14 image = np.concatenate([image, image, image], axis=2)
15 detected_map = torch.from_numpy(image).float() / 255.0
16 hint = detected_map.permute(2, 0, 1)
17 return hint
18
19img = load_image(
20 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinskyv22/cat.png"
21).resize((768, 768))
22
23# We can use the `depth-estimation` pipeline from transformers to process the image and retrieve its depth map.
24depth_estimator = pipeline("depth-estimation")
25hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")
26
27# Now, we load the prior pipeline and the text-to-image controlnet pipeline
28pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
29 "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
30)
31pipe_prior = pipe_prior.to("cuda")
32
33pipe = KandinskyV22ControlnetPipeline.from_pretrained(
34 "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16
35)
36pipe = pipe.to("cuda")
37
38# We pass the prompt and negative prompt through the prior to generate image embeddings
39prompt = "A robot, 4k photo"
40negative_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"
41
42generator = torch.Generator(device="cuda").manual_seed(43)
43image_emb, zero_image_emb = pipe_prior(
44 prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator
45).to_tuple()
46
47# 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.
48images = pipe(
49 image_embeds=image_emb,
50 negative_image_embeds=zero_image_emb,
51 hint=hint,
52 num_inference_steps=50,
53 generator=generator,
54 height=768,
55 width=768,
56).images
57images[0].save("robot_cat.png")

1import torch
2import numpy as np
3
4from diffusers import KandinskyV22PriorEmb2EmbPipeline, KandinskyV22ControlnetImg2ImgPipeline
5from diffusers.utils import load_image
6from transformers import pipeline
7
8img = load_image(
9 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/cat.png"
10).resize((768, 768))
11
12def make_hint(image, depth_estimator):
13 image = depth_estimator(image)["depth"]
14 image = np.array(image)
15 image = image[:, :, None]
16 image = np.concatenate([image, image, image], axis=2)
17 detected_map = torch.from_numpy(image).float() / 255.0
18 hint = detected_map.permute(2, 0, 1)
19 return hint
20
21depth_estimator = pipeline("depth-estimation")
22hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")
23
24pipe_prior = KandinskyV22PriorEmb2EmbPipeline.from_pretrained(
25 "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
26)
27pipe_prior = pipe_prior.to("cuda")
28
29pipe = KandinskyV22ControlnetImg2ImgPipeline.from_pretrained(
30 "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16
31)
32pipe = pipe.to("cuda")
33
34prompt = "A robot, 4k photo"
35negative_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"
36
37generator = torch.Generator(device="cuda").manual_seed(43)
38
39# run prior pipeline
40
41img_emb = pipe_prior(prompt=prompt, image=img, strength=0.85, generator=generator)
42negative_emb = pipe_prior(prompt=negative_prior_prompt, image=img, strength=1, generator=generator)
43
44# run controlnet img2img pipeline
45images = pipe(
46 image=img,
47 strength=0.5,
48 image_embeds=img_emb.image_embeds,
49 negative_image_embeds=negative_emb.image_embeds,
50 hint=hint,
51 num_inference_steps=50,
52 generator=generator,
53 height=768,
54 width=768,
55).images
56
57images[0].save("robot_cat.png")

| FID (30k) | |
|---|---|
| eDiff-I (2022) | 6.95 |
| Image (2022) | 7.27 |
| Kandinsky 2.1 (2023) | 8.21 |
| Stable Diffusion 2.1 (2022) | 8.59 |
| GigaGAN, 512x512 (2023) | 9.09 |
| DALL-E 2 (2022) | 10.39 |
| GLIDE (2022) | 12.24 |
| Kandinsky 1.0 (2022) | 15.40 |
| DALL-E (2021) | 17.89 |
| Kandinsky 2.0 (2022) | 20.00 |
| GLIGEN (2022) | 21.04 |
@misc{kandinsky 2.2,
title = {kandinsky 2.2},
author = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
year = {2023},
howpublished = {},
}