🚀 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 accelerate
✨ 主要特性
Kandinsky 2.1继承了Dall - E 2和潜在扩散模型的优点,同时引入新方法,使用CLIP模型进行编码和潜在空间映射,提升了视觉表现和图像操作能力。
📦 安装指南
安装所需的库:
pip install diffusers transformers accelerate
💻 使用示例
基础用法
文本转图像
from diffusers import AutoPipelineForText2Image
import torch
pipe = AutoPipelineForText2Image.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.enable_model_cpu_offload()
prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
negative_prompt = "low quality, bad quality"
image = pipe(prompt=prompt, negative_prompt=negative_prompt, prior_guidance_scale =1.0, height=768, width=768).images[0]
image.save("cheeseburger_monster.png")

文本引导的图像到图像生成
from diffusers import AutoPipelineForImage2Image
import torch
import requests
from io import BytesIO
from PIL import Image
import os
pipe = AutoPipelineForImage2Image.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.enable_model_cpu_offload()
prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"
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.thumbnail((768, 768))
image = pipe(prompt=prompt, image=original_image, strength=0.3).images[0]
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"
)
images_texts = ["a cat", img1, img2]
weights = [0.3, 0.3, 0.4]
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模型生成的CLIP文本和图像嵌入上进行训练。训练好的图像先验模型用于为输入文本提示生成mCLIP图像嵌入。输入文本提示及其mCLIP图像嵌入都在扩散过程中使用。MoVQGAN模型作为模型的最后一个模块,将潜在表示解码为实际图像。
模型训练细节
模型的图像先验训练在LAION Improved Aesthetics数据集上进行,然后在LAION HighRes数据上进行微调。
主要的文本到图像扩散模型基于来自LAION HighRes数据集的1.7亿个文本 - 图像对进行训练(一个重要条件是存在分辨率至少为768x768的图像)。使用1.7亿对是因为我们保留了Kandinsky 2.0的UNet扩散块,这使我们不必从头开始训练它。此外,在微调阶段,使用了一个单独从开放源收集的包含200万个非常高质量高分辨率图像及其描述的数据集(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 |
更多信息请参考即将发布的技术报告。
📄 许可证
本项目采用Apache - 2.0许可证。
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 = {},
}