EasyPhoto

repository·main·Indexed 26 days ago

https://github.com/aigc-apps/sd-webui-easyphoto

A Stable Diffusion WebUI plugin for training and generating personalized AI portraits (digital doppelgangers). It features a two-stage diffusion process for high-resolution realism, supporting single and multi-person inference, text-to-image, text-to-video, and attribute editing. The tool includes a specialized LoRA training workflow with face detection, saliency detection, and skin beautification to create high-fidelity digital twins.

Tokens
3.4K
Snippets
3
Records
20
Agent score
90%

What's inside sd-webui-easyphoto

  1. Overview of EasyPhoto

    main
    EasyPhoto is a Stable Diffusion WebUI plugin designed for generating AI portraits and training digital avatars of specific individuals. It allows users to train a model using 5 to 20 portrait images (ideally half-body shots without glasses) and then generate new images using preset templates or uploaded custom images.
  2. Understand the EasyPhoto Algorithm Architecture

    main

    EasyPhoto generates realistic AI portraits by combining a pre-prepared template (the generation scene) with a user's digital twin (a fine-tuned LoRA model). Unlike traditional face-swapping methods (like roop) that can introduce unnatural lighting, EasyPhoto uses Stable Diffusion's image-to-image capabilities to ensure the face is seamlessly integrated into the scene.

    Training Process

    1. Face Detection & Cropping: Detects the face in user images and crops them at a specific ratio.
    2. Preprocessing: Uses saliency detection and skin retouching models to create clean, face-focused training images.
    3. Labeling: Applies a fixed tag to each image (no external tagger required).
    4. Fine-tuning: Fine-tunes a Stable Diffusion model to create the user's digital twin LoRA.
    5. Validation: Uses template images for real-time validation during training, calculating the face ID gap between validation and user images to ensure a perfect digital twin.

    Inference Process

    1. First Diffusion:
      • Detects the face in the template image to create a mask.
      • Performs face fusion between the template and the best user image (creating fusion_image).
      • Uses affine transformation to paste the best face_id image onto the template (creating replace_image).
      • Applies ControlNet (Canny on fusion_image and OpenPose on replace_image) to maintain similarity and stability.
      • Generates the image using Stable Diffusion and the user's LoRA.
    2. Second Diffusion:
      • Performs face fusion between the first result and the best user image.
      • Runs a second Stable Diffusion pass at a higher resolution for enhanced quality.
  3. Train a Digital Avatar Model

    main

    To train a model for a specific person:

    1. Upload Images: Upload 5 to 20 portrait images. For best results, use different angles and lighting. Avoid glasses if possible.
    2. Set User ID: Enter a unique User ID (e.g., a username) to identify the trained model.
    3. Configure Parameters: Adjust training settings (see parameter reference below).
    4. Start Training: Click "开始培训" (Start Training). The WebUI will automatically refresh the training logs. If it doesn't, click "Refresh Log".
  4. Perform double-blind testing for model versions

    main

    Double-blind testing allows you to compare the generation results of two different code versions (e.g., version1 and version2) using preset templates and specific user LoRAs.

    Workflow

    1. Setup Models: Prepare the user_id models required for the test.
    2. Prepare Assets:
      • Place preset templates in a templates directory (e.g., 1.jpg, 2.jpg).
      • Place reference images for the specific user IDs in a ref_image directory (e.g., id1.jpg, id2.jpg).
    3. Run Batch Inference: Run the inference script for both versions separately to generate test data.
      python3 post_infer.py --template_dir templates --output_path test_data/version1 --user_ids your_id
      (Repeat for version2 with a different --output_path)
    4. Format Data: Convert the generated inference results into a JSON format for comparison.
      python3 ./double_blind/format_data2json.py --ref_images ref_image --version1_dir test_data/version1 --version2_dir test_data/version2 --output_json test_v1_v2.json
    5. Launch Double-Blind UI: Start the Gradio application to view and conduct the blind test. Note: Use gradio==3.48.0 to avoid known bugs with BarPlot and other UI issues.
      python3 ./double_blind/app.py --data-path test_v1_v2.json --result-path ./result.jsonl

    After running the UI, you can determine the WinningRate for version1 and version2 to document the performance for Pull Requests (PRs).

    python3 post_infer.py --template_dir templates --output_path test_data/version1 --user_ids your_id
    
    python3 ./double_blind/format_data2json.py --ref_images ref_image --version1_dir test_data/version1 --version2_dir test_data/version2 --output_json test_v1_v2.json
    
    python3 ./double_blind/app.py --data-path test_v1_v2.json --result-path ./result.jsonl
  5. Quick Start via Docker

    main

    To run EasyPhoto using Docker, ensure your machine has the correct GPU drivers and CUDA environment installed. Use the following commands to pull the image, run the container with GPU support and port mapping, and launch the WebUI.

    # 拉取镜像
    docker pull registry.cn-beijing.aliyuncs.com/mybigpai/sd-webui-easyphoto:0.0.3
    
    # 进入镜像
    docker run -it -p 7860:7860 --network host --gpus all registry.cn-beijing.aliyuncs.com/mybigpai/sd-webui-easyphoto:0.0.3
    
    # 启动webui
    python3 launch.py --port 7860
  6. Configure ControlNet for EasyPhoto

    main
    EasyPhoto requires the sd-webui-controlnet extension for inference. You must install it before using EasyPhoto. Additionally, you must configure the WebUI to support multiple ControlNet models by setting Multi ControlNet: Max models amount (requires restart) to at least 3.
  7. Run EasyPhoto via Docker

    main

    To run EasyPhoto using Docker, ensure your machine has the correct graphics card drivers and CUDA environment installed. Use the following commands to pull the image, run the container with GPU support and host networking, and launch the WebUI.

    # pull image
    docker pull registry.cn-beijing.aliyuncs.com/mybigpai/sd-webui-easyphoto:0.0.3
    
    # enter image
    docker run -it -p 7860:7860 --network host --gpus all registry.cn-beijing.aliyuncs.com/mybigpai/sd-webui-easyphoto:0.0.3
    
    # launch webui
    python3 launch.py --port 7860
  8. Perform Inference for Single and Multi-person generation

    main

    Single Person Inference

    1. Click the refresh button to query trained models.
    2. Select the desired user ID.
    3. Select a template image.
    4. Click Generate.

    Multi-person Inference

    1. Go to the EasyPhoto Settings page.
    2. Set num_of_faceid to a value greater than 1.
    3. Apply settings and restart the WebUI.
    4. Return to EasyPhoto and upload a template containing two people.
    5. Select the user IDs for both people.
    6. Click Generate.
  9. Perform Inference with EasyPhoto

    main

    Inference is a two-stage diffusion process designed to maximize realism and resolution:

    Stage 1: First Diffusion

    1. Masking: Face detection is performed on the template image to create an inpainting mask.
    2. Face Fusion: The template image undergoes face fusion with the optimal user image.
    3. Inpainting: The fusion_image is created by inpainting the template using the fused face.
    4. Affine Transformation: The face_id image (obtained during training) is affixed to the template via affine transformation to create a replaced_image.
    5. ControlNet Application:
      • Canny (with color) is applied to fusion_image to extract features.
      • OpenPose is applied to replaced_image to ensure stability.
    6. Generation: Stable Diffusion combines these elements with the user's digital doppelgänger (LoRA) to generate the initial result.

    Stage 2: Second Diffusion

    1. Refinement: The result from the First Diffusion is fused again with the optimal user image.
    2. High-Resolution Generation: Stable Diffusion is run a second time with the digital doppelgänger at a higher resolution to produce the final portrait.
  10. Local Installation Requirements

    main

    EasyPhoto requires approximately 60GB of available disk space for weights and datasets.

    Supported Environments:

    ComponentWindows 10Linux (Ubuntu 20.04, CentOS)
    Python3.103.10 & 3.11
    PyTorch2.0.12.0.1
    TensorFlow-CPU2.13.02.13.0
    CUDA11.711.7
    CUDNN8+8+
    Recommended GPUNvidia-3060 12GNvidia-A10 24G, V100 16G, A100 40G