forked from Megatvini/DeepFaceForgeryDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
179 lines (137 loc) · 5.46 KB
/
model.py
File metadata and controls
179 lines (137 loc) · 5.46 KB
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
import torch
import torchvision
from facenet_pytorch import InceptionResnetV1
from torch import nn
import resnet3d
class NNLambda(nn.Module):
def __init__(self, fn) -> None:
super().__init__()
self.fn = fn
def forward(self, x):
return self.fn(x)
class CNN_LSTM(nn.Module):
def __init__(self, face_recognition_cnn_path, hidden_size=64):
super(CNN_LSTM, self).__init__()
image_encoding_size = 64
face_cnn = FaceRecognitionCNN()
state_dict = torch.load(face_recognition_cnn_path, map_location='cpu')
face_cnn.load_state_dict(state_dict)
face_cnn = nn.Sequential(*list(face_cnn.resnet.children()))[:-12]
for p in face_cnn.parameters():
p.requires_grad_(False)
self.cnn_encoder = nn.Sequential(
face_cnn,
nn.Conv2d(192, 128, 5, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, image_encoding_size, 5, bias=False),
nn.BatchNorm2d(image_encoding_size),
nn.ReLU(),
nn.AdaptiveAvgPool2d(1)
)
self.relu1 = nn.ReLU()
self.dropout1 = nn.Dropout(0.5)
self.lstm = nn.LSTM(
image_encoding_size, hidden_size, num_layers=2, bias=True,
batch_first=True, bidirectional=True, dropout=0.5
)
self.fc = nn.Linear(2*hidden_size, 1)
def forward(self, images):
batch_size, num_channels, depth, height, width = images.shape
inp = images.permute(0, 2, 1, 3, 4).reshape(batch_size * depth, num_channels, height, width)
inp = self.cnn_encoder(inp).reshape(batch_size, depth, -1)
inp = self.relu1(inp)
inp = self.dropout1(inp)
out, _ = self.lstm(inp)
mid_out = out[:, depth // 2, :]
res = self.fc(mid_out)
return res.squeeze()
class ResNet3d(nn.Module):
def __init__(self):
super(ResNet3d, self).__init__()
self.model = resnet3d.resnet10(num_classes=1)
def forward(self, images):
return self.model(images).squeeze()
class ResNet2d(nn.Module):
def __init__(self, final_hidden_dim=256, dropout=0.5, pretrained=True):
super(ResNet2d, self).__init__()
resnet = torchvision.models.resnet18(pretrained=pretrained)
resnet.fc = nn.Linear(512, final_hidden_dim)
self.model = nn.Sequential(
resnet,
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(final_hidden_dim, 1)
)
def forward(self, images):
return self.model(images.squeeze()).squeeze()
class SqueezeNet2d(nn.Module):
def __init__(self, dropout=0.5, pretrained=True):
super(SqueezeNet2d, self).__init__()
squeeze_net = torchvision.models.squeezenet1_1(pretrained=pretrained)
self.model = nn.Sequential(
squeeze_net,
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(1000, 1),
)
def forward(self, images):
return self.model(images.squeeze()).squeeze()
class FaceRecognitionCNN(nn.Module):
def __init__(self):
super(FaceRecognitionCNN, self).__init__()
self.resnet = InceptionResnetV1(pretrained='vggface2')
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self.fc = nn.Linear(512, 1)
def forward(self, images):
out = self.resnet(images)
out = self.relu(out)
out = self.dropout(out)
out = self.fc(out)
return out.squeeze()
class Encoder2DConv3D(nn.Module):
def __init__(self, face_recognition_cnn_path=None):
super(Encoder2DConv3D, self).__init__()
face_cnn = FaceRecognitionCNN()
if face_recognition_cnn_path is not None:
state_dict = torch.load(face_recognition_cnn_path, map_location='cpu')
face_cnn.load_state_dict(state_dict)
self.encoder2d = nn.Sequential(*list(face_cnn.resnet.children()))[:-10]
self.encoder3d = nn.Sequential(
nn.Conv3d(256, 256, 3, padding=1, bias=False),
nn.BatchNorm3d(256),
nn.ReLU(),
nn.Conv3d(256, 512, 3, padding=1, bias=False),
nn.BatchNorm3d(512),
nn.ReLU(),
nn.AdaptiveAvgPool3d(1),
nn.Flatten(),
nn.Dropout(0.5),
nn.Linear(512, 1)
)
def forward(self, images):
batch_size, num_channels, depth, height, width = images.shape
images = images.permute(0, 2, 1, 3, 4)
images = images.reshape(batch_size * depth, num_channels, height, width)
out = self.encoder2d(images)
out = out.reshape(batch_size, depth, 256, 17, 17)
out = out.permute(0, 2, 1, 3, 4)
out = self.encoder3d(out)
return out.squeeze()
class MajorityVoteModel(nn.Module):
def __init__(self, face_recognition_cnn_path):
super(MajorityVoteModel, self).__init__()
face_cnn = FaceRecognitionCNN()
state_dict = torch.load(face_recognition_cnn_path, map_location='cpu')
face_cnn.load_state_dict(state_dict)
self.cnn_encoder = face_cnn
def forward(self, images):
batch_size, num_channels, depth, height, width = images.shape
images = images.permute(0, 2, 1, 3, 4)
images = images.reshape(batch_size * depth, num_channels, height, width)
out = self.cnn_encoder(images)
out = out.reshape(batch_size, depth)
out = ((out > 0.0).sum(axis=1) > depth // 2).float()
# out = out[:, depth//2]
return out