InstantID README

The project's README from github.com/instantX-research/InstantID. As one markdown file: README.md.

InstantID: Zero-shot Identity-Preserving Generation in Seconds

Qixun Wang12 · Xu Bai12 · Haofan Wang12* · Zekui Qin12 · Anthony Chen123

Huaxia Li2 · Xu Tang2 · Yao Hu2

1InstantX Team · 2Xiaohongshu Inc · 3Peking University

*corresponding authors

GitHub

ModelScope Open in OpenXLab

InstantID is a new state-of-the-art tuning-free method to achieve ID-Preserving generation with only single image, supporting various downstream tasks.

Release

Demos

Stylized Synthesis

Comparison with Previous Works

Comparison with existing tuning-free state-of-the-art techniques. InstantID achieves better fidelity and retain good text editability (faces and styles blend better).

Comparison with pre-trained character LoRAs. We don't need multiple images and still can achieve competitive results as LoRAs without any training.

Comparison with InsightFace Swapper (also known as ROOP or Refactor). However, in non-realistic style, our work is more flexible on the integration of face and background.

Kolors Version

We have adapted InstantID for Kolors. Leveraging Kolors' robust text generation capabilities 👍👍👍, InstantID can be integrated with Kolors to simultaneously generate ID and text.

demo demo demo

Download

You can directly download the model from Huggingface. You also can download the model in python script:

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")

Or run the following command to download all models:

pip install -r gradio_demo/requirements.txt
python gradio_demo/download_models.py

If you cannot access to Huggingface, you can use hf-mirror to download models.

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

For face encoder, you need to manually download via this URL to models/antelopev2 as the default link is invalid. Once you have prepared all models, the folder tree should be like:

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

Usage

If you want to reproduce results in the paper, please refer to the code in infer_full.py. If you want to compare the results with other methods, even without using depth-controlnet, it is recommended that you use this code.

If you are pursuing better results, it is recommended to follow InstantID-Rome.

The following code👇 comes from infer.py. If you want to quickly experience InstantID, please refer to the code in infer.py.

# !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)

Then, you can customized your own face images

# 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,
    negative_prompt=negative_prompt,
    image_embeds=face_emb,
    image=face_kps,
    controlnet_conditioning_scale=0.8,
    ip_adapter_scale=0.8,
).images[0]

To save VRAM, you can enable CPU offloading

pipe.enable_model_cpu_offload()
pipe.enable_vae_tiling()

Speed Up with LCM-LoRA

Our work is compatible with LCM-LoRA. First, download the model.

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")

To use it, you just need to load it and infer with a small num_inference_steps. Note that it is recommendated to set guidance_scale between [0, 1].

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

Start a local gradio demo

Run the following command:

python gradio_demo/app.py

or MultiControlNet version:

gradio_demo/app-multicontrolnet.py 

Usage Tips

Community Resources

Replicate Demo

WebUI

ComfyUI

Windows

Acknowledgements

Disclaimer

The code of InstantID is released under Apache License for both academic and commercial usage. However, both manual-downloading and auto-downloading face models from insightface are for non-commercial research purposes only according to their license. Our released checkpoints are also for research purposes only. Users are granted the freedom to create images using this tool, but they are obligated to comply with local laws and utilize it responsibly. The developers will not assume any responsibility for potential misuse by users.

Star History

Star History Chart

Sponsor Us

If you find this project useful, you can buy us a coffee via Github Sponsor! We support Paypal and WeChat Pay.

Cite

If you find InstantID useful for your research and applications, please cite us using this 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}
}

For any question, please feel free to contact us via [email protected] or [email protected].

InstantID (instantX-research/InstantID)

Searching Database…

这是什么东西?InstantID 是一种无需微调、能保持人物身份的图像生成方法:只需一张人脸参考照片加一句文字提示词,几秒钟内就能生成保留此人长相的新图片。
这是一个要安装的应用,还是接到其他东西里用的代码和模型?它是研究代码,而不是打磨好的应用:包括一套已合并进 Hugging Face diffusers 的 Python 流程,仓库里还有推理脚本和本地 gradio 演示。社区也做了 ComfyUI、sd-webui-controlnet 和 Replicate 的集成。
我在本地怎么把它跑起来?克隆仓库,从 Hugging Face 下载模型检查点(如果访问受阻可以通过 hf-mirror),再手动把 antelopev2 人脸编码器下载到 models/antelopev2 目录。然后运行推理脚本,或者启动本地 gradio 演示。
除了代码之外我还需要什么——显卡、模型,还是别的?需要一个带显卡的 Python 环境,以及几个模型文件:InstantID 检查点、一个 IdentityNet ControlNet、一个 IP-Adapter、一个 Stable Diffusion 基础模型,还有 antelopev2 编码器。如果显存不够,可以开启 CPU offloading 来缓解。
它目前还做不到什么?它只能处理单张人脸:不支持多人场景,流程会直接把照片中最大的一张脸当作参考。
我可以把它用在商业项目里吗?部分可以——代码采用 Apache 2.0 协议,学术和商业用途都没问题,但 insightface 的人脸模型和 InstantID 发布的检查点仅限研究用途,所以商业项目只能依赖代码本身,不能依赖附带的模型。
这和给某个人的脸训练一个 LoRA 有什么区别?完全不用训练:角色 LoRA 需要这个人的多张照片和一次微调训练,而 InstantID 无需微调,一张图片就能用——README 声称效果有竞争力,人脸和风格的融合比以往的无微调方法更好。
生成的结果和我的参考照片不够像,该怎么调?调高 controlnet_conditioning_scale(IdentityNet)和 ip_adapter_scale 可以提高相似度。如果颜色变得过饱和,先降低 ip_adapter_scale,如果还不行再降低 controlnet_conditioning_scale。调低 ip_adapter_scale 还能让你的文字提示词有更大的发挥空间。
我能不装任何东西就试用 InstantID 吗?可以——Hugging Face Spaces 上有在线演示,支持 LCM 加速和 Multi-ControlNet,另外还有 Replicate 演示(zsxkib/instant-id)。WebUI 用户可以使用 Mikubill 的 sd-webui-controlnet 扩展。
我能让生成速度更快吗?可以,InstantID 兼容 LCM-LoRA:加载它之后用较小的 num_inference_steps 推理,并把 guidance_scale 保持在 0 到 1 之间。发布说明里还提到 OneDiff 是一种加速推理的选择。
Size
155 MB
Stars
12K
Updated
CommitJul 18
Age
2y
Commits · 90 Days