InstantID - InstantID是一种基于强大的扩散模型的解决方案,专门用于进行图像个性化处理,同时确保身份的保留和高保真度。

Created at: 2023-12-11 21:16:04
Language: Python
License: Apache-2.0

即时 ID

InstantID:在几秒钟内实现零样本身份保留生成

InstantID 是一种新的最先进的免调谐方法,只需一张图像即可实现 ID 保留生成,支持各种下游任务。

释放

演示

复制 模型范围

程式化合成

与以往作品的比较

与现有的免调谐先进技术进行比较。InstantID 实现了更好的保真度,并保留了良好的文本可编辑性(面孔和样式融合得更好)。

与预训练角色 LoRA 的比较。我们不需要多个图像,并且无需任何培训即可获得具有竞争力的 LoRA 结果。

与 InsightFace Swapper(也称为 ROOP 或 Refactor)的比较。然而,在非现实主义风格中,我们的作品在脸部和背景的整合上更加灵活。

下载

你可以直接从 Huggingface 下载模型。 你还可以在 python 脚本中下载模型:

from huggingface_hub import hf_hub_download
hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/config.json", local_dir="./checkpoints")
hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/diffusion_pytorch_model.safetensors", local_dir="./checkpoints")
hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")

如果无法访问 Huggingface,可以使用 hf-mirror 下载模型。

export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download --resume-download InstantX/InstantID --local-dir checkpoints

对于人脸编码器,你需要通过此 URL 手动下载,因为默认链接无效。准备好所有模型后,文件夹树应如下所示:

models/antelopev2

  .
  ├── models
  ├── checkpoints
  ├── ip_adapter
  ├── pipeline_stable_diffusion_xl_instantid.py
  └── README.md

用法

# !pip install opencv-python transformers accelerate insightface
import diffusers
from diffusers.utils import load_image
from diffusers.models import ControlNetModel

import cv2
import torch
import numpy as np
from PIL import Image

from insightface.app import FaceAnalysis
from pipeline_stable_diffusion_xl_instantid import StableDiffusionXLInstantIDPipeline, draw_kps

# prepare 'antelopev2' under ./models
app = FaceAnalysis(name='antelopev2', root='./', providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))

# prepare models under ./checkpoints
face_adapter = f'./checkpoints/ip-adapter.bin'
controlnet_path = f'./checkpoints/ControlNetModel'

# load IdentityNet
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)

base_model = 'wangqixun/YamerMIX_v8'  # from https://civitai.com/models/84040?modelVersionId=196039
pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
    base_model,
    controlnet=controlnet,
    torch_dtype=torch.float16
)
pipe.cuda()

# load adapter
pipe.load_ip_adapter_instantid(face_adapter)

然后,你可以自定义自己的面部图像

# load an image
face_image = load_image("./examples/yann-lecun_resize.jpg")

# prepare face emb
face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*x['bbox'][3]-x['bbox'][1])[-1]  # only use the maximum face
face_emb = face_info['embedding']
face_kps = draw_kps(face_image, face_info['kps'])

# prompt
prompt = "film noir style, ink sketch|vector, male man, highly detailed, sharp focus, ultra sharpness, monochrome, high contrast, dramatic shadows, 1940s style, mysterious, cinematic"
negative_prompt = "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, vibrant, colorful"

# generate image
image = pipe(
    prompt,
    image_embeds=face_emb,
    image=face_kps,
    controlnet_conditioning_scale=0.8,
    ip_adapter_scale=0.8,
).images[0]

使用 LCM-LoRA 加速

我们的工作与 LCM-LoRA 兼容.首先,下载模型。

from huggingface_hub import hf_hub_download
hf_hub_download(repo_id="latent-consistency/lcm-lora-sdxl", filename="pytorch_lora_weights.safetensors", local_dir="./checkpoints")

要使用它,你只需要加载它并用一个小num_inference_steps进行推断。请注意,建议在 [0, 1] 之间设置guidance_scale。

from diffusers import LCMScheduler

lcm_lora_path = "./checkpoints/pytorch_lora_weights.safetensors"

pipe.load_lora_weights(lcm_lora_path)
pipe.fuse_lora()
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)

num_inference_steps = 10
guidance_scale = 0

开始本地 gradio 演示

运行以下命令:

python gradio_demo/app.py

使用技巧

  • 为了获得更高的相似性,请增加 controlnet_conditioning_scale (IdentityNet) 和 ip_adapter_scale (Adapter) 的权重。
  • 对于过度饱和,请降低ip_adapter_scale。如果不工作,请减少controlnet_conditioning_scale。
  • 为了获得更高的文本控制能力,请降低ip_adapter_scale。
  • 对于具体款式,选择相应的基本型号会有所不同。
  • 我们目前还不支持多人,只会使用最大的脸作为参考姿势。

社区资源

Gradio演示

复制演示

ComfyUI的

窗户

确认

免責聲明

InstantID 的代码在 Apache 许可下发布,用于学术和商业用途。但是,从insightface手动下载和自动下载的人脸模型仅用于非商业研究目的,并仅符合其许可证。用户有权使用此工具创建图像的自由,但他们有义务遵守当地法律并负责任地使用它。开发人员不对用户的潜在滥用承担任何责任。

明星历史

星历图

引用

如果你发现 InstantID 对你的研究和应用有用,请使用此 BibTeX 引用我们:

@article{wang2024instantid,
  title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},
  author={Wang, Qixun and Bai, Xu and Wang, Haofan and Qin, Zekui and Chen, Anthony},
  journal={arXiv preprint arXiv:2401.07519},
  year={2024}
}