ConsistentID

repository·main·Indexed 21 days ago

https://github.com/jackailab/consistentid

A portrait generation framework for high ID fidelity and fine-grained identity preservation. ConsistentID integrates FaceParsing and FaceID information into Diffusion models to enable rapid customization without additional LoRA training. The framework includes support for standard inference, inpainting, Controlnet, and a Gradio-based web interface for generating consistent portraits from reference images.

Tokens
1.4K
Snippets
7
Records
7
Agent score
27%

What's inside ConsistentID

  1. Download ConsistentID model weights

    main

    The model weights can be automatically downloaded from Hugging Face using the huggingface_hub library. Alternatively, they are available via Google Drive or Baidu Netdisk.

    from huggingface_hub import hf_hub_download
    ConsistentID_path = hf_hub_download(repo_id="JackAILab/ConsistentID", filename="ConsistentID-v1.bin", repo_type="model")
  2. Prepare data for training

    main

    To train ConsistentID, organize your data in the following directory structure:

    ├── data
    |   ├── JSON_all.json 
    |   ├── resize_IMG # Images 
    |   ├── all_faceID  # FaceID
    |   └── parsing_mask_IMG # Parsing Mask 

    The JSON_all.json file must contain an array of objects with the following keys:

    • IMG: Path to the image.
    • parsing_mask_IMG: Path to the parsing mask.
    • vqa_llva: VQA/LLaVA related data.
    • id_embed_file_resize: Path to the resized FaceID embedding file.
    • vqa_llva_facial: VQA/LLaVA facial related data.
    [
        {
            "IMG": "Path of image...",
            "parsing_mask_IMG": "...",
            "vqa_llva": "...",
            "id_embed_file_resize": "...",
            "vqa_llva_facial": "..."
        },
        ...
    ]
  3. Install ConsistentID

    main

    ConsistentID requires Python >= 3.8, PyTorch >= 2.0.0, and CUDA 11.8. It is recommended to use Anaconda or Miniconda for environment management.

    Follow these steps to set up the environment:

    1. Create a new Conda environment with Python 3.8.10.
    2. Activate the environment.
    3. Upgrade pip.
    4. Install the project requirements using the provided requirements.txt.
    conda create --name ConsistentID python=3.8.10
    conda activate ConsistentID
    pip install -U pip
    
    # Install requirements
    pip install -r requirements.txt
  4. Run inference with ConsistentID

    main

    Before running inference, it is recommended to run convert_weights.py to save weights efficiently. Ensure you are in the project root directory.

    Standard Inference

    Run the standard inference script:

    python infer.py

    Inpaint and Controlnet Inference

    To use the specialized inpainting or Controlnet demos, use the following module commands:

    python -m demo.inpaint_demo
    python -m demo.controlnet_demo
    # Convert weights first
    python evaluation/convert_weights.py
    
    # Standard inference
    python infer.py
    
    # Inpaint/Controlnet demos
    python -m demo.inpaint_demo
    python -m demo.controlnet_demo
  5. Run the ConsistentID portrait generation service via Gradio

    main

    The app.py file provides a Gradio-based web interface for generating consistent portraits. Users can upload a reference image and provide text prompts to generate a new image that maintains the identity of the reference subject.

    Requirements & Setup:

    • GPU Memory: Requires approximately 6GB of VRAM.
    • Models: You must download the base model (SG161222/Realistic_Vision_V6.0_B1_noVAE) and the ConsistentID checkpoint (JackAILab/ConsistentID/ConsistentID-v1.bin) locally. Ensure the paths in the code point to your local directories.

    Interface Inputs:

    • Upload Image: A reference portrait image.
    • prompt: A text description of the desired scene (defaults to "A man, in a forest, adventuring").
    • negative prompt: Text to guide what should be excluded from the image (defaults to standard quality-related negative prompts).

    Output:

    • A generated image saved to the ./images/gradio_outputs directory with a filename format of {date}-{seed}.jpg.
    python app.py
  6. Use the process function for portrait generation

    main

    The process function is the core logic for the generation service. It handles model loading (Base SD model + ConsistentID checkpoint), prompt augmentation, and inference.

    Function Signature: process(inputImage, prompt, negative_prompt)

    • inputImage: The reference image (as a numpy array from Gradio).
    • prompt: The user-provided text prompt.
    • negative_prompt: The user-provided negative prompt.

    Internal Logic & Hyper-parameters:

    • Prompt Augmentation: The function automatically appends cinematic descriptors (e.g., cinematic photo,, 50mm photograph, bokeh) to the user's prompt and appends a large group of quality-related negative prompts.
    • Inference Settings:
      • num_steps: 50
      • merge_steps: 30
      • width/height: 512x512
    • Model Loading: Uses ConsistentIDStableDiffusionPipeline with EulerDiscreteScheduler.
    # Example conceptual usage of the process function
    # input_image is a numpy array from an image source
    output_path = process(input_image, "A man in a suit", "blurry, low quality")
    print(f"Generated image saved at: {output_path}")