模型:

kandinsky-community/kandinsky-2-2-decoder

类库:

Diffusers

其他:

kandinsky

任务:

文生图
英文

Kandinsky 2.2

Kandinsky 2.2继承了Dall-E 2和Latent扩散的最佳实践,同时引入了一些新的想法。

它使用CLIP模型作为文本和图像编码器,并使用潜在扩散的图像先验(映射)将CLIP模态的潜在空间连接起来。这种方法提高了模型的视觉性能,并揭示了将图像与文本引导的图像操作相结合的新视野。

Kandinsky模型是由 Arseniy Shakhmatov 、 Anton Razzhigaev 、 Aleksandr Nikolich 、 Igor Pavlov 、 Andrey Kuznetsov 和 Denis Dimitrov 创建的

用法

Kandinsky 2.2 在diffusers中可用!

pip install diffusers transformers accelerate

文本到图像

from diffusers import DiffusionPipeline
import torch

pipe_prior = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16)
pipe_prior.to("cuda")

t2i_pipe = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
t2i_pipe.to("cuda")

prompt = "portrait of a young women, blue eyes, cinematic"
negative_prompt = "low quality, bad quality"

image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt, guidance_scale=1.0).to_tuple()

image = t2i_pipe(image_embeds=image_embeds, negative_image_embeds=negative_image_embeds, height=768, width=768).images[0]
image.save("portrait.png")

文本引导的图像到图像生成

from PIL import Image
import requests
from io import BytesIO

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
response = requests.get(url)
original_image = Image.open(BytesIO(response.content)).convert("RGB")
original_image = original_image.resize((768, 512))
from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline
import torch

# create prior
pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
)
pipe_prior.to("cuda")

# create img2img pipeline
pipe = KandinskyV22Img2ImgPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
pipe.to("cuda")

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt).to_tuple()

out = pipe(
    image=original_image,
    image_embeds=image_embeds,
    negative_image_embeds=negative_image_embeds,
    height=768,
    width=768,
    strength=0.3,
)

out.images[0].save("fantasy_land.png")

插值

from diffusers import KandinskyV22PriorPipeline, KandinskyV22Pipeline
from diffusers.utils import load_image
import PIL

import torch

pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
)
pipe_prior.to("cuda")

img1 = load_image(
    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
)

img2 = load_image(
    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/starry_night.jpeg"
)

# add all the conditions we want to interpolate, can be either text or image
images_texts = ["a cat", img1, img2]

# specify the weights for each condition in images_texts
weights = [0.3, 0.3, 0.4]

# We can leave the prompt empty
prompt = ""
prior_out = pipe_prior.interpolate(images_texts, weights)

pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16)
pipe.to("cuda")

image = pipe(**prior_out, height=768, width=768).images[0]

image.save("starry_cat.png")

模型架构

概述

Kandinsky 2.2是基于unCLIP和latent扩散的文本条件扩散模型,由基于Transformer的图像先验模型、Unet扩散模型和解码器组成。

模型架构如下图所示-左侧的图表描述了训练图像先验模型的过程,中间的图是文本到图像生成过程,右侧的图是图像插值过程。

具体而言,图像先验模型是在CLIP文本和图像嵌入上进行训练的,这些嵌入是使用预训练的 CLIP-ViT-G model 生成的。然后,训练好的图像先验模型用于为输入的文本提示生成CLIP图像嵌入。输入的文本提示及其CLIP图像嵌入都在扩散过程中使用。一个 MoVQGAN 模型作为模型的最后一块,将潜在表示解码为实际图像。

细节

模型的图像先验训练是在 LAION Improved Aesthetics dataset 上进行的,然后在 LAION HighRes data 上进行了微调。

主要的Text2Image扩散模型是在 LAION HighRes dataset 上训练的,然后使用一个包含200万张非常高质量的高分辨率图片与描述的数据集进行微调(来自COYO、动漫、俄罗斯地标等开放资源的不同图片)。

Kandinsky 2.2的主要变化是替换了CLIP-ViT-G。它的图像编码器显著提高了模型生成更具美感的图片和更好地理解文本的能力,从而增强了其整体性能。

由于切换了CLIP模型,图像先验模型被重新训练,Text2Image扩散模型进行了2000次迭代的微调。Kandinsky 2.2在各种分辨率的数据(512 x 512到1536 x 1536)和不同的显示比例上进行训练。因此,Kandinsky 2.2可以生成具有任意显示比例的1024 x 1024输出。

评估

我们在COCO_30k数据集上以零样本模式定量地测量了Kandinsky 2.1的性能。下表显示了FID指标。

用于COCO_30k上生成模型的FID指标值

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

有关更多信息,请参阅即将发布的技术报告。

BibTex

如果您觉得本仓库对您的研究有用,请引用:

@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  = {},
}