File size: 8,585 Bytes
ddde5ea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | # Deepfake Detector V8 - Foundation Model
**Baseline accuracy: 100.00%** | **F1-Score: 1.0000**
This is a foundation model designed for continuous improvement. It provides a solid starting point with room for enhancement.
## π Performance
- **Accuracy**: 100.00%
- **F1-Score**: 1.0000
- **Precision**: 1.0000
- **Recall**: 1.0000
- **Training Samples**: 4,800
- **Validation Samples**: 1,200
## ποΈ Architecture
- **Backbone**: EfficientNetV2-S (pretrained on ImageNet)
- **Classifier**: 3-layer MLP with BatchNorm and Dropout
- **Input Size**: 224x224 RGB images
- **Output**: Binary classification (Real vs Fake)
## π Files in This Folder
```
./deepfake_v8_model/
βββ model.safetensors β Model weights (HuggingFace format)
βββ pytorch_model.bin β Fallback weights (if safetensors unavailable)
βββ optimizer.pt β Optimizer state for continuing training
βββ scheduler.pt β Learning rate scheduler state
βββ config.json β Model architecture configuration
βββ training_args.json β All hyperparameters used
βββ rng_state.pth β Random states for reproducibility
βββ metrics.json β Performance metrics
βββ tokenizer.json β HuggingFace compatibility
βββ tokenizer_config.json β HuggingFace compatibility
βββ README.md β This file
```
## π How to Use This Model
### Option 1: Inference (Predict on New Images)
```python
import torch
import torch.nn as nn
import timm
from PIL import Image
from torchvision import transforms
# Define model architecture
class DeepfakeDetector(nn.Module):
def __init__(self, dropout=0.5):
super().__init__()
self.backbone = timm.create_model('tf_efficientnetv2_s', pretrained=False, num_classes=0)
self.classifier = nn.Sequential(
nn.Linear(1280, 512), nn.BatchNorm1d(512), nn.SiLU(), nn.Dropout(dropout),
nn.Linear(512, 256), nn.BatchNorm1d(256), nn.SiLU(), nn.Dropout(dropout * 0.8),
nn.Linear(256, 1)
)
def forward(self, x):
return self.classifier(self.backbone(x)).squeeze(-1)
# Load model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DeepfakeDetector(0.5)
try:
from safetensors.torch import load_file
state_dict = load_file('./deepfake_v8_model/model.safetensors')
except:
state_dict = torch.load('./deepfake_v8_model/pytorch_model.bin', map_location=device)
model.load_state_dict(state_dict)
model = model.to(device)
model.eval()
# Predict on image
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
img = Image.open('test_image.jpg').convert('RGB')
img_tensor = transform(img).unsqueeze(0).to(device)
with torch.no_grad():
logit = model(img_tensor)
prob = torch.sigmoid(logit).item()
if prob > 0.7:
print(f"π΄ LIKELY FAKE ({prob:.1%} confidence)")
elif prob > 0.5:
print(f"β οΈ POSSIBLY FAKE ({prob:.1%} confidence)")
elif prob > 0.3:
print(f"β οΈ POSSIBLY REAL ({(1-prob):.1%} confidence)")
else:
print(f"π’ LIKELY REAL ({(1-prob):.1%} confidence)")
```
### Option 2: Continue Training (Improve the Model)
```python
import torch
import torch.nn as nn
import torch.optim as optim
import timm
import json
import random
import numpy as np
# Load training configuration
with open('./deepfake_v8_model/training_args.json', 'r') as f:
config = json.load(f)
# Define model (same architecture as above)
class DeepfakeDetector(nn.Module):
def __init__(self, dropout=0.5):
super().__init__()
self.backbone = timm.create_model('tf_efficientnetv2_s', pretrained=False, num_classes=0)
self.classifier = nn.Sequential(
nn.Linear(1280, 512), nn.BatchNorm1d(512), nn.SiLU(), nn.Dropout(dropout),
nn.Linear(512, 256), nn.BatchNorm1d(256), nn.SiLU(), nn.Dropout(dropout * 0.8),
nn.Linear(256, 1)
)
def forward(self, x):
return self.classifier(self.backbone(x)).squeeze(-1)
# Load model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DeepfakeDetector(config['dropout'])
try:
from safetensors.torch import load_file
state_dict = load_file('./deepfake_v8_model/model.safetensors')
except:
state_dict = torch.load('./deepfake_v8_model/pytorch_model.bin', map_location=device)
model.load_state_dict(state_dict)
model = model.to(device)
# Load optimizer
optimizer = optim.AdamW(
filter(lambda p: p.requires_grad, model.parameters()),
lr=config['learning_rate'] * 0.1 # Lower LR for fine-tuning
)
optimizer.load_state_dict(torch.load('./deepfake_v8_model/optimizer.pt'))
# Load scheduler
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=config['epochs'])
scheduler.load_state_dict(torch.load('./deepfake_v8_model/scheduler.pt'))
# Load RNG state for reproducibility
rng_state = torch.load('./deepfake_v8_model/rng_state.pth')
random.setstate(rng_state['python'])
np.random.set_state(rng_state['numpy'])
torch.set_rng_state(rng_state['torch'])
if torch.cuda.is_available() and rng_state['torch_cuda']:
torch.cuda.set_rng_state_all(rng_state['torch_cuda'])
print("β Model loaded and ready for continued training!")
# Now continue training with:
# - More epochs
# - More/better data
# - Different augmentations
# - Fine-tuning strategies
```
## π€ How to Upload to HuggingFace
### Step 1: Install HuggingFace CLI
```bash
pip install -U huggingface_hub
```
### Step 2: Login to HuggingFace
```python
from huggingface_hub import notebook_login
notebook_login()
```
Or via CLI:
```bash
huggingface-cli login
```
### Step 3: Upload Your Model
```python
from huggingface_hub import HfApi
api = HfApi()
api.upload_folder(
folder_path="./deepfake_v8_model",
repo_id="YOUR_USERNAME/deepfake-detector-v8",
repo_type="model"
)
print("β Model uploaded to HuggingFace!")
print("Visit: https://huggingface.co/YOUR_USERNAME/deepfake-detector-v8")
```
### Step 4: Load from HuggingFace in New Notebook
```python
from huggingface_hub import hf_hub_download
import torch
# Download files
model_path = hf_hub_download(
repo_id="YOUR_USERNAME/deepfake-detector-v8",
filename="model.safetensors"
)
optimizer_path = hf_hub_download(
repo_id="YOUR_USERNAME/deepfake-detector-v8",
filename="optimizer.pt"
)
# Load and continue training...
```
## π‘ How to Improve This Model
### 1. Add More Real Data
```python
# In new notebook, load more datasets
from datasets import load_dataset
# Add real AI-generated images
stable_diffusion_images = load_dataset("your_sd_dataset")
dalle_images = load_dataset("your_dalle_dataset")
# Combine with existing model and retrain
```
### 2. Train Longer
```python
# Load model (as shown above)
# Then train for 5-10 more epochs
CONFIG['epochs'] = 10 # or more
# Continue training loop...
```
### 3. Unfreeze More Layers
```python
# Unfreeze all backbone layers for fine-tuning
for param in model.backbone.parameters():
param.requires_grad = True
# Use lower learning rate
optimizer = optim.AdamW(model.parameters(), lr=1e-5)
```
### 4. Use Real Deepfakes
```python
# Load actual deepfake datasets
# - FaceForensics++
# - Celeb-DF
# - DFDC
# And retrain on real deepfakes
```
### 5. Ensemble Multiple Models
```python
# Train multiple versions
# Average predictions for better accuracy
```
## π― Improvement Roadmap
**Current**: 100.0% (Foundation)
**Next Steps**:
1. **Add 10K more samples** β Target: 93-95%
2. **Train 5 more epochs** β Target: 94-96%
3. **Add real AI images** β Target: 95-97%
4. **Fine-tune all layers** β Target: 96-98%
5. **Ensemble 3 models** β Target: 97-99%
## β οΈ Important Notes
- This model was trained on **synthetic fakes**
- For production use, train on **real AI-generated images**
- Use **confidence thresholds** (>0.7 for high confidence)
- Always **validate on diverse test sets**
- Consider **ethical implications** of deployment
## π License
MIT License - Free to use with attribution
## π€ Contributing
This is a foundation model meant to be improved! Feel free to:
- Add more training data
- Experiment with architectures
- Share your improvements
- Create better versions
## π§ Contact
For questions or improvements, open an issue on the repository.
---
**Generated by**: Deepfake Detector V8
**Training Time**: 35.1 minutes
**Date**: 2025-10-22 20:27:12
|