模型:

kandinsky-community/kandinsky-2-1

类库:

Diffusers

其他:

kandinsky

任务:

文生图
英文

Kandinsky 2.1

Kandinsky 2.1从Dall-E 2和潜在扩散中继承了最佳实践,同时引入了一些新的想法。

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

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

用法

Kandinsky 2.1在扩散器中可用!

pip install diffusers transformers accelerate

文本到图像

from diffusers import DiffusionPipeline
import torch

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

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

prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
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(prompt, negative_prompt=negative_prompt, image_embeds=image_embeds, negative_image_embeds=negative_image_embeds, height=768, width=768).images[0]
image.save("cheeseburger_monster.png")

文本引导的图像生成

from diffusers import KandinskyImg2ImgPipeline, KandinskyPriorPipeline
import torch

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))

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

# create img2img pipeline
pipe = KandinskyImg2ImgPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", 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(
    prompt,
    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 KandinskyPriorPipeline, KandinskyPipeline
from diffusers.utils import load_image
import PIL

import torch

pipe_prior = KandinskyPriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-1-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 = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.to("cuda")

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

image.save("starry_cat.png")

模型架构

概述

Kandinsky 2.1是基于unCLIP和潜在扩散的文本条件扩散模型,由基于transformer的图像先验模型、unet扩散模型和解码器组成。

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

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

细节

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

主要的Text2Image扩散模型是基于来自 LAION HighRes dataset 的170M个文本-图像对进行训练的(重要条件是至少具有768x768的图像分辨率)。使用170M对的原因是我们保留了来自Kandinsky 2.0的UNet扩散块,这使我们不需要从头开始训练它。进一步,在微调阶段,使用了从公开来源单独收集的2M个非常高质量的高分辨率图像与描述(COYO、动漫、俄罗斯地标等)组成的数据集。

评估

在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.1,
  title         = {kandinsky 2.1},
  author        = {Arseniy Shakhmatov, Anton Razzhigaev, Aleksandr Nikolich, Vladimir Arkhipkin, Igor Pavlov, Andrey Kuznetsov, Denis Dimitrov},
  year          = {2023},
  howpublished  = {},
}