EasyPhoto
repository·main·Indexed 26 days ago
https://github.com/aigc-apps/sd-webui-easyphotoA 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.
What's inside sd-webui-easyphoto
- 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.
Understand the EasyPhoto Algorithm Architecture
mainEasyPhoto 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
- Face Detection & Cropping: Detects the face in user images and crops them at a specific ratio.
- Preprocessing: Uses saliency detection and skin retouching models to create clean, face-focused training images.
- Labeling: Applies a fixed tag to each image (no external tagger required).
- Fine-tuning: Fine-tunes a Stable Diffusion model to create the user's digital twin LoRA.
- 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
- 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_idimage onto the template (creatingreplace_image). - Applies ControlNet (Canny on
fusion_imageand OpenPose onreplace_image) to maintain similarity and stability. - Generates the image using Stable Diffusion and the user's LoRA.
- 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.
Train a Digital Avatar Model
mainTo train a model for a specific person:
- Upload Images: Upload 5 to 20 portrait images. For best results, use different angles and lighting. Avoid glasses if possible.
- Set User ID: Enter a unique User ID (e.g., a username) to identify the trained model.
- Configure Parameters: Adjust training settings (see parameter reference below).
- Start Training: Click "开始培训" (Start Training). The WebUI will automatically refresh the training logs. If it doesn't, click "Refresh Log".
Modify AnimateDiff implementation
mainTo modify the AnimateDiff implementation within the
sd-webui-easyphotostructure, follow these steps:- Copy
motion_module.pyfrom the root directory into thescriptsfolder. - Update all
.pyfiles within thescriptsfolder to use relative imports instead of absolute imports. For example, changescripts.animatediff_loggerto.animatediff_logger.
- Copy
Perform double-blind testing for model versions
mainDouble-blind testing allows you to compare the generation results of two different code versions (e.g.,
version1andversion2) using preset templates and specific user LoRAs.Workflow
- Setup Models: Prepare the
user_idmodels required for the test. - Prepare Assets:
- Place preset templates in a
templatesdirectory (e.g.,1.jpg,2.jpg). - Place reference images for the specific user IDs in a
ref_imagedirectory (e.g.,id1.jpg,id2.jpg).
- Place preset templates in a
- Run Batch Inference:
Run the inference script for both versions separately to generate test data.
(Repeat for version2 with a differentpython3 post_infer.py --template_dir templates --output_path test_data/version1 --user_ids your_id--output_path) - 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 - Launch Double-Blind UI:
Start the Gradio application to view and conduct the blind test.
Note: Use
gradio==3.48.0to 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
WinningRateforversion1andversion2to 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- Setup Models: Prepare the
Quick Start via Docker
mainTo 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 7860Install EasyPhoto via Git
mainYou can install the EasyPhoto plugin directly into your Stable Diffusion WebUI using its Git repository URL:https://github.com/aigc-apps/sd-webui-EasyPhoto.Configure ControlNet for EasyPhoto
mainEasyPhoto requires thesd-webui-controlnetextension 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.Run EasyPhoto via Docker
mainTo 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 7860Perform Inference for Single and Multi-person generation
mainSingle Person Inference
- Click the refresh button to query trained models.
- Select the desired
user ID. - Select a template image.
- Click
Generate.
Multi-person Inference
- Go to the EasyPhoto Settings page.
- Set
num_of_faceidto a value greater than1. - Apply settings and restart the WebUI.
- Return to EasyPhoto and upload a template containing two people.
- Select the
user IDsfor both people. - Click
Generate.
Perform Inference with EasyPhoto
mainInference is a two-stage diffusion process designed to maximize realism and resolution:
Stage 1: First Diffusion
- Masking: Face detection is performed on the template image to create an inpainting mask.
- Face Fusion: The template image undergoes face fusion with the optimal user image.
- Inpainting: The
fusion_imageis created by inpainting the template using the fused face. - Affine Transformation: The
face_idimage (obtained during training) is affixed to the template via affine transformation to create areplaced_image. - ControlNet Application:
- Canny (with color) is applied to
fusion_imageto extract features. - OpenPose is applied to
replaced_imageto ensure stability.
- Canny (with color) is applied to
- Generation: Stable Diffusion combines these elements with the user's digital doppelgänger (LoRA) to generate the initial result.
Stage 2: Second Diffusion
- Refinement: The result from the First Diffusion is fused again with the optimal user image.
- High-Resolution Generation: Stable Diffusion is run a second time with the digital doppelgänger at a higher resolution to produce the final portrait.
Local Installation Requirements
mainEasyPhoto requires approximately 60GB of available disk space for weights and datasets.
Supported Environments:
Component Windows 10 Linux (Ubuntu 20.04, CentOS) Python 3.10 3.10 & 3.11 PyTorch 2.0.1 2.0.1 TensorFlow-CPU 2.13.0 2.13.0 CUDA 11.7 11.7 CUDNN 8+ 8+ Recommended GPU Nvidia-3060 12G Nvidia-A10 24G, V100 16G, A100 40G