Spaces:
Runtime error
Runtime error
last changes, hopefully!
Browse files- .gitattributes +1 -4
- .gitignore +2 -0
- __pycache__/inference_2.cpython-39.pyc +0 -0
- checkpoints/model.pth +2 -2
- inference_2.py +34 -22
- checkpoints/model_best.pt → model.pth +2 -2
- requirements.txt +3 -1
- save_ckpts.py +89 -0
.gitattributes
CHANGED
@@ -1,6 +1,3 @@
|
|
1 |
-
<<<<<<< HEAD
|
2 |
-
checkpoints/model_best.pt filter=lfs diff=lfs merge=lfs -text
|
3 |
-
=======
|
4 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
5 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
6 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
@@ -36,5 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
36 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
37 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
38 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
39 |
-
>>>>>>> 10b34e9e01a793df83cca1499ece5c6b29f10a90
|
40 |
checkpoints/model.pth filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
36 |
checkpoints/model.pth filter=lfs diff=lfs merge=lfs -text
|
37 |
+
checkpoints/efficientnet.onnx filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
checkpoints/efficientnet.onnx
|
2 |
+
checkpoints/RawNet2.pth
|
__pycache__/inference_2.cpython-39.pyc
CHANGED
Binary files a/__pycache__/inference_2.cpython-39.pyc and b/__pycache__/inference_2.cpython-39.pyc differ
|
|
checkpoints/model.pth
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3de812710093068acee6200b8d162aab074975edffa3edf2ccbe562868e4adf6
|
3 |
+
size 117418889
|
inference_2.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import cv2
|
|
|
3 |
import torch
|
4 |
import argparse
|
5 |
import numpy as np
|
@@ -7,6 +8,11 @@ import torch.nn as nn
|
|
7 |
from models.TMC import ETMC
|
8 |
from models import image
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
#Set random seed for reproducibility.
|
11 |
torch.manual_seed(42)
|
12 |
|
@@ -70,28 +76,29 @@ def load_multimodal_model(args):
|
|
70 |
'''Load multimodal model'''
|
71 |
model = ETMC(args)
|
72 |
ckpt = torch.load('checkpoints\\model.pth', map_location = torch.device('cpu'))
|
73 |
-
model.load_state_dict(ckpt,strict = True)
|
74 |
model.eval()
|
75 |
return model
|
76 |
|
77 |
def load_img_modality_model(args):
|
78 |
'''Loads image modality model.'''
|
79 |
-
rgb_encoder =
|
80 |
-
|
81 |
-
|
|
|
82 |
rgb_encoder.eval()
|
83 |
return rgb_encoder
|
84 |
|
85 |
def load_spec_modality_model(args):
|
86 |
spec_encoder = image.RawNet(args)
|
87 |
ckpt = torch.load('checkpoints/model.pth', map_location = torch.device('cpu'))
|
88 |
-
spec_encoder.load_state_dict(ckpt, strict =
|
89 |
spec_encoder.eval()
|
90 |
return spec_encoder
|
91 |
|
92 |
|
93 |
#Load models.
|
94 |
-
parser = argparse.ArgumentParser(description="
|
95 |
get_args(parser)
|
96 |
args, remaining_args = parser.parse_known_args()
|
97 |
assert remaining_args == [], remaining_args
|
@@ -104,7 +111,7 @@ img_model = load_img_modality_model(args)
|
|
104 |
def preprocess_img(face):
|
105 |
face = face / 255
|
106 |
face = cv2.resize(face, (256, 256))
|
107 |
-
face = face.transpose(2, 0, 1) #(W, H, C) -> (C, W, H)
|
108 |
face_pt = torch.unsqueeze(torch.Tensor(face), dim = 0)
|
109 |
return face_pt
|
110 |
|
@@ -137,18 +144,18 @@ def deepfakes_spec_predict(input_audio):
|
|
137 |
|
138 |
def deepfakes_image_predict(input_image):
|
139 |
face = preprocess_img(input_image)
|
140 |
-
|
141 |
img_grads = img_model.forward(face)
|
142 |
img_grads = img_grads.cpu().detach().numpy()
|
143 |
img_grads_np = np.squeeze(img_grads)
|
144 |
|
145 |
-
if img_grads_np > 0.5:
|
146 |
-
preds = round(
|
147 |
-
text2 = f"The image is REAL."
|
148 |
|
149 |
else:
|
150 |
-
preds = round(img_grads_np * 100, 3)
|
151 |
-
text2 = f"The image is FAKE."
|
152 |
|
153 |
return text2
|
154 |
|
@@ -182,7 +189,8 @@ def preprocess_video(input_video, n_frames = 3):
|
|
182 |
def deepfakes_video_predict(input_video):
|
183 |
'''Perform inference on a video.'''
|
184 |
video_frames = preprocess_video(input_video)
|
185 |
-
|
|
|
186 |
|
187 |
for face in video_frames:
|
188 |
# face = preprocess_img(face)
|
@@ -190,15 +198,19 @@ def deepfakes_video_predict(input_video):
|
|
190 |
img_grads = img_model.forward(face)
|
191 |
img_grads = img_grads.cpu().detach().numpy()
|
192 |
img_grads_np = np.squeeze(img_grads)
|
193 |
-
|
|
|
|
|
|
|
|
|
194 |
|
195 |
-
|
|
|
|
|
196 |
|
197 |
-
if grads_list_mean > 0.5:
|
198 |
-
res = round(grads_list_mean * 100, 3)
|
199 |
-
text = f"The video is REAL."
|
200 |
else:
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
204 |
|
|
|
1 |
import os
|
2 |
import cv2
|
3 |
+
import onnx
|
4 |
import torch
|
5 |
import argparse
|
6 |
import numpy as np
|
|
|
8 |
from models.TMC import ETMC
|
9 |
from models import image
|
10 |
|
11 |
+
from onnx2pytorch import ConvertModel
|
12 |
+
|
13 |
+
onnx_model = onnx.load('checkpoints\\efficientnet.onnx')
|
14 |
+
pytorch_model = ConvertModel(onnx_model)
|
15 |
+
|
16 |
#Set random seed for reproducibility.
|
17 |
torch.manual_seed(42)
|
18 |
|
|
|
76 |
'''Load multimodal model'''
|
77 |
model = ETMC(args)
|
78 |
ckpt = torch.load('checkpoints\\model.pth', map_location = torch.device('cpu'))
|
79 |
+
model.load_state_dict(ckpt, strict = True)
|
80 |
model.eval()
|
81 |
return model
|
82 |
|
83 |
def load_img_modality_model(args):
|
84 |
'''Loads image modality model.'''
|
85 |
+
rgb_encoder = pytorch_model
|
86 |
+
|
87 |
+
ckpt = torch.load('checkpoints\\model.pth', map_location = torch.device('cpu'))
|
88 |
+
rgb_encoder.load_state_dict(ckpt['rgb_encoder'], strict = True)
|
89 |
rgb_encoder.eval()
|
90 |
return rgb_encoder
|
91 |
|
92 |
def load_spec_modality_model(args):
|
93 |
spec_encoder = image.RawNet(args)
|
94 |
ckpt = torch.load('checkpoints/model.pth', map_location = torch.device('cpu'))
|
95 |
+
spec_encoder.load_state_dict(ckpt['spec_encoder'], strict = True)
|
96 |
spec_encoder.eval()
|
97 |
return spec_encoder
|
98 |
|
99 |
|
100 |
#Load models.
|
101 |
+
parser = argparse.ArgumentParser(description="Inference models")
|
102 |
get_args(parser)
|
103 |
args, remaining_args = parser.parse_known_args()
|
104 |
assert remaining_args == [], remaining_args
|
|
|
111 |
def preprocess_img(face):
|
112 |
face = face / 255
|
113 |
face = cv2.resize(face, (256, 256))
|
114 |
+
# face = face.transpose(2, 0, 1) #(W, H, C) -> (C, W, H)
|
115 |
face_pt = torch.unsqueeze(torch.Tensor(face), dim = 0)
|
116 |
return face_pt
|
117 |
|
|
|
144 |
|
145 |
def deepfakes_image_predict(input_image):
|
146 |
face = preprocess_img(input_image)
|
147 |
+
print(f"Face shape is: {face.shape}")
|
148 |
img_grads = img_model.forward(face)
|
149 |
img_grads = img_grads.cpu().detach().numpy()
|
150 |
img_grads_np = np.squeeze(img_grads)
|
151 |
|
152 |
+
if img_grads_np[0] > 0.5:
|
153 |
+
preds = round(img_grads_np[0] * 100, 3)
|
154 |
+
text2 = f"The image is REAL. \nConfidence score is: {preds}"
|
155 |
|
156 |
else:
|
157 |
+
preds = round(img_grads_np[1] * 100, 3)
|
158 |
+
text2 = f"The image is FAKE. \nConfidence score is: {preds}"
|
159 |
|
160 |
return text2
|
161 |
|
|
|
189 |
def deepfakes_video_predict(input_video):
|
190 |
'''Perform inference on a video.'''
|
191 |
video_frames = preprocess_video(input_video)
|
192 |
+
real_faces_list = []
|
193 |
+
fake_faces_list = []
|
194 |
|
195 |
for face in video_frames:
|
196 |
# face = preprocess_img(face)
|
|
|
198 |
img_grads = img_model.forward(face)
|
199 |
img_grads = img_grads.cpu().detach().numpy()
|
200 |
img_grads_np = np.squeeze(img_grads)
|
201 |
+
real_faces_list.append(img_grads_np[0])
|
202 |
+
fake_faces_list.append(img_grads_np[1])
|
203 |
+
|
204 |
+
real_faces_mean = np.mean(real_faces_list)
|
205 |
+
fake_faces_mean = np.mean(fake_faces_list)
|
206 |
|
207 |
+
if real_faces_mean > 0.5:
|
208 |
+
preds = round(real_faces_mean * 100, 3)
|
209 |
+
text2 = f"The video is REAL. \nConfidence score is: {preds}%"
|
210 |
|
|
|
|
|
|
|
211 |
else:
|
212 |
+
preds = round(fake_faces_mean * 100, 3)
|
213 |
+
text2 = f"The video is FAKE. \nConfidence score is: {preds}%"
|
214 |
+
|
215 |
+
return text2
|
216 |
|
checkpoints/model_best.pt → model.pth
RENAMED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7124691a506c8b7ea6baf3284a2b52f58879d3de89a8154d5ad3f69ba744912a
|
3 |
+
size 152905281
|
requirements.txt
CHANGED
@@ -7,4 +7,6 @@ librosa
|
|
7 |
ffmpeg
|
8 |
albumentations
|
9 |
opencv-python
|
10 |
-
torchsummary
|
|
|
|
|
|
7 |
ffmpeg
|
8 |
albumentations
|
9 |
opencv-python
|
10 |
+
torchsummary
|
11 |
+
onnx
|
12 |
+
onnx2pytorch
|
save_ckpts.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import onnx
|
2 |
+
import torch
|
3 |
+
import argparse
|
4 |
+
import numpy as np
|
5 |
+
import torch.nn as nn
|
6 |
+
from models.TMC import ETMC
|
7 |
+
from models import image
|
8 |
+
from onnx2pytorch import ConvertModel
|
9 |
+
|
10 |
+
onnx_model = onnx.load('checkpoints\\efficientnet.onnx')
|
11 |
+
pytorch_model = ConvertModel(onnx_model)
|
12 |
+
|
13 |
+
# Define the audio_args dictionary
|
14 |
+
audio_args = {
|
15 |
+
'nb_samp': 64600,
|
16 |
+
'first_conv': 1024,
|
17 |
+
'in_channels': 1,
|
18 |
+
'filts': [20, [20, 20], [20, 128], [128, 128]],
|
19 |
+
'blocks': [2, 4],
|
20 |
+
'nb_fc_node': 1024,
|
21 |
+
'gru_node': 1024,
|
22 |
+
'nb_gru_layer': 3,
|
23 |
+
'nb_classes': 2
|
24 |
+
}
|
25 |
+
|
26 |
+
|
27 |
+
def get_args(parser):
|
28 |
+
parser.add_argument("--batch_size", type=int, default=8)
|
29 |
+
parser.add_argument("--data_dir", type=str, default="datasets/train/fakeavceleb*")
|
30 |
+
parser.add_argument("--LOAD_SIZE", type=int, default=256)
|
31 |
+
parser.add_argument("--FINE_SIZE", type=int, default=224)
|
32 |
+
parser.add_argument("--dropout", type=float, default=0.2)
|
33 |
+
parser.add_argument("--gradient_accumulation_steps", type=int, default=1)
|
34 |
+
parser.add_argument("--hidden", nargs="*", type=int, default=[])
|
35 |
+
parser.add_argument("--hidden_sz", type=int, default=768)
|
36 |
+
parser.add_argument("--img_embed_pool_type", type=str, default="avg", choices=["max", "avg"])
|
37 |
+
parser.add_argument("--img_hidden_sz", type=int, default=1024)
|
38 |
+
parser.add_argument("--include_bn", type=int, default=True)
|
39 |
+
parser.add_argument("--lr", type=float, default=1e-4)
|
40 |
+
parser.add_argument("--lr_factor", type=float, default=0.3)
|
41 |
+
parser.add_argument("--lr_patience", type=int, default=10)
|
42 |
+
parser.add_argument("--max_epochs", type=int, default=500)
|
43 |
+
parser.add_argument("--n_workers", type=int, default=12)
|
44 |
+
parser.add_argument("--name", type=str, default="MMDF")
|
45 |
+
parser.add_argument("--num_image_embeds", type=int, default=1)
|
46 |
+
parser.add_argument("--patience", type=int, default=20)
|
47 |
+
parser.add_argument("--savedir", type=str, default="./savepath/")
|
48 |
+
parser.add_argument("--seed", type=int, default=1)
|
49 |
+
parser.add_argument("--n_classes", type=int, default=2)
|
50 |
+
parser.add_argument("--annealing_epoch", type=int, default=10)
|
51 |
+
parser.add_argument("--device", type=str, default='cpu')
|
52 |
+
parser.add_argument("--pretrained_image_encoder", type=bool, default = False)
|
53 |
+
parser.add_argument("--freeze_image_encoder", type=bool, default = False)
|
54 |
+
parser.add_argument("--pretrained_audio_encoder", type = bool, default=False)
|
55 |
+
parser.add_argument("--freeze_audio_encoder", type = bool, default = False)
|
56 |
+
parser.add_argument("--augment_dataset", type = bool, default = True)
|
57 |
+
|
58 |
+
for key, value in audio_args.items():
|
59 |
+
parser.add_argument(f"--{key}", type=type(value), default=value)
|
60 |
+
|
61 |
+
def load_spec_modality_model(args):
|
62 |
+
spec_encoder = image.RawNet(args)
|
63 |
+
ckpt = torch.load('checkpoints\RawNet2.pth', map_location = torch.device('cpu'))
|
64 |
+
spec_encoder.load_state_dict(ckpt, strict = True)
|
65 |
+
spec_encoder.eval()
|
66 |
+
return spec_encoder
|
67 |
+
|
68 |
+
|
69 |
+
#Load models.
|
70 |
+
parser = argparse.ArgumentParser(description="Train Models")
|
71 |
+
get_args(parser)
|
72 |
+
args, remaining_args = parser.parse_known_args()
|
73 |
+
assert remaining_args == [], remaining_args
|
74 |
+
|
75 |
+
spec_model = load_spec_modality_model(args)
|
76 |
+
|
77 |
+
print(f"Image model is: {pytorch_model}")
|
78 |
+
|
79 |
+
print(f"Audio model is: {spec_model}")
|
80 |
+
|
81 |
+
|
82 |
+
PATH = 'checkpoints\\model.pth'
|
83 |
+
|
84 |
+
torch.save({
|
85 |
+
'spec_encoder': spec_model.state_dict(),
|
86 |
+
'rgb_encoder': pytorch_model.state_dict()
|
87 |
+
}, PATH)
|
88 |
+
|
89 |
+
print("Model saved.")
|