Deep Learning · Audio-Visual · Computer Vision

Audio-Driven
Lip Synchronization

A production-grade pipeline for generating photorealistic lip-synced face videos from arbitrary speech audio, combining a SyncNet sync expert with a U-Net generator trained via perceptual, adversarial, and audio-visual sync losses.

Lip-sync model: melspectrogram and masked face frames enter dual encoders; decoder outputs synthesized frames; pre-trained lip-sync expert and visual discriminator provide sync and adversarial supervision.
Generator overview: audio-visual encoders, skip-connected decoder, L1 reconstruction, frozen SyncNet sync loss, and visual quality discriminator.

Overview

Lip synchronization replaces the mouth region of a talking-head video to match a target audio stream — enabling dubbing, accessibility tools, and avatar animation. This project builds a full end-to-end pipeline: raw video is preprocessed into aligned face crops and 80-channel Mel spectrograms, a SyncNet audio-visual discriminator is trained to detect temporal alignment, and a Wav2Lip U-Net generator is supervised by a three-stage loss — L1 reconstruction, VGG perceptual, adversarial, and SyncNet sync — to produce high-quality, temporally coherent output frames.

Method

🗂️

Data Pipeline

Raw videos are segmented into 3-second clips at 25 fps, then processed through 8 sequential stages: face detection with RetinaNet-ResNet50, 5-point affine alignment to 288×288, FRCRN neural denoising, and librosa Mel extraction.

mel_idx = ⌊i × (80 / fps)⌋
🔊

SyncNet — Sync Expert

Dual-stream encoder producing 512-dim L2-normalized embeddings from 5-frame visual crops (lower half only) and Mel windows. Trained with BCE loss on cosine similarity. Frozen during generator training.

Lsync = BCE(σ(cos(a, v)), y)
🎨

Wav2Lip Generator

A 9-level U-Net takes a 6-channel masked face and a 512-dim audio embedding, producing 288×288 RGB frames via skip-connected transposed convolutions. Optional SAM spatial attention gates skip features to focus on the lip region.

L = L1 + LPIPS + 0.025·Ladv + 0.03·Lsync
Raw Video
.mp4 / .avi
Preprocess
8 stages
Mel + Faces
[N, 80] + 288²
SyncNet
sync expert
Wav2Lip
generator
Output Video
lip-synced

Architecture Detail

Wav2Lip generator encoder–decoder with precise tensor shapes at each stage.

🎙️
Audio Encoder
[B, 1, 80, 16] → [B, 512, 1, 1]
Conv × 3 [B, 32, 80, 16] stride=1, residual×2
Conv ↓ [B, 64, 27, 16] stride=(3,1)
Conv ↓ [B, 128, 9, 6] stride=3
Conv ↓ [B, 256, 3, 3] stride=(3,2)
Conv 3×3 [B, 512, 1, 1] no padding
👤
Face Encoder (U-Net left)
[B, 6, 288, 288] → feat[0..8]
Block 0 [B, 16, 288, 288] feat[0]
Block 1–2 [B, 32, 144→72] feat[1–2]
Block 3–4 [B, 64→128, 36→18] feat[3–4]
Block 5–6 [B, 256→512, 9→5] feat[5–6]
Block 7–8 [B, 512, 3→1] feat[7–8], no pad
🖼️
Face Decoder (U-Net right)
audio+feat[8] → [B, 3, 288, 288]
Fuse [B, 1024, 1, 1] cat(audio, feat[8])
Up ×3 [B, 1024, 3→5→9] + feat[7–5]
Up ×3 [B, 512→320, 18→36→72] + feat[4–2]
Up ×2 [B, 160→96, 144→288] + feat[1–0]
Output [B, 3, 288, 288] Conv80→32→3, Sigmoid
Skip connections: each decoder block concatenates the transposed-conv output with the corresponding encoder feature along the channel dim. Optional SAM (Spatial Attention Module) gates each skip feature by avg+max channel pooling before concatenation, focusing the decoder on the lip region. Each Conv2d block = Conv2d → BatchNorm2d → ReLU; residual adds input before activation.

Training Schedule

Three-phase warm-up strategy to stabilise generator before introducing adversarial and sync supervision.

Phase 1
Reconstruction Only  steps 0 → 15,000

Generator trained with L1 pixel loss and VGG perceptual (LPIPS) loss only. Discriminator and SyncNet gradients are off. Establishes basic face fidelity.

L = L1 + 1.0 × LLPIPS
Phase 2
+ Adversarial  steps 15,000 → 60,000

Discriminator (Wav2LipDiscriminator, 9 nonorm blocks, lower-half input) enters training alongside the generator. Sharpens lip texture realism.

L = Lrecon + 0.025 × softplus(−D(g))
Phase 3
+ Sync Loss  steps 60,000 → 10M

Frozen SyncNet scores each generated frame against the driving audio. BCE on cosine similarity penalises temporal misalignment, completing the full training objective.

L = Lrecon + 0.025·Ladv + 0.03 × BCE(cos(a,v), 1)

Preprocessing Pipeline

8-stage distributed pipeline (Ray) transforming raw video into model-ready tensors.

01
Video Clipping
ffmpeg segments into ~5 s clips resampled to 25 fps
02
Fine-grain Clip
MoviePy cuts into 3 s sub-clips
03
Frame + Audio Extract
JPG frames (qscale=2) + 16 kHz mono WAV
04
Face Detection
RetinaNet-ResNet50 (conf ≥ 0.99) → affine align 288×288
05
Audio Denoising
FRCRN (ModelScope) → enhanced.wav
06
Mel Extraction
librosa STFT → 80-bin Mel, norm [−4, 4]
07
Sync Detection
SyncNet offset + confidence score per clip
Dataset Ready
Aligned faces + Mel NPY arrays

Tech Stack

PyTorch
Model training & inference
Lightning
Training loop & multi-GPU
Ray
Distributed preprocessing
librosa
Mel spectrogram extraction
ffmpeg
Video I/O & frame extraction
DSFD / RetinaNet
Face detection
FRCRN
Neural audio denoising
LPIPS / VGG
Perceptual loss
Real-ESRGAN
Post-process super-res
Hydra
Config management

Hyperparameters

ParameterSyncNetWav2LipDescription
max_steps2,000,00010,000,000Total training steps
batch_size1286Samples per step
learning_rate5e-55e-5AdamW base LR
betas(0.5, 0.999)(0.5, 0.999)Adam momentum terms
schedulerReduceLROnPlateauNoneLR schedule
disc_start15,000Step to enable discriminator
sync_start60,000Step to enable sync loss
disc_wt0.025Adversarial loss weight
sync_wt0.03Sync loss weight
face_size288 (H/2×W)288×288Input face resolution
mel_channels8080Mel spectrogram bins
mel_step_size1616Mel time steps per frame
embed_dim512512Audio / visual embedding dim