Views
No views yet
| Component | Parameters | Trainable |
|---|---|---|
| Swin-Large Backbone | 197M | ❌ Frozen |
| Adapter Layers | 1.5M | ✅ Yes |
| Total | 198.5M | 1.5M |
1import torch
2import timm
3from safetensors.torch import load_file
4from torchvision import transforms
5from PIL import Image
6
7class DeepfakeDetector(torch.nn.Module):
8 def __init__(self):
9 super().__init__()
10 self.backbone = timm.create_model('swin_large_patch4_window7_224',
11 pretrained=False, num_classes=0)
12 feat_dim = 1536
13
14 self.adapter = torch.nn.Sequential(
15 torch.nn.Linear(feat_dim, 512),
16 torch.nn.LayerNorm(512),
17 torch.nn.ReLU(),
18 torch.nn.Dropout(0.1),
19 torch.nn.Linear(512, feat_dim)
20 )
21
22 self.classifier = torch.nn.Sequential(
23 torch.nn.Linear(feat_dim, 512),
24 torch.nn.BatchNorm1d(512),
25 torch.nn.GELU(),
26 torch.nn.Dropout(0.3),
27 torch.nn.Linear(512, 128),
28 torch.nn.BatchNorm1d(128),
29 torch.nn.GELU(),
30 torch.nn.Dropout(0.15),
31 torch.nn.Linear(128, 1)
32 )
33
34 def forward(self, x):
35 features = self.backbone(x)
36 adapted = features + 0.1 * self.adapter(features)
37 return self.classifier(adapted).squeeze(-1)
38
39# Load
40device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
41model = DeepfakeDetector()
42model.load_state_dict(load_file("model.safetensors"))
43model = model.to(device)
44model.eval()
45
46# Preprocess
47transform = transforms.Compose([
48 transforms.Resize((224, 224)),
49 transforms.ToTensor(),
50 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
51])
52
53# Predict
54image = Image.open("test.jpg").convert("RGB")
55with torch.no_grad():
56 prob = torch.sigmoid(model(transform(image).unsqueeze(0).to(device))).item()
57
58print(f"Fake: {prob:.1%}" if prob > 0.5 else f"Real: {1-prob:.1%}")| Version | F1 Score | Improvement |
|---|---|---|
| V14 Base | 0.9586 | - |
| V15 (+50 samples) | ~0.962 | +0.3% |
| V15 (+200 samples) | ~0.968 | +1.0% |
| V15 (+500 samples) | ~0.975 | +1.6% |
V12 → V13 → V14 → V15 (Self-Learning)