ash12321 commited on
Commit
ddde5ea
·
verified ·
1 Parent(s): 4f8ebb4

Upload folder using huggingface_hub

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