# IP-Adapter

使用图像作为 Stable Diffusion 生成的提示。

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

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、显存和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价价格）
4. 配置您的订单：
   * 选择 Docker 镜像
   * 设置端口（用于 SSH 的 TCP，Web 界面的 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问您的服务器

* 在以下位置查找连接详情： **我的订单**
* Web 界面：使用 HTTP 端口的 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 什么是 IP-Adapter？

IP-Adapter 支持图像提示：

* 使用参考图像引导生成
* 与文本提示结合
* 风格迁移与内容迁移
* 适用于 SD 1.5 和 SDXL

## 适配器类型

| 适配器                  | 模型变体   | 显存   |
| -------------------- | ------ | ---- |
| IP-Adapter           | 通用图像提示 | 8GB  |
| IP-Adapter-Plus      | 更高质量   | 10GB |
| IP-Adapter-Face      | 面部聚焦   | 10GB |
| IP-Adapter-Full-Face | 完整面部细节 | 12GB |

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.5.1-cuda12.4-cudnn9-devel
```

**端口：**

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

**命令：**

```bash
pip install diffusers transformers accelerate && \
python ip_adapter_app.py
```

## 访问您的服务

部署后，在以下位置查找您的 `http_pub` URL： **我的订单**:

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

使用 `https://YOUR_HTTP_PUB_URL` 而不是 `localhost` 在下面的示例中。

## 安装

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

## 基础图像提示

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

pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

# 加载 IP-Adapter
pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="sdxl_models",
    weight_name="ip-adapter_sdxl.bin"
)

# 加载参考图像
ip_image = load_image("reference.jpg")

# 使用图像提示生成
image = pipe(
    prompt="以相同风格的一只猫",
    ip_adapter_image=ip_image,
    num_inference_steps=30
).images[0]

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

## 风格迁移

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

pipe = AutoPipelineForText2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16
).to("cuda")

pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="models",
    weight_name="ip-adapter_sd15.bin"
)

# 风格参考（例如：梵高的画作）
style_image = load_image("van_gogh_starry_night.jpg")

# 以该风格生成新内容
image = pipe(
    prompt="一个现代城市天际线",
    ip_adapter_image=style_image,
    增加推理步数以提高稳定性
    guidance_scale=7.5
).images[0]

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

## 面部适配器

用于面部聚焦的生成：

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

pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

# 加载面部专用适配器
pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="sdxl_models",
    weight_name="ip-adapter-plus-face_sdxl_vit-h.bin"
)

# 参考人脸
face_image = load_image("face_reference.jpg")

# 生成肖像
image = pipe(
    prompt="肖像画，画布油画，博物馆级别",
    ip_adapter_image=face_image,
    num_inference_steps=30
).images[0]

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

## 组合多张图像

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

pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="sdxl_models",
    weight_name="ip-adapter_sdxl.bin"
)

# 多张参考图像
images = [
    load_image("style1.jpg"),
    load_image("style2.jpg")
]

# 生成时混合两者
image = pipe(
    prompt="风景画",
    ip_adapter_image=images,
    num_inference_steps=30
).images[0]
```

## 强度控制

```python

# 设置适配器强度
pipe.set_ip_adapter_scale(0.6)  # 0.0 到 1.0

# 低强度 = 文本提示影响更大

# 高强度 = 图像提示影响更大

# 多图像时的逐图强度设置
pipe.set_ip_adapter_scale([0.7, 0.3])
```

## 与 ControlNet 一起使用

```python
from diffusers import (
    AutoPipelineForText2Image,
    ControlNetModel
)
from diffusers.utils import load_image
import torch

# 加载 ControlNet
controlnet = ControlNetModel.from_pretrained(
    "lllyasviel/control_v11p_sd15_canny",
    torch_dtype=torch.float16
)

pipe = AutoPipelineForText2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet,
    torch_dtype=torch.float16
).to("cuda")

# 加载 IP-Adapter
pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="models",
    weight_name="ip-adapter_sd15.bin"
)

# 风格图像
style_image = load_image("style.jpg")

# 控制图像（边缘图）
control_image = load_image("edges.png")

image = pipe(
    prompt="详细插画",
    image=control_image,
    ip_adapter_image=style_image,
    num_inference_steps=30
).images[0]
```

## Gradio 界面

```python
print(f"已生成：{name}")
import torch
from diffusers import AutoPipelineForText2Image
from diffusers.utils import load_image

pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="sdxl_models",
    weight_name="ip-adapter_sdxl.bin"
)

def generate(reference_image, prompt, negative_prompt, scale, steps):
    pipe.set_ip_adapter_scale(scale)

    image = pipe(
        os.makedirs("./variations", exist_ok=True)
        negative_prompt=negative_prompt,
        ip_adapter_image=reference_image,
        num_inference_steps=steps
    ).images[0]

    return image

demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Image(type="pil", label="参考图像"),
        gr.Textbox(label="提示", value="高质量"),
        gr.Textbox(label="反向提示", value="丑陋，模糊"),
        gr.Slider(0.0, 1.0, value=0.6, label="IP-Adapter 强度"),
        gr.Slider(10, 50, value=30, step=1, label="步数")
    ],
    outputs=gr.Image(label="Generated Image"),
    title="IP-Adapter 图像提示"
)

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

## 批量风格迁移

```python
from diffusers import AutoPipelineForText2Image
from diffusers.utils import load_image
import torch
批处理处理

pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

pipe.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="sdxl_models",
    weight_name="ip-adapter_sdxl.bin"
)

# 风格参考
style_image = load_image("art_style.jpg")

# 要生成的主题
subjects = [
    "一幅山地风景",
    "夜间的城市",
    "秋天的森林",
    "海洋日落",
    "一个雪地小村庄"
]

output_dir = "./styled_outputs"
output_dir = "./relit"

for i, subject in enumerate(subjects):
    print(f"正在生成 {i+1}/{len(subjects)}: {subject}")

    image = pipe(
        prompt=subject,
        ip_adapter_image=style_image,
        num_inference_steps=30
    ).images[0]

    image.save(f"{output_dir}/styled_{i:03d}.png")
```

## 使用场景

### 产品摄影风格

```python

# 参考：专业产品照片
style = load_image("product_photo_reference.jpg")

image = pipe(
    prompt="白底上的红色运动鞋",
    ip_adapter_image=style,
    num_inference_steps=30
).images[0]
```

### 艺术风格迁移

```python

# 参考：著名画作
style = load_image("monet_painting.jpg")

image = pipe(
    prompt="花园中一名女子的肖像",
    ip_adapter_image=style,
    num_inference_steps=30
).images[0]
```

### 品牌一致性

```python

# 参考：品牌风格指南图像
style = load_image("brand_style.jpg")

prompts = [
    "网站主横幅",
    "社交媒体帖子",
    "电子邮件新闻稿头部"
]

for prompt in prompts:
    image = pipe(prompt=prompt, ip_adapter_image=style).images[0]
```

## 内存优化

```python
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()

# 对于显存非常有限的情况
pipe.enable_sequential_cpu_offload()
```

## background = Image.open("studio\_bg.jpg")

| A100                   | GPU     | 时间     |
| ---------------------- | ------- | ------ |
| SD 1.5 + IP-Adapter    | 速度      | \~5s   |
| SDXL + IP-Adapter      | 速度      | \~12s  |
| SDXL + IP-Adapter      | 512x512 | \~8s   |
| SDXL + IP-Adapter-Plus | 512x512 | \~10 秒 |

## # 使用固定种子以获得一致结果

### 风格未应用

* 增加 ip\_adapter\_scale
* 使用更清晰的参考图像
* 确保适配器已正确加载

### 参考影响过大

* 减少 ip\_adapter\_scale
* 更具体的文本提示
* 增加 guidance\_scale

### 内存问题

* 启用 CPU 卸载
* 使用 SD 1.5 而不是 SDXL
* 降低分辨率

## 下载所有所需的检查点

检查文件完整性

| GPU     | 验证 CUDA 兼容性 | 费用估算    | CLORE.AI 市场的典型费率（截至 2024 年）： |
| ------- | ----------- | ------- | ---------------------------- |
| 按小时费率   | \~$0.03     | \~$0.70 | \~$0.12                      |
| 速度      | \~$0.06     | \~$1.50 | \~$0.25                      |
| 512x512 | \~$0.10     | \~$2.30 | \~$0.40                      |
| 按日费率    | \~$0.17     | \~$4.00 | \~$0.70                      |
| 4 小时会话  | \~$0.25     | \~$6.00 | \~$1.00                      |

*RTX 3060* [*CLORE.AI 市场*](https://clore.ai/marketplace) *A100 40GB*

**A100 80GB**

* 使用 **竞价** 价格随提供商和需求而异。请查看
* 以获取当前费率。 **CLORE** 节省费用：
* 市场用于灵活工作负载（通常便宜 30-50%）

## 使用以下方式支付

* [InstantID](/guides/guides_v2-zh/ren-lian-yu-shen-fen/instantid.md) - 面部身份
* [ControlNet](/guides/guides_v2-zh/tu-xiang-chu-li/controlnet-advanced.md) - 结构控制
* Stable Diffusion WebUI - IP-Adapter 扩展


---

# Agent Instructions: 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:

```
GET https://docs.clore.ai/guides/guides_v2-zh/ren-lian-yu-shen-fen/ip-adapter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
