DiffTalk

repository·main·Indexed 19 days ago

https://github.com/sstzal/difftalk

A PyTorch implementation of a CVPR 2023 paper utilizing Diffusion Models for generalized audio-driven portrait animation. DiffTalk focuses on high-quality talking head generation from audio signals, employing an LDM-based iterative denoising process. The project includes tools for training and inference via main.py, supporting HDTF dataset preprocessing, OmegaConf-based YAML configurations, and linear learning rate scaling.

Tokens
2.3K
Snippets
8
Records
13
Agent score
67%

What's inside DiffTalk

  1. Install DiffTalk requirements

    main

    DiffTalk requires Python 3.7.0 and specific versions of PyTorch and PyTorch Lightning. Experiments are conducted using 8 NVIDIA 3090Ti GPUs. Ensure you have the following dependencies installed:

    • python 3.7.0
    • pytorch 1.10.0
    • pytorch-lightning 1.2.5
    • torchvision 0.11.0

    Refer to requirements.txt for the full dependency list.

    # Ensure these versions are met:
    python 3.7.0
    pytorch 1.10.0
    pytorch-lightning 1.2.5
    torchvision 0.11.0
  2. Prepare first stage models for DiffTalk

    main

    To use a pretrained model for the first stage of the DiffTalk pipeline, you must place the model checkpoint file in the models/ directory and ensure it is named exactly model.ckpt.

    mv path/to/your/pretrained_model.ckpt models/model.ckpt
  3. Prepare the HDTF dataset for training and testing

    main

    To use the HDTF dataset, you must preprocess it following these steps:

    1. Set all videos to 25 fps.
    2. Extract audio signals and facial landmarks.
    3. Organize the processed data in ./data/HDTF using the following structure:
    ./data/HDTF
        |——images
        |  |——0_0.jpg
        |  |——N_M.bin
        |——landmarks
        |  |——0_0.lmd
        |  |——N_M.lms
        |——audio_smooth
        |  |——0_0.npy
        |  |——N_M.npy
    1. Create data_train.txt and data_test.txt files containing the identifiers (e.g., 0_0, 0_1, ..., N_M) where N is the total number of classes and M is the class size.
  4. How Learning Rate Scaling works

    main

    The project implements a linear scaling rule for the learning rate to maintain stability when changing batch sizes or GPU counts.

    If --scale_lr is enabled (default), the model's learning rate is calculated as:

    model.learning_rate = accumulate_grad_batches * ngpu * batch_size * base_learning_rate

    Where:

    • accumulate_grad_batches: Number of gradient accumulation steps.
    • ngpu: Number of GPUs being used.
    • batch_size: The per-device batch size.
    • base_learning_rate: The value provided in the model.base_learning_rate config.
  5. Run DiffTalk training and testing via CLI

    main

    The main.py script serves as the primary entrypoint for training and testing models. It uses a combination of YAML configuration files and command-line arguments to define the model, data, and trainer settings.

    Configuration Hierarchy:

    1. Base YAML configs (loaded via -b/--base, merged left-to-right).
    2. Command-line arguments (overwriting YAML parameters using --key value or nested.key=value syntax).

    Key CLI Flags:

    • -t, --train: Enables training mode.
    • --no-test: Disables the testing phase after training.
    • -r, --resume: Resumes training from a specific log directory or checkpoint.
    • -r2, --resume2: Loads a state dict from a specific checkpoint file into the model (non-strict).
    • -n, --name: Adds a postfix to the log directory name.
    • -l, --logdir: Specifies the base directory for logging (default: logs).
    • -s, --seed: Sets the random seed (default: 23).
    • -d, --debug: Enables post-mortem debugging on failure.
    # Example: Train using a base config and override a parameter
    python main.py -b configs/base_config.yaml --model.params.lr=0.0001 -t
    
    # Example: Resume training from a log directory
    python main.py -r logs/2023-01-01T12-00-00_config_name -t
  6. Known limitations and weaknesses of DiffTalk

    main

    Users should be aware of the following limitations:

    • Synthesis Speed: As an iterative denoising process (LDM-based), it is slower at synthesizing frames compared to GAN-based approaches.
    • Identity Generalization: While trained on HDTF, it may occasionally fail on identities from other datasets.
    • Audio-Lip Sync: Performance on cross-identity audio may be slightly inferior to self-driven settings.
    • Mask Sensitivity: During inference, the mask in z_T must completely cover the mouth region and must not leak any lip shape information.
  7. Configure training via YAML structure

    main

    The project uses OmegaConf to manage complex configurations. The expected YAML structure for a complete configuration includes the following top-level keys:

    • model: Defines the model architecture and hyperparameters.
      • base_learning_rate: The base LR used for scaling.
      • target: The import path to the Lightning module.
      • params: Dictionary of parameters for the model.
    • data: Defines the DataModuleFromConfig settings.
      • target: Usually main.DataModuleFromConfig.
      • params: Contains batch_size, train, validation, test, etc.
    • lightning: (Optional) Configuration for the PyTorch Lightning Trainer and its components.
      • trainer: Arguments passed to the Trainer (e.g., gpus, accumulate_grad_batches).
      • logger: Configuration for the logger (e.g., WandbLogger or TestTubeLogger).
      • modelcheckpoint: Configuration for ModelCheckpoint.
      • callbacks: A dictionary of callbacks to instantiate.
    model:
      base_learning_rate: 0.0001
      target: ldm.models.diffusion.ddpm.LatentDiffusion
      params:
        some_param: value
    data:
      target: main.DataModuleFromConfig
      params:
        batch_size: 4
        train:
          target: my_dataset_module
          params:
            path: /data/train
    lightning:
      trainer:
        gpus: 2
        accumulate_grad_batches: 4
      logger:
        target: pytorch_lightning.loggers.WandbLogger
        params:
          name: my_experiment
  8. Reference: CLI Argument Parser

    main

    The following arguments are available via the command line:

    FlagLong FlagTypeDescription
    -n--namestrPostfix for logdir
    -r--resumestrResume from logdir or checkpoint in logdir
    -r2--resume2strResume from logdir or checkpoint in logdir
    -b--baselistPaths to base configs (loaded left-to-right)
    -t--trainboolEnable training
    --no-test--no-testboolDisable test phase
    -p--projectstrName of new or path to existing project
    -d--debugboolEnable post-mortem debugging
    -s--seedintSeed for seed_everything (default: 23)
    -f--postfixstrPost-postfix for default name
    -l--logdirstrDirectory for logging (default: logs)
    --scale_lr--scale_lrboolScale base-lr by ngpu * batch_size * n_accumulate (default: True)
  9. ImageLogger Callback

    main

    The ImageLogger callback is used to log images during training and validation. It can log to local files or to a logger like TestTubeLogger.

    Key Parameters:

    • batch_frequency: How often to log (in batches).
    • max_images: Maximum number of images to log per batch.
    • clamp: If True, clamps images to [-1, 1].
    • rescale: If True, rescales images from [-1, 1] to [0, 1] for visualization.
    • log_on_batch_idx: If True, uses batch_idx for frequency check; otherwise uses global_step.
    • log_first_step: If True, logs the very first step regardless of frequency.