Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow import keras
|
| 3 |
+
from tensorflow.keras import layers
|
| 4 |
+
from tensorflow.keras.applications import VGG16
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 8 |
+
|
| 9 |
+
# Set up data directories
|
| 10 |
+
data_dir = "/kaggle/input/deepfake-and-real-images/Dataset"
|
| 11 |
+
train_dir = f"{data_dir}/Train"
|
| 12 |
+
val_dir = f"{data_dir}/Validation"
|
| 13 |
+
test_dir = f"{data_dir}/Test"
|
| 14 |
+
|
| 15 |
+
# Data augmentation and preprocessing
|
| 16 |
+
datagen = ImageDataGenerator(rescale=1.0/255)
|
| 17 |
+
|
| 18 |
+
train_generator = datagen.flow_from_directory(
|
| 19 |
+
train_dir,
|
| 20 |
+
target_size=(150, 150),
|
| 21 |
+
batch_size=32,
|
| 22 |
+
class_mode='binary'
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
val_generator = datagen.flow_from_directory(
|
| 26 |
+
val_dir,
|
| 27 |
+
target_size=(150, 150),
|
| 28 |
+
batch_size=32,
|
| 29 |
+
class_mode='binary'
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
test_generator = datagen.flow_from_directory(
|
| 33 |
+
test_dir,
|
| 34 |
+
target_size=(150, 150),
|
| 35 |
+
batch_size=32,
|
| 36 |
+
class_mode='binary',
|
| 37 |
+
shuffle=False
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Load pre-trained VGG16 model
|
| 41 |
+
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
|
| 42 |
+
base_model.trainable = False # Freeze the base model
|
| 43 |
+
|
| 44 |
+
# Build the model
|
| 45 |
+
model = keras.Sequential([
|
| 46 |
+
base_model,
|
| 47 |
+
layers.Flatten(),
|
| 48 |
+
layers.Dense(512, activation='relu'),
|
| 49 |
+
layers.Dropout(0.5),
|
| 50 |
+
layers.Dense(1, activation='sigmoid')
|
| 51 |
+
])
|
| 52 |
+
|
| 53 |
+
# Compile the model
|
| 54 |
+
model.compile(optimizer='adam',
|
| 55 |
+
loss='binary_crossentropy',
|
| 56 |
+
metrics=['accuracy'])
|
| 57 |
+
|
| 58 |
+
# Train the model
|
| 59 |
+
history = model.fit(
|
| 60 |
+
train_generator,
|
| 61 |
+
validation_data=val_generator,
|
| 62 |
+
epochs=10
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Save the model
|
| 66 |
+
model.save("real_fake_classifier_vgg16.h5")
|
| 67 |
+
|
| 68 |
+
# Evaluate on test data
|
| 69 |
+
test_loss, test_acc = model.evaluate(test_generator)
|
| 70 |
+
print(f"Test Accuracy: {test_acc:.4f}")
|
| 71 |
+
|
| 72 |
+
# Plot training history
|
| 73 |
+
plt.plot(history.history['accuracy'], label='accuracy')
|
| 74 |
+
plt.plot(history.history['val_accuracy'], label='val_accuracy')
|
| 75 |
+
plt.xlabel('Epoch')
|
| 76 |
+
plt.ylabel('Accuracy')
|
| 77 |
+
plt.legend()
|
| 78 |
+
plt.show()
|