SyncTalk Documentation

repository·main·Indexed 23 days ago

https://github.com/ziqiaopeng/synctalk

SyncTalk is a talking head synthesis system that utilizes tri-plane hash representations to generate synchronized lip movements, facial expressions, and stable head poses from audio and video input. The system supports training, evaluation, and inference, offering multiple ASR model options including ave, deepspeech, and hubert. It includes utilities for extracting DeepSpeech features, capturing facial blendshapes via MediaPipe, and specialized torso training to resolve double chin artifacts.

Tokens
3K
Snippets
9
Records
16
Agent score
82%

What's inside SyncTalk

  1. Process Video for SyncTalk

    main

    To prepare a video for training or inference, use data_utils/process.py.

    Requirements:

    • Video must be 25FPS.
    • All frames must contain the talking person.
    • Resolution should be approximately 512x512.
    • Duration should be about 4-5 minutes.

    Command:

    python data_utils/process.py data/<ID>/<ID>.mp4 --asr ave

    ASR Options (--asr):

    • ave: Audio Visual Encoder (recommended for accurate lip sync).
    • deepspeech
    • hubert

    Processed videos are saved in the data folder.

    python data_utils/process.py data/<ID>/<ID>.mp4 --asr ave
  2. Prepare Data and Pre-trained Models

    main

    Before running SyncTalk, you must organize your data and models as follows:

    1. Pre-trained Models:

      • Place May.zip in the data folder and unzip it.
      • Place trial_may.zip in the model folder and unzip it.
    2. Face Parsing Model:

      wget https://github.com/YudongGuo/AD-NeRF/blob/master/data_util/face_parsing/79999_iter.pth?raw=true -O data_utils/face_parsing/79999_iter.pth
    3. 3DMM Model for Head Pose Estimation: Download the following files to data_utils/face_tracking/3DMM/:

      • exp_info.npy
      • keys_info.npy
      • sub_mesh.obj
      • topology_info.npy
    4. Basel Face Model (BFM):

      • Copy 01_MorphableModel.mat to data_util/face_tracking/3DMM/.
      • Run the conversion script:
        cd data_utils/face_tracking
        python convert_BFM.py
  3. Train SyncTalk Models

    main

    Training can be performed using the main.py script. By default, data is loaded from disk on the fly (--preload 0).

    Standard Training

    # Basic training
    python main.py data/May --workspace model/trial_may -O --iters 60000 --asr_model ave
    
    # Training with lip fine-tuning
    python main.py data/May --workspace model/trial_may -O --iters 100000 --finetune_lips --patch_size 64 --asr_model ave

    Audio Encoder Options (--asr_model)

    • ave: Best for characters with large lip movements. Use this for better lip sync.
    • deepspeech: Use if training with ave results in lip jitter.
    • hubert: Use if training with ave results in lip jitter.

    Advanced Options

    • Eye Blinking (OpenFace AU45): If you have obtained AU45 data via OpenFace, add the --au45 flag.
    • Preloading Data:
      • --preload 0: Load from disk (default, slower).
      • --preload 1: Load to CPU (slightly slower).
      • --preload 2: Load to GPU (fast, but high memory usage).
    python main.py data/May --workspace model/trial_may -O --iters 60000 --asr_model ave
  4. Train and Test Torso to Repair Double Chin

    main

    If the head-only trained model produces a "double chin" effect, you can perform torso training.

    Warning: When using torso training, you cannot use the --portrait mode. Adding --portrait will cause the torso model to fail.

    Torso Training

    Pass the latest head checkpoint using --head_ckpt:

    # Example: training torso using a head checkpoint
    python main.py data/May/ --workspace model/trial_may_torso/ -O --torso --head_ckpt model/trial_may/ngp_ep0019.pth --iters 150000 --asr_model ave

    Torso Inference/Testing

    # Test torso model
    python main.py data/May/ --workspace model/trial_may_torso/ -O --torso --test --asr_model ave
    
    # Inference with target audio
    python main.py data/May/ --workspace model/trial_may_torso/ -O --torso --test --test_train --asr_model ave --aud ./demo/test.wav
    python main.py data/May/ --workspace model/trial_may_torso/ -O --torso --head_ckpt model/trial_may/ngp_ep0019.pth --iters 150000 --asr_model ave
  5. Install SyncTalk on Linux

    main

    SyncTalk is tested on Ubuntu 18.04 with Pytorch 1.12.1 and CUDA 11.3. Follow these steps to set up the environment:

    1. Clone the repository:
      git clone https://github.com/ZiqiaoPeng/SyncTalk.git
      cd SyncTalk
    2. Create and activate a Conda environment:
      conda create -n synctalk python==3.8.8
      conda activate synctalk
    3. Install PyTorch and dependencies:
      pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113
      sudo apt-get install portaudio19-dev
      pip install -r requirements.txt
      pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py38_cu113_pyt1121/download.html
      pip install tensorflow-gpu==2.8.1
    4. Install specific encoders and modules:
      pip install ./freqencoder
      pip install ./shencoder
      pip install ./gridencoder
      pip install ./raymarching

    Note: If PyTorch3D installation fails, use the provided script:

    python ./scripts/install_pytorch3d.py
    git clone https://github.com/ZiqiaoPeng/SyncTalk.git
    cd SyncTalk
  6. Run SyncTalk via CLI

    main

    The main.py script serves as the primary entrypoint for the SyncTalk pipeline, supporting training, testing, and GUI modes. The script requires a path argument (likely the dataset path) and provides extensive configuration via command-line flags.

    Common Execution Modes

    • Training: The default mode when no specific test flags are provided.
    • Testing: Use --test to load a model and run on the test dataset. Use --test_train to load a model and test specifically on the training dataset.
    • GUI Mode: Use --gui to launch an interactive GUI for rendering and inspection.

    Important Hardware Note

    To prevent low numerical accuracy on RTX 30xx series GPUs, the script automatically attempts to disable tf32 features:

    torch.backends.cuda.matmul.allow_tf32 = False
    torch.backends.cudnn.allow_tf32 = False
  7. Run Evaluation and Inference

    main

    Evaluation

    Run evaluation on a processed subject:

    # Standard evaluation
    python main.py data/May --workspace model/trial_may -O --test --asr_model ave
    
    # Evaluation with Portrait mode (pasting face back onto original image for higher quality)
    python main.py data/May --workspace model/trial_may -O --test --asr_model ave --portrait

    Inference with Target Audio

    To generate a talking head from a specific audio file:

    python main.py data/May --workspace model/trial_may -O --test --test_train --asr_model ave --portrait --aud ./demo/test.wav
    • Use .wav files for inference.
    • Results are saved in model/trial_may/results/.
    • If not using the Audio Visual Encoder (ave), replace the .wav path with the corresponding .npy file path.
    python main.py data/May --workspace model/trial_may -O --test --test_train --asr_model ave --portrait --aud ./demo/test.wav
  8. infer_bs function

    main

    The infer_bs(root_path) function is the core logic for the blendshape capture process. It initializes a MediaPipe FaceLandmarker with output_face_blendshapes=True and output_facial_transformation_matrixes=True.

    It iterates through all .mp4 files in the root_path. For each video, it:

    • Checks if bs.npy already exists to avoid redundant processing.
    • Extracts blendshape scores from the MediaPipe result.
    • Slices the scores (skipping the first index) and appends a zero to maintain a specific coefficient shape.
    • Smooths the coefficients using scipy.signal.savgol_filter.
    • Saves the final array to bs.npy using np.save.