> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/qi-ta-gong-zuo-fu-zai/kandinsky.md).

# Kandinsky

在 Clore.ai 上使用 Kandinsky 的多语言模型生成图像

利用强大的多语言文本理解生成图像。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 什么是 Kandinsky？

Kandinsky 是由 Sber AI 开发的图像生成模型：

* 强大的多语言文本理解
* 高质量图像生成
* 图像混合与插值
* 支持局部重绘与扩展绘制
* 开源权重

## 资源

* **GitHub：** [ai-forever/Kandinsky-3](https://github.com/ai-forever/Kandinsky-3)
* **HuggingFace：** [kandinsky-community](https://huggingface.co/kandinsky-community)
* **论文：** [Kandinsky 论文](https://arxiv.org/abs/2310.03502)

## 模型版本

| 版本            | 分辨率       | 质量 | 速度 |
| ------------- | --------- | -- | -- |
| Kandinsky 2.1 | 768x768   | 好  | 快  |
| Kandinsky 2.2 | 1024x1024 | 更好 | 中等 |
| Kandinsky 3   | 1024x1024 | 最佳 | 较慢 |

## 硬件要求

| 模型                | 显存   | 推荐 GPU   |
| ----------------- | ---- | -------- |
| Kandinsky 2.2     | 8GB  | RTX 3070 |
| Kandinsky 3       | 12GB | RTX 3090 |
| Kandinsky 3（高分辨率） | 16GB | RTX 4090 |

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel
```

**端口：**

```
22/tcp
7860/http
```

**命令：**

```bash
pip install diffusers transformers accelerate gradio && \
python -c "
import gradio as gr
import torch
from diffusers import AutoPipelineForText2Image

pipe = AutoPipelineForText2Image.from_pretrained(
    'kandinsky-community/kandinsky-3',
    variant='fp16',
    torch_dtype=torch.float16
).to('cuda')

def generate(prompt, negative, steps, guidance, seed):
    generator = torch.Generator('cuda').manual_seed(seed) if seed > 0 else None
    image = pipe(
        prompt=prompt,
        negative_prompt=negative,
        num_inference_steps=steps,
        guidance_scale=guidance,
        generator=generator
    ).images[0]
    return image

gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label='提示词'),
        gr.Textbox(label='负向提示词', value='低质量，模糊'),
        gr.Slider(10, 100, value=50, label='步数'),
        gr.Slider(1, 20, value=4, label='引导强度'),
        gr.Number(value=-1, label='种子')
    ],
    outputs=gr.Image(),
    title='Kandinsky 3'
).launch(server_name='0.0.0.0', server_port=7860)
"
```

## 访问你的服务

部署后，找到你的 `http_pub` URL 在 **我的订单**:

1. 前往 **我的订单** 页面
2. 点击你的订单
3. 找到 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 替代 `localhost` 在下面的示例中。

## 安装

```bash
pip install diffusers transformers accelerate torch
```

## 基础用法

### Kandinsky 3

```python
import torch
from diffusers import AutoPipelineForText2Image

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-3",
    variant="fp16",
    torch_dtype=torch.float16
)
pipe.to("cuda")

image = pipe(
    prompt="一只漂浮在太空中的猫宇航员，数字艺术，鲜艳色彩",
    num_inference_steps=50,
    guidance_scale=4.0
).images[0]

image.save("cat_astronaut.png")
```

### Kandinsky 2.2

```python
import torch
from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline

# 加载 prior（文本编码器）
prior = KandinskyV22PriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-2-prior",
    torch_dtype=torch.float16
).to("cuda")

# 加载解码器
decoder = KandinskyV22Pipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder",
    torch_dtype=torch.float16
).to("cuda")

# 生成图像嵌入
prompt = "山间美丽的日落，油画风格"
image_embeds, negative_embeds = prior(
    prompt=prompt,
    guidance_scale=1.0
).to_tuple()

# 生成图像
image = decoder(
    image_embeds=image_embeds,
    negative_image_embeds=negative_embeds,
    height=768,
    width=768,
    num_inference_steps=50
).images[0]

image.save("sunset.png")
```

## 多语言提示词

Kandinsky 支持多种语言：

```python
import torch
from diffusers import AutoPipelineForText2Image

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-3",
    variant="fp16",
    torch_dtype=torch.float16
).to("cuda")

# 英语
image_en = pipe("雪地森林中的一只红狐狸").images[0]

# 俄语
image_ru = pipe("Красная лиса в снежном лесу").images[0]

# 中文
image_zh = pipe("雪林中的红狐狸").images[0]

# 德语
image_de = pipe("一只红狐狸在雪覆盖的森林里").images[0]

# 生成的图像都很相似！
```

## 图像混合

```python
import torch
from diffusers import KandinskyV22PriorPipeline, KandinskyV22Pipeline
from diffusers.utils import load_image

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

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

# 两个要混合的提示词
prompt1 = "一只猫"
prompt2 = "一只狗"

# 获取两者的嵌入
embeds1, neg1 = prior(prompt1).to_tuple()
embeds2, neg2 = prior(prompt2).to_tuple()

# 混合嵌入（各 50%）
mixed_embeds = 0.5 * embeds1 + 0.5 * embeds2
mixed_neg = 0.5 * neg1 + 0.5 * neg2

# 生成混合图像
image = decoder(
    image_embeds=mixed_embeds,
    negative_image_embeds=mixed_neg,
    height=768,
    width=768
).images[0]

image.save("cat_dog_mix.png")
```

## 局部重绘

```python
import torch
from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image

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

# 加载图像和蒙版
image = load_image("photo.png")
mask = load_image("mask.png")

# 局部重绘
result = pipe(
    prompt="一顶金色王冠",
    image=image,
    mask_image=mask,
    num_inference_steps=50
).images[0]

result.save("inpainted.png")
```

## 图生图

```python
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import load_image

pipe = AutoPipelineForImage2Image.from_pretrained(
    "kandinsky-community/kandinsky-3",
    variant="fp16",
    torch_dtype=torch.float16
).to("cuda")

init_image = load_image("sketch.png")

image = pipe(
    prompt="一幅城堡的细致数字绘画，奇幻艺术",
    image=init_image,
    strength=0.75,
    num_inference_steps=50
).images[0]

image.save("castle.png")
```

## 批量生成

```python
import torch
from diffusers import AutoPipelineForText2Image
import os

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-3",
    variant="fp16",
    torch_dtype=torch.float16
).to("cuda")

prompts = [
    "一个宁静的日本庭院，樱花盛开",
    "一个夜晚霓虹灯闪烁的赛博朋克城市",
    "一个摆满魔法书的古老图书馆",
    "冬天山中的一间温馨小木屋"
]

os.makedirs("outputs", exist_ok=True)

for i, prompt in enumerate(prompts):
    image = pipe(
        prompt=prompt,
        num_inference_steps=50,
        guidance_scale=4.0
    ).images[0]

    image.save(f"outputs/image_{i}.png")
    print(f"Generated: {prompt[:30]}...")

    torch.cuda.empty_cache()
```

## Gradio 界面

```python
import gradio as gr
import torch
from diffusers import AutoPipelineForText2Image

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-3",
    variant="fp16",
    torch_dtype=torch.float16
).to("cuda")

def generate(prompt, negative_prompt, steps, guidance, width, height, seed):
    generator = torch.Generator("cuda").manual_seed(seed) if seed > 0 else None

    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        num_inference_steps=steps,
        guidance_scale=guidance,
        width=width,
        height=height,
        generator=generator
    ).images[0]

    return image

demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="提示词", placeholder="描述你的图像..."),
        gr.Textbox(label="负向提示词", value="低质量，模糊，失真"),
        gr.Slider(10, 100, value=50, step=5, label="步数"),
        gr.Slider(1, 20, value=4, step=0.5, label="引导比例"),
        gr.Slider(512, 1024, value=1024, step=64, label="宽度"),
        gr.Slider(512, 1024, value=1024, step=64, label="高度"),
        gr.Number(value=-1, label="种子（-1 表示随机）")
    ],
    outputs=gr.Image(label="生成的图像"),
    title="Kandinsky 3 - 图像生成",
    description="使用多语言提示词生成图像。运行在 CLORE.AI 上。"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## 内存优化

```python
import torch
from diffusers import AutoPipelineForText2Image

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-3",
    variant="fp16",
    torch_dtype=torch.float16
)

# 启用内存优化
pipe.enable_model_cpu_offload()

# 或用于超低显存
pipe.enable_sequential_cpu_offload()

# 启用注意力切片
pipe.enable_attention_slicing()

image = pipe(
    prompt="美丽的风景",
    num_inference_steps=50
).images[0]
```

## 性能

| 模型            | 分辨率       | GPU      | 时间  |
| ------------- | --------- | -------- | --- |
| Kandinsky 3   | 1024x1024 | RTX 3090 | 15秒 |
| Kandinsky 3   | 1024x1024 | RTX 4090 | 10s |
| Kandinsky 2.2 | 768x768   | RTX 3090 | 8s  |
| Kandinsky 2.2 | 768x768   | RTX 4090 | 5s  |

## 故障排查

### 内存不足

**问题：** 生成时 CUDA 显存溢出

**解决方案：**

* 启用 CPU 卸载
* 降低分辨率
* 使用 Kandinsky 2.2 替代 3
* 启用注意力切片

```python
pipe.enable_model_cpu_offload()
pipe.enable_attention_slicing()
```

### 文本渲染效果差

**问题：** 图像中的文本看起来不对

**解决方案：**

* Kandinsky 在文本渲染方面表现较弱（和大多数扩散模型一样）
* 在后处理阶段添加文本
* 使用避免文本的提示词

### 颜色看起来不对

**问题：** 图像颜色发白或过饱和

**解决方案：**

* 调整引导比例（尝试 3-6 范围）
* 在提示词中指定颜色偏好
* 通过颜色校正进行后处理

### 生成缓慢

**问题：** 生成耗时过长

**解决方案：**

* 减少推理步数（30 通常就足够）
* 使用 fp16 精度
* 使用 Kandinsky 2.2 以获得更快结果
* 在预览时降低分辨率

## 与其他模型的比较

| 功能   | Kandinsky 3 | SDXL | FLUX |
| ---- | ----------- | ---- | ---- |
| 多语言  | 优秀          | 有限   | 有限   |
| 图像质量 | 高           | 非常高  | 最高   |
| 速度   | 中等          | 中等   | 慢    |
| 显存   | 12GB        | 12GB | 24GB |
| 局部重绘 | 是           | 是    | 有限   |

## 成本估算

CLORE.AI 市场常见费率（截至 2024 年）：

| GPU       | 小时费率    | 日费率     | 4 小时会话  |
| --------- | ------- | ------- | ------- |
| RTX 3060  | \~$0.03 | \~$0.70 | \~$0.12 |
| RTX 3090  | \~$0.06 | \~$1.50 | \~$0.25 |
| RTX 4090  | \~$0.10 | \~$2.30 | \~$0.40 |
| A100 40GB | \~$0.17 | \~$4.00 | \~$0.70 |
| A100 80GB | \~$0.25 | \~$6.00 | \~$1.00 |

*价格因提供商而异。查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前费率。*

## 下一步

* FLUX 生成 - 最高质量图像
* Stable Diffusion - 最受欢迎的选项
* [PixArt](/guides/guides_v2-zh/tu-xiang-sheng-cheng/pixart-image-gen.md) - 快速生成
* ComfyUI - 高级工作流


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/qi-ta-gong-zuo-fu-zai/kandinsky.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
