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pytorch
|
| 10 |
+
- research-only
|
| 11 |
+
- vit
|
| 12 |
+
- cross-attention
|
| 13 |
+
language:
|
| 14 |
+
- en
|
| 15 |
+
library_name: pytorch
|
| 16 |
+
pipeline_tag: image-to-image
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# FaceForge Generator: Vision Transformer-based Face Manipulation
|
| 20 |
+
|
| 21 |
+
[](https://doi.org/10.5281/zenodo.18530439)
|
| 22 |
+
[](https://github.com/Huzaifanasir95/FaceForge)
|
| 23 |
+
[](https://opensource.org/licenses/MIT)
|
| 24 |
+
|
| 25 |
+
π¨ **252M Parameters | ViT-Based | Baseline Training Complete**
|
| 26 |
+
|
| 27 |
+
β οΈ **RESEARCH USE ONLY** - This model is for academic research and developing detection systems.
|
| 28 |
+
|
| 29 |
+
## Model Description
|
| 30 |
+
|
| 31 |
+
FaceForge Generator is a sophisticated Vision Transformer-based facial manipulation system that learns to synthesize realistic face swaps. The model combines dual ViT encoders, cross-attention mechanisms, transformer decoders, and CNN upsamplers to generate high-quality facial manipulations.
|
| 32 |
+
|
| 33 |
+
**Key Features:**
|
| 34 |
+
- ποΈ 252 million trainable parameters
|
| 35 |
+
- π Dual encoder architecture for source and target faces
|
| 36 |
+
- π― Cross-attention fusion mechanism
|
| 37 |
+
- πΌοΈ Generates 224Γ224 RGB face images
|
| 38 |
+
- β‘ ~300ms inference time per image
|
| 39 |
+
- π Achieved 0.204 validation loss after 3 epochs
|
| 40 |
+
|
| 41 |
+
## Model Architecture
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
FaceForge Generator (252.5M parameters)
|
| 45 |
+
β
|
| 46 |
+
βββ ViT Encoders (172M params)
|
| 47 |
+
β βββ Source Encoder: ViT-B/16 (86M)
|
| 48 |
+
β β βββ 12 layers, 768-dim, 12 heads
|
| 49 |
+
β βββ Target Encoder: ViT-B/16 (86M)
|
| 50 |
+
β βββ 12 layers, 768-dim, 12 heads
|
| 51 |
+
β
|
| 52 |
+
βββ Cross-Attention Module (14M params)
|
| 53 |
+
β βββ 2 layers, 8 heads
|
| 54 |
+
β βββ FFN: 768 β 3072 β 768
|
| 55 |
+
β βββ Dropout: 0.1
|
| 56 |
+
β
|
| 57 |
+
βββ Transformer Decoder (58M params)
|
| 58 |
+
β βββ 256 learnable queries (16Γ16)
|
| 59 |
+
β βββ 6 decoder layers, 8 heads
|
| 60 |
+
β βββ 2D positional embeddings
|
| 61 |
+
β
|
| 62 |
+
βββ CNN Upsampler (9M params)
|
| 63 |
+
βββ TransposeConv: 768β512β256β128β64
|
| 64 |
+
βββ 4 upsampling stages (16Γ16 β 224Γ224)
|
| 65 |
+
βββ Conv: 64β32β3 + Tanh
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
## Training Progress
|
| 69 |
+
|
| 70 |
+
### Baseline Training (3 Epochs)
|
| 71 |
+
|
| 72 |
+
| Epoch | Train Loss | Val Loss | Time (min) |
|
| 73 |
+
|-------|-----------|----------|------------|
|
| 74 |
+
| 1 | 0.2873 | 0.2804 | 227.5 |
|
| 75 |
+
| 2 | 0.2432 | 0.2304 | 231.2 |
|
| 76 |
+
| 3 | 0.2143 | 0.2043 | 228.8 |
|
| 77 |
+
|
| 78 |
+
**Total Training Time:** 11.5 hours (687.5 minutes)
|
| 79 |
+
|
| 80 |
+
### Loss Reduction
|
| 81 |
+
- Training loss: 0.287 β 0.214 (25.3% reduction)
|
| 82 |
+
- Validation loss: 0.280 β 0.204 (27.1% reduction)
|
| 83 |
+
- Minimal overfitting (train-val gap: 0.010)
|
| 84 |
+
|
| 85 |
+
## Usage
|
| 86 |
+
|
| 87 |
+
### Installation
|
| 88 |
+
|
| 89 |
+
```bash
|
| 90 |
+
pip install torch torchvision timm pillow numpy
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### Loading the Model
|
| 94 |
+
|
| 95 |
+
```python
|
| 96 |
+
import torch
|
| 97 |
+
import torch.nn as nn
|
| 98 |
+
import timm
|
| 99 |
+
from torchvision import transforms
|
| 100 |
+
|
| 101 |
+
class FaceForgeGenerator(nn.Module):
|
| 102 |
+
def __init__(self):
|
| 103 |
+
super().__init__()
|
| 104 |
+
# Source and Target ViT Encoders
|
| 105 |
+
self.source_encoder = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=0)
|
| 106 |
+
self.target_encoder = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=0)
|
| 107 |
+
|
| 108 |
+
# Cross-attention (implement your architecture)
|
| 109 |
+
# Transformer decoder
|
| 110 |
+
# CNN upsampler
|
| 111 |
+
# ... (see full architecture in paper)
|
| 112 |
+
|
| 113 |
+
def forward(self, source_face, target_face):
|
| 114 |
+
# Encode both faces
|
| 115 |
+
source_features = self.source_encoder.forward_features(source_face)
|
| 116 |
+
target_features = self.target_encoder.forward_features(target_face)
|
| 117 |
+
|
| 118 |
+
# Cross-attention fusion
|
| 119 |
+
fused_features = self.cross_attention(source_features, target_features)
|
| 120 |
+
|
| 121 |
+
# Decode to spatial map
|
| 122 |
+
spatial_features = self.transformer_decoder(fused_features)
|
| 123 |
+
|
| 124 |
+
# Upsample to 224Γ224
|
| 125 |
+
generated_face = self.cnn_upsampler(spatial_features)
|
| 126 |
+
|
| 127 |
+
return generated_face
|
| 128 |
+
|
| 129 |
+
# Load checkpoint
|
| 130 |
+
model = FaceForgeGenerator()
|
| 131 |
+
checkpoint = torch.load('generator_best.pth', map_location='cpu')
|
| 132 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
| 133 |
+
model.eval()
|
| 134 |
+
|
| 135 |
+
# Preprocessing
|
| 136 |
+
transform = transforms.Compose([
|
| 137 |
+
transforms.Resize((224, 224)),
|
| 138 |
+
transforms.ToTensor(),
|
| 139 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
| 140 |
+
])
|
| 141 |
+
|
| 142 |
+
# Generate face swap
|
| 143 |
+
def generate_face_swap(source_path, target_path):
|
| 144 |
+
source = transform(Image.open(source_path).convert('RGB')).unsqueeze(0)
|
| 145 |
+
target = transform(Image.open(target_path).convert('RGB')).unsqueeze(0)
|
| 146 |
+
|
| 147 |
+
with torch.no_grad():
|
| 148 |
+
generated = model(source, target)
|
| 149 |
+
|
| 150 |
+
# Denormalize and convert to PIL
|
| 151 |
+
generated = (generated[0] * 0.5 + 0.5).clamp(0, 1)
|
| 152 |
+
generated = transforms.ToPILImage()(generated)
|
| 153 |
+
|
| 154 |
+
return generated
|
| 155 |
+
|
| 156 |
+
# Example
|
| 157 |
+
result = generate_face_swap("source.jpg", "target.jpg")
|
| 158 |
+
result.save("generated.jpg")
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
## Training Details
|
| 162 |
+
|
| 163 |
+
### Dataset
|
| 164 |
+
- **Source:** FaceForensics++ (c40 compression)
|
| 165 |
+
- **Training:** 7,000 face images (triplets: source, target, ground truth)
|
| 166 |
+
- **Validation:** 1,500 face images
|
| 167 |
+
- **Resolution:** 224Γ224 RGB
|
| 168 |
+
|
| 169 |
+
### Hyperparameters
|
| 170 |
+
```yaml
|
| 171 |
+
optimizer: AdamW
|
| 172 |
+
learning_rate: 1e-4
|
| 173 |
+
betas: [0.9, 0.999]
|
| 174 |
+
weight_decay: 1e-4
|
| 175 |
+
batch_size: 16
|
| 176 |
+
epochs: 3 (baseline)
|
| 177 |
+
loss_function: L1 (Mean Absolute Error)
|
| 178 |
+
lr_schedule: Cosine Annealing (1e-4 β 1e-6)
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
### Training Configuration
|
| 182 |
+
- **Hardware:** CPU
|
| 183 |
+
- **Throughput:** ~32 samples/minute
|
| 184 |
+
- **Batch Processing:** 219 train batches, 47 val batches per epoch
|
| 185 |
+
- **Best Model:** Saved at epoch 3
|
| 186 |
+
|
| 187 |
+
## Current Status
|
| 188 |
+
|
| 189 |
+
β οΈ **Baseline Training:** This model has completed 3 epochs of baseline training. For production-quality face generation, extended training (15-20 epochs) is recommended.
|
| 190 |
+
|
| 191 |
+
**Current Capabilities:**
|
| 192 |
+
- β
Learns pose transfer
|
| 193 |
+
- β
Captures facial structures
|
| 194 |
+
- β
Shows convergence trend
|
| 195 |
+
- β³ Some blur in generated images (expected at baseline)
|
| 196 |
+
- β³ Benefits from extended training
|
| 197 |
+
|
| 198 |
+
## Use Cases
|
| 199 |
+
|
| 200 |
+
### Research Applications
|
| 201 |
+
1. **Detector Training:** Generate challenging samples for deepfake detection
|
| 202 |
+
2. **Adversarial Training:** Min-max game with detector
|
| 203 |
+
3. **Understanding Manipulation:** Study how synthetic faces are created
|
| 204 |
+
4. **Benchmark Creation:** Generate test sets for evaluation
|
| 205 |
+
|
| 206 |
+
### Educational Uses
|
| 207 |
+
- Demonstrate face generation techniques
|
| 208 |
+
- Teach computer vision concepts
|
| 209 |
+
- Illustrate transformer architectures
|
| 210 |
+
- Show attention mechanism visualization
|
| 211 |
+
|
| 212 |
+
## Limitations
|
| 213 |
+
|
| 214 |
+
1. **Training Duration:** Only 3 epochs completed; extended training needed for photo-realism
|
| 215 |
+
2. **Blur:** Generated faces show some blur at baseline stage
|
| 216 |
+
3. **Dataset Scale:** Trained on 10K images; larger datasets would improve quality
|
| 217 |
+
4. **Single Frame:** Doesn't consider temporal consistency for video
|
| 218 |
+
5. **Compute:** Large model (252M params) requires significant memory
|
| 219 |
+
|
| 220 |
+
## Ethical Guidelines
|
| 221 |
+
|
| 222 |
+
β οΈ **Responsible Use Required**
|
| 223 |
+
|
| 224 |
+
This model is intended for:
|
| 225 |
+
β
Academic research
|
| 226 |
+
β
Deepfake detection development
|
| 227 |
+
β
Educational demonstrations
|
| 228 |
+
β
Ethical AI studies
|
| 229 |
+
|
| 230 |
+
**Prohibited uses:**
|
| 231 |
+
β Creating misinformation
|
| 232 |
+
β Identity theft or impersonation
|
| 233 |
+
β Non-consensual face manipulation
|
| 234 |
+
β Malicious content creation
|
| 235 |
+
|
| 236 |
+
**Recommendations:**
|
| 237 |
+
- Watermark generated content
|
| 238 |
+
- Maintain audit logs
|
| 239 |
+
- Require user consent
|
| 240 |
+
- Implement content filters
|
| 241 |
+
|
| 242 |
+
## Future Improvements
|
| 243 |
+
|
| 244 |
+
Planned enhancements:
|
| 245 |
+
- [ ] Extended training (15-20 epochs)
|
| 246 |
+
- [ ] Perceptual loss functions (VGG, LPIPS)
|
| 247 |
+
- [ ] GAN-based adversarial training
|
| 248 |
+
- [ ] Multi-scale architecture
|
| 249 |
+
- [ ] Attention visualization
|
| 250 |
+
- [ ] Video temporal consistency
|
| 251 |
+
|
| 252 |
+
## Citation
|
| 253 |
+
|
| 254 |
+
```bibtex
|
| 255 |
+
@techreport{nasir2026faceforge,
|
| 256 |
+
title={FaceForge: A Deep Learning Framework for Facial Manipulation Generation and Detection},
|
| 257 |
+
author={Nasir, Huzaifa},
|
| 258 |
+
institution={National University of Computer and Emerging Sciences},
|
| 259 |
+
year={2026},
|
| 260 |
+
doi={10.5281/zenodo.18530439}
|
| 261 |
+
}
|
| 262 |
+
```
|
| 263 |
+
|
| 264 |
+
## Links
|
| 265 |
+
|
| 266 |
+
- π **Paper:** https://doi.org/10.5281/zenodo.18530439
|
| 267 |
+
- π» **Code:** https://github.com/Huzaifanasir95/FaceForge
|
| 268 |
+
- π **Detector Model:** https://huggingface.co/Huzaifanasir95/faceforge-detector
|
| 269 |
+
- π **Notebooks:** See repository for training/inference notebooks
|
| 270 |
+
|
| 271 |
+
## Architecture Details
|
| 272 |
+
|
| 273 |
+
### Vision Transformer Encoder
|
| 274 |
+
- **Patch Size:** 16Γ16
|
| 275 |
+
- **Patches:** 196 + 1 CLS token
|
| 276 |
+
- **Embedding Dim:** 768
|
| 277 |
+
- **Layers:** 12
|
| 278 |
+
- **Attention Heads:** 12
|
| 279 |
+
- **MLP Ratio:** 4.0
|
| 280 |
+
|
| 281 |
+
### Cross-Attention Mechanism
|
| 282 |
+
- **Query:** Source features
|
| 283 |
+
- **Key/Value:** Target features
|
| 284 |
+
- **Attention:** Multi-head (8 heads)
|
| 285 |
+
- **FFN Expansion:** 4Γ (768 β 3072 β 768)
|
| 286 |
+
|
| 287 |
+
### CNN Upsampler
|
| 288 |
+
- **Input:** 768Γ16Γ16
|
| 289 |
+
- **Output:** 3Γ224Γ224
|
| 290 |
+
- **Stages:** 4 transpose convolutions
|
| 291 |
+
- **Kernel:** 4Γ4, Stride: 2, Padding: 1
|
| 292 |
+
- **Activation:** ReLU β Tanh (output)
|
| 293 |
+
|
| 294 |
+
## License
|
| 295 |
+
|
| 296 |
+
This model is released under CC BY 4.0 license. Use responsibly and ethically.
|
| 297 |
+
|
| 298 |
+
## Author
|
| 299 |
+
|
| 300 |
+
**Huzaifa Nasir**
|
| 301 |
+
National University of Computer and Emerging Sciences (NUCES)
|
| 302 |
+
Islamabad, Pakistan
|
| 303 |
+
π§ nasirhuzaifa95@gmail.com
|
| 304 |
+
|
| 305 |
+
## Acknowledgments
|
| 306 |
+
|
| 307 |
+
- Vision Transformer (Dosovitskiy et al.)
|
| 308 |
+
- FaceForensics++ dataset
|
| 309 |
+
- PyTorch and timm libraries
|
| 310 |
+
- Open-source AI community
|