> 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/ren-lian-yu-shen-fen/instantid.md).

# InstantID

从一张参考照片生成具有任意人脸身份的图像

仅使用一张参考照片即可生成任意面部身份的图像。

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

## 在 CLORE.AI 上租用

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

### 访问你的服务器

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

## 什么是 InstantID？

InstantID 保留面部身份：

* 使用任意参考人脸
* 零样本 - 无需训练
* 适用于任何风格/提示词
* 优于 LoRA 训练

## 需求

| 模式  | 显存    | 推荐       |
| --- | ----- | -------- |
| 基础  | 12GB  | RTX 4080 |
| 高质量 | 16GB  | RTX 4090 |
| 带姿势 | 16GB+ | RTX 4090 |

## 快速部署

**Docker 镜像：**

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

**端口：**

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

**命令：**

```bash
pip install diffusers transformers accelerate opencv-python insightface onnxruntime-gpu && \
huggingface-cli download InstantX/InstantID --local-dir ./checkpoints && \
python instantid_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
pip install opencv-python insightface onnxruntime-gpu
pip install huggingface_hub

# 下载模型
huggingface-cli download InstantX/InstantID --local-dir ./checkpoints
```

## 基础用法

```python
import torch
import cv2
import numpy as np
from PIL import Image
from diffusers import StableDiffusionXLPipeline, DDIMScheduler
from insightface.app import FaceAnalysis

# 初始化人脸分析器
app = FaceAnalysis(name='antelopev2', root='./', providers=['CUDAExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))

# 加载管道
pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16
).to("cuda")

# 加载 InstantID 组件
from diffusers import ControlNetModel

controlnet = ControlNetModel.from_pretrained(
    "./checkpoints/ControlNetModel",
    torch_dtype=torch.float16
)

# 为人脸加载 IP-Adapter
pipe.load_ip_adapter(
    "./checkpoints",
    subfolder="",
    weight_name="ip-adapter.bin"
)

# 处理参考人脸
face_image = cv2.imread("reference_face.jpg")
faces = app.get(face_image)
face_emb = faces[0].normed_embedding

# 使用人脸身份生成
image = pipe(
    prompt="宇航员肖像，太空背景",
    negative_prompt="丑陋、模糊、低质量",
    ip_adapter_image_embeds=[torch.tensor(face_emb).unsqueeze(0)],
    num_inference_steps=30,
    guidance_scale=7.5
).images[0]

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

## 使用 Diffusers 管道

```python
from diffusers import StableDiffusionXLInstantIDPipeline, DDIMScheduler
from insightface.app import FaceAnalysis
import torch
import cv2

# 加载人脸分析器
app = FaceAnalysis(name='antelopev2', providers=['CUDAExecutionProvider'])
app.prepare(ctx_id=0)

# 加载管道
pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    controlnet="./checkpoints/ControlNetModel",
    torch_dtype=torch.float16
).to("cuda")

pipe.load_ip_adapter_instantid("./checkpoints/ip-adapter.bin")

# 获取人脸嵌入
face_image = cv2.imread("face.jpg")
face_info = app.get(face_image)[0]

face_emb = face_info.normed_embedding
face_kps = face_info.kps

# 生成
image = pipe(
    prompt="水彩肖像画，艺术风格",
    face_emb=face_emb,
    face_kps=face_kps,
    num_inference_steps=30
).images[0]

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

## 风格示例

### 专业头像照

```python
prompt = "专业商务头像照，影棚灯光，灰色背景，商务着装"
negative = "卡通、动漫、插画、模糊"
```

### 艺术肖像

```python
prompt = "伦勃朗风格的油画肖像，戏剧性灯光，博物馆级品质"
negative = "照片、写实、现代"
```

### 奇幻角色

```python
prompt = "奇幻精灵角色，尖耳朵，魔法森林背景，空灵光线"
negative = "人类耳朵、现代服装、写实"
```

### 动漫风格

```python
prompt = "动漫角色肖像，吉卜力风格，细节丰富，精美"
negative = "写实、照片、3D 渲染"
```

## 带姿势控制

```python
from diffusers.utils import load_image

# 加载姿势参考
pose_image = load_image("pose_reference.jpg")

# 使用人脸和姿势生成
image = pipe(
    prompt="动作姿势的人物，动感，高质量",
    face_emb=face_emb,
    face_kps=face_kps,
    image=pose_image,  # 姿势参考
    controlnet_conditioning_scale=0.8,
    num_inference_steps=30
).images[0]
```

## Gradio 界面

```python
import gradio as gr
import torch
import cv2
import numpy as np
from diffusers import StableDiffusionXLInstantIDPipeline
from insightface.app import FaceAnalysis

app = FaceAnalysis(name='antelopev2', providers=['CUDAExecutionProvider'])
app.prepare(ctx_id=0)

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

pipe.load_ip_adapter_instantid("./checkpoints/ip-adapter.bin")

def generate(face_image, prompt, negative_prompt, strength, steps):
    # 转换为 cv2 格式
    face_cv = cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR)

    # 获取人脸信息
    faces = app.get(face_cv)
    if len(faces) == 0:
        return None, "未检测到人脸！"

    face_info = faces[0]
    face_emb = face_info.normed_embedding
    face_kps = face_info.kps

    # 生成
    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        face_emb=face_emb,
        face_kps=face_kps,
        ip_adapter_scale=strength,
        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.1, 1.0, value=0.8, label="身份强度"),
        gr.Slider(10, 50, value=30, step=1, label="步数")
    ],
    outputs=[
        gr.Image(label="生成的图像"),
        gr.Textbox(label="状态")
    ],
    title="InstantID - 身份保留生成"
)

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

## 批量换脸

```python
import os
from pathlib import Path

def batch_generate(face_image_path, prompts, output_dir):
    # 加载人脸
    face_cv = cv2.imread(face_image_path)
    face_info = app.get(face_cv)[0]
    face_emb = face_info.normed_embedding
    face_kps = face_info.kps

    os.makedirs(output_dir, exist_ok=True)

    for i, prompt in enumerate(prompts):
        print(f"正在生成 {i+1}/{len(prompts)}: {prompt[:50]}...")

        image = pipe(
            prompt=prompt,
            negative_prompt="丑陋、模糊、变形",
            face_emb=face_emb,
            face_kps=face_kps,
            num_inference_steps=30
        ).images[0]

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

# 用法
prompts = [
    "太空服宇航员，地球背景",
    "身穿盔甲的中世纪骑士",
    "实验室里的科学家",
    "餐厅厨房里的厨师",
    "运动场上的运动员"
]

batch_generate("my_face.jpg", prompts, "./outputs")
```

## 身份强度控制

```python

# 低强度 - 更多风格，更少身份
image_stylized = pipe(
    prompt=prompt,
    face_emb=face_emb,
    ip_adapter_scale=0.4,  # 低
    num_inference_steps=30
).images[0]

# 高强度 - 更多身份，更少风格
image_faithful = pipe(
    prompt=prompt,
    face_emb=face_emb,
    ip_adapter_scale=0.9,  # 高
    num_inference_steps=30
).images[0]
```

## 内存优化

```python

# 启用优化
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()

# 或者在极低显存下使用顺序卸载
pipe.enable_sequential_cpu_offload()
```

## 性能

| 模式  | GPU      | 每张图像耗时 |
| --- | -------- | ------ |
| 基础  | RTX 4090 | \~8s   |
| 带姿势 | RTX 4090 | \~12 秒 |
| 基础  | RTX 3090 | \~15s  |
| 基础  | A100     | 约 5 秒  |

## 故障排查

### 未检测到人脸

* 确保人脸清晰可见
* 参考图像光线良好
* 人脸应正面朝向

### 身份未保留

* 提高 ip\_adapter\_scale
* 使用更清晰的参考照片
* 避免极端角度

### 未应用风格

* 降低 ip\_adapter\_scale
* 更具描述性的提示词
* 提高 guidance\_scale

## 成本估算

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) *以获取当前费率。*

**节省费用：**

* 使用 **竞价** 可中断工作市场——约三分之一的服务器将现货价格定得低于按需价格（中位数约优惠 13%），其余则与按需价格持平
* 使用 **CLORE** 代币支付
* 比较不同提供商的价格

## 下一步

* [IP-Adapter](/guides/guides_v2-zh/ren-lian-yu-shen-fen/ip-adapter.md) - 图像提示
* Stable Diffusion WebUI - InstantID 扩展
* [ControlNet](/guides/guides_v2-zh/tu-xiang-chu-li/controlnet-advanced.md) - 姿势控制


---

# 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/ren-lian-yu-shen-fen/instantid.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.
