--- license: mit tags: - deepfake-detection - computer-vision - transfer-learning - hybrid-cnn - pytorch datasets: - xhlulu/140k-real-and-fake-faces metrics: - accuracy --- # Deepfake Face Detector A hybrid CNN model for detecting AI-generated (deepfake) face images, built using transfer learning on a dual-stream architecture. ## Model Architecture This model combines two parallel CNN streams: - **Spatial Stream** — EfficientNet-B4 pretrained on ImageNet, fine-tuned to detect visible artifacts such as unnatural skin texture, blending edges, and facial inconsistencies - **Frequency Stream** — Xception backbone with fixed SRM (Steganalysis Rich Model) filters to detect invisible GAN noise fingerprints in pixel residuals - **Fusion Module** — Both stream outputs are concatenated (1792 + 2048 = 3840 features) and passed through a classification head with Dropout regularization - **Output** — Sigmoid activation producing a probability score (0 = fake, 1 = real) ## Training Strategy Three-phase transfer learning approach: | Phase | Layers Trained | Learning Rate | Epochs | |-------|---------------|---------------|--------| | 1 | Classifier head only | 1e-3 | 3 | | 2 | Top EfficientNet blocks + classifier | 1e-4 | 3 | ## Dataset Trained and evaluated on [140k Real and Fake Faces](https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces) | Split | Real | Fake | Total | |-------|------|------|-------| | Train | 50,000 | 50,000 | 100,000 | | Valid | 10,000 | 10,000 | 20,000 | | Test | 10,000 | 10,000 | 20,000 | Fake images are StyleGAN2-generated faces. Real images are sourced from Flickr-Faces-HQ (FFHQ). ## Performance | Metric | Score | |--------|-------| | Test Accuracy | 98.59% | | Validation Accuracy | 98.55% | ## Usage ```python import torch from torchvision import transforms from PIL import Image model = HybridDeepfakeDetector() model.load_state_dict(torch.load("deepfake_detector_phase2.pth", map_location="cpu")) model.eval() transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) image = Image.open("face.jpg").convert("RGB") tensor = transform(image).unsqueeze(0) with torch.no_grad(): prob = model(tensor).item() label = "REAL" if prob > 0.5 else "FAKE" print(f"{label} ({prob*100:.1f}% confidence)") ``` ## Limitations - Optimized for StyleGAN2 generated faces — may perform differently on other GAN architectures - Best results on frontal face images - Not tested on video deepfakes ## Try It Live demo available at [Hugging Face Spaces](https://huggingface.co/spaces/AdityaManojShinde/deepfake-detector-app)