FaceDancer: Pose- and Occlusion-Aware High Fidelity Face Swapping
Paper β’ 2210.10473 β’ Published
YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
MobileNetV3-Small encoder + Depthwise-Separable UNet decoder
Designed for real-time on-device inference on Apple Neural Engine (A17+).
Input: [B, 4, 256, 256] β RGB face crop + binary hair mask
β
βββββββββββββββββ€ MobileNetV3-Small Encoder
β β (pretrained ImageNet)
s0 [16,128,128] βββ stage0 βββββ€ stride-2 stem conv
s1 [16, 64, 64] βββ stage1 βββββ€ InvertedResidual
s2 [24, 32, 32] βββ stage2 βββββ€ InvertedResidual Γ2
s3 [40, 16, 16] βββ stage3 βββββ€ InvertedResidual Γ3
s4 [96, 8, 8] βββ stage4 βββββ InvertedResidual Γ5 (bottleneck)
β
β DWS Decoder (bilinearβ + DW conv + PW conv)
d3 [64, 16, 16] βββ dec3(s4+s3)
d2 [48, 32, 32] βββ dec2(d3+s2)
d1 [32, 64, 64] βββ dec1(d2+s1)
d0 [24,128,128] βββ dec0(d1+s0)
β
Output: [B, 3, 256, 256] β RGB bald face (via bilinearβ + 1Γ1 conv + Tanh)
| Metric | Value |
|---|---|
| Parameters | 891K |
| fp16 size | 1.7 MB |
| CoreML package | ~3-4 MB (6-bit palettized) |
| Target latency | <40 ms on A17 ANE |
| Input | 4-ch (RGB + hair mask), 256Γ256 |
| Output | 3-ch RGB, 256Γ256 |
~80K pairs from FFHQ + CelebA-HQ:
Generated via gen_bald_pairs.py pipeline:
# Install dependencies
pip install torch torchvision accelerate lpips onnxruntime-gpu trackio huggingface_hub
# Single GPU (debug)
python train.py --data_dir /path/to/data --batch_size 16 --num_epochs 2
# 8ΓA100 distributed
bash launch_train.sh
import torch
from model import UNetMobile
model = UNetMobile(in_channels=4, out_channels=3, pretrained=False)
state_dict = torch.load('generator_latest.pt', map_location='cpu')
model.load_state_dict(state_dict)
model.eval()
# Input: [1, 4, 256, 256] β RGB in [-1,1] + mask in [0,1]
input_4ch = torch.cat([face_rgb_normalized, hair_mask], dim=1)
with torch.no_grad():
bald_face = model(input_4ch) # [-1, 1]
bald_face = (bald_face + 1) / 2 # β [0, 1]
python export_coreml.py \
--checkpoint generator_latest.pt \
--output UNetMobileBald.mlpackage \
--compress 6bit
let config = MLModelConfiguration()
config.computeUnits = .all // CPU + GPU + ANE
let model = try UNetMobileBald(configuration: config)
// Input: MLMultiArray [1, 4, 256, 256], pixel values 0-255
let prediction = try model.prediction(input_image: inputArray)
let output = prediction.output_image // [1, 3, 256, 256], values 0-255
| File | Description |
|---|---|
model.py |
UNet-Mobile architecture |
losses.py |
L1 + LPIPS + PatchGAN + ArcFace losses |
dataset.py |
Dataset loader for bald-pair data |
train.py |
Full training script (Accelerate DDP) |
export_coreml.py |
PyTorch β CoreML mlprogram conversion |
launch_train.sh |
8ΓA100 launch script |
accelerate_config.yaml |
Accelerate distributed config |
Based on: