Detecting Manipulation, Preserving Truth, Securing Authenticity
Built with the tools and technologies:
- Image Analysis: Single-image deepfake detection with probability scores
- Video Analysis: Frame-by-frame video analysis with temporal consistency checking
- Batch Processing: Support for multiple file formats and sizes
- Real-time Inference: Fast prediction with GPU acceleration support
- CNN-LSTM Architecture: Combines convolutional feature extraction with temporal sequence analysis
- Binary Classification: Outputs probability scores for real vs. fake content
- Flexible Input: Accepts images (128x128) and videos with automatic preprocessing
- Multiple Architecture Support: Compatible with ResNet50, EfficientNet, and custom CNN models
- Web-based interface with drag-and-drop functionality
- REST API for programmatic access
- Command-line interface for batch processing
- Detailed confidence scores and frame-level analysis for videos
- Python 3.7 or higher
- pip package manager
- (Optional) CUDA-compatible GPU for faster inference
-
Clone the repository:
git clone <repository-url> cd deepfake
-
Install required dependencies:
pip install -r requirements.txt
-
Verify model file exists:
# Ensure deepfake_detector_best.pth is in the project directory
python app.pyAccess the application at http://localhost:5000
# For single image
python detector.py path/to/image.jpg
# For video
python detector.py path/to/video.mp4# Evaluate on test dataset
python evaluate.py --test_data path/to/test_dataset
# Test single image
python evaluate.py --image path/to/image.jpgdeepfake/
├── app.py # Flask web server and API endpoints
├── detector.py # Core detection inference engine
├── model.py # Neural network architecture definitions
├── evaluate.py # Model evaluation and testing utilities
├── deepfake_detector_best.pth # Pre-trained model weights (CNN-LSTM)
├── requirements.txt # Python package dependencies
├── README.md # Project documentation
├── templates/
│ └── index.html # Frontend web interface
└── uploads/ # Temporary file storage (auto-generated)
The current implementation uses a hybrid CNN-LSTM architecture optimized for deepfake detection:
Convolutional Neural Network (Feature Extraction)
- 3 Convolutional layers: 3→32→64→128 channels
- MaxPooling after each convolution (2x2)
- Input size: 128x128x3 RGB images
- Feature compression to 512-dimensional vector
Long Short-Term Memory (Temporal Analysis)
- Bidirectional LSTM with 2 layers
- Hidden size: 256 units per direction
- Captures temporal inconsistencies in video sequences
Classification Head
- Fully connected layers: 512→128→1
- Sigmoid activation for binary classification
- Dropout (0.5) for regularization
- ResNet50: Transfer learning with custom classifier head
- EfficientNet-B0: Lightweight architecture for resource-constrained environments
- Simple CNN: Basic convolutional network for baseline comparisons
The system automatically detects and loads the appropriate architecture based on the checkpoint file.
{
"success": true,
"result": {
"prediction": "FAKE",
"confidence": 95.5,
"real_probability": 4.5,
"fake_probability": 95.5,
"is_deepfake": true
}
}{
"success": true,
"result": {
"prediction": "FAKE",
"confidence": 87.3,
"fake_probability": 87.3,
"real_probability": 12.7,
"is_deepfake": true,
"frames_analyzed": 30,
"fake_frames": 26,
"fake_ratio": 86.7,
"frame_predictions": [...]
}
}Upload and analyze an image or video file.
Request: multipart/form-data with 'file' field
Response: JSON with prediction results
Check server and model status.
Response:
{
"status": "healthy",
"model_loaded": true
}Change model checkpoint path (app.py or detector.py):
detector = DeepfakeDetectorInference('path/to/custom_model.pth')Modify image preprocessing:
# In detector.py, adjust transform parameters
self.transform = transforms.Compose([
transforms.Resize((128, 128)), # Input size
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])Adjust frame sampling:
result = detector.predict_video(
video_path,
num_frames=50, # Number of frames to analyze
threshold=0.6 # Classification threshold (default: 0.5)
)Modify Flask settings (app.py):
# Change port
app.run(debug=True, host='0.0.0.0', port=5001)
# Adjust file size limit
MAX_FILE_SIZE = 200 * 1024 * 1024 # 200MB
# Add allowed file extensions
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'mp4', 'webm'}The system automatically detects and uses CUDA-compatible GPUs:
# Force CPU usage
detector = DeepfakeDetectorInference(device=torch.device('cpu'))
# Force GPU usage
detector = DeepfakeDetectorInference(device=torch.device('cuda'))For processing multiple files, use the command-line interface in a loop or modify the detector for batch inference.
- Video processing uses frame sampling to reduce memory footprint
- Temporary files are automatically cleaned up after processing
- Adjust
num_framesparameter for videos based on available RAM
To evaluate the model on a test dataset:
python evaluate.py --test_data path/to/test_dataset --batch_size 32Expected test dataset structure:
test_dataset/
├── real/
│ ├── image1.jpg
│ ├── image2.jpg
│ └── ...
└── fake/
├── image1.jpg
├── image2.jpg
└── ...
Evaluation outputs:
- Overall accuracy, precision, recall, F1-score
- Per-class performance metrics
- Confusion matrix (saved as PNG)
- Detailed classification report
PyTorch Version Compatibility
# Error: weights_only parameter issue
# Solution: The code handles PyTorch 2.6+ automatically
# If issues persist, use: weights_only=False in torch.load()Model Architecture Mismatch
Error: Missing keys or unexpected keys in state_dict
Solution:
1. Verify the checkpoint file is not corrupted
2. Ensure model.py architecture matches training architecture
3. Check num_classes parameter (1 for sigmoid, 2 for softmax)
CUDA Out of Memory
# Reduce batch size or use CPU
detector = DeepfakeDetectorInference(device=torch.device('cpu'))
# For videos, reduce frame count
result = detector.predict_video(video_path, num_frames=10)Port Already in Use
# Windows: Find and kill process
netstat -ano | findstr :5000
taskkill /PID <process_id> /F
# Linux/Mac
lsof -ti:5000 | xargs kill -9- Images: JPG, JPEG, PNG, GIF, BMP
- Videos: MP4, AVI, MOV, MKV, FLV, WMV
- Minimum RAM: 4GB (8GB recommended for video processing)
- Storage: 500MB for dependencies + model weights
- GPU: Optional, NVIDIA GPU with CUDA support for acceleration
- Image Inference: ~50-200ms per image (CPU), ~10-50ms (GPU)
- Video Processing: Depends on frame count and resolution
- Model Size: ~100-200MB depending on architecture
- model.py: Define new architectures or modify existing ones
- detector.py: Core inference logic and preprocessing
- app.py: API endpoints and server configuration
- evaluate.py: Testing and validation utilities
- Define architecture class in
model.py - Add to
models_to_trylist indetector.py - Ensure forward pass returns correct output shape
# Test model loading
python -c "from detector import DeepfakeDetectorInference; detector = DeepfakeDetectorInference('deepfake_detector_best.pth')"
# Test single prediction
python detector.py test_image.jpgThis project is provided as-is for educational and research purposes.
Built using PyTorch, Flask, and OpenCV for deep learning-based deepfake detection.