Spaces:
Build error
Build error
Commit
·
c894298
1
Parent(s):
ba4e0ba
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,79 @@
|
|
1 |
-
import tensorflow.keras as K
|
2 |
-
import os
|
3 |
-
from tensorflow.keras import layers
|
4 |
-
import tensorflow as tf
|
5 |
-
import gradio as gr
|
6 |
-
from extract_landmarks import get_data_for_test,extract_landmark,merge_video_prediction
|
7 |
-
|
8 |
-
block_size = 60
|
9 |
-
DROPOUT_RATE = 0.5
|
10 |
-
RNN_UNIT = 64
|
11 |
-
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
12 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
13 |
-
|
14 |
-
gpus = tf.config.list_physical_devices(device_type='GPU')
|
15 |
-
for gpu in gpus:
|
16 |
-
tf.config.experimental.set_memory_growth(device=gpu, enable=True)
|
17 |
-
device = "CPU" if len(gpus) == 0 else "GPU"
|
18 |
-
|
19 |
-
|
20 |
-
def predict(video):
|
21 |
-
path = extract_landmark(video)
|
22 |
-
test_samples, test_samples_diff, _, _, test_sv, test_vc = get_data_for_test(path, 1, block_size)
|
23 |
-
|
24 |
-
model = K.Sequential([
|
25 |
-
layers.InputLayer(input_shape=(block_size, 136)),
|
26 |
-
layers.Dropout(0.25),
|
27 |
-
layers.Bidirectional(layers.GRU(RNN_UNIT)),
|
28 |
-
layers.Dropout(DROPOUT_RATE),
|
29 |
-
layers.Dense(64, activation='relu'),
|
30 |
-
layers.Dropout(DROPOUT_RATE),
|
31 |
-
layers.Dense(2, activation='softmax')
|
32 |
-
])
|
33 |
-
model_diff = K.Sequential([
|
34 |
-
layers.InputLayer(input_shape=(block_size - 1, 136)),
|
35 |
-
layers.Dropout(0.25),
|
36 |
-
layers.Bidirectional(layers.GRU(RNN_UNIT)),
|
37 |
-
layers.Dropout(DROPOUT_RATE),
|
38 |
-
layers.Dense(64, activation='relu'),
|
39 |
-
layers.Dropout(DROPOUT_RATE),
|
40 |
-
layers.Dense(2, activation='softmax')
|
41 |
-
])
|
42 |
-
|
43 |
-
lossFunction = K.losses.SparseCategoricalCrossentropy(from_logits=False)
|
44 |
-
optimizer = K.optimizers.Adam(learning_rate=0.001)
|
45 |
-
model.compile(optimizer=optimizer,
|
46 |
-
loss=lossFunction,
|
47 |
-
metrics=['accuracy'])
|
48 |
-
model_diff.compile(optimizer=optimizer,
|
49 |
-
loss=lossFunction,
|
50 |
-
metrics=['accuracy'])
|
51 |
-
|
52 |
-
#----Using Deeperforensics 1.0 Parameters----#
|
53 |
-
model.load_weights('g1.h5')
|
54 |
-
model_diff.load_weights('g2.h5')
|
55 |
-
|
56 |
-
prediction = model.predict(test_samples)
|
57 |
-
prediction_diff = model_diff.predict(test_samples_diff)
|
58 |
-
mix_predict = []
|
59 |
-
for i in range(len(prediction)):
|
60 |
-
mix = prediction[i][1] + prediction_diff[i][1]
|
61 |
-
mix_predict.append(mix/2)
|
62 |
-
|
63 |
-
prediction_video = merge_video_prediction(mix_predict, test_sv, test_vc)
|
64 |
-
|
65 |
-
video_names = []
|
66 |
-
for key in test_vc.keys():
|
67 |
-
video_names.append(key)
|
68 |
-
for i, pd in enumerate(prediction_video):
|
69 |
-
if pd >= 0.5:
|
70 |
-
label = "Fake"
|
71 |
-
else:
|
72 |
-
label = "Real"
|
73 |
-
return label
|
74 |
-
|
75 |
-
inputs = gr.inputs.Video()
|
76 |
-
outputs = gr.outputs.Textbox()
|
77 |
-
iface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs,
|
78 |
-
examples=[["sample_fake.mp4"],["sample_real.mp4"]])
|
79 |
iface.launch()
|
|
|
1 |
+
import tensorflow.keras as K
|
2 |
+
import os
|
3 |
+
from tensorflow.keras import layers
|
4 |
+
import tensorflow as tf
|
5 |
+
import gradio as gr
|
6 |
+
from extract_landmarks import get_data_for_test,extract_landmark,merge_video_prediction
|
7 |
+
|
8 |
+
block_size = 60
|
9 |
+
DROPOUT_RATE = 0.5
|
10 |
+
RNN_UNIT = 64
|
11 |
+
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
12 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
13 |
+
|
14 |
+
gpus = tf.config.list_physical_devices(device_type='GPU')
|
15 |
+
for gpu in gpus:
|
16 |
+
tf.config.experimental.set_memory_growth(device=gpu, enable=True)
|
17 |
+
device = "CPU" if len(gpus) == 0 else "GPU"
|
18 |
+
print("using {}".format(device))
|
19 |
+
|
20 |
+
def predict(video):
|
21 |
+
path = extract_landmark(video)
|
22 |
+
test_samples, test_samples_diff, _, _, test_sv, test_vc = get_data_for_test(path, 1, block_size)
|
23 |
+
|
24 |
+
model = K.Sequential([
|
25 |
+
layers.InputLayer(input_shape=(block_size, 136)),
|
26 |
+
layers.Dropout(0.25),
|
27 |
+
layers.Bidirectional(layers.GRU(RNN_UNIT)),
|
28 |
+
layers.Dropout(DROPOUT_RATE),
|
29 |
+
layers.Dense(64, activation='relu'),
|
30 |
+
layers.Dropout(DROPOUT_RATE),
|
31 |
+
layers.Dense(2, activation='softmax')
|
32 |
+
])
|
33 |
+
model_diff = K.Sequential([
|
34 |
+
layers.InputLayer(input_shape=(block_size - 1, 136)),
|
35 |
+
layers.Dropout(0.25),
|
36 |
+
layers.Bidirectional(layers.GRU(RNN_UNIT)),
|
37 |
+
layers.Dropout(DROPOUT_RATE),
|
38 |
+
layers.Dense(64, activation='relu'),
|
39 |
+
layers.Dropout(DROPOUT_RATE),
|
40 |
+
layers.Dense(2, activation='softmax')
|
41 |
+
])
|
42 |
+
|
43 |
+
lossFunction = K.losses.SparseCategoricalCrossentropy(from_logits=False)
|
44 |
+
optimizer = K.optimizers.Adam(learning_rate=0.001)
|
45 |
+
model.compile(optimizer=optimizer,
|
46 |
+
loss=lossFunction,
|
47 |
+
metrics=['accuracy'])
|
48 |
+
model_diff.compile(optimizer=optimizer,
|
49 |
+
loss=lossFunction,
|
50 |
+
metrics=['accuracy'])
|
51 |
+
|
52 |
+
#----Using Deeperforensics 1.0 Parameters----#
|
53 |
+
model.load_weights('g1.h5')
|
54 |
+
model_diff.load_weights('g2.h5')
|
55 |
+
|
56 |
+
prediction = model.predict(test_samples)
|
57 |
+
prediction_diff = model_diff.predict(test_samples_diff)
|
58 |
+
mix_predict = []
|
59 |
+
for i in range(len(prediction)):
|
60 |
+
mix = prediction[i][1] + prediction_diff[i][1]
|
61 |
+
mix_predict.append(mix/2)
|
62 |
+
|
63 |
+
prediction_video = merge_video_prediction(mix_predict, test_sv, test_vc)
|
64 |
+
|
65 |
+
video_names = []
|
66 |
+
for key in test_vc.keys():
|
67 |
+
video_names.append(key)
|
68 |
+
for i, pd in enumerate(prediction_video):
|
69 |
+
if pd >= 0.5:
|
70 |
+
label = "Fake"
|
71 |
+
else:
|
72 |
+
label = "Real"
|
73 |
+
return label
|
74 |
+
|
75 |
+
inputs = gr.inputs.Video()
|
76 |
+
outputs = gr.outputs.Textbox()
|
77 |
+
iface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs,
|
78 |
+
examples=[["sample_fake.mp4"],["sample_real.mp4"]])
|
79 |
iface.launch()
|