tony133777 commited on
Commit
4412b92
·
verified ·
1 Parent(s): 87532b7

Upload 3 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* 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
 
 
 
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
+ dlib-19.24.1-cp311-cp311-win_amd64.whl filter=lfs diff=lfs merge=lfs -text
37
+ pipeline_architecture.png filter=lfs diff=lfs merge=lfs -text
dlib-19.24.1-cp311-cp311-win_amd64.whl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f1a5ee167975d7952b28e0ce4495f1d9a77644761cf5720fb66d7c6188ae496
3
+ size 2825619
pipeline_architecture.png ADDED

Git LFS Details

  • SHA256: 67a84c6be7d8f5b7bdfa2a07b98375b10051cc719293b74db9c7db87dad46d73
  • Pointer size: 131 Bytes
  • Size of remote file: 222 kB
prediction.py ADDED
@@ -0,0 +1,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import argparse
3
+ import json
4
+ from time import perf_counter
5
+ from datetime import datetime
6
+ from model.pred_func import *
7
+ from model.config import load_config
8
+
9
+ config = load_config()
10
+ print('CONFIG')
11
+ print(config)
12
+ def vids(
13
+ ed_weight, vae_weight, root_dir="sample_prediction_data", dataset=None, num_frames=15, net=None, fp16=False
14
+ ):
15
+ result = set_result()
16
+ r = 0
17
+ f = 0
18
+ count = 0
19
+
20
+ model = load_genconvit(config, net, ed_weight, vae_weight, fp16)
21
+
22
+ for filename in os.listdir(root_dir):
23
+ curr_vid = os.path.join(root_dir, filename)
24
+
25
+ try:
26
+ if is_video(curr_vid):
27
+ result, accuracy, count, pred = predict(
28
+ curr_vid,
29
+ model,
30
+ fp16,
31
+ result,
32
+ num_frames,
33
+ net,
34
+ "uncategorized",
35
+ count,
36
+ )
37
+ f, r = (f + 1, r) if "FAKE" == real_or_fake(pred[0]) else (f, r + 1)
38
+ print(
39
+ f"Prediction: {pred[1]} {real_or_fake(pred[0])} \t\tFake: {f} Real: {r}"
40
+ )
41
+ else:
42
+ print(f"Invalid video file: {curr_vid}. Please provide a valid video file.")
43
+
44
+ except Exception as e:
45
+ print(f"An error occurred: {str(e)}")
46
+
47
+ return result
48
+
49
+
50
+ def faceforensics(
51
+ ed_weight, vae_weight, root_dir="FaceForensics\\data", dataset=None, num_frames=15, net=None, fp16=False
52
+ ):
53
+ vid_type = ["original_sequences", "manipulated_sequences"]
54
+ result = set_result()
55
+ result["video"]["compression"] = []
56
+ ffdirs = [
57
+ "DeepFakeDetection",
58
+ "Deepfakes",
59
+ "Face2Face",
60
+ "FaceSwap",
61
+ "NeuralTextures",
62
+ ]
63
+
64
+ # load files not used in the training set, the files are appended with compression type, _c23 or _c40
65
+ with open(os.path.join("json_file", "ff_file_list.json")) as j_file:
66
+ ff_file = list(json.load(j_file))
67
+
68
+ count = 0
69
+ accuracy = 0
70
+ model = load_genconvit(config, net, ed_weight, vae_weight, fp16)
71
+
72
+ for v_t in vid_type:
73
+ for dirpath, dirnames, filenames in os.walk(os.path.join(root_dir, v_t)):
74
+ klass = next(
75
+ filter(lambda x: x in dirpath.split(os.path.sep), ffdirs),
76
+ "original",
77
+ )
78
+ label = "REAL" if klass == "original" else "FAKE"
79
+ for filename in filenames:
80
+ try:
81
+ if filename in ff_file:
82
+ curr_vid = os.path.join(dirpath, filename)
83
+ compression = "c23" if "c23" in curr_vid else "c40"
84
+ if is_video(curr_vid):
85
+ result, accuracy, count, _ = predict(
86
+ curr_vid,
87
+ model,
88
+ fp16,
89
+ result,
90
+ num_frames,
91
+ net,
92
+ klass,
93
+ count,
94
+ accuracy,
95
+ label,
96
+ compression,
97
+ )
98
+ else:
99
+ print(f"Invalid video file: {curr_vid}. Please provide a valid video file.")
100
+
101
+ except Exception as e:
102
+ print(f"An error occurred: {str(e)}")
103
+
104
+ return result
105
+
106
+
107
+ def timit(ed_weight, vae_weight, root_dir="DeepfakeTIMIT", dataset=None, num_frames=15, net=None, fp16=False):
108
+ keywords = ["higher_quality", "lower_quality"]
109
+ result = set_result()
110
+ model = load_genconvit(config, net, ed_weight, vae_weight, fp16)
111
+ count = 0
112
+ accuracy = 0
113
+ i = 0
114
+ for keyword in keywords:
115
+ keyword_folder_path = os.path.join(root_dir, keyword)
116
+ for subfolder_name in os.listdir(keyword_folder_path):
117
+ subfolder_path = os.path.join(keyword_folder_path, subfolder_name)
118
+ if os.path.isdir(subfolder_path):
119
+ # Loop through the AVI files in the subfolder
120
+ for filename in os.listdir(subfolder_path):
121
+ if filename.endswith(".avi"):
122
+ curr_vid = os.path.join(subfolder_path, filename)
123
+ try:
124
+ if is_video(curr_vid):
125
+ result, accuracy, count, _ = predict(
126
+ curr_vid,
127
+ model,
128
+ fp16,
129
+ result,
130
+ num_frames,
131
+ net,
132
+ "DeepfakeTIMIT",
133
+ count,
134
+ accuracy,
135
+ "FAKE",
136
+ )
137
+ else:
138
+ print(f"Invalid video file: {curr_vid}. Please provide a valid video file.")
139
+
140
+ except Exception as e:
141
+ print(f"An error occurred: {str(e)}")
142
+
143
+ return result
144
+
145
+
146
+ def dfdc(
147
+ ed_weight,
148
+ vae_weight,
149
+ root_dir="deepfake-detection-challenge\\train_sample_videos",
150
+ dataset=None,
151
+ num_frames=15,
152
+ net=None,
153
+ fp16=False,
154
+ ):
155
+ result = set_result()
156
+ if os.path.isfile(os.path.join("json_file", "dfdc_files.json")):
157
+ with open(os.path.join("json_file", "dfdc_files.json")) as data_file:
158
+ dfdc_data = json.load(data_file)
159
+
160
+ if os.path.isfile(os.path.join(root_dir, "metadata.json")):
161
+ with open(os.path.join(root_dir, "metadata.json")) as data_file:
162
+ dfdc_meta = json.load(data_file)
163
+ model = load_genconvit(config, net, ed_weight, vae_weight, fp16)
164
+ count = 0
165
+ accuracy = 0
166
+ for dfdc in dfdc_data:
167
+ dfdc_file = os.path.join(root_dir, dfdc)
168
+
169
+ try:
170
+ if is_video(dfdc_file):
171
+ result, accuracy, count, _ = predict(
172
+ dfdc_file,
173
+ model,
174
+ fp16,
175
+ result,
176
+ num_frames,
177
+ net,
178
+ "dfdc",
179
+ count,
180
+ accuracy,
181
+ dfdc_meta[dfdc]["label"],
182
+ )
183
+ else:
184
+ print(f"Invalid video file: {dfdc_file}. Please provide a valid video file.")
185
+
186
+ except Exception as e:
187
+ print(f"An error occurred: {str(e)}")
188
+
189
+ return result
190
+
191
+
192
+ def celeb(ed_weight, vae_weight, root_dir="Celeb-DF-v2", dataset=None, num_frames=15, net=None, fp16=False):
193
+ with open(os.path.join("json_file", "celeb_test.json"), "r") as f:
194
+ cfl = json.load(f)
195
+ result = set_result()
196
+ ky = ["Celeb-real", "Celeb-synthesis"]
197
+ count = 0
198
+ accuracy = 0
199
+ model = load_genconvit(config, net, ed_weight, vae_weight, fp16)
200
+
201
+ for ck in cfl:
202
+ ck_ = ck.split("/")
203
+ klass = ck_[0]
204
+ filename = ck_[1]
205
+ correct_label = "FAKE" if klass == "Celeb-synthesis" else "REAL"
206
+ vid = os.path.join(root_dir, ck)
207
+
208
+ try:
209
+ if is_video(vid):
210
+ result, accuracy, count, _ = predict(
211
+ vid,
212
+ model,
213
+ fp16,
214
+ result,
215
+ num_frames,
216
+ net,
217
+ klass,
218
+ count,
219
+ accuracy,
220
+ correct_label,
221
+ )
222
+ else:
223
+ print(f"Invalid video file: {vid}. Please provide a valid video file.")
224
+
225
+ except Exception as e:
226
+ print(f"An error occurred x: {str(e)}")
227
+
228
+ return result
229
+
230
+
231
+ def predict(
232
+ vid,
233
+ model,
234
+ fp16,
235
+ result,
236
+ num_frames,
237
+ net,
238
+ klass,
239
+ count=0,
240
+ accuracy=-1,
241
+ correct_label="unknown",
242
+ compression=None,
243
+ ):
244
+ count += 1
245
+ print(f"\n\n{str(count)} Loading... {vid}")
246
+
247
+ df = df_face(vid, num_frames, net) # extract face from the frames
248
+ if fp16:
249
+ df.half()
250
+ y, y_val = (
251
+ pred_vid(df, model)
252
+ if len(df) >= 1
253
+ else (torch.tensor(0).item(), torch.tensor(0.5).item())
254
+ )
255
+ result = store_result(
256
+ result, os.path.basename(vid), y, y_val, klass, correct_label, compression
257
+ )
258
+
259
+ if accuracy > -1:
260
+ if correct_label == real_or_fake(y):
261
+ accuracy += 1
262
+ print(
263
+ f"\nPrediction: {y_val} {real_or_fake(y)} \t\t {accuracy}/{count} {accuracy/count}"
264
+ )
265
+
266
+ return result, accuracy, count, [y, y_val]
267
+
268
+
269
+ def gen_parser():
270
+ parser = argparse.ArgumentParser("GenConViT prediction")
271
+ parser.add_argument("--p", type=str, help="video or image path")
272
+ parser.add_argument(
273
+ "--f", type=int, help="number of frames to process for prediction"
274
+ )
275
+ parser.add_argument(
276
+ "--d", type=str, help="dataset type, dfdc, faceforensics, timit, celeb"
277
+ )
278
+ parser.add_argument(
279
+ "--s", help="model size type: tiny, large.",
280
+ )
281
+ parser.add_argument(
282
+ "--e", nargs='?', const='genconvit_ed_inference', default='genconvit_ed_inference', help="weight for ed.",
283
+ )
284
+ parser.add_argument(
285
+ "--v", '--value', nargs='?', const='genconvit_vae_inference', default='genconvit_vae_inference', help="weight for vae.",
286
+ )
287
+
288
+ parser.add_argument("--fp16", type=str, help="half precision support")
289
+
290
+ args = parser.parse_args()
291
+ path = args.p
292
+ num_frames = args.f if args.f else 15
293
+ dataset = args.d if args.d else "other"
294
+ fp16 = True if args.fp16 else False
295
+
296
+ net = 'genconvit'
297
+ ed_weight = 'genconvit_ed_inference'
298
+ vae_weight = 'genconvit_vae_inference'
299
+
300
+ if args.e and args.v:
301
+ ed_weight = args.e
302
+ vae_weight = args.v
303
+ elif args.e:
304
+ net = 'ed'
305
+ ed_weight = args.e
306
+ elif args.v:
307
+ net = 'vae'
308
+ vae_weight = args.v
309
+
310
+
311
+ print(f'\nUsing {net}\n')
312
+
313
+
314
+ if args.s:
315
+ if args.s in ['tiny', 'large']:
316
+ config["model"]["backbone"] = f"convnext_{args.s}"
317
+ config["model"]["embedder"] = f"swin_{args.s}_patch4_window7_224"
318
+ config["model"]["type"] = args.s
319
+
320
+ return path, dataset, num_frames, net, fp16, ed_weight, vae_weight
321
+
322
+
323
+ def main():
324
+ start_time = perf_counter()
325
+ path, dataset, num_frames, net, fp16, ed_weight, vae_weight = gen_parser()
326
+ result = (
327
+ globals()[dataset](ed_weight, vae_weight, path, dataset, num_frames, net, fp16)
328
+ if dataset in ["dfdc", "faceforensics", "timit", "celeb"]
329
+ else vids(ed_weight, vae_weight, path, dataset, num_frames, net, fp16)
330
+ )
331
+
332
+ curr_time = datetime.now().strftime("%B_%d_%Y_%H_%M_%S")
333
+ file_path = os.path.join("result", f"prediction_{dataset}_{net}_{curr_time}.json")
334
+
335
+ with open(file_path, "w") as f:
336
+ json.dump(result, f)
337
+ end_time = perf_counter()
338
+ print("\n\n--- %s seconds ---" % (end_time - start_time))
339
+
340
+
341
+ if __name__ == "__main__":
342
+ main()