模型:

radames/kandinsky-2-1-img2img

类库:

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在DIFFUSERS中可用!

pip install diffusers transformers

文本到图像

from diffusers import KandinskyPipeline, KandinskyPriorPipeline
import torch


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

prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
negative_prompt = "low quality, bad quality"

image_emb = pipe_prior(
    prompt, guidance_scale=1.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
).images

zero_image_emb = pipe_prior(
    negative_prompt, guidance_scale=1.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
).images

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


images = pipe(
    prompt,
    image_embeds=image_emb,
    negative_image_embeds=zero_image_emb,
    num_images_per_prompt=2,
    height=768,
    width=768,
    num_inference_steps=100,
    guidance_scale=4.0,
    generator=generator,
).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_emb = pipe_prior(
    prompt, guidance_scale=4.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
).images

zero_image_emb = pipe_prior(
    negative_prompt, guidance_scale=4.0, num_inference_steps=25, generator=generator, negative_prompt=negative_prompt
).images

out = pipe(
    prompt,
    image=original_image,
    image_embeds=image_emb,
    negative_image_embeds=zero_image_emb,
    height=768,
    width=768,
    num_inference_steps=500,
    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
from torchvision import transforms

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

images_texts = ["a cat", img1, img2]
weights = [0.3, 0.3, 0.4]
image_emb, zero_image_emb = pipe_prior.interpolate(images_texts, weights)

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

image = pipe(
    "", image_embeds=image_emb, negative_image_embeds=zero_image_emb, height=768, width=768, num_inference_steps=150
).images[0]

image.save("starry_cat.png")

模型架构

概述

Kandinsky 2.1是基于unCLIP和潜在扩散的文本条件扩散模型,由基于变压器的图像先验模型、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  = {},
}