File size: 9,169 Bytes
c37abab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# DeepFake Detector V13 🎯

**State-of-the-art deepfake detection ensemble with 699M parameters**

[![Model](https://img.shields.io/badge/Model-V13-blue)](https://huggingface.co/ash12321/deepfake-detector-v13)
[![Parameters](https://img.shields.io/badge/Parameters-699M-green)](https://huggingface.co/ash12321/deepfake-detector-v13)
[![F1 Score](https://img.shields.io/badge/F1-0.9313-brightgreen)](https://huggingface.co/ash12321/deepfake-detector-v13)

## πŸš€ Performance Highlights

- **Average Ensemble F1**: 0.9313
- **Best Model F1**: 0.9586 (Model 13.3 - Swin-Large)
- **Total Parameters**: 699M (exceeds 500M requirement βœ…)
- **Training Time**: ~6.1 hours on T4 GPU

## πŸ“Š Architecture

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 Files

- `model_1.safetensors` - ConvNeXt-Large (752 MB)
- `model_2.safetensors` - ViT-Large (1159 MB)
- `model_3.safetensors` - Swin-Large (747 MB)

## 🎯 Usage

### Installation

```bash
pip install torch torchvision timm safetensors pillow
```

### Quick Start - Single Model

```python
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%}")
```

### Full Ensemble (Recommended)

```python
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']]}")
```

## πŸ“ˆ Training Details

### Architecture Design

Each model uses:
- **Backbone**: Large pre-trained vision model (frozen initially, fine-tuned)
- **Classifier Head**: 
  - Linear(feat_dim β†’ 512) + BatchNorm + GELU + Dropout
  - Linear(512 β†’ 128) + BatchNorm + GELU + Dropout
  - Linear(128 β†’ 1)

### Training Configuration

- **Loss Function**: Focal Loss with Label Smoothing
  - Alpha: 0.25
  - Gamma: 2.5
  - Label Smoothing: 0.12
- **Optimizer**: AdamW
  - Learning Rates: [2e-5, 1.5e-5, 1.8e-5]
  - Weight Decay: 3e-4
- **Scheduler**: CosineAnnealingWarmRestarts (T_0=3, T_mult=2)
- **Epochs**: 10 per model
- **Batch Sizes**: [32, 24, 32]
- **Mixed Precision**: FP16 enabled
- **Gradient Accumulation**: 4 steps
- **Gradient Checkpointing**: Enabled (memory efficiency)

### Data Augmentation

- Random Horizontal Flip (p=0.5)
- Random Rotation (Β±12Β°)
- Color Jitter (brightness, contrast, saturation: Β±0.15)
- Normalization: ImageNet stats

## πŸ“Š Performance Analysis

### Model Comparison

**Model 13.1 (ConvNeXt-Large)**
- βœ“ Solid baseline: F1 = 0.8971
- βœ“ CNN-based architecture
- βœ“ Good for local feature extraction

**Model 13.2 (ViT-Large)**
- βœ“ Strong performance: F1 = 0.9382
- βœ“ Fastest training (52.7 min)
- βœ“ Global attention mechanism

**Model 13.3 (Swin-Large)** ⭐ **Best Model**
- βœ“ Excellent performance: F1 = 0.9586
- βœ“ Hierarchical vision transformer
- βœ“ Best balance of accuracy and efficiency

### Ensemble Benefits

The ensemble approach provides:
- **Improved Robustness**: Different architectures capture different patterns
- **Reduced Variance**: Averaging reduces prediction noise
- **Better Generalization**: Complementary strengths minimize overfitting
- **Higher Accuracy**: Expected ensemble F1 β‰ˆ 0.94-0.96

## πŸ”§ System Requirements

**Inference (Single Model)**
- GPU: 4GB+ VRAM
- RAM: 8GB+
- Storage: ~1.2 GB per model

**Inference (Full Ensemble)**
- GPU: 12GB+ VRAM (or run models sequentially on smaller GPU)
- RAM: 16GB+
- Storage: ~2.7 GB total

**Training**
- GPU: T4 (16GB) or better
- RAM: 12GB+
- Storage: 8GB+ for checkpoints

## πŸ“š Dataset

Trained on: [`ash12321/deepfake-v13-dataset`](https://huggingface.co/datasets/ash12321/deepfake-v13-dataset)

## πŸ”— Related Models

- Predecessor: [`ash12321/deepfake-detector-v12`](https://huggingface.co/ash12321/deepfake-detector-v12)

## πŸ“„ Citation

```bibtex
@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}}
}
```

## πŸ“ License

MIT License - See LICENSE file for details

## πŸ™ Acknowledgments

- Built with PyTorch, timm, and Hugging Face
- Trained on Google Colab T4 GPU
- Architectures: ConvNeXt (Meta), ViT (Google), Swin (Microsoft)

---

**Model Version**: 13.0  
**Last Updated**: November 2024  
**Status**: Production Ready βœ