Deep-learning powered detector for AI-generated images and videos.
Identifies content from DALL·E, Midjourney, Stable Diffusion, Sora, and deepfake pipelines.
- Project Overview
- Architecture
- Installation
- Dataset Setup
- Training
- Evaluation & Grad-CAM
- Inference CLI
- Web UI
- Performance Targets
- Model Comparison
- Security Considerations
- License
- References
This project provides a production-ready pipeline to detect whether an image or short video clip was created by a human or generated by an AI system. It operates in two domains simultaneously:
| Domain | Signal | Module |
|---|---|---|
| Spatial (RGB) | Pixel-level semantic + texture artefacts | EfficientNet-B4 / Xception backbone |
| Frequency (FFT) | Unnatural spectral peaks from GAN upsampling | fft_features.py + DualStreamDetector |
A DualStreamDetector fuses both branches; an EnsembleDetector averages multiple models for maximum robustness.
Input Image (380 × 380 × 3)
│
├──────────────────────────────────┐
│ │
┌────▼──────────┐ ┌───────▼────────┐
│ EfficientNet │ │ FFT Branch │
│ -B4 (RGB) │ │ (1×H×W mag.) │
└────┬──────────┘ └───────┬────────┘
│ 1792-dim feature │ 256-dim feature
└────────────┬─────────────────────┘
│ Concatenate (2048-dim)
┌──────▼──────┐
│ MLP Head │
│ BN → Drop │
│ → 512 → 2 │
└──────┬──────┘
Logits
[P(real), P(fake)]
# 1. Clone the repository
git clone https://github.com/YOUR_USERNAME/TruthLens.git
cd TruthLens
# 2. Create a virtual environment
python -m venv venv
# Windows
venv\Scripts\activate
# Linux / macOS
source venv/bin/activate
# 3. Install dependencies
pip install --upgrade pip
pip install -r requirements.txtGPU note: If you have an NVIDIA GPU, replace the
torchline in
requirements.txtwith the CUDA wheel matching your driver:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
This project uses CIFAKE — 120,000 images (60k real from CIFAR-10, 60k AI-generated with Stable Diffusion).
- Visit kaggle.com/account
- Click Create New API Token → downloads
kaggle.json - Place it at
~/.kaggle/kaggle.json - On Linux/macOS:
chmod 600 ~/.kaggle/kaggle.json
python setup_data.py
# Optional: specify a different destination
python setup_data.py --dest_dir /path/to/dataAfter completion, the directory layout will be:
data/
├── train/
│ ├── real/ # 50,000 images
│ └── fake/ # 50,000 images
└── val/
├── real/ # 10,000 images
└── fake/ # 10,000 images
# Quick start — EfficientNet-B4 for 30 epochs
python src/train.py \
--data_dir data \
--model efficientnet \
--epochs 30 \
--batch_size 32 \
--lr 3e-4 \
--warmup 3 \
--patience 7 \
--output_dir checkpoints
# Train the dual-stream (RGB + FFT) model
python src/train.py \
--data_dir data \
--model dual_stream \
--epochs 40 \
--batch_size 24
# Train Xception
python src/train.py \
--data_dir data \
--model xception \
--epochs 30| Flag | Default | Description |
|---|---|---|
--data_dir |
data |
Root data directory |
--model |
efficientnet |
Architecture: efficientnet, xception, dual_stream, ensemble |
--epochs |
30 |
Total training epochs |
--batch_size |
32 |
Mini-batch size |
--lr |
3e-4 |
Peak learning rate (after warm-up) |
--warmup |
3 |
Linear warm-up epochs |
--patience |
7 |
Early stopping patience |
--output_dir |
checkpoints |
Checkpoint + log destination |
--num_workers |
4 |
DataLoader worker threads |
Training artefacts:
checkpoints/best_model.pth— best validation checkpointcheckpoints/training_log.csv— epoch-by-epoch metrics
# Evaluate on the validation set
python src/evaluate.py \
--checkpoint checkpoints/best_model.pth \
--data_dir data \
--output_dir eval_results
# Output:
# eval_results/confusion_matrix.png
# eval_results/roc_curve.png
# prints classification_report + AUC
# Grad-CAM visualisation for a single image
python src/evaluate.py \
--checkpoint checkpoints/best_model.pth \
--data_dir data \
--gradcam_img path/to/suspicious_image.jpg \
--output_dir eval_results
# Output: eval_results/gradcam_overlay.png# Image
python src/predict.py image.jpg \
--checkpoint checkpoints/best_model.pth \
--threshold 0.5
# Video
python src/predict.py video.mp4 \
--checkpoint checkpoints/best_model.pth \
--threshold 0.45 \
--every_n 5 \
--max_frames 128
# FFT visualisation only (no model needed)
python src/fft_features.py --image image.jpg --output fft_spectrum.pngExample output:
─────────────────────────────────────────────
VERDICT : FAKE
Real probability : 4.2%
Fake probability : 95.8%
Confidence : 95.8%
─────────────────────────────────────────────
# Launch on localhost:7860
python app.py
# Custom checkpoint + public share link
python app.py \
--checkpoint checkpoints/best_model.pth \
--port 7860 \
--shareOpen http://localhost:7860 in your browser.
Image tab — upload any image → see real/fake probabilities, FFT spectrum, and verdict card.
Video tab — upload a clip → see frame-level probability chart and overall verdict.
| Metric | Target | Notes |
|---|---|---|
| Accuracy (val) | ≥ 95% | On CIFAKE validation set |
| AUC-ROC | ≥ 0.98 | Area under the ROC curve |
| Inference speed | < 100ms/img | Single image on GPU |
| False Positive Rate | < 5% | Real images classified as fake |
| Model | Params | Val Accuracy* | AUC-ROC* | Speed (GPU) |
|---|---|---|---|---|
| EfficientNet-B4 | 19M | ~96–97% | ~0.990 | ~30ms |
| Xception | 23M | ~95–96% | ~0.988 | ~35ms |
| DualStream (RGB+FFT) | 20M | ~97–98% | ~0.993 | ~40ms |
| Ensemble (Eff+Xc) | 42M | ~98% | ~0.995 | ~65ms |
*Approximate targets based on published results on CIFAKE.
Actual numbers depend on hardware, training duration, and augmentation settings.
This project involves handling user-uploaded files and loading pre-trained model weights, which have security implications.
- Model Checkpoints (
.pthfiles): Loading model weights withtorch.loadcan be risky if the source is untrusted, as it may execute arbitrary code. This project has been updated to usetorch.load(..., weights_only=True), which is a safer method that prevents arbitrary code execution. Always ensure you are loading checkpoint files that you have trained yourself or that come from a trusted source. - File Processing: The application processes images and videos using libraries like OpenCV and Pillow. Malformed files could potentially exploit vulnerabilities in these underlying libraries. It is recommended to run the application in a containerized environment (e.g., Docker) to isolate it from the host system, especially when exposing it to the internet.
- Gradio Web UI: When launching the Gradio app with
--share, your local server is exposed to the public internet via a tunnel. Be aware of the risks associated with exposing local services.
This project is licensed under the MIT License. See the LICENSE file for details.
- FaceForensics++ — Rössler et al. (2019) — Benchmark for face manipulation detection.
https://arxiv.org/abs/1901.08971 - Detecting Fake Images in the Wild (CNNDetect) — Wang et al. (2020)
https://arxiv.org/abs/1912.11035 - CIFAKE Dataset — Bird & Lotfi (2023)
https://arxiv.org/abs/2303.14126
Kaggle: https://www.kaggle.com/datasets/birdy654/cifake-real-and-ai-generated-synthetic-images - EfficientNet — Tan & Le (2019)
https://arxiv.org/abs/1905.11946 - Grad-CAM — Selvaraju et al. (2017)
https://arxiv.org/abs/1610.02391 - Xception — Chollet (2017) — Used for deepfake detection (FaceForensics++ baseline)
https://arxiv.org/abs/1610.02357 - Towards Universal Fake Image Detection Exploiting Diffusion Models — Ojha et al. (2023)
https://arxiv.org/abs/2306.10719
.
├── app.py # Gradio web UI
├── setup_data.py # Download & organise CIFAKE
├── requirements.txt
├── .gitignore
├── README.md
├── src/
│ ├── dataset.py # DataLoader + augmentations
│ ├── model.py # EfficientNet, Xception, DualStream, Ensemble
│ ├── fft_features.py # Frequency-domain feature extraction
│ ├── train.py # Training loop with AMP + scheduler
│ ├── evaluate.py # Metrics, Grad-CAM
│ ├── predict.py # Inference CLI
│ └── video_utils.py # Frame extraction + video prediction
├── checkpoints/ # Saved model weights (git-ignored)
└── data/ # Dataset (git-ignored)
├── train/
│ ├── real/
│ └── fake/
└── val/
├── real/
└── fake/
# 1. Install dependencies
pip install -r requirements.txt
# 2. Download dataset
python setup_data.py
# 3. Train
python src/train.py --data_dir data --model efficientnet --epochs 30
# 4. Evaluate
python src/evaluate.py --checkpoint checkpoints/best_model.pth --data_dir data --output_dir eval_results
# 5. Predict a single image
python src/predict.py test_image.jpg --checkpoint checkpoints/best_model.pth
# 6. Launch Web UI
python app.py