Spaces:
Sleeping
Sleeping
File size: 1,685 Bytes
669e8e0 | 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 | import torch
import torch.nn as nn
from efficientnet_pytorch import EfficientNet
from src.config_reader import Config
class DeepfakeDetector(nn.Module):
def __init__(self):
super().__init__()
self.cfg = Config()
# Spatial branch
self.spatial_branch = self.build_spatial_branch()
# Frequency branch
self.frequency_branch = self.build_frequency_branch()
# Classifier
self.classifier = self.build_classifier()
def build_spatial_branch(self):
model = EfficientNet.from_pretrained('efficientnet-b4')
model._fc = nn.Identity()
return model
def build_frequency_branch(self):
return nn.Sequential(
nn.Conv2d(1, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, 3, padding=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d((8, 8)),
nn.Flatten(),
nn.Linear(64 * 8 * 8, 256)
)
def build_classifier(self):
return nn.Sequential(
nn.Linear(1792 + 256, 512),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(512, 1),
nn.Sigmoid()
)
def forward(self, x):
# Spatial features
spatial_feat = self.spatial_branch(x)
# Frequency features
gray = x.mean(dim=1, keepdim=True)
freq_map = torch.fft.fft2(gray)
freq_map = torch.log1p(torch.abs(freq_map))
freq_feat = self.frequency_branch(freq_map)
# Combine aur classify
combined = torch.cat([spatial_feat, freq_feat], dim=1)
return self.classifier(combined) |