Spaces:
Running
Running
Vinh.Vu commited on
Commit ·
dae52bd
1
Parent(s): bb58d5b
Update code for train large data
Browse files- 00-convert_video_to_image.py +12 -7
- 01a-crop_faces_with_mtcnn.py +13 -5
- 02-prepare_fake_real_dataset.py +13 -11
- 03-train_cnn.py +0 -24
00-convert_video_to_image.py
CHANGED
|
@@ -1,28 +1,33 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
import cv2
|
| 4 |
import math
|
| 5 |
|
| 6 |
base_path = '.\\train_sample_videos\\'
|
|
|
|
| 7 |
|
| 8 |
def get_filename_only(file_path):
|
| 9 |
file_basename = os.path.basename(file_path)
|
| 10 |
filename_only = file_basename.split('.')[0]
|
| 11 |
return filename_only
|
| 12 |
|
| 13 |
-
with open(os.path.join(base_path, '
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
print(len(metadata))
|
| 16 |
|
| 17 |
for filename in metadata.keys():
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
print('Creating Directory: ' + tmp_path)
|
| 22 |
os.makedirs(tmp_path, exist_ok=True)
|
| 23 |
print('Converting Video to Images...')
|
| 24 |
count = 0
|
| 25 |
-
video_file = os.path.join(
|
| 26 |
cap = cv2.VideoCapture(video_file)
|
| 27 |
frame_rate = cap.get(5) #frame rate
|
| 28 |
while(cap.isOpened()):
|
|
|
|
| 1 |
+
import csv
|
| 2 |
import os
|
| 3 |
import cv2
|
| 4 |
import math
|
| 5 |
|
| 6 |
base_path = '.\\train_sample_videos\\'
|
| 7 |
+
videos_path = os.path.join(base_path, 'Deepfakes')
|
| 8 |
|
| 9 |
def get_filename_only(file_path):
|
| 10 |
file_basename = os.path.basename(file_path)
|
| 11 |
filename_only = file_basename.split('.')[0]
|
| 12 |
return filename_only
|
| 13 |
|
| 14 |
+
with open(os.path.join(base_path, 'csv', 'Deepfakes.csv'), newline='', encoding='utf-8') as csvfile:
|
| 15 |
+
reader = csv.DictReader(csvfile)
|
| 16 |
+
metadata = {}
|
| 17 |
+
for row in reader:
|
| 18 |
+
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 19 |
print(len(metadata))
|
| 20 |
|
| 21 |
for filename in metadata.keys():
|
| 22 |
+
video_basename = os.path.basename(filename)
|
| 23 |
+
print(video_basename)
|
| 24 |
+
if (video_basename.endswith(".mp4")):
|
| 25 |
+
tmp_path = os.path.join(videos_path, get_filename_only(video_basename))
|
| 26 |
print('Creating Directory: ' + tmp_path)
|
| 27 |
os.makedirs(tmp_path, exist_ok=True)
|
| 28 |
print('Converting Video to Images...')
|
| 29 |
count = 0
|
| 30 |
+
video_file = os.path.join(videos_path, video_basename)
|
| 31 |
cap = cv2.VideoCapture(video_file)
|
| 32 |
frame_rate = cap.get(5) #frame rate
|
| 33 |
while(cap.isOpened()):
|
01a-crop_faces_with_mtcnn.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import cv2
|
| 2 |
from mtcnn import MTCNN
|
|
|
|
| 3 |
import sys, os.path
|
| 4 |
-
import json
|
| 5 |
from keras import backend as K
|
| 6 |
import tensorflow as tf
|
| 7 |
print(tf.__version__)
|
|
@@ -13,21 +13,29 @@ if physical_devices:
|
|
| 13 |
tf.config.experimental.set_memory_growth(physical_devices[0], True)
|
| 14 |
|
| 15 |
base_path = '.\\train_sample_videos\\'
|
|
|
|
| 16 |
|
| 17 |
def get_filename_only(file_path):
|
| 18 |
file_basename = os.path.basename(file_path)
|
| 19 |
filename_only = file_basename.split('.')[0]
|
| 20 |
return filename_only
|
| 21 |
|
| 22 |
-
with open(os.path.join(base_path, '
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
print(len(metadata))
|
| 25 |
|
| 26 |
for filename in metadata.keys():
|
| 27 |
-
|
|
|
|
| 28 |
print('Processing Directory: ' + tmp_path)
|
| 29 |
-
frame_images = [x for x in os.listdir(tmp_path) if os.path.isfile(os.path.join(tmp_path, x))]
|
| 30 |
faces_path = os.path.join(tmp_path, 'faces')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
print('Creating Directory: ' + faces_path)
|
| 32 |
os.makedirs(faces_path, exist_ok=True)
|
| 33 |
print('Cropping Faces from Images...')
|
|
|
|
| 1 |
import cv2
|
| 2 |
from mtcnn import MTCNN
|
| 3 |
+
import csv
|
| 4 |
import sys, os.path
|
|
|
|
| 5 |
from keras import backend as K
|
| 6 |
import tensorflow as tf
|
| 7 |
print(tf.__version__)
|
|
|
|
| 13 |
tf.config.experimental.set_memory_growth(physical_devices[0], True)
|
| 14 |
|
| 15 |
base_path = '.\\train_sample_videos\\'
|
| 16 |
+
videos_path = os.path.join(base_path, 'Deepfakes')
|
| 17 |
|
| 18 |
def get_filename_only(file_path):
|
| 19 |
file_basename = os.path.basename(file_path)
|
| 20 |
filename_only = file_basename.split('.')[0]
|
| 21 |
return filename_only
|
| 22 |
|
| 23 |
+
with open(os.path.join(base_path, 'csv', 'Deepfakes.csv'), newline='', encoding='utf-8') as csvfile:
|
| 24 |
+
reader = csv.DictReader(csvfile)
|
| 25 |
+
metadata = {}
|
| 26 |
+
for row in reader:
|
| 27 |
+
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 28 |
print(len(metadata))
|
| 29 |
|
| 30 |
for filename in metadata.keys():
|
| 31 |
+
video_basename = os.path.basename(filename)
|
| 32 |
+
tmp_path = os.path.join(videos_path, get_filename_only(video_basename))
|
| 33 |
print('Processing Directory: ' + tmp_path)
|
|
|
|
| 34 |
faces_path = os.path.join(tmp_path, 'faces')
|
| 35 |
+
if os.path.isdir(faces_path) and len(os.listdir(faces_path)) > 0:
|
| 36 |
+
print('Skipping (faces already exist): ' + faces_path)
|
| 37 |
+
continue
|
| 38 |
+
frame_images = [x for x in os.listdir(tmp_path) if os.path.isfile(os.path.join(tmp_path, x))]
|
| 39 |
print('Creating Directory: ' + faces_path)
|
| 40 |
os.makedirs(faces_path, exist_ok=True)
|
| 41 |
print('Cropping Faces from Images...')
|
02-prepare_fake_real_dataset.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
-
from distutils.dir_util import copy_tree
|
| 4 |
import shutil
|
| 5 |
import numpy as np
|
| 6 |
import splitfolders as split_folders
|
| 7 |
|
| 8 |
-
base_path = '.\\train_sample_videos\\'
|
| 9 |
dataset_path = '.\\prepared_dataset\\'
|
| 10 |
print('Creating Directory: ' + dataset_path)
|
| 11 |
os.makedirs(dataset_path, exist_ok=True)
|
|
@@ -19,8 +18,11 @@ def get_filename_only(file_path):
|
|
| 19 |
filename_only = file_basename.split('.')[0]
|
| 20 |
return filename_only
|
| 21 |
|
| 22 |
-
with open(os.path.join(
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
print(len(metadata))
|
| 25 |
|
| 26 |
real_path = os.path.join(dataset_path, 'real')
|
|
@@ -31,18 +33,18 @@ fake_path = os.path.join(dataset_path, 'fake')
|
|
| 31 |
print('Creating Directory: ' + fake_path)
|
| 32 |
os.makedirs(fake_path, exist_ok=True)
|
| 33 |
|
| 34 |
-
for filename in metadata.
|
| 35 |
print(filename)
|
| 36 |
-
print(
|
| 37 |
tmp_path = os.path.join(os.path.join(base_path, get_filename_only(filename)), 'faces')
|
| 38 |
print(tmp_path)
|
| 39 |
if os.path.exists(tmp_path):
|
| 40 |
-
if
|
| 41 |
print('Copying to :' + real_path)
|
| 42 |
-
|
| 43 |
-
elif
|
| 44 |
print('Copying to :' + tmp_fake_path)
|
| 45 |
-
|
| 46 |
else:
|
| 47 |
print('Ignored..')
|
| 48 |
|
|
|
|
| 1 |
+
import csv
|
| 2 |
import os
|
|
|
|
| 3 |
import shutil
|
| 4 |
import numpy as np
|
| 5 |
import splitfolders as split_folders
|
| 6 |
|
| 7 |
+
base_path = '.\\train_sample_videos\\Deepfakes\\'
|
| 8 |
dataset_path = '.\\prepared_dataset\\'
|
| 9 |
print('Creating Directory: ' + dataset_path)
|
| 10 |
os.makedirs(dataset_path, exist_ok=True)
|
|
|
|
| 18 |
filename_only = file_basename.split('.')[0]
|
| 19 |
return filename_only
|
| 20 |
|
| 21 |
+
with open(os.path.join('.\\train_sample_videos\\csv\\', 'Deepfakes.csv'), newline='', encoding='utf-8') as csvfile:
|
| 22 |
+
reader = csv.DictReader(csvfile)
|
| 23 |
+
metadata = {}
|
| 24 |
+
for row in reader:
|
| 25 |
+
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 26 |
print(len(metadata))
|
| 27 |
|
| 28 |
real_path = os.path.join(dataset_path, 'real')
|
|
|
|
| 33 |
print('Creating Directory: ' + fake_path)
|
| 34 |
os.makedirs(fake_path, exist_ok=True)
|
| 35 |
|
| 36 |
+
for filename, label in metadata.items():
|
| 37 |
print(filename)
|
| 38 |
+
print(label)
|
| 39 |
tmp_path = os.path.join(os.path.join(base_path, get_filename_only(filename)), 'faces')
|
| 40 |
print(tmp_path)
|
| 41 |
if os.path.exists(tmp_path):
|
| 42 |
+
if label == 'REAL':
|
| 43 |
print('Copying to :' + real_path)
|
| 44 |
+
shutil.copytree(tmp_path, real_path, dirs_exist_ok=True)
|
| 45 |
+
elif label == 'FAKE':
|
| 46 |
print('Copying to :' + tmp_fake_path)
|
| 47 |
+
shutil.copytree(tmp_path, tmp_fake_path, dirs_exist_ok=True)
|
| 48 |
else:
|
| 49 |
print('Ignored..')
|
| 50 |
|
03-train_cnn.py
CHANGED
|
@@ -142,30 +142,6 @@ history = model.fit(
|
|
| 142 |
)
|
| 143 |
print(history.history)
|
| 144 |
|
| 145 |
-
'''
|
| 146 |
-
# Plot results
|
| 147 |
-
import matplotlib.pyplot as plt
|
| 148 |
-
|
| 149 |
-
acc = history.history['acc']
|
| 150 |
-
val_acc = history.history['val_acc']
|
| 151 |
-
loss = history.history['loss']
|
| 152 |
-
val_loss = history.history['val_loss']
|
| 153 |
-
|
| 154 |
-
epochs = range(1, len(acc) + 1)
|
| 155 |
-
|
| 156 |
-
plt.plot(epochs, acc, 'bo', label = 'Training Accuracy')
|
| 157 |
-
plt.plot(epochs, val_acc, 'b', label = 'Validation Accuracy')
|
| 158 |
-
plt.title('Training and Validation Accuracy')
|
| 159 |
-
plt.legend()
|
| 160 |
-
plt.figure()
|
| 161 |
-
|
| 162 |
-
plt.plot(epochs, loss, 'bo', label = 'Training loss')
|
| 163 |
-
plt.plot(epochs, val_loss, 'b', label = 'Validation Loss')
|
| 164 |
-
plt.title('Training and Validation Loss')
|
| 165 |
-
plt.legend()
|
| 166 |
-
|
| 167 |
-
plt.show()
|
| 168 |
-
'''
|
| 169 |
|
| 170 |
# load the saved model that is considered the best
|
| 171 |
best_model = load_model(os.path.join(checkpoint_filepath, 'best_model.h5'))
|
|
|
|
| 142 |
)
|
| 143 |
print(history.history)
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
# load the saved model that is considered the best
|
| 147 |
best_model = load_model(os.path.join(checkpoint_filepath, 'best_model.h5'))
|