JoyHallo Documentation

repository·main·Indexed 19 days ago

https://github.com/jdh-algo/joyhallo

JoyHallo is a digital human model optimized for Mandarin audio-driven video generation. It features a semi-decoupled structure to improve the integration of lip, expression, and pose features, utilizing a Chinese wav2vec2 model for audio embedding. The repository provides tools for full training, fine-tuning, and inference via a CLI or Gradio web demo, supporting Ubuntu 20.04 and CUDA 11.3.

Tokens
2.8K
Snippets
11
Records
11
Agent score
67%

What's inside JoyHallo

  1. Train or fine-tune JoyHallo

    main

    JoyHallo supports two training modes:

    1. Full Training (Stage 1 and Stage 2): Use sh joyhallo-alltrain.sh. This automatically runs both stages. Parameters can be adjusted in configs/train/stage1_alltrain.yaml and configs/train/stage2_alltrain.yaml.
    2. Stage 2 Only Training: Use sh joyhallo-train.sh. This is useful for fine-tuning. Parameters are adjusted in configs/train/stage2.yaml.
    # Start training from Stage 1
    sh joyhallo-alltrain.sh
    
    # Train only Stage 2
    sh joyhallo-train.sh
  2. Prepare training data for JoyHallo

    main

    Data Requirements

    Images: Must be cropped to a square; the face should be facing forward with the facial area occupying 50%-70% of the frame. Audio: Must be in .wav format; audio should be clear with suitable background music. These requirements apply to both training and inference.

    Data Processing Pipeline

    1. Organize files: Place videos in a directory structure like jdh-Hallo/videos/.
    2. Extract features: Run the preprocessing script in two steps.
      • Step 1: Converts video to frames, extracts audio, and generates masks.
      • Step 2: Generates facial embeddings (via InsightFace) and audio embeddings (via Chinese wav2vec2). This step requires a GPU.
    3. Generate Metadata: Run the metadata extraction scripts for Stage 1 and Stage 2.

    Parallel Processing: Use -p to specify the total number of instances and -r to specify the current instance index to split the workload.

    # 1. Extract features (Step 1 and Step 2)
    python -m scripts.data_preprocess --input_dir jdh-Hallo/videos --step 1 -p 1 -r 0
    python -m scripts.data_preprocess --input_dir jdh-Hallo/videos --step 2 -p 1 -r 0
    
    # 2. Generate dataset metadata
    python scripts/extract_meta_info_stage1.py -r jdh-Hallo -n jdh-Hallo
    python scripts/extract_meta_info_stage2.py -r jdh-Hallo -n jdh-Hallo
  3. Run JoyHallo inference via CLI

    main

    To perform audio-driven video generation using the command line, run the provided shell script.

    Configuration: Modify configs/inference/inference.yaml to set your input files and model paths.

    Key parameters in inference.yaml:

    • audio_ckpt_dir: Path to the model weights.
    • ref_img_path: Path to the reference images.
    • audio_path: Path to the reference audios.
    • output_dir: Output directory.
    • exp_name: Output file folder name.

    Optimization Tip: To increase inference speed, reduce inference_steps from 40 to 15 in configs/inference/inference.yaml. You may also need to adjust cfg_scale to maintain quality.

    Results are saved in opts/joyhallo.

    sh joyhallo-infer.sh
  4. Prepare JoyHallo model checkpoints

    main

    You must download several sets of weights into a pretrained_models directory. Ensure git lfs is installed before cloning.

    1. Download base Hallo checkpoints: git clone https://huggingface.co/fudan-generative-ai/hallo pretrained_models
    2. Download Chinese wav2vec2-base: git clone https://huggingface.co/TencentGameMate/chinese-wav2vec2-base (inside pretrained_models)
    3. Download JoyHallo-v1 weights: git clone https://huggingface.co/jdh-algo/JoyHallo-v1 pretrained_models/joyhallo
    git lfs install
    git clone https://huggingface.co/fudan-generative-ai/hallo pretrained_models
    
    cd pretrained_models
    git lfs install
    git clone https://huggingface.co/TencentGameMate/chinese-wav2vec2-base 
    
    cd ..
    git lfs install
    git clone https://huggingface.co/jdh-algo/JoyHallo-v1 pretrained_models/joyhallo
  5. Run inference via CLI or Web Demo

    main

    CLI Inference

    Run inference using the provided shell script:

    sh joyhallo-infer.sh

    Configure your inputs in configs/inference/inference.yaml. Key parameters include:

    • audio_ckpt_dir: Path to model weights.
    • ref_img_path: Path to the reference image.
    • audio_path: Path to the reference audio.
    • output_dir: Directory for output files.
    • exp_name: Name of the output folder.

    Tip: To increase inference speed, reduce inference_steps from 40 to 15 in configs/inference/inference.yaml. You may also adjust cfg_scale.

    Web Demo

    To launch a web-based interface, run:

    sh joyhallo-app.sh

    The demo will be available at http://127.0.0.1:7860.

    # CLI Inference
    sh joyhallo-infer.sh
    
    # Web Demo
    sh joyhallo-app.sh
  6. Install JoyHallo

    main

    JoyHallo requires Ubuntu 20.04 and CUDA 11.3. It is tested on A100 GPUs. Follow these steps to set up the environment:

    1. Create a Conda environment with Python 3.10.
    2. Install Python requirements via pip.
    3. Install ffmpeg using apt-get.
    # 1. Create base environment
    conda create -n joyhallo python=3.10 -y
    conda activate joyhallo
    
    # 2. Install requirements
    pip install -r requirements.txt
    
    # 3. Install ffmpeg
    sudo apt-get update  
    sudo apt-get install ffmpeg -y
  7. Run the JoyHallo Gradio Web UI

    main

    You can launch a Gradio-based web interface to animate a static image using an audio clip. The UI allows you to configure various weights and ratios to control the animation quality.

    Requirements:

    • A source image (JPEG format, .webp is not supported).
    • A source audio clip.
    • A minimum version of diffusers (>= 0.10.0.dev0).

    Usage: Run the script from the command line:

    python scripts/app.py

    Configurable Parameters in UI:

    • pose weight: Controls pose influence.
    • face weight: Controls face influence.
    • lip weight: Controls lip movement influence.
    • face expand ratio: Controls the expansion ratio for face processing.
    python scripts/app.py
  8. Load configuration files

    main

    The load_config function supports loading configuration settings from either .yaml or .py files.

    Arguments:

    • config_path (str): Path to the configuration file.

    Returns:

    • dict: The loaded configuration dictionary.

    Supported Formats:

    • .yaml: Loaded via OmegaConf.load.
    • .py: Loaded by importing the file and accessing its .cfg attribute.
    config = load_config('configs/inference/inference.yaml')
  9. Train the JoyHallo model with get_model()

    main

    The get_model function initializes the training environment, including the Accelerator, models (VAE, UNets, projection models), optimizer, and data loaders. It is designed for distributed training.

    Arguments:

    • cfg (argparse.Namespace): A configuration object containing training parameters (e.g., solver, data, vae_model_path, base_model_path, audio_ckpt_dir).

    Returns:

    • accelerator (Accelerator): The accelerator instance for distributed training.
    • vae (AutoencoderKL): The VAE model.
    • net (Net): The main neural network model.
    • val_noise_scheduler (DDIMScheduler): The noise scheduler for validation.
    • cfg (dict): The configuration dictionary.
    • validation_dir (str): The directory for validation results.
    accelerator, vae, net, val_noise_scheduler, cfg, validation_dir = get_model(args)
  10. Net class for face animation

    main

    The Net class is the core neural network model that combines several components to animate a face in a static image. It integrates a reference UNet, a denoising UNet, a face locator, and projection models for image and audio.

    Initialization Arguments:

    • reference_unet (UNet2DConditionModel): The reference UNet used for face animation.
    • denoising_unet (UNet3DConditionModel): The denoising UNet used for face animation.
    • face_locator (FaceLocator): The face locator model.
    • reference_control_writer: Component for writing reference control.
    • reference_control_reader: Component for reading reference control.
    • imageproj: Image projection model.
    • audioproj: Audio projection model.

    Forward Pass Inputs:

    • noisy_latents (torch.Tensor): The noisy latents tensor.
    • timesteps (torch.Tensor): The timesteps tensor.
    • ref_image_latents (torch.Tensor): The reference image latents tensor.
    • face_emb (torch.Tensor): The face embeddings tensor.
    • audio_emb (torch.Tensor): The audio embeddings tensor.
    • mask (torch.Tensor): Hard face mask for face locator.
    • full_mask (torch.Tensor): Pose Mask.
    • face_mask (torch.Tensor): Face Mask.
    • lip_mask (torch.Tensor): Lip Mask.
    • uncond_img_fwd (bool, optional): Flag for reference image unconditional forward pass.
    • uncond_audio_fwd (bool, optional): Flag for audio unconditional forward pass.
    class Net(nn.Module):
        def __init__(
            self,
            reference_unet: UNet2DConditionModel,
            denoising_unet: UNet3DConditionModel,
            face_locator: FaceLocator,
            reference_control_writer,
            reference_control_reader,
            imageproj,
            audioproj,
        ):
            # ... implementation
    
        def forward(
            self,
            noisy_latents: torch.Tensor,
            timesteps: torch.Tensor,
            ref_image_latents: torch.Tensor,
            face_emb: torch.Tensor,
            audio_emb: torch.Tensor,
            mask: torch.Tensor,
            full_mask: torch.Tensor,
            face_mask: torch.Tensor,
            lip_mask: torch.Tensor,
            uncond_img_fwd: bool = False,
            uncond_audio_fwd: bool = False,
        ):
            # ... implementation