Downloads ยท 30 days
9
5% of all-time downloads
ash12321/deepfake-detector-v13
deepfake-detector-v13 is a machine learning model from ash12321. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
State-of-the-art deepfake detection ensemble with 699M parameters
Downloads ยท 30 days
9
5% of all-time downloads
All-time downloads
197
Public
Repo size
2.8 GB
Likes
0
Public
Click a slice to open those files.
.safetensors2.8 GB ยท 100%
From the Hugging Face model README
State-of-the-art deepfake detection ensemble with 699M parameters
This model consists of 3 large-scale transformer and CNN models trained sequentially:
| Model | Backbone | Parameters | F1 Score | Training Time |
|---|---|---|---|---|
| Model 13.1 | ConvNeXt-Large | 198M | 0.8971 | 205.7 min |
| Model 13.2 | ViT-Large | 304M | 0.9382 | 52.7 min |
| Model 13.3 | Swin-Large | 197M | 0.9586 | 106.2 min |
Total: 699M parameters
model_1.safetensors - ConvNeXt-Large (752 MB)model_2.safetensors - ViT-Large (1159 MB)model_3.safetensors - Swin-Large (747 MB)pip install torch torchvision timm safetensors pillow
import torch
import timm
from PIL import Image
from torchvision import transforms
from safetensors.torch import load_file
# Define model architecture
class DeepfakeDetector(torch.nn.Module):
def __init__(self, backbone_name, dropout=0.3):
super().__init__()
self.backbone = timm.create_model(backbone_name, pretrained=False, num_classes=0)
if hasattr(self.backbone, 'num_features'):
feat_dim = self.backbone.num_features
else:
with torch.no_grad():
feat_dim = self.backbone(torch.randn(1, 3, 224, 224)).shape[1]
self.classifier = torch.nn.Sequential(
torch.nn.Linear(feat_dim, 512),
torch.nn.BatchNorm1d(512),
torch.nn.GELU(),
torch.nn.Dropout(dropout),
torch.nn.Linear(512, 128),
torch.nn.BatchNorm1d(128),
torch.nn.GELU(),
torch.nn.Dropout(dropout * 0.5),
torch.nn.Linear(128, 1)
)
def forward(self, x):
features = self.backbone(x)
return self.classifier(features).squeeze(-1)
# Load best model (Model 13.3 - Swin-Large)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DeepfakeDetector('swin_large_patch4_window7_224', dropout=0.3)
state_dict = load_file('model_3.safetensors')
model.load_state_dict(state_dict)
model = model.to(device)
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])
])
# Predict
image = Image.open('test_image.jpg').convert('RGB')
input_tensor = transform(image).unsqueeze(0).to(device)
with torch.no_grad():
logits = model(input_tensor)
probability = torch.sigmoid(logits).item()
prediction = 'FAKE' if probability > 0.5 else 'REAL'
print(f"Prediction: {prediction}")
print(f"Confidence: {probability:.2%}")
import torch
import timm
from PIL import Image
from torchvision import transforms
from safetensors.torch import load_file
class DeepfakeDetector(torch.nn.Module):
def __init__(self, backbone_name, dropout=0.3):
super().__init__()
self.backbone = timm.create_model(backbone_name, pretrained=False, num_classes=0)
if hasattr(self.backbone, 'num_features'):
feat_dim = self.backbone.num_features
else:
with torch.no_grad():
feat_dim = self.backbone(torch.randn(1, 3, 224, 224)).shape[1]
self.classifier = torch.nn.Sequential(
torch.nn.Linear(feat_dim, 512),
torch.nn.BatchNorm1d(512),
torch.nn.GELU(),
torch.nn.Dropout(dropout),
torch.nn.Linear(512, 128),
torch.nn.BatchNorm1d(128),
torch.nn.GELU(),
torch.nn.Dropout(dropout * 0.5),
torch.nn.Linear(128, 1)
)
def forward(self, x):
features = self.backbone(x)
return self.classifier(features).squeeze(-1)
# Model configurations
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
configs = [
('convnext_large', 0.3, 'model_1.safetensors'),
('vit_large_patch16_224', 0.35, 'model_2.safetensors'),
('swin_large_patch4_window7_224', 0.3, 'model_3.safetensors')
]
# Load all models
models = []
for backbone, dropout, filename in configs:
model = DeepfakeDetector(backbone, dropout)
state_dict = load_file(filename)
model.load_state_dict(state_dict)
model = model.to(device)
model.eval()
models.append(model)
print(f"โ Loaded {len(models)} models")
# Preprocessing
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# Ensemble prediction
def predict_ensemble(image_path):
image = Image.open(image_path).convert('RGB')
input_tensor = transform(image).unsqueeze(0).to(device)
predictions = []
with torch.no_grad():
for model in models:
logits = model(input_tensor)
prob = torch.sigmoid(logits).item()
predictions.append(prob)
# Average ensemble
avg_prob = sum(predictions) / len(predictions)
prediction = 'FAKE' if avg_prob > 0.5 else 'REAL'
return {
'prediction': prediction,
'confidence': avg_prob,
'individual_predictions': predictions
}
# Use it
result = predict_ensemble('test_image.jpg')
print(f"Prediction: {result['prediction']}")
print(f"Ensemble Confidence: {result['confidence']:.2%}")
print(f"Individual Models: {[f'{p:.2%}' for p in result['individual_predictions']]}")
Each model uses:
Model 13.1 (ConvNeXt-Large)
Model 13.2 (ViT-Large)
Model 13.3 (Swin-Large) โญ Best Model
The ensemble approach provides:
Inference (Single Model)
Inference (Full Ensemble)
Training
Trained on: ash12321/deepfake-v13-dataset
ash12321/deepfake-detector-v12@model{v13-deepfake-detector,
title={DeepFake Detector V13: Large-Scale Ensemble},
author={Ash},
year={2024},
publisher={Hugging Face},
howpublished={\url{https://huggingface.co/ash12321/deepfake-detector-v13}}
}
MIT License - See LICENSE file for details
Model Version: 13.0
Last Updated: November 2024
Status: Production Ready โ