AdityaManojShinde commited on
Commit
1c7346f
·
verified ·
1 Parent(s): 34f236a

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +60 -0
model.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn as nn
4
+ import timm
5
+
6
+ class SRMLayer(nn.Module):
7
+ def __init__(self):
8
+ super().__init__()
9
+ f1 = torch.tensor([
10
+ [ 0, 0, 0, 0, 0],
11
+ [ 0, -1, 2, -1, 0],
12
+ [ 0, 2, -4, 2, 0],
13
+ [ 0, -1, 2, -1, 0],
14
+ [ 0, 0, 0, 0, 0]
15
+ ], dtype=torch.float32)
16
+ f2 = torch.tensor([
17
+ [-1, 2, -2, 2, -1],
18
+ [ 2, -6, 8, -6, 2],
19
+ [-2, 8,-12, 8, -2],
20
+ [ 2, -6, 8, -6, 2],
21
+ [-1, 2, -2, 2, -1]
22
+ ], dtype=torch.float32)
23
+ f3 = torch.tensor([
24
+ [ 0, 0, 0, 0, 0],
25
+ [ 0, 0, 0, 0, 0],
26
+ [ 0, 1, -2, 1, 0],
27
+ [ 0, 0, 0, 0, 0],
28
+ [ 0, 0, 0, 0, 0]
29
+ ], dtype=torch.float32)
30
+ filters = torch.stack([f1, f2, f3]).unsqueeze(1)
31
+ self.conv = nn.Conv2d(1, 3, kernel_size=5, padding=2, bias=False)
32
+ self.conv.weight.data = filters
33
+ self.conv.weight.requires_grad = False
34
+
35
+ def forward(self, x):
36
+ gray = 0.299*x[:,0:1] + 0.587*x[:,1:2] + 0.114*x[:,2:3]
37
+ return self.conv(gray)
38
+
39
+ class HybridDeepfakeDetector(nn.Module):
40
+ def __init__(self):
41
+ super().__init__()
42
+ self.spatial = timm.create_model(
43
+ "efficientnet_b4", pretrained=False, num_classes=0)
44
+ self.srm = SRMLayer()
45
+ self.frequency = timm.create_model(
46
+ "xception", pretrained=False, num_classes=0, in_chans=3)
47
+ self.classifier = nn.Sequential(
48
+ nn.Linear(3840, 256),
49
+ nn.ReLU(),
50
+ nn.Dropout(0.5),
51
+ nn.Linear(256, 1),
52
+ nn.Sigmoid()
53
+ )
54
+
55
+ def forward(self, x):
56
+ spatial_features = self.spatial(x)
57
+ noise_maps = self.srm(x)
58
+ freq_features = self.frequency(noise_maps)
59
+ combined = torch.cat([spatial_features, freq_features], dim=1)
60
+ return self.classifier(combined)