A comprehensive deep learning-based system for detecting deepfake images using TensorFlow/Keras. This project implements state-of-the-art CNN architectures (EfficientNetB0 and custom models) to classify images as real or fake with high accuracy.
- Multiple Model Architectures: EfficientNetB0, Custom CNN, and Deep CNN
- Complete Pipeline: Data preprocessing, training, evaluation, and inference
- Face Extraction: Optional MTCNN or OpenCV-based face detection
- Data Augmentation: Comprehensive augmentation for better generalization
- Real-time Detection: Webcam support for live deepfake detection
- Detailed Metrics: Accuracy, Precision, Recall, F1-Score, ROC-AUC
- Visualization: Confusion matrix, ROC curves, training history plots
- Modular Design: Clean, maintainable, and extensible codebase
- Production Ready: Complete with inference scripts and model deployment
- Installation
- Project Structure
- Dataset Preparation
- Usage
- Configuration
- Model Architectures
- Results
- Contributing
- License
- Python 3.8 or higher
- pip package manager
- (Optional) CUDA-compatible GPU for faster training
git clone https://github.com/CursedOn3/DeepFake-Detection-for-Image.git
cd DeepFake-Detection-for-Imagepip install -r requirements.txtOr install manually:
pip install tensorflow keras opencv-python mtcnn numpy pandas scikit-learn matplotlib seaborn tqdmpython main.py --setupThis creates the necessary directory structure for your dataset.
DeepFake-Detection-for-Image/
│
├── src/
│ ├── config.py # Configuration and hyperparameters
│ ├── preprocess.py # Image preprocessing and augmentation
│ ├── model.py # Model architectures (EfficientNet, Custom CNN)
│ ├── train.py # Training script with callbacks
│ ├── evaluate.py # Model evaluation and metrics
│ └── inference.py # Inference on new images
│
├── data/ # Dataset directory
│ ├── train/
│ │ ├── real/ # Real training images
│ │ └── fake/ # Fake training images
│ ├── val/
│ │ ├── real/ # Real validation images
│ │ └── fake/ # Fake validation images
│ └── test/
│ ├── real/ # Real test images
│ └── fake/ # Fake test images
│
├── models/ # Saved models
│ ├── deepfake_detector.h5
│ └── checkpoint_best.h5
│
├── results/ # Evaluation results and plots
│ ├── confusion_matrix.png
│ ├── roc_curve.png
│ ├── training_history.png
│ └── classification_report.txt
│
├── main.py # Main entry point
├── requirements.txt # Python dependencies
├── README.md # This file
└── LICENSE # License information
Organize your dataset as follows:
data/
├── train/
│ ├── real/ # 70-80% of real images
│ └── fake/ # 70-80% of fake images
├── val/
│ ├── real/ # 10-15% of real images
│ └── fake/ # 10-15% of fake images
└── test/
├── real/ # 10-15% of real images
└── fake/ # 10-15% of fake images
Recommended Split: 80% training, 10% validation, 10% testing
- Format: JPG, JPEG, PNG, or BMP
- Resolution: Images will be automatically resized to 224×224 (configurable)
- Quantity: Minimum 1000 images per class recommended
Initialize the project directories:
python main.py --setupTrain with default EfficientNetB0 model:
python main.py --train# Train with custom CNN architecture
python main.py --train --model custom_cnn --epochs 50 --batch_size 32
# Train with deep CNN
python main.py --train --model deep_cnn --epochs 100
# Train EfficientNet with fine-tuning
python main.py --train --model efficientnet --epochs 30 --fine_tunecd src
python train.py --model efficientnet --epochs 50 --batch_size 32Evaluate the trained model on test data:
python main.py --evaluateOr specify custom model and test directory:
python main.py --evaluate --model_path models/checkpoint_best.h5 --test_dir data/testIndividual evaluation script:
cd src
python evaluate.py --model_path ../models/deepfake_detector.h5python main.py --inference --image path/to/image.jpgpython main.py --inference --directory path/to/images/ --save_resultspython main.py --inference --webcamPress 'C' to capture and analyze, 'Q' to quit.
cd src
python inference.py --image ../data/test/fake/sample.jpg
python inference.py --directory ../data/test/fake/
python inference.py --webcamTest all components:
python main.py --testEdit src/config.py to customize:
# Model Configuration
MODEL_TYPE = 'efficientnet' # Options: 'efficientnet', 'custom_cnn', 'deep_cnn'
IMG_HEIGHT = 224
IMG_WIDTH = 224
# Training Hyperparameters
BATCH_SIZE = 32
EPOCHS = 50
LEARNING_RATE = 0.001
# Data Augmentation
AUGMENTATION_CONFIG = {
'rotation_range': 20,
'width_shift_range': 0.2,
'height_shift_range': 0.2,
'horizontal_flip': True,
'zoom_range': 0.15
}
# Face Extraction
USE_FACE_EXTRACTION = False # Set to True to enable
FACE_DETECTION_METHOD = 'mtcnn' # Options: 'mtcnn', 'opencv_haar'
# Classification Threshold
CLASSIFICATION_THRESHOLD = 0.5- Base: Pre-trained on ImageNet
- Transfer Learning: Fine-tuning supported
- Parameters: ~4-5M trainable parameters
- Best For: High accuracy with reasonable training time
- Architecture: 4 conv blocks + dense layers
- Parameters: ~50K parameters
- Best For: Fast training, lightweight deployment
- Architecture: 4 conv blocks with batch normalization
- Parameters: ~500K parameters
- Best For: Balance between accuracy and speed
With proper training on quality datasets:
| Metric | EfficientNetB0 | Custom CNN | Deep CNN |
|---|---|---|---|
| Accuracy | 94-98% | 88-92% | 90-95% |
| Precision | 93-97% | 87-91% | 89-94% |
| Recall | 94-98% | 88-92% | 90-95% |
| F1-Score | 93-97% | 87-91% | 89-94% |
Results may vary based on dataset quality and size
Training generates the following in results/:
- confusion_matrix.png: Visual confusion matrix
- roc_curve.png: ROC curve with AUC score
- training_history.png: Loss and accuracy curves
- classification_report.txt: Detailed metrics per class
- training_log.csv: Epoch-by-epoch metrics
If you have a single directory with labeled data:
from sklearn.model_selection import train_test_split
# Split your data
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.2)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5)from src.train import ModelTrainer
trainer = ModelTrainer('efficientnet')
trainer.load_model('models/deepfake_detector.h5')
trainer.fine_tune(epochs=10, initial_learning_rate=1e-5)Add your model in src/model.py:
@staticmethod
def create_custom_model(input_shape):
model = models.Sequential()
# Add your layers here
return modelIssue: Import "tensorflow" could not be resolved
- Solution: Install TensorFlow:
pip install tensorflow
Issue: Out of memory during training
- Solution: Reduce batch size in
config.py:BATCH_SIZE = 16
Issue: Webcam not working
- Solution: Check camera permissions and OpenCV installation
Issue: Low accuracy
- Solution:
- Increase dataset size
- Enable data augmentation
- Train for more epochs
- Try EfficientNetB0 with fine-tuning
For faster training with GPU:
pip install tensorflow-gpu==2.13.0Verify GPU availability:
import tensorflow as tf
print("GPU Available:", tf.config.list_physical_devices('GPU'))- Add support for video deepfake detection
- Implement additional architectures (ResNet, VGG, Xception)
- Add explainability features (Grad-CAM, LIME)
- Web interface for easy deployment
- Mobile app support
- API endpoint for cloud deployment
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- TensorFlow/Keras for the deep learning framework
- EfficientNet paper by Tan & Le (2019)
- MesoNet architecture inspiration
- FaceForensics++ dataset creators
- All open-source contributors
For questions, issues, or suggestions:
- GitHub Issues: Create an issue
- Email: [Your email if you want to add]
- Tan, M., & Le, Q. (2019). EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks
- Afchar, D., et al. (2018). MesoNet: a Compact Facial Video Forgery Detection Network
- Rossler, A., et al. (2019). FaceForensics++: Learning to Detect Manipulated Facial Images
⭐ If you find this project helpful, please consider giving it a star!
Made with ❤️ by CursedOn3