Views
No views yet
🔴 Live demo: Forensa — Deepfake Detection App
| 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) |
1import torch.nn as nn
2import timm
3
4class DeepfakeDetector(nn.Module):
5 def __init__(self):
6 super().__init__()
7 self.backbone = timm.create_model('efficientnet_b4', pretrained=False, num_classes=0)
8 self.classifier = nn.Sequential(
9 nn.Linear(self.backbone.num_features, 256),
10 nn.ReLU(),
11 nn.Dropout(0.4),
12 nn.Linear(256, 1),
13 nn.Sigmoid()
14 )
15
16 def forward(self, x):
17 return self.classifier(self.backbone(x))1import torch
2import timm
3import torch.nn as nn
4from torchvision import transforms
5from PIL import Image
6from huggingface_hub import hf_hub_download
7
8# Model definition
9class DeepfakeDetector(nn.Module):
10 def __init__(self):
11 super().__init__()
12 self.backbone = timm.create_model('efficientnet_b4', pretrained=False, num_classes=0)
13 self.classifier = nn.Sequential(
14 nn.Linear(self.backbone.num_features, 256),
15 nn.ReLU(),
16 nn.Dropout(0.4),
17 nn.Linear(256, 1),
18 nn.Sigmoid()
19 )
20 def forward(self, x):
21 return self.classifier(self.backbone(x))
22
23# Load model
24model_path = hf_hub_download(repo_id="Yashikaysn29/deepshield", filename="best_model.pth")
25model = DeepfakeDetector()
26model.load_state_dict(torch.load(model_path, map_location='cpu'))
27model.eval()
28
29# Preprocessing
30transform = transforms.Compose([
31 transforms.Resize((224, 224)),
32 transforms.ToTensor(),
33 transforms.Normalize([0.485, 0.456, 0.406],
34 [0.229, 0.224, 0.225])
35])
36
37# Inference
38def predict(image_path):
39 img = Image.open(image_path).convert("RGB")
40 tensor = transform(img).unsqueeze(0)
41 with torch.no_grad():
42 prob = model(tensor).item()
43 label = "REAL" if prob >= 0.5 else "FAKE"
44 confidence = prob * 100 if prob >= 0.5 else (1 - prob) * 100
45 return label, round(confidence, 2)
46
47label, confidence = predict("your_image.jpg")
48print(f"{label} — {confidence}% confidence")| Metric | Value |
|---|---|
| Validation Accuracy | ~99% |
| Task | Binary Classification |
| Threshold | 0.5 (score ≥ 0.5 = REAL) |