--- license: mit tags: - deepfake-detection - image-classification - efficientnet - pytorch - computer-vision - timm metrics: - accuracy pipeline_tag: image-classification --- # DeepShield — EfficientNet-B4 Deepfake Detector A fine-tuned **EfficientNet-B4** model for binary deepfake detection, trained on 140,000 real and AI-generated face images. Achieves ~99% validation accuracy. > 🔴 Live demo: [Forensa — Deepfake Detection App](https://huggingface.co/spaces/Yashikaysn29/forensa) --- ## Model Details | Property | Value | |----------|-------| | **Architecture** | EfficientNet-B4 (via `timm`) + custom classification head | | **Task** | Binary image classification (Real vs Fake) | | **Input** | RGB image, resized to 224×224 | | **Output** | Probability score (0 = Fake, 1 = Real) | | **Validation Accuracy** | ~99% | | **Model Size** | 72.8 MB | | **Training Hardware** | Google Colab (Tesla T4 GPU) | --- ## Architecture ```python import torch.nn as nn import timm class DeepfakeDetector(nn.Module): def __init__(self): super().__init__() self.backbone = timm.create_model('efficientnet_b4', pretrained=False, num_classes=0) self.classifier = nn.Sequential( nn.Linear(self.backbone.num_features, 256), nn.ReLU(), nn.Dropout(0.4), nn.Linear(256, 1), nn.Sigmoid() ) def forward(self, x): return self.classifier(self.backbone(x)) ``` --- ## Training - **Dataset**: 140,000 real and AI-generated/deepfaked face images (Kaggle) - **Class balance**: 50% real, 50% fake - **Preprocessing**: Resize to 224×224, ImageNet normalization - **Optimizer**: Adam - **Loss**: Binary Cross-Entropy - **Epochs**: Trained until convergence on validation set - **Augmentation**: Random horizontal flip, rotation, color jitter --- ## Usage ```python import torch import timm import torch.nn as nn from torchvision import transforms from PIL import Image from huggingface_hub import hf_hub_download # Model definition class DeepfakeDetector(nn.Module): def __init__(self): super().__init__() self.backbone = timm.create_model('efficientnet_b4', pretrained=False, num_classes=0) self.classifier = nn.Sequential( nn.Linear(self.backbone.num_features, 256), nn.ReLU(), nn.Dropout(0.4), nn.Linear(256, 1), nn.Sigmoid() ) def forward(self, x): return self.classifier(self.backbone(x)) # Load model model_path = hf_hub_download(repo_id="Yashikaysn29/deepshield", filename="best_model.pth") model = DeepfakeDetector() model.load_state_dict(torch.load(model_path, map_location='cpu')) model.eval() # Preprocessing transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # Inference def predict(image_path): img = Image.open(image_path).convert("RGB") tensor = transform(img).unsqueeze(0) with torch.no_grad(): prob = model(tensor).item() label = "REAL" if prob >= 0.5 else "FAKE" confidence = prob * 100 if prob >= 0.5 else (1 - prob) * 100 return label, round(confidence, 2) label, confidence = predict("your_image.jpg") print(f"{label} — {confidence}% confidence") ``` --- ## Performance | Metric | Value | |--------|-------| | Validation Accuracy | ~99% | | Task | Binary Classification | | Threshold | 0.5 (score ≥ 0.5 = REAL) | --- ## Limitations - Optimized for **face images** — performance may degrade on non-face content - May not generalize to all deepfake generation techniques, especially newer methods - Not intended for use as a sole evidence source in legal or forensic contexts --- ## Live Demo Try the model live via the **Forensa** web app: 👉 [huggingface.co/spaces/Yashikaysn29/forensa](https://huggingface.co/spaces/Yashikaysn29/forensa) Supports image and video input with confidence scoring and session analytics. --- ## About Built by **Yashika Saxena** — B.Tech AI & ML, Institute of Technology and Management, Gwalior (2023–2027). - GitHub: [github.com/Yashikaysn](https://github.com/Yashikaysn) - HF Space: [Forensa](https://huggingface.co/spaces/Yashikaysn29/forensa)